Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasoppermann committed Dec 5, 2024
1 parent dae7eab commit 9d87039
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 0 additions & 2 deletions src/preprocessor/themeOverrides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ describe('Preprocessor: themeOverrides', () => {
}),
).toStrictEqual(resultDictionary.tokens)
})
})

describe('Preprocessor: themeOverrides', () => {
it('works with custom configuration', () => {
const dictionary = getMockDictionary({
valueOverride: getMockToken({
Expand Down
6 changes: 4 additions & 2 deletions src/preprocessor/themeOverrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export const themeOverrides: Preprocessor = {
preprocessor: (dictionary: PreprocessedTokens, config: PlatformConfig): PreprocessedTokens => {
const extensionProp = config.options?.themeOverrides?.extensionProp || 'org.primer.overrides'
const valueProp = config.options?.themeOverrides?.valueProp || '$value'
const currentTheme = config.options?.themeOverrides?.theme
let currentTheme = config.options?.themeOverrides?.theme

Check failure on line 9 in src/preprocessor/themeOverrides.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

'currentTheme' is never reassigned. Use 'const' instead

const tokens = transformTokens(dictionary, token => {
// return early if no theme value is set
if (!currentTheme || !token.$extensions?.[extensionProp] || !token.$extensions?.[extensionProp][currentTheme])
if (!currentTheme || !token.$extensions?.[extensionProp] || !token.$extensions?.[extensionProp][currentTheme]) {
return token
}

// get override
const override = token.$extensions?.[extensionProp][currentTheme]
// token an theme value exist
Expand Down
5 changes: 1 addition & 4 deletions src/test-utilities/getMockDictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const flattenTokens = (tokenTree: TransformedTokens): TransformedToken[] => {

const getToken = (tokens: TransformedTokens, flatTokens: TransformedToken[]) => {
for (const token of Object.values(tokens)) {
if (
Object.prototype.hasOwnProperty.call(token, 'value') ||
Object.prototype.hasOwnProperty.call(token, '$value')
) {
if (Object.prototype.hasOwnProperty.call(token, 'name')) {
flatTokens.push(token as TransformedToken)
continue
}
Expand Down
33 changes: 26 additions & 7 deletions src/test-utilities/getMockToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type StyleDictionary from 'style-dictionary'
import {TransformedToken} from 'style-dictionary/types'

Check warning on line 1 in src/test-utilities/getMockToken.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

All imports in the declaration are only used as types. Use `import type`

const mockTokenDefaults = {
name: 'tokenName',
Expand All @@ -11,15 +11,34 @@ const mockTokenDefaults = {
isSource: true,
$value: 'transformedValue',
attributes: {},
} as const

type getMockTokenOptions = {
remove: Array<keyof typeof mockTokenDefaults>
}

const removeProps = (
token: Record<string, unknown>,
props?: getMockTokenOptions['remove'],
): Partial<TransformedToken> => {
;(props || []).forEach(prop => {

Check failure on line 24 in src/test-utilities/getMockToken.ts

View workflow job for this annotation

GitHub Actions / Test & Lint

Prefer for...of instead of Array.forEach
delete token[prop]
})
return token
}
/**
*
* @param valueOverrides partial StyleDictionary.TransformedToken
* @returns StyleDictionary.TransformedToken - a merge of {@link mockTokenDefaults} and any valid properties provided in the valueOverrides param
*/
export const getMockToken = (valueOverrides: {
[key: keyof StyleDictionary.TransformedToken]: unknown
}): StyleDictionary.TransformedToken => ({
...mockTokenDefaults,
...valueOverrides,
})
export const getMockToken = (
valueOverrides: {
[key: keyof TransformedToken]: unknown
},
options?: getMockTokenOptions,
) => {
return {
...removeProps(mockTokenDefaults, options?.remove),
...valueOverrides,
}
}

0 comments on commit 9d87039

Please sign in to comment.