Skip to content

Commit

Permalink
Add ignoreFunctions: [] to length-zero-no-unit (#5314)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeddy3 committed May 19, 2021
1 parent 9152ab4 commit 492ae33
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/rules/length-zero-no-unit/README.md
Expand Up @@ -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:

<!-- prettier-ignore -->
```css
a { top: var(--foo, 0px); }
```

<!-- prettier-ignore -->
```css
a { top: --bar(0px); }
```
24 changes: 24 additions & 0 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Expand Up @@ -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],
Expand Down
9 changes: 9 additions & 0 deletions lib/rules/length-zero-no-unit/index.js
Expand Up @@ -2,6 +2,7 @@

'use strict';

const _ = require('lodash');
const valueParser = require('postcss-value-parser');

const atRuleParamIndex = require('../../utils/atRuleParamIndex');
Expand Down Expand Up @@ -37,6 +38,7 @@ function rule(primary, secondary, context) {
actual: secondary,
possible: {
ignore: ['custom-properties'],
ignoreFunctions: [_.isString, _.isRegExp],
},
optional: true,
},
Expand All @@ -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);
Expand Down Expand Up @@ -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';
}
Expand Down

0 comments on commit 492ae33

Please sign in to comment.