diff --git a/lib/rules/length-zero-no-unit/README.md b/lib/rules/length-zero-no-unit/README.md index 0b3591f7fc..f0e749b6d9 100644 --- a/lib/rules/length-zero-no-unit/README.md +++ b/lib/rules/length-zero-no-unit/README.md @@ -67,3 +67,23 @@ The following pattern is _not_ considered a violation: ```css a { --x: 0px; } ``` + +### `ignoreFunctions: ["/regex/", /regex/, "string"]` + +Given: + +```json +["var", "/^--/"] +``` + +The following patterns are _not_ considered violations: + + +```css +a { top: var(--foo, 0px); } +``` + + +```css +a { top: --bar(0px); } +``` diff --git a/lib/rules/length-zero-no-unit/__tests__/index.js b/lib/rules/length-zero-no-unit/__tests__/index.js index d14d5def39..9c0685890a 100644 --- a/lib/rules/length-zero-no-unit/__tests__/index.js +++ b/lib/rules/length-zero-no-unit/__tests__/index.js @@ -608,6 +608,30 @@ testRule({ ], }); +testRule({ + ruleName, + config: [true, { ignoreFunctions: ['var', /^--/] }], + fix: true, + accept: [ + { + code: 'a { top: var(--foo, 0px); }', + }, + { + code: 'a { top: --bar(0px); }', + }, + ], + reject: [ + { + code: 'a { margin: var(--foo, 0px) 0px --bar(0px); }', + fixed: 'a { margin: var(--foo, 0px) 0 --bar(0px); }', + + message: messages.rejected, + line: 1, + column: 30, + }, + ], +}); + testRule({ ruleName, config: [true], diff --git a/lib/rules/length-zero-no-unit/index.js b/lib/rules/length-zero-no-unit/index.js index 5daf8e8eb2..aad1ea58f2 100644 --- a/lib/rules/length-zero-no-unit/index.js +++ b/lib/rules/length-zero-no-unit/index.js @@ -2,6 +2,7 @@ 'use strict'; +const _ = require('lodash'); const valueParser = require('postcss-value-parser'); const atRuleParamIndex = require('../../utils/atRuleParamIndex'); @@ -37,6 +38,7 @@ function rule(primary, secondary, context) { actual: secondary, possible: { ignore: ['custom-properties'], + ignoreFunctions: [_.isString, _.isRegExp], }, optional: true, }, @@ -51,6 +53,9 @@ function rule(primary, secondary, context) { if (isMathFunction(valueNode)) return false; + if (isFunction(valueNode) && optionsMatches(secondary, 'ignoreFunctions', value)) + return false; + if (!isWord(valueNode)) return; const numberUnit = valueParser.unit(value); @@ -156,6 +161,10 @@ function isLength(unit) { return keywordSets.lengthUnits.has(unit.toLowerCase()); } +function isFunction({ type }) { + return type === 'function'; +} + function isFraction(unit) { return unit.toLowerCase() === 'fr'; }