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

Add ignoreFunctions: [] to length-zero-no-unit #5082

Closed
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
45 changes: 45 additions & 0 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,48 @@ testRule({
},
],
});

testRule({
ruleName,
config: [true, { ignoreFunctions: ['var', '--custom'] }],
fix: true,
accept: [
{
code: '.foo { margin-top: var(--y, 0px); margin-bottom: --custom(0px); };',
description: 'Should ignore especified functions',
},
{
code: '.foo { margin-top: --custom1(var(--y, 0px)); margin-bottom: --custom(0px); };',
description: 'Should ignore especified inner functions #1',
},
],
reject: [
{
code: '.foo { margin-top: var(--y, 0px); margin-bottom: --custom1(0px); };',
fixed: '.foo { margin-top: var(--y, 0px); margin-bottom: --custom1(0); };',
description: 'Should error value in --custom1',

message: messages.rejected,
line: 1,
column: 61,
},
{
code: '.foo { margin-top: var(--y, --custom1(0px)); margin-bottom: --custom(0px); };',
fixed: '.foo { margin-top: var(--y, --custom1(0)); margin-bottom: --custom(0px); };',
description: 'Should error value in inner --custom1 call',

message: messages.rejected,
line: 1,
column: 40,
},
{
code: '.foo { margin-top: --custom(var1(--y, 0px)); margin-bottom: --custom(0px); };',
fixed: '.foo { margin-top: --custom(var1(--y, 0)); margin-bottom: --custom(0px); };',
description: 'Should error value in unspecified function',

message: messages.rejected,
line: 1,
column: 40,
},
],
});
22 changes: 18 additions & 4 deletions lib/rules/length-zero-no-unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@ const messages = ruleMessages(ruleName, {
rejected: 'Unexpected unit',
});

function rule(actual, secondary, context) {
function rule(actual, options, context) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(
result,
ruleName,
{ actual },
{
actual: options,
possible: {
ignore: ['custom-properties'],
ignoreFunctions: [_.isString],
},
optional: true,
},
);

if (!validOptions) {
return;
}

const ignoreFunctions = (options && options.ignoreFunctions) || [];

root.walkDecls((decl) => {
if (decl.prop.toLowerCase() === 'line-height') {
return;
Expand Down Expand Up @@ -59,7 +73,7 @@ function rule(actual, secondary, context) {
// TODO: Issue #4985
// eslint-disable-next-line no-shadow
const stringValue = valueParser.stringify(node);
const ignoreFlag = isMathFunction(node);
const ignoreFlag = isMathFunction(node) || ignoreFunctions.some((fn) => fn === node.value);

for (let i = 0; i < stringValue.length; i++) {
ignorableIndexes[node.sourceIndex + i] = ignoreFlag;
Expand All @@ -83,7 +97,7 @@ function rule(actual, secondary, context) {
});

function check(value, node, ignorableIndexes = []) {
if (optionsMatches(secondary, 'ignore', 'custom-properties') && isCustomProperty(value)) {
if (optionsMatches(options, 'ignore', 'custom-properties') && isCustomProperty(value)) {
return;
}

Expand Down