Skip to content

Commit

Permalink
Merge pull request #269 from kristerkari/feature/at-function-named-ar…
Browse files Browse the repository at this point in the history
…guments-ignore-functions

at-function-named-arguments: Add ignoreFunctions option
  • Loading branch information
kristerkari committed Aug 12, 2018
2 parents ca633b3 + 3d6e892 commit 977d999
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 9 deletions.
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

0 comments on commit 977d999

Please sign in to comment.