-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow outputReferences function, publish filter util for it (#1145
) * feat: allow outputReferences function, publish filter util for it * chore: update types and docs
- Loading branch information
1 parent
5fb5a61
commit 8873031
Showing
34 changed files
with
661 additions
and
850 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,65 @@ | ||
--- | ||
'style-dictionary': major | ||
--- | ||
|
||
BREAKING: Allow specifying a `function` for `outputReferences`, conditionally outputting a ref or not per token. Also exposes `outputReferencesFilter` utility function which will determine whether a token should be outputting refs based on whether those referenced tokens were filtered out or not. | ||
|
||
If you are maintaining a custom format that allows `outputReferences` option, you'll need to take into account that it can be a function, and pass the correct options to it. | ||
|
||
Before: | ||
|
||
```js | ||
StyleDictionary.registerFormat({ | ||
name: 'custom/es6', | ||
formatter: async (dictionary) => { | ||
const { allTokens, options, file } = dictionary; | ||
const { usesDtcg } = options; | ||
|
||
const compileTokenValue = (token) => { | ||
let value = usesDtcg ? token.$value : token.value; | ||
const originalValue = usesDtcg ? token.original.$value : token.original.value; | ||
|
||
// Look here 👇 | ||
const shouldOutputRefs = outputReferences && usesReferences(originalValue); | ||
|
||
if (shouldOutputRefs) { | ||
// ... your code for putting back the reference in the output | ||
value = ... | ||
} | ||
return value; | ||
} | ||
return `${allTokens.reduce((acc, token) => `${acc}export const ${token.name} = ${compileTokenValue(token)};\n`, '')}`; | ||
}, | ||
}); | ||
``` | ||
|
||
After | ||
|
||
```js | ||
StyleDictionary.registerFormat({ | ||
name: 'custom/es6', | ||
formatter: async (dictionary) => { | ||
const { allTokens, options, file } = dictionary; | ||
const { usesDtcg } = options; | ||
|
||
const compileTokenValue = (token) => { | ||
let value = usesDtcg ? token.$value : token.value; | ||
const originalValue = usesDtcg ? token.original.$value : token.original.value; | ||
|
||
// Look here 👇 | ||
const shouldOutputRef = | ||
usesReferences(original) && | ||
(typeof options.outputReferences === 'function' | ||
? outputReferences(token, { dictionary, usesDtcg }) | ||
: options.outputReferences); | ||
|
||
if (shouldOutputRefs) { | ||
// ... your code for putting back the reference in the output | ||
value = ... | ||
} | ||
return value; | ||
} | ||
return `${allTokens.reduce((acc, token) => `${acc}export const ${token.name} = ${compileTokenValue(token)};\n`, '')}`; | ||
}, | ||
}); | ||
``` |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"ignorePatterns": ["/docs/dist/**/*"], | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"sourceType": "module" | ||
|
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.changeset/ | ||
**/*.snap.js | ||
/docs/dist |
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
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,31 @@ | ||
/* | ||
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with | ||
* the License. A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
import { expect } from 'chai'; | ||
import { GroupMessages } from '../../lib/utils/groupMessages.js'; | ||
|
||
// TODO: add more tests | ||
|
||
describe('groupMessage', () => { | ||
it('should allow removing messages', () => { | ||
const grpMessages = new GroupMessages(); | ||
const FILTER_WARNINGS = grpMessages.GROUP.FilteredOutputReferences; | ||
grpMessages.add(FILTER_WARNINGS, '{foo.bar}'); | ||
grpMessages.add(FILTER_WARNINGS, '{baz.qux}'); | ||
grpMessages.add(FILTER_WARNINGS, '{another.one}'); | ||
|
||
expect(grpMessages.count(FILTER_WARNINGS)).to.equal(3); | ||
grpMessages.remove(FILTER_WARNINGS, '{baz.qux}'); | ||
expect(grpMessages.count(FILTER_WARNINGS)).to.equal(2); | ||
expect(grpMessages.fetchMessages(FILTER_WARNINGS)).to.eql(['{foo.bar}', '{another.one}']); | ||
}); | ||
}); |
Oops, something went wrong.