Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aa4449
commit 6e6db13
Showing
6 changed files
with
211 additions
and
3 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
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,64 @@ | ||
# function-no-interpolation | ||
|
||
Since the release of [first-class `calc()`](https://sass-lang.com/documentation/values/calculations/), | ||
calculation functions `calc()`, `clamp()`, `min()`, and `max()` accept variables | ||
and function calls as arguments. | ||
|
||
This rule forbids interpolation in `calc()`, `clamp()`, `min()`, and `max()` | ||
functions to avoid extra verbose or even invalid CSS. | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
.a { .b: calc(#{$c} + 1); } | ||
/** ↑ | ||
* This argument */ | ||
``` | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered warnings: | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$c: 1; | ||
.a { .b: calc(#{$c + 1}); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$c: 1; | ||
.a { .b: calc(max(#{$c})); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$c: 1; | ||
.a { .b: min(#{$c}); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$c: 1; | ||
.a { .b: clamp(#{$c} + 2px); } | ||
``` | ||
|
||
The following patterns are _not_ considered warnings: | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
.a { .b: calc(1 + 1); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$c: 1; | ||
.a { .b: abc(#{$c} + 1px); } | ||
``` | ||
|
||
<!-- prettier-ignore --> | ||
```scss | ||
$c: 1; | ||
.a { .b: calc(abc(#{$c})); } | ||
``` |
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,82 @@ | ||
"use strict"; | ||
|
||
const { messages, ruleName } = require(".."); | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true], | ||
customSyntax: "postcss-scss", | ||
|
||
accept: [ | ||
{ | ||
code: `.a { .b: calc(1 + 1); }`, | ||
description: "`calc` function, no interpolation" | ||
}, | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: abc(#{$c} + 1px); } | ||
`, | ||
description: "Allowed function with interpolation" | ||
}, | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: calc(abc(#{$c})); } | ||
`, | ||
description: "Allowed function with interpolation nested in `calc`" | ||
} | ||
], | ||
reject: [ | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: calc(#{$c} + 1); } | ||
`, | ||
line: 3, | ||
column: 12, | ||
message: messages.rejected("calc"), | ||
description: "`calc` function one argument interpolated" | ||
}, | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: calc(#{$c + 1}); } | ||
`, | ||
line: 3, | ||
column: 12, | ||
message: messages.rejected("calc"), | ||
description: "`calc` function all arguments interpolated" | ||
}, | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: calc(max(#{$c})); } | ||
`, | ||
line: 3, | ||
column: 12, | ||
message: messages.rejected("max"), | ||
description: "`max` function with interpolation" | ||
}, | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: min(#{$c} + 1px); } | ||
`, | ||
line: 3, | ||
column: 12, | ||
message: messages.rejected("min"), | ||
description: "`max` function with interpolation" | ||
}, | ||
{ | ||
code: ` | ||
$c: 1; | ||
.a { .b: clamp(#{$c} + #{$d}); } | ||
`, | ||
line: 3, | ||
column: 12, | ||
message: messages.rejected("clamp"), | ||
description: "`clamp` function with interpolation" | ||
} | ||
] | ||
}); |
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,56 @@ | ||
"use strict"; | ||
|
||
const { utils } = require("stylelint"); | ||
const namespace = require("../../utils/namespace"); | ||
const ruleUrl = require("../../utils/ruleUrl"); | ||
const valueParser = require("postcss-value-parser"); | ||
|
||
const ruleName = namespace("function-no-interpolation"); | ||
|
||
const messages = utils.ruleMessages(ruleName, { | ||
rejected: func => `Unexpected interpolation in "${func}".` | ||
}); | ||
|
||
const meta = { | ||
url: ruleUrl(ruleName) | ||
}; | ||
|
||
function rule(actual) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { actual }); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
const calculationFunctions = ["calc", "max", "min", "clamp"]; | ||
|
||
root.walkDecls(decl => { | ||
valueParser(decl.value).walk(node => { | ||
if (node.type !== "function" || node.value === "") { | ||
return; | ||
} | ||
if ( | ||
calculationFunctions.includes(node.value) && | ||
node.nodes.some( | ||
args => args.type === "word" && /^#{.*|\s*}/.test(args.value) | ||
) | ||
) { | ||
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