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 maps in property-no-unknown #5690

Merged
merged 9 commits into from Nov 13, 2021
4 changes: 4 additions & 0 deletions lib/rules/color-no-invalid-hex/__tests__/index.js
Expand Up @@ -61,6 +61,10 @@ testRule({
'font-style: normal;\n' +
'}',
},
{
code: 'a { color: #colors[somecolor]; }',
description: 'Less map usage',
},
Copy link
Member

@jeddy3 jeddy3 Nov 8, 2021

Choose a reason for hiding this comment

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

Let's move this into its own testRule case (towards the bottom of the file), which sets the custom syntax to postcss-less as testRule without syntax should only include vanilla CSS constructs.

testRule({
	ruleName,
	config: [true],
	customSyntax: 'postcss-less',
	accept: [
		{
			code: 'a { color: #colors[somecolor]; }',
			description: 'Less map usage',
		},
	],
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Sure, as soon as I have at least some time.

],

reject: [
Expand Down
5 changes: 5 additions & 0 deletions lib/rules/color-no-invalid-hex/index.js
@@ -1,6 +1,7 @@
'use strict';

const declarationValueIndex = require('../../utils/declarationValueIndex');
const isStandardSyntaxHexColor = require('../../utils/isStandardSyntaxHexColor');
const isValidHex = require('../../utils/isValidHex');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
Expand All @@ -23,6 +24,10 @@ const rule = (primary) => {
}

root.walkDecls((decl) => {
if (!isStandardSyntaxHexColor(decl.value)) {
return;
}

valueParser(decl.value).walk(({ value, type, sourceIndex }) => {
if (type === 'function' && value.endsWith('url')) return false;

Expand Down
4 changes: 4 additions & 0 deletions lib/utils/__tests__/isStandardSyntaxDeclaration.test.js
Expand Up @@ -139,6 +139,10 @@ describe('isStandardSyntaxDeclaration', () => {
expect(isStandardSyntaxDeclaration(lessDecl('@map: { key: value; }'))).toBe(false);
});

it('less another map', () => {
expect(isStandardSyntaxDeclaration(lessDecl('#my-map() { key: value; }'))).toBe(false);
});

it('scss map declaration', () => {
expect(isStandardSyntaxDeclaration(scssDecl('$foo: (key: value, key2: value2)'))).toBe(false);
});
Expand Down
12 changes: 12 additions & 0 deletions lib/utils/__tests__/isStandardSyntaxHexColor.test.js
@@ -0,0 +1,12 @@
'use strict';

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

describe('isStandardSyntaxHexColor', () => {
it('default hex', () => {
expect(isStandardSyntaxHexColor('#000000')).toBeTruthy();
});
it('less map usage', () => {
expect(isStandardSyntaxHexColor('#prop[someprop]')).toBeFalsy();
strigefleur marked this conversation as resolved.
Show resolved Hide resolved
});
});
11 changes: 11 additions & 0 deletions lib/utils/isStandardSyntaxDeclaration.js
Expand Up @@ -46,6 +46,17 @@ module.exports = function (decl) {
return false;
}

// Less map (e.g. #my-map() { myprop: red; })
if (
parent &&
isRule(parent) &&
parent.selector &&
parent.selector[0] === '#' &&
parent.selector[parent.selector.length - 1] === ')'
strigefleur marked this conversation as resolved.
Show resolved Hide resolved
) {
return false;
}

// Sass nested properties (e.g. border: { style: solid; color: red; })
if (
parent &&
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/isStandardSyntaxHexColor.js
@@ -0,0 +1,16 @@
'use strict';

/**
* Check whether a hex color is standard
*
* @param {string} property
strigefleur marked this conversation as resolved.
Show resolved Hide resolved
* @returns {boolean}
*/
module.exports = function (property) {
strigefleur marked this conversation as resolved.
Show resolved Hide resolved
// Less map usage (e.g. .myclass { color: #colors[somecolor]; })
if (property.includes('[')) {
strigefleur marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

return true;
};