Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use scalar-color function instead of other color adjustment functions
- Loading branch information
1 parent
90a2f93
commit 74bb488
Showing
4 changed files
with
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# function-quote-no-quoted-strings-inside | ||
|
||
Disallow quoted strings inside the [quote function](https://sass-lang.com/documentation/functions/string#quote) | ||
|
||
```scss | ||
p { | ||
color: saturate(blue, 20%); | ||
/** ↑ ↑ | ||
* This function should be scalar-color | ||
*/ | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered violations: | ||
|
||
```scss | ||
p { | ||
color: saturate(blue, 20%); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: desaturate(blue, 20%); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: darken(blue, .2); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: lighten(blue, .2); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: opacify(blue, .2); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: fade-in(blue, .2); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: transparentize(blue, .2); | ||
} | ||
``` | ||
|
||
```scss | ||
p { | ||
color: fade-out(blue, .2); | ||
} | ||
``` | ||
|
||
The following patterns are _not_ considered violations: | ||
|
||
```scss | ||
p { | ||
color: scale-color(blue, $alpha: -40%); | ||
} | ||
``` |
93 changes: 93 additions & 0 deletions
93
src/rules/function-scalar-color-use-always/__tests__/index.js
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,93 @@ | ||
import rule, { ruleName, messages } from ".."; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
syntax: "scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
p { | ||
color: scale-color(blue, $alpha: -40%); | ||
} | ||
`, | ||
description: "accepts the scalar-color function" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
p { | ||
color: saturate(blue, 20%); | ||
} | ||
`, | ||
description: "does not accept the saturate function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: desaturate(blue, 20%); | ||
} | ||
`, | ||
description: "does not accept the desaturate function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: darken(blue, .2); | ||
} | ||
`, | ||
description: "does not accept the darken function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: lighten(blue, .2); | ||
} | ||
`, | ||
description: "does not accept the lighten function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: opacify(blue, .2); | ||
} | ||
`, | ||
description: "does not accept the opacify function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: fade-in(blue, .2); | ||
} | ||
`, | ||
description: "does not accept the fade-in function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: transparentize(blue, .2); | ||
} | ||
`, | ||
description: "does not accept the transparentize function", | ||
message: messages.rejected | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
color: fade-out(blue, .2); | ||
} | ||
`, | ||
description: "does not accept the fade-out function", | ||
message: messages.rejected | ||
} | ||
] | ||
}); |
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,52 @@ | ||
import { utils } from "stylelint"; | ||
import { namespace } from "../../utils"; | ||
import valueParser from "postcss-value-parser"; | ||
|
||
export const ruleName = namespace("function-scalar-color-use-always"); | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
rejected: "Use the scalar function." | ||
}); | ||
|
||
const function_names = [ | ||
"saturate", | ||
"desaturate", | ||
"darken", | ||
"lighten", | ||
"opacify", | ||
"fade-in", | ||
"transparentize", | ||
"fade-out" | ||
]; | ||
|
||
function rule(primary) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { | ||
actual: primary | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkDecls(decl => { | ||
valueParser(decl.value).walk(node => { | ||
// Verify that we're only looking at functions. | ||
if (node.type !== "function" || node.value === "") { | ||
return; | ||
} | ||
|
||
if (function_names.includes(node.value)) { | ||
utils.report({ | ||
message: messages.rejected, | ||
node: decl, | ||
result, | ||
ruleName | ||
}); | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
export default 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