forked from stylelint-scss/stylelint-scss
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function-disallowed-list rule (stylelint-scss#422)
- Loading branch information
1 parent
3684e7d
commit 91c34cc
Showing
5 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# function-disallowed-list | ||
|
||
Specify a list of disallowed functions. | ||
|
||
<!-- prettier-ignore --> | ||
```css | ||
a { margin-left: math.random(100); } | ||
/** ↑ | ||
* This function */ | ||
``` | ||
|
||
## Options | ||
|
||
`array|string|regex`: `["array", "of", "unprefixed", /functions/, "regex"]|"function"|"/regex/"|/regex/` | ||
|
||
If a string is surrounded with `"/"` (e.g. `"/^rgb/"`), it is interpreted as a regular expression. | ||
|
||
Given: | ||
|
||
```json | ||
["math.random", "double"] | ||
``` | ||
|
||
The following patterns are considered warnings: | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
a { margin-left: math.random(100); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@function double($num) {@return $num * 2;} | ||
a { | ||
margin-left: double($num); | ||
} | ||
``` | ||
|
||
The following patterns are _not_ considered warnings: | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
a { margin-left: math.abs(100); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@function timesTwo($num) {@return $num * 2;} | ||
a { | ||
margin-left: timesTwo($num); | ||
} | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
@function random($num) {@return $num;} | ||
a { | ||
margin-left: random($num); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"use strict"; | ||
|
||
const { messages, ruleName } = require(".."); | ||
|
||
// Testing single value | ||
testRule({ | ||
ruleName, | ||
config: ["math.random"], | ||
customSyntax: "postcss-scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
@use "sass:math" | ||
.a { margin-left: math.round(100.5); } | ||
`, | ||
description: "Math library function, not disallowed." | ||
} | ||
], | ||
reject: [ | ||
{ | ||
code: ` | ||
@use "sass:math" | ||
.a { margin-left: math.random(100); } | ||
`, | ||
line: 3, | ||
column: 18, | ||
message: messages.rejected("math.random"), | ||
description: "Math library function, disallowed." | ||
} | ||
] | ||
}); | ||
|
||
// Testing an array | ||
testRule({ | ||
ruleName, | ||
config: [["math.random", "test"]], | ||
customSyntax: "postcss-scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
@use "sass:math" | ||
.a { margin-left: math.round(100.5); } | ||
`, | ||
description: "Math library function, not disallowed." | ||
}, | ||
{ | ||
code: ` | ||
@function funcName($num){@return $num;} | ||
.a { margin-left: funcName(3); } | ||
`, | ||
description: "Declared function is not disallowed." | ||
} | ||
], | ||
reject: [ | ||
{ | ||
code: ` | ||
@use "sass:math" | ||
.a { margin-left: math.random(100); } | ||
`, | ||
line: 3, | ||
column: 16, | ||
message: messages.rejected("math.random"), | ||
description: "Math library function, disallowed." | ||
}, | ||
{ | ||
code: ` | ||
@function test($num){@return $num;} | ||
.a { margin-left: test(3); } | ||
`, | ||
line: 3, | ||
column: 14, | ||
message: messages.rejected("test"), | ||
description: "Declared function is disallowed." | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
"use strict"; | ||
|
||
const { utils } = require("stylelint"); | ||
const { isRegExp, isString } = require("../../utils/validateTypes"); | ||
const namespace = require("../../utils/namespace"); | ||
const ruleUrl = require("../../utils/ruleUrl"); | ||
const valueParser = require("postcss-value-parser"); | ||
const isNativeCssFunction = require("../../utils/isNativeCssFunction"); | ||
|
||
const ruleName = namespace("function-disallowed-list"); | ||
|
||
const messages = utils.ruleMessages(ruleName, { | ||
rejected: func => `Unexpected function "${func}"` | ||
}); | ||
|
||
const meta = { | ||
url: ruleUrl(ruleName) | ||
}; | ||
|
||
function rule(blacklistOption) { | ||
const blacklist = [].concat(blacklistOption); | ||
|
||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { | ||
actual: blacklistOption, | ||
possible: [isString, isRegExp] | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkDecls(decl => { | ||
valueParser(decl.value).walk(node => { | ||
if ( | ||
node.type !== "function" || | ||
isNativeCssFunction(node.value) || | ||
node.value === "" | ||
) { | ||
return; | ||
} | ||
blacklist.forEach(functionName => { | ||
if (node.value === functionName) { | ||
utils.report({ | ||
message: messages.rejected(node.value), | ||
node: decl, | ||
word: decl.name, | ||
result, | ||
ruleName | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
rule.ruleName = ruleName; | ||
rule.messages = messages; | ||
rule.meta = meta; | ||
|
||
module.exports = rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters