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 unit-disallowed-list #6592

Merged
merged 3 commits into from Jan 29, 2023
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
5 changes: 5 additions & 0 deletions .changeset/shy-ladybugs-bathe.md
@@ -0,0 +1,5 @@
---
"stylelint": minor
---

Added: `ignoreFunctions: []` to `unit-disallowed-list`
24 changes: 24 additions & 0 deletions lib/rules/unit-disallowed-list/README.md
Expand Up @@ -157,3 +157,27 @@ The following patterns are considered problems:
```css
@media print and (max-resolution: 100dpi) {}
```

### `ignoreFunctions: ["function", "/regex/", /regex/]|"function"|"/regex/"|/regex/`

Ignore units that are inside of the specified functions.

For example, with `["px"]`.

Given:

```json
["calc", "/^translate/"]
```

The following patterns are _not_ considered problems:

<!-- prettier-ignore -->
```css
a { margin: calc(50% - 100px) }
```

<!-- prettier-ignore -->
```css
a { transform: translateX(100px) }
```
42 changes: 42 additions & 0 deletions lib/rules/unit-disallowed-list/__tests__/index.js
Expand Up @@ -388,6 +388,48 @@ testRule({
],
});

testRule({
ruleName,

config: [
['px'],
{
ignoreFunctions: ['calc', /^translate/],
},
],

accept: [
{
code: 'a { margin: calc(50% - 100px) }',
},
{
code: 'a { transform: translate(100px, 100px) }',
},
{
code: 'a { transform: translateX(100px) }',
},
],

reject: [
{
code: 'a { margin: 100px }',
message: messages.rejected('px'),
line: 1,
column: 16,
endLine: 1,
endColumn: 18,
},
{
code: 'a { translate: 100px }',
message: messages.rejected('px'),
line: 1,
column: 19,
endLine: 1,
endColumn: 21,
},
],
});

testRule({
ruleName,

Expand Down
9 changes: 8 additions & 1 deletion lib/rules/unit-disallowed-list/index.js
Expand Up @@ -51,6 +51,7 @@ const rule = (primary, secondaryOptions) => {
optional: true,
actual: secondaryOptions,
possible: {
ignoreFunctions: [isString, isRegExp],
ignoreProperties: [validateObjectWithArrayProps(isString, isRegExp)],
ignoreMediaFeatureNames: [validateObjectWithArrayProps(isString, isRegExp)],
},
Expand Down Expand Up @@ -137,8 +138,14 @@ const rule = (primary, secondaryOptions) => {
value = value.replace(/\*/g, ',');

valueParser(value).walk((valueNode) => {
const valueLowerCase = valueNode.value.toLowerCase();

// Ignore wrong units within `url` function
if (valueNode.type === 'function' && valueNode.value.toLowerCase() === 'url') {
if (
valueNode.type === 'function' &&
(valueLowerCase === 'url' ||
optionsMatches(secondaryOptions, 'ignoreFunctions', valueLowerCase))
) {
return false;
}

Expand Down