Skip to content

Commit 747b9c9

Browse files
working for borders
1 parent 3e5c407 commit 747b9c9

File tree

7 files changed

+75
-36
lines changed

7 files changed

+75
-36
lines changed

eslint.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default tseslint.config([
7070
},
7171
// rules which apply to JS, TS, etc.
7272
rules: {
73+
'eslint-comments/no-use': 'off',
7374
'i18n-text/no-en': 0,
7475
'importPlugin/no-nodejs-modules': 'off',
7576
'importPlugin/extensions': [
@@ -111,7 +112,7 @@ export default tseslint.config([
111112
{
112113
files: ['**/*.{js,jsx}'],
113114
rules: {
114-
'eslint-comments/no-use': 0,
115+
'eslint-comments/no-use': 'off',
115116
'import/no-namespace': 0,
116117
'no-shadow': 0,
117118
'import/no-commonjs': 0,
@@ -138,6 +139,7 @@ export default tseslint.config([
138139
},
139140
files: ['**/*.{ts,tsx}'],
140141
rules: {
142+
'eslint-comments/no-use': 'off',
141143
'no-shadow': 'off',
142144
'@typescript-eslint/no-shadow': 'error',
143145
'@typescript-eslint/no-explicit-any': 2,

scripts/buildTokens.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {Config} from 'style-dictionary/types'
1+
import type {Config, LogConfig} from 'style-dictionary/types'
22
import {PrimerStyleDictionary} from '../src/primerStyleDictionary.js'
33
import {copyFromDir} from '../src/utilities/index.js'
44
import {deprecatedJson, css, docJson, fallbacks, styleLint} from '../src/platforms/index.js'
@@ -12,6 +12,14 @@ import {themes} from './themes.config.js'
1212
import fs from 'fs'
1313
import {getFallbackTheme} from './utilities/getFallbackTheme.js'
1414

15+
const log: LogConfig = {
16+
warnings: 'disabled', // 'warn' | 'error' | 'disabled'
17+
verbosity: 'silent', // 'default' | 'silent' | 'verbose'
18+
errors: {
19+
brokenReferences: 'throw', // 'throw' | 'console'
20+
},
21+
}
22+
1523
/**
1624
* getStyleDictionaryConfig
1725
* @param filename output file name without extension
@@ -29,13 +37,7 @@ const getStyleDictionaryConfig: StyleDictionaryConfigGenerator = (
2937
): Config => ({
3038
source, // build the special formats
3139
include,
32-
log: {
33-
warnings: 'disabled', // 'warn' | 'error' | 'disabled'
34-
verbosity: 'silent', // 'default' | 'silent' | 'verbose'
35-
errors: {
36-
brokenReferences: 'throw', // 'throw' | 'console'
37-
},
38-
},
40+
log,
3941
platforms: Object.fromEntries(
4042
Object.entries({
4143
css: css(`css/${filename}.css`, options.prefix, options.buildPath, {
@@ -64,6 +66,7 @@ export const buildDesignTokens = async (buildOptions: ConfigGeneratorOptions): P
6466
const extendedSD = await PrimerStyleDictionary.extend({
6567
source: [...source, ...include], // build the special formats
6668
include,
69+
log,
6770
platforms: {
6871
css: css(`internalCss/${filename}.css`, buildOptions.prefix, buildOptions.buildPath, {
6972
themed: true,

src/formats/cssAdvanced.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ export const cssAdvanced: FormatFn = async ({
7878
const css = getFormattedVariables({
7979
format: 'css',
8080
dictionary: filteredDictionary,
81-
// @ts-ignore
8281
outputReferences,
8382
formatting: mergedFormatting,
8483
usesDtcg,

src/formats/utilities/createPropertyFormatterWithRef.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable eslintComments/no-use */
2+
/* eslint-disable camelcase */
13
/*
24
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
35
*
@@ -10,6 +12,7 @@
1012
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
1113
* and limitations under the License.
1214
*/
15+
import type {Dictionary, FormattingOptions, OutputReferences, TransformedToken} from 'style-dictionary/types'
1316
import {getReferences, usesReferences} from 'style-dictionary/utils'
1417

1518
/**
@@ -39,7 +42,7 @@ const defaultFormatting = {
3942
* @param {Formatting} options
4043
* @returns {string}
4144
*/
42-
export function addComment(to_ret_token, comment, options) {
45+
export function addComment(to_ret_token: string, comment: string, options: FormattingOptions) {
4346
const {commentStyle, indentation} = options
4447
let {commentPosition} = options
4548

@@ -124,9 +127,17 @@ export default function createPropertyFormatterWithRef({
124127
formatting = {},
125128
themeable = false,
126129
usesDtcg = false,
130+
}: {
131+
outputReferences?: OutputReferences
132+
outputReferenceFallbacks?: boolean
133+
dictionary: Dictionary
134+
format?: string
135+
formatting?: FormattingOptions
136+
themeable?: boolean
137+
usesDtcg?: boolean
127138
}) {
128139
/** @type {Formatting} */
129-
const formatDefaults = {}
140+
const formatDefaults: FormattingOptions = {}
130141
switch (format) {
131142
case 'css':
132143
formatDefaults.prefix = '--'
@@ -141,7 +152,7 @@ export default function createPropertyFormatterWithRef({
141152
}
142153
const {prefix, commentStyle, indentation, separator, suffix} = mergedOptions
143154
const {tokens, unfilteredTokens} = dictionary
144-
return function (token) {
155+
return function (token: TransformedToken) {
145156
let to_ret_token = `${indentation}${prefix}${token.name}${separator} `
146157
let value = usesDtcg ? token.$value : token.value
147158
const originalValue = usesDtcg ? token.original.$value : token.original.value
@@ -192,31 +203,16 @@ export default function createPropertyFormatterWithRef({
192203
})
193204
.join(' ')
194205
}
195-
// if (token.attributes.referenceMap) {
196-
// // console.log(token)
197-
// // console.log('original is object', token.attributes || '')
198-
// value = token.attributes.referenceMap
199-
// // console.log('referenceMap', token.attributes.referenceMap)
200-
// for (const [key, val] of Object.entries(originalValue)) {
201-
// const regex = new RegExp(`\\[${key}\\]`)
202-
// value = value.replace(regex, val)
203-
// }
204-
// }
205206
}
206-
// console.log('originalValue:', originalValue, JSON.stringify(originalIsObject))
207-
// if (ref.$type === 'border') {
208-
// console.log('BORDER:', ref.original)
209-
// }
210-
// console.log('refs: ', refs)
211-
207+
/* eslint-disable-next-line github/array-foreach */
212208
refs.forEach(ref => {
213209
// value should be a string that contains the resolved reference
214210
// because Style Dictionary resolved this in the resolution step.
215211
// Here we are undoing that by replacing the value with
216212
// the reference's name
217213
if (Object.hasOwn(ref, `${usesDtcg ? '$' : ''}value`) && Object.hasOwn(ref, 'name')) {
218214
const refVal = usesDtcg ? ref.$value : ref.value
219-
const replaceFunc = function () {
215+
const replaceFunc = () => {
220216
if (format === 'css') {
221217
if (outputReferenceFallbacks) {
222218
return `var(${prefix}${ref.name}, ${refVal})`
@@ -248,7 +244,7 @@ export default function createPropertyFormatterWithRef({
248244

249245
const comment = token.$description ?? token.comment
250246
if (comment && commentStyle !== 'none') {
251-
to_ret_token = addComment(to_ret_token, comment, mergedOptions)
247+
to_ret_token = addComment(to_ret_token, comment, mergedOptions as FormattingOptions)
252248
}
253249
// console.log('to_ret_token', to_ret_token)
254250
return to_ret_token

src/formats/utilities/getFormattedVariables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
1111
* and limitations under the License.
1212
*/
13-
import type {Dictionary} from 'style-dictionary/types'
13+
import type {Dictionary, OutputReferences} from 'style-dictionary/types'
1414
import {sortByReference} from 'style-dictionary/utils'
1515
import createPropertyFormatterWithRef from './createPropertyFormatterWithRef.js'
1616

@@ -29,7 +29,7 @@ export default function getFormattedVariables({
2929
}: {
3030
format: string
3131
dictionary: Dictionary
32-
outputReferences?: boolean
32+
outputReferences?: OutputReferences
3333
outputReferenceFallbacks?: boolean
3434
formatting?: {
3535
lineSeparator?: string

src/platforms/css.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {isFromFile, isSource} from '../filters/index.js'
22
import type {PlatformInitializer} from '../types/platformInitializer.js'
33
import type {PlatformConfig, TransformedToken} from 'style-dictionary/types'
44
import {outputReferencesTransformed, outputReferencesFilter} from 'style-dictionary/utils'
5+
import {outputReferencesTransformedWithObject} from './utilities/outputReferencesTransformedWithObject.js'
56

67
const getCssSelectors = (outputFile: string) => {
78
// check for dark in the beginning of the output filename
@@ -58,9 +59,10 @@ export const css: PlatformInitializer = (outputFile, prefix, buildPath, options)
5859
]),
5960
options: {
6061
showFileHeader: false,
61-
outputReferences: true,
62-
// outputReferences: (token, platformOptions) =>
63-
// outputReferencesFilter(token, platformOptions) && outputReferencesTransformed(token, platformOptions),
62+
// outputReferences: true,
63+
outputReferences: (token, platformOptions) =>
64+
outputReferencesFilter(token, platformOptions) &&
65+
outputReferencesTransformedWithObject(token, platformOptions),
6466
descriptions: false,
6567
queries: getCssSelectors(outputFile),
6668
...options?.options,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {Dictionary, TransformedToken} from 'style-dictionary/types'
2+
import {resolveReferences} from 'style-dictionary/utils'
3+
4+
export function outputReferencesTransformedWithObject(
5+
token: TransformedToken,
6+
{dictionary, usesDtcg}: {dictionary: Dictionary; usesDtcg?: boolean},
7+
): boolean {
8+
const originalValue = usesDtcg ? token.original.$value : token.original.value
9+
const value = usesDtcg ? token.$value : token.value
10+
11+
// double check if this is a string, technically speaking the token could also be an object
12+
// and pass the usesReferences check
13+
if (typeof originalValue === 'string') {
14+
// Check if the token's value is the same as if we were resolve references on the original value
15+
// This checks whether the token's value has been transformed e.g. transitive transforms.
16+
// If it has been, that means we should not be outputting refs because this would undo the work of those transforms.
17+
return (
18+
value ===
19+
resolveReferences(originalValue, dictionary.unfilteredTokens ?? dictionary.tokens, {
20+
usesDtcg,
21+
warnImmediately: false,
22+
})
23+
)
24+
}
25+
if (typeof originalValue === 'object') {
26+
const values = Object.values(originalValue).filter(val => typeof val === 'string')
27+
return values.every(val =>
28+
values.includes(
29+
resolveReferences(val, dictionary.unfilteredTokens ?? dictionary.tokens, {
30+
usesDtcg,
31+
warnImmediately: false,
32+
}),
33+
),
34+
)
35+
}
36+
return false
37+
}

0 commit comments

Comments
 (0)