-
-
Notifications
You must be signed in to change notification settings - Fork 75
design-tokens : add support for at rules #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
romainmenke
merged 8 commits into
postcss-preset-env--v8
from
design-tokens-add-support-for-at-rules--sympathetic-gila-monster-3b558642cd
Nov 29, 2022
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
86ae58f
design-tokens : add support for at rules
romainmenke c096791
lint
romainmenke 91d8350
Merge branch 'postcss-preset-env--v8' into design-tokens-add-support-…
romainmenke 1e7cdaa
more error handling
romainmenke f1ea599
optimize
romainmenke bfae96d
Update plugins/postcss-design-tokens/src/transform.ts
romainmenke 0c0a1c4
Merge branch 'postcss-preset-env--v8' into design-tokens-add-support-…
romainmenke 73f685f
Merge branch 'postcss-preset-env--v8' into design-tokens-add-support-…
romainmenke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
33 changes: 33 additions & 0 deletions
33
plugins/postcss-design-tokens/src/parse-component-values.ts
This file contains hidden or 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,33 @@ | ||
import { parseListOfComponentValues } from '@csstools/css-parser-algorithms'; | ||
import { CSSToken, tokenizer } from '@csstools/css-tokenizer'; | ||
|
||
export function parseComponentValuesFromTokens(tokens: Array<CSSToken>) { | ||
return parseListOfComponentValues(tokens, { | ||
onParseError: (err) => { | ||
throw new Error(JSON.stringify(err)); | ||
}, | ||
}); | ||
} | ||
|
||
export function parseComponentValues(source: string) { | ||
const t = tokenizer({ css: source }, { | ||
commentsAreTokens: true, | ||
onParseError: (err) => { | ||
throw new Error(JSON.stringify(err)); | ||
}, | ||
}); | ||
|
||
const tokens: Array<CSSToken> = []; | ||
|
||
{ | ||
while (!t.endOfFile()) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
tokens.push(t.nextToken()!); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
tokens.push(t.nextToken()!); // EOF-token | ||
} | ||
|
||
return parseComponentValuesFromTokens(tokens); | ||
} |
This file contains hidden or 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,135 @@ | ||
import { ComponentValue, isCommentNode, isFunctionNode, isTokenNode, isWhitespaceNode } from '@csstools/css-parser-algorithms'; | ||
import { TokenType } from '@csstools/css-tokenizer'; | ||
import type { Node, Result } from 'postcss'; | ||
import { Token, TokenTransformOptions } from './data-formats/base/token'; | ||
import { parsedPluginOptions } from './options'; | ||
import { parseComponentValues } from './parse-component-values'; | ||
|
||
export function transform(tokens: Map<string, Token>, result: Result, postCSSNode: Node, source: string, opts: parsedPluginOptions) { | ||
const componentValues = parseComponentValues(source); | ||
|
||
let didChangeSomething = false; | ||
componentValues.forEach((componentValue, index) => { | ||
if (!('walk' in componentValue)) { | ||
return; | ||
} | ||
|
||
{ | ||
const replacements = transformComponentValue(componentValue, tokens, result, postCSSNode, opts); | ||
if (replacements) { | ||
componentValues.splice(index, 1, ...replacements); | ||
didChangeSomething = true; | ||
return; | ||
} | ||
} | ||
|
||
componentValue.walk((entry, nodeIndex) => { | ||
if (typeof nodeIndex === 'string') { | ||
// Should never happen in FunctionNode | ||
return; | ||
} | ||
|
||
const replacements = transformComponentValue(entry.node, tokens, result, postCSSNode, opts); | ||
if (replacements) { | ||
entry.parent.value.splice(nodeIndex, 1, ...replacements); | ||
didChangeSomething = true; | ||
return false; | ||
} | ||
}); | ||
}); | ||
|
||
if (!didChangeSomething) { | ||
return source; | ||
} | ||
|
||
return componentValues.map((x) => x.toString()).join(''); | ||
} | ||
|
||
|
||
function transformComponentValue(node: ComponentValue, tokens: Map<string, Token>, result: Result, postCSSNode: Node, opts: parsedPluginOptions) { | ||
if (!isFunctionNode(node)) { | ||
return; | ||
} | ||
|
||
if (node.nameTokenValue().toLowerCase() !== opts.valueFunctionName) { | ||
return; | ||
} | ||
|
||
let tokenName = ''; | ||
let operator = ''; | ||
let operatorSubject = ''; | ||
|
||
for (let i = 0; i < node.value.length; i++) { | ||
const subValue = node.value[i]; | ||
if (isWhitespaceNode(subValue) || isCommentNode(subValue)) { | ||
continue; | ||
} | ||
|
||
if ( | ||
!tokenName && | ||
isTokenNode(subValue) && | ||
subValue.value[0] === TokenType.String | ||
) { | ||
tokenName = subValue.value[4].value; | ||
continue; | ||
} | ||
|
||
if ( | ||
tokenName && | ||
!operator && | ||
isTokenNode(subValue) && | ||
subValue.value[0] === TokenType.Ident && | ||
subValue.value[4].value.toLowerCase() === 'to' | ||
) { | ||
operator = 'to'; | ||
continue; | ||
} | ||
|
||
if ( | ||
tokenName && | ||
operator && | ||
isTokenNode(subValue) && | ||
subValue.value[0] === TokenType.Ident | ||
) { | ||
operatorSubject = subValue.value[4].value; | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
|
||
if (!tokenName) { | ||
postCSSNode.warn(result, 'Expected at least a single string literal for the design-token function.'); | ||
return; | ||
} | ||
|
||
const replacement = tokens.get(tokenName); | ||
if (!replacement) { | ||
postCSSNode.warn(result, `design-token: "${tokenName}" is not configured.`); | ||
return; | ||
} | ||
|
||
if (!operator) { | ||
return parseComponentValues(replacement.cssValue()); | ||
} | ||
|
||
const transformOptions: TokenTransformOptions = { | ||
pluginOptions: opts.unitsAndValues, | ||
}; | ||
|
||
if (operator === 'to') { | ||
if (!operatorSubject) { | ||
postCSSNode.warn(result, `Invalid or missing unit in "${node.toString()}"`); | ||
return; | ||
} | ||
|
||
transformOptions.toUnit = operatorSubject; | ||
|
||
try { | ||
return parseComponentValues(replacement.cssValue(transformOptions)); | ||
} catch (err) { | ||
postCSSNode.warn(result, (err as Error).message); | ||
return; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.