Skip to content

Commit

Permalink
Fix false positives for variables and color functions in color-functi…
Browse files Browse the repository at this point in the history
…on-notation (#5793)
  • Loading branch information
lachieh committed Dec 21, 2021
1 parent b9785cd commit 217e685
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
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

0 comments on commit 217e685

Please sign in to comment.