Skip to content
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

Fix isStandardSyntaxFunction false positives for interpolation and backticks in CSS-in-JS #6565

Merged
merged 3 commits into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/rules/function-no-unknown/__tests__/index.js
Expand Up @@ -70,6 +70,22 @@ testRule({
],
});

testRule({
skip: true,
ruleName,
config: true,
customSyntax: 'postcss-styled-syntax',

accept: [
{
code: 'const buttonFontSize = css`font-size: ${({ size }) => (size === "small") ? "0.8em" : "1em"};`;',
},
{
code: 'const buttonFontSize = css`border-radius: ${`calc(${token.radiusBase} + 2px)`};`;',
},
],
});

ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
testRule({
ruleName,
config: [true, { ignoreFunctions: ['theme', '/^foo-/', /^bar$/i] }],
Expand Down
24 changes: 24 additions & 0 deletions lib/utils/__tests__/isStandardSyntaxFunction.test.js
Expand Up @@ -26,6 +26,30 @@ describe('isStandardSyntaxFunction', () => {
false,
);
});

it('CSS-in-JS interpolation', () => {
const functions = [];

valueParser('${({ size }) => (size === "small") ? "0.8em" : "1em"}').walk((valueNode) => {
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
if (valueNode.type === 'function') {
functions.push(valueNode);
}
});

expect(isStandardSyntaxFunction(functions[0])).toBe(false);
});

it('CSS-in-JS syntax', () => {
const functions = [];

valueParser('`calc(${token.radiusBase} + 2px)`').walk((valueNode) => {
if (valueNode.type === 'function') {
functions.push(valueNode);
}
});

expect(isStandardSyntaxFunction(functions[0])).toBe(false);
});
});

function func(css) {
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/isStandardSyntaxFunction.js
Expand Up @@ -16,5 +16,15 @@ module.exports = function isStandardSyntaxFunction(node) {
return false;
}

// CSS-in-JS interpolation
if (node.value.startsWith('${')) {
return false;
}

// CSS-in-JS syntax
if (node.value.startsWith('`')) {
return false;
}

return true;
};