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 hex colours in color-function-notation #5650

Merged
merged 6 commits into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions lib/rules/color-function-notation/__tests__/index.js
Expand Up @@ -378,6 +378,9 @@ testRule({
{
code: 'a { color: rgb($a $b $c / $d) }',
},
{
code: 'a { color: rgba(#fff, 0.85) }',
},
],

reject: [
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/color-function-notation/index.js
Expand Up @@ -4,6 +4,7 @@ const valueParser = require('postcss-value-parser');

const declarationValueIndex = require('../../utils/declarationValueIndex');
const getDeclarationValue = require('../../utils/getDeclarationValue');
const isStandardSyntaxColorFunction = require('../../utils/isStandardSyntaxColorFunction');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const setDeclarationValue = require('../../utils/setDeclarationValue');
Expand Down Expand Up @@ -33,7 +34,7 @@ const rule = (primary, _secondaryOptions, context) => {
const parsedValue = valueParser(getDeclarationValue(decl));

parsedValue.walk((node) => {
if (node.type !== 'function') return;
if (!isStandardSyntaxColorFunction(node)) return;

const { value, sourceIndex, nodes } = node;

Expand Down
69 changes: 69 additions & 0 deletions lib/utils/__tests__/isStandardSyntaxColorFunction.test.js
@@ -0,0 +1,69 @@
'use strict';

const isStandardSyntaxColorFunction = require('../isStandardSyntaxColorFunction');
const postcss = require('postcss');
const valueParser = require('postcss-value-parser');

describe('isStandardSyntaxFunction', () => {
lachieh marked this conversation as resolved.
Show resolved Hide resolved
it('legacy syntax', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgb(0, 0, 0) }'))).toBe(true);
});

it('legacy syntax alpha', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgba(0, 0, 0, 0) }'))).toBe(true);
});

it('modern syntax', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgba(0 0 0) }'))).toBe(true);
});

it('modern syntax alpha', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgba(0 0 0 / 50%) }'))).toBe(true);
});

it('space', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgba( 0 0 0 / 50% ) }'))).toBe(true);
});

it('comment', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgba(/* #aaa */ 0 0 0 / 50%) }'))).toBe(
true,
);
});

it('custom property', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rgba(var(--red) 0 0 / 50%) }'))).toBe(
true,
);
});

it('function', () => {
expect(
isStandardSyntaxColorFunction(func('a { color: rgba(calc(var(--red) + 50) 0 0 / 50%) }')),
).toBe(true);
});

it('scss color function conversion', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rbga(#aaa, 0.5) }'))).toBe(false);
});
lachieh marked this conversation as resolved.
Show resolved Hide resolved

it('scss color function conversion with comment', () => {
expect(isStandardSyntaxColorFunction(func('a { color: rbga(/* comment */ #aaa, 0.5) }'))).toBe(
false,
);
});
});

function func(css) {
const functions = [];

postcss.parse(css).walkDecls((decl) => {
valueParser(decl.value).walk((valueNode) => {
if (valueNode.type === 'function') {
functions.push(valueNode);
}
});
});

return functions[0];
}
22 changes: 22 additions & 0 deletions lib/utils/isStandardSyntaxColorFunction.js
@@ -0,0 +1,22 @@
'use strict';

const isStandardSyntaxFunction = require('./isStandardSyntaxFunction');

/**
* Check whether a function is standard syntax color function
*
* @param {import('postcss-value-parser').Node} node
* @returns { node is import('postcss-value-parser').FunctionNode }
*/
module.exports = function (node) {
lachieh marked this conversation as resolved.
Show resolved Hide resolved
if (node.type !== 'function') return false;
lachieh marked this conversation as resolved.
Show resolved Hide resolved

if (!isStandardSyntaxFunction(node)) return false;

// scss rgba() function can accept a hex as the first param
for (const [, fnNode] of node.nodes.entries()) {
lachieh marked this conversation as resolved.
Show resolved Hide resolved
if (fnNode.type === 'word' && fnNode.value.startsWith('#')) return false;
Copy link

@kamechb kamechb Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use a color variable like $black in the function, do we handle that case? In that case, it still reports an error which is not expected.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kamechb See #5671.

That issue is labelled as ready to implement. Please consider contributing if you have time.

There are steps on how to fix a bug in a rule in the Developer guide.

}

return true;
};