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 #5314

Merged
merged 3 commits into from May 19, 2021
Merged
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
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 @@ -600,6 +600,30 @@ testRule({
],
});

testRule({
ruleName,
config: [true, { ignoreFunctions: ['var', /^--/] }],
jeddy3 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -150,6 +155,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