Skip to content

Commit

Permalink
Add ignoreFunctions option to function-no-unknown (#5901)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Feb 10, 2022
1 parent 95b686e commit 57bc6ef
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/rules/function-no-unknown/README.md
Expand Up @@ -35,3 +35,29 @@ a { transform: scale(1); }
```css
a { transform: --custom-function(1); }
```

## Optional secondary options

### `ignoreFunctions: ["/regex/", /regex/, "non-regex"]`

Ignore the specified functions.

For example, with `true`.

Given:

```json
["theme", "/^foo-/"]
```

The following patterns are _not_ considered problems:

<!-- prettier-ignore -->
```css
a { transform: theme(1); }
```

<!-- prettier-ignore -->
```css
a { transform: foo-func(1); }
```
39 changes: 39 additions & 0 deletions lib/rules/function-no-unknown/__tests__/index.js
Expand Up @@ -56,3 +56,42 @@ testRule({
},
],
});

testRule({
ruleName,
config: [true, { ignoreFunctions: ['theme', '/^foo-/', /^bar$/i] }],
skipBasicChecks: true,

accept: [
{
code: 'a { transform: translate(1px); }',
},
{
code: 'a { transform: theme(1px); }',
},
{
code: 'a { transform: foo-func(1px); }',
},
{
code: 'a { transform: bar(1px); }',
},
{
code: 'a { transform: BAR(1px); }',
},
],

reject: [
{
code: 'a { transform: unknown(1px); }',
message: messages.rejected('unknown'),
line: 1,
column: 16,
},
{
code: 'a { transform: theme-custom(1px); }',
message: messages.rejected('theme-custom'),
line: 1,
column: 16,
},
],
});
21 changes: 19 additions & 2 deletions lib/rules/function-no-unknown/index.js
Expand Up @@ -6,11 +6,13 @@ const valueParser = require('postcss-value-parser');
const functionsListPath = require('css-functions-list');

const declarationValueIndex = require('../../utils/declarationValueIndex');
const optionsMatches = require('../../utils/optionsMatches');
const report = require('../../utils/report');
const ruleMessages = require('../../utils/ruleMessages');
const validateOptions = require('../../utils/validateOptions');
const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
const isCustomFunction = require('../../utils/isCustomFunction');
const { isRegExp, isString } = require('../../utils/validateTypes');

const ruleName = 'function-no-unknown';

Expand All @@ -23,9 +25,20 @@ const meta = {
};

/** @type {import('stylelint').Rule} */
const rule = (primary) => {
const rule = (primary, secondaryOptions) => {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual: primary });
const validOptions = validateOptions(
result,
ruleName,
{ actual: primary },
{
actual: secondaryOptions,
possible: {
ignoreFunctions: [isString, isRegExp],
},
optional: true,
},
);

if (!validOptions) {
return;
Expand All @@ -50,6 +63,10 @@ const rule = (primary) => {
return;
}

if (optionsMatches(secondaryOptions, 'ignoreFunctions', node.value)) {
return;
}

if (functionsList.includes(node.value.toLowerCase())) {
return;
}
Expand Down

0 comments on commit 57bc6ef

Please sign in to comment.