-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from tokens-studio/exclude-parent-keys
feat: optionally exclude parent keys from token file
- Loading branch information
Showing
10 changed files
with
183 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tokens-studio/sd-transforms': patch | ||
--- | ||
|
||
Add excludeParentKeys option to the transform options, in order to exclude parent keys from your token files. This is useful if you use a single-file export from Tokens Studio Figma Plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DeepKeyTokenMap } from '@tokens-studio/types'; | ||
import { TransformOptions } from '../TransformOptions.js'; | ||
|
||
export function excludeParentKeys( | ||
dictionary: DeepKeyTokenMap<false>, | ||
transformOpts?: TransformOptions, | ||
): DeepKeyTokenMap<false> { | ||
if (!transformOpts?.excludeParentKeys) { | ||
return dictionary; | ||
} | ||
const copy = {} as DeepKeyTokenMap<false>; | ||
Object.values(dictionary).forEach(set => { | ||
Object.entries(set).forEach(([key, tokenGroup]) => { | ||
copy[key] = tokenGroup; | ||
}); | ||
}); | ||
return copy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import StyleDictionary from 'style-dictionary'; | ||
import { promises } from 'fs'; | ||
import path from 'path'; | ||
import { cleanup, init } from './utils.js'; | ||
|
||
const outputDir = 'test/integration/tokens/'; | ||
const outputFileName = 'vars.css'; | ||
const outputFilePath = path.resolve(outputDir, outputFileName); | ||
|
||
const cfg = { | ||
source: ['test/integration/tokens/exclude-parent-keys.tokens.json'], | ||
platforms: { | ||
css: { | ||
transformGroup: 'tokens-studio', | ||
prefix: 'sd', | ||
buildPath: outputDir, | ||
files: [ | ||
{ | ||
destination: outputFileName, | ||
format: 'css/variables', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
let transformOpts = {}; | ||
let dict: StyleDictionary.Core | undefined; | ||
|
||
function before() { | ||
if (dict) { | ||
cleanup(dict); | ||
} | ||
dict = init(cfg, transformOpts); | ||
} | ||
|
||
function after() { | ||
if (dict) { | ||
cleanup(dict); | ||
} | ||
} | ||
|
||
describe('exclude parent keys', () => { | ||
afterEach(() => { | ||
after(); | ||
}); | ||
|
||
it('does not expand parent keys by default and throws on broken references', async () => { | ||
expect(before).to.throw('Problems were found when trying to resolve property references'); | ||
}); | ||
|
||
it('optionally excludes parent keys', async () => { | ||
transformOpts = { | ||
excludeParentKeys: true, | ||
}; | ||
before(); | ||
|
||
const file = await promises.readFile(outputFilePath, 'utf-8'); | ||
expect(file).to.include( | ||
` | ||
--sdCoreColor: #FFFFFF; | ||
--sdSemanticColor: #FFFFFF; | ||
--sdButtonColor: #FFFFFF;`, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"foo": { | ||
"core": { | ||
"color": { | ||
"value": "#FFFFFF", | ||
"type": "color" | ||
} | ||
}, | ||
"semantic": { | ||
"color": { | ||
"value": "{core.color}", | ||
"type": "color" | ||
} | ||
} | ||
}, | ||
"bar": { | ||
"button": { | ||
"color": { | ||
"value": "{semantic.color}", | ||
"type": "color" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { DeepKeyTokenMap } from '@tokens-studio/types'; | ||
import { excludeParentKeys } from '../../../src/parsers/exclude-parent-keys.js'; | ||
|
||
const tokenObj = { | ||
foo: { | ||
core: { | ||
color: { | ||
value: '#FFFFFF', | ||
type: 'color', | ||
}, | ||
}, | ||
semantic: { | ||
color: { | ||
value: '{core.color}', | ||
type: 'color', | ||
}, | ||
}, | ||
}, | ||
bar: { | ||
button: { | ||
color: { | ||
value: '{semantic.color}', | ||
type: 'color', | ||
}, | ||
}, | ||
}, | ||
} as DeepKeyTokenMap<false>; | ||
|
||
describe('exclude parent keys', () => { | ||
it('should not exclude parent keys by default', () => { | ||
expect(excludeParentKeys(tokenObj)).to.eql(tokenObj); | ||
expect(excludeParentKeys(tokenObj, { excludeParentKeys: false })).to.eql(tokenObj); | ||
}); | ||
|
||
it('should exclude parent keys if the option is passed', () => { | ||
expect(excludeParentKeys(tokenObj, { excludeParentKeys: true })).to.eql({ | ||
core: { | ||
color: { | ||
value: '#FFFFFF', | ||
type: 'color', | ||
}, | ||
}, | ||
semantic: { | ||
color: { | ||
value: '{core.color}', | ||
type: 'color', | ||
}, | ||
}, | ||
button: { | ||
color: { | ||
value: '{semantic.color}', | ||
type: 'color', | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |