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 interpolation in property name in custom-property-pattern #5949

Merged
merged 8 commits into from
Mar 7, 2022
11 changes: 10 additions & 1 deletion lib/rules/custom-property-pattern/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ testRule({
code: 'a { color: var($foo-color); }',
},
{
code: 'a { color: var(tokens.$bar-color); }',
code: 'a { color: var(bar.$baz); }',
},
{
code: 'a { --#{$bar}: 0; }',
},
{
code: 'a { color: var( #{$bar} ); }',
},
{
code: 'a { color: var( --#{$bar} ); }',
},
],
});
25 changes: 15 additions & 10 deletions lib/rules/custom-property-pattern/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const validateOptions = require('../../utils/validateOptions');
const declarationValueIndex = require('../../utils/declarationValueIndex');
const { isRegExp, isString } = require('../../utils/validateTypes');
const { isValueFunction } = require('../../utils/typeGuards');
const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');

const ruleName = 'custom-property-pattern';

Expand All @@ -33,6 +34,18 @@ const rule = (primary) => {

const regexpPattern = isString(primary) ? new RegExp(primary) : primary;

/**
* @param {string} property
* @returns {boolean}
*/
function check(property) {
return (
!isStandardSyntaxProperty(property) ||
!isCustomProperty(property) ||
regexpPattern.test(property.slice(2))
);
}

root.walkDecls((decl) => {
const { prop, value } = decl;

Expand All @@ -47,20 +60,12 @@ const rule = (primary) => {

const { value: firstNodeValue } = nodes[0];

if (!isCustomProperty(firstNodeValue)) return;

if (regexpPattern.test(firstNodeValue.slice(2))) return;
if (check(firstNodeValue)) return;

complain(declarationValueIndex(decl) + sourceIndex, decl);
});

if (!isCustomProperty(prop)) {
return;
}

if (regexpPattern.test(prop.slice(2))) {
return;
}
if (check(prop)) return;

complain(0, decl);
});
Expand Down