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 option to function-no-unknown #5901

Merged
merged 3 commits into from Feb 10, 2022
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
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