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 1 commit
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
43 changes: 43 additions & 0 deletions lib/rules/function-no-unknown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,46 @@ 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 considered problems:

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

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

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

ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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,
},
],
});
30 changes: 28 additions & 2 deletions lib/rules/function-no-unknown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const valueParser = require('postcss-value-parser');
const functionsListPath = require('css-functions-list');

const declarationValueIndex = require('../../utils/declarationValueIndex');
const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
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,14 +25,34 @@ 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;
}

/** @type {Array<string | RegExp>} */
const ignoreFunctions = (secondaryOptions && secondaryOptions.ignoreFunctions) || [];

/** @type {(funcName: string) => boolean} */
const isFunctionIgnored =
ignoreFunctions.length === 0
? () => false
: (funcName) => Boolean(matchesStringOrRegExp(funcName, ignoreFunctions));

ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
const path = new URL(functionsListPath.toString(), 'file://');
const functionsList = JSON.parse(fs.readFileSync(path, 'utf8'));

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

if (isFunctionIgnored(node.value)) {
return;
}

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