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 false positives for variables and color functions in color-function-notation #5793

Merged
merged 5 commits into from Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 25 additions & 9 deletions lib/rules/color-function-notation/__tests__/index.js
Expand Up @@ -108,6 +108,13 @@ testRule({
line: 1,
column: 12,
},
{
code: 'a { color: rgba(var(--r, 20), var(--g), var(--b), 0.6); }',
fixed: 'a { color: rgb(var(--r, 20) var(--g) var(--b) / 0.6); }',
message: messages.expected('modern'),
line: 1,
column: 12,
},
{
code: stripIndent`
a {
Expand Down Expand Up @@ -378,9 +385,21 @@ testRule({
{
code: 'a { color: rgb($a $b $c / $d) }',
},
{
code: 'a { color: rgb($a, $b, $c, $d) }',
},
{
code: 'a { color: rgb($color-var, 50%); }',
},
{
code: 'a { color: rgba(color.mix(#000, #fff, 35%), 0.6); }',
},
{
code: 'a { color: rgba(#fff, 0.85) }',
},
{
code: 'a { color: rgba($a, $b, $c, $d) }',
},
],

reject: [
Expand All @@ -392,11 +411,11 @@ testRule({
column: 5,
},
{
code: 'a { color: rgba($a, $b, $c, $d) }',
fixed: 'a { color: rgb($a $b $c / $d) }',
code: '$a: rgb(0, 0, 0, 0);',
fixed: '$a: rgb(0 0 0 / 0);',
message: messages.expected('modern'),
line: 1,
column: 12,
column: 5,
},
],
});
Expand All @@ -416,6 +435,9 @@ testRule({
{
code: 'a { color: rgba($a, $b, $c, $d) }',
},
{
code: 'a { color: rgb($a $b $c / $d) }',
},
],

reject: [
Expand All @@ -425,11 +447,5 @@ testRule({
line: 1,
column: 5,
},
{
code: 'a { color: rgb($a $b $c / $d) }',
message: messages.expected('legacy'),
line: 1,
column: 12,
},
],
});
14 changes: 14 additions & 0 deletions lib/utils/__tests__/isStandardSyntaxColorFunction.test.js
Expand Up @@ -52,6 +52,20 @@ describe('isStandardSyntaxColorFunction', () => {
false,
);
});

it('scss variable', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rbga($var, 0.5) }'))).toBe(false);
});

it('scss variable in last param', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rbg(0 0 0 / $var) }'))).toBe(false);
});

it('scss nested function', () => {
expect(
isStandardSyntaxColorFunction(func('a { color: rgba(color.mix(#000, #fff, 35%), 0.6); }')),
).toBe(false);
});
});

function func(css) {
Expand Down
7 changes: 5 additions & 2 deletions lib/utils/isStandardSyntaxColorFunction.js
Expand Up @@ -11,9 +11,12 @@ const isStandardSyntaxFunction = require('./isStandardSyntaxFunction');
module.exports = function isStandardSyntaxColorFunction(node) {
if (!isStandardSyntaxFunction(node)) return false;

// scss rgba() function can accept a hex as the first param
// scss can accept a #hex, or $var variables and we need to check all nested fn nodes
for (const fnNode of node.nodes) {
if (fnNode.type === 'word' && fnNode.value.startsWith('#')) return false;
if (fnNode.type === 'function') return isStandardSyntaxColorFunction(fnNode);

if (fnNode.type === 'word' && (fnNode.value.startsWith('#') || fnNode.value.startsWith('$')))
return false;
}

return true;
Expand Down