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

at-function-named-arguments: Add ignoreFunctions option #269

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,5 +1,6 @@
# HEAD

- Added: `at-function-named-arguments` add `ignoreFunctions` option.
- Fixed: `at-function-named-arguments` correctly parse data uris as function parameters.

# 3.2.0
Expand Down
45 changes: 37 additions & 8 deletions src/rules/at-function-named-arguments/README.md
Expand Up @@ -19,22 +19,22 @@ The following patterns are considered warnings:
```scss
.foo {
animation: animation(250ms, 100ms, infinite);
}
}
```

```scss
.foo {
animation: animation(250ms);
}
}
```

```scss
.foo {
border: reset($value: 20, 'bar', $color: #FFF);
border: reset($value: 20, "bar", $color: #fff);
}
```

The following patterns are *not* considered warnings:
The following patterns are _not_ considered warnings:

```scss
.foo {
Expand Down Expand Up @@ -66,28 +66,29 @@ The following patterns are considered warnings:

```scss
.foo {
border: reset($value: 20, 'bar', $color: #FFF);
border: reset($value: 20, "bar", $color: #fff);
}
```

The following patterns are *not* considered warnings:
The following patterns are _not_ considered warnings:

```scss
.foo {
animation: animation(250ms, 100ms, infinite);
}
}
```

## Optional secondary options

### `"ignore": ["single-argument"]`

Given:

```json
{ "ignore": ["single-argument"] }
```

The following patterns are *not* considered warnings:
The following patterns are _not_ considered warnings:

```scss
.foo {
Expand All @@ -100,3 +101,31 @@ The following patterns are *not* considered warnings:
@include reset(20);
}
```

### `ignoreFunctions: ["/regex/", "string"]`

Given:

```js
["always", { ignoreFunctions: ["/^my-/i", "custom"] }];
```

The following patterns are _not_ considered warnings:

```scss
.foo {
border: custom(20, 30);
}
```

```scss
.foo {
border: my-func(20, 30);
}
```

```scss
.foo {
border: MY-FUNC(20, 30);
}
```
152 changes: 152 additions & 0 deletions src/rules/at-function-named-arguments/__tests__/index.js
Expand Up @@ -915,3 +915,155 @@ testRule(rule, {
}
]
});

testRule(rule, {
ruleName,
config: ["always", { ignoreFunctions: ["map-get", "/^my-/i", "/funct$/"] }],
syntax: "scss",

accept: [
{
code: `
.b {
border: reset($value: 40px);
}
`,
description:
"Always and ignore function. Example: single argument is named."
},
{
code: `
.b {
content: map-get($map, key);
}
`,
description: "Always and ignore function: ignored function."
},
{
code: `
.b {
content: my-func($map, key);
}
`,
description: "Always and ignore function: ignored function."
},
{
code: `
.b {
content: MY-FUNC($map, key);
}
`,
description: "Always and ignore function: ignored function."
},
{
code: `
.b {
content: ffunct($map, key);
}
`,
description: "Always and ignore function: ignored function."
}
],

reject: [
{
code: `
.b {
border: reset(40px);
}
`,
line: 3,
column: 9,
message: messages.expected,
description:
"Always and ignore function. Example: single argument that is not named."
},
{
code: `
.b {
content: FFUNCT($map, key);
}
`,
line: 3,
column: 9,
message: messages.expected,
description:
"Always and ignore function. Example: function name's case does not match regex."
},
{
code: `
.b {
content: fmap-get($map, key);
}
`,
line: 3,
column: 9,
message: messages.expected,
description:
"Always and ignore function. Example: function name does not match string or regex."
}
]
});

testRule(rule, {
ruleName,
config: ["never", { ignoreFunctions: ["somefunc", "/^my-/", "/funct$/"] }],
syntax: "scss",

accept: [
{
code: `
.b {
border: reset(40px);
}
`,
description: "Never. Example: single argument that is not named."
},
{
code: `
.b {
border: reset(40px, 10px);
}
`,
description: "Never. Example: multiple arguments that are not named."
},
{
code: `
.b {
content: somefunc($key: 1, $key2: 2);
}
`,
description: "Never and ignore function: ignored function."
},
{
code: `
.b {
content: my-func($key: 1, $key2: 2);
}
`,
description: "Never and ignore function: ignored function."
},
{
code: `
.b {
content: ffunct($key: 1, $key2: 2);
}
`,
description: "Never and ignore function: ignored function."
}
],

reject: [
{
code: `
.b {
border: reset($value: 40px);
}
`,
line: 3,
column: 9,
message: messages.rejected,
description: "Never. Example: single argument is named."
}
]
});
22 changes: 21 additions & 1 deletion src/rules/at-function-named-arguments/index.js
Expand Up @@ -5,6 +5,7 @@ import {
isNativeCssFunction,
parseFunctionArguments
} from "../../utils";
import { isString } from "lodash";
import valueParser from "postcss-value-parser";

export const ruleName = namespace("at-function-named-arguments");
Expand All @@ -28,7 +29,8 @@ export default function(expectation, options) {
{
actual: options,
possible: {
ignore: ["single-argument"]
ignore: ["single-argument"],
ignoreFunctions: [isString]
},
optional: true
}
Expand All @@ -52,6 +54,24 @@ export default function(expectation, options) {
return;
}

const hasFuncIgnored =
options &&
options.ignoreFunctions &&
options.ignoreFunctions.some(f => {
const isRegex = /^\/.*\//.test(f);

if (!isRegex) {
return f === node.value;
}

const parts = f.split("/");
return new RegExp(parts[1], parts[2] || "").test(node.value);
});

if (hasFuncIgnored) {
return;
}

const args = parseFunctionArguments(decl.value);
const isSingleArgument = args.length === 1;

Expand Down