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
Showing
5 changed files
with
165 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
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,38 @@ | ||
# no-dollar-variables | ||
|
||
Disallow dollar variables within a stylesheet. | ||
|
||
```scss | ||
$a: 1; | ||
/** ↑ | ||
* These dollar variables */ | ||
``` | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered violations: | ||
|
||
```scss | ||
$a: 1; | ||
``` | ||
|
||
```scss | ||
$a: 1; | ||
$b: 2; | ||
``` | ||
|
||
```scss | ||
.b { | ||
$a: 1; | ||
} | ||
``` | ||
|
||
The following patterns are *not* considered violations: | ||
|
||
```scss | ||
a { | ||
color: blue; | ||
} | ||
``` |
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,91 @@ | ||
import rule, { ruleName, messages } from ".."; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
syntax: "scss", | ||
|
||
accept: [ | ||
{ | ||
code: "a { color: blue; }", | ||
description: "No variables" | ||
}, | ||
{ | ||
code: "a { @less: 0; @less: @less + 1; }", | ||
description: "Less variables are ignored" | ||
}, | ||
{ | ||
code: "a { --custom-property: 0; --custom-property: 1; }", | ||
description: "Custom properties are ignored" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
$a: 1; | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected("$a"), | ||
description: "A dollar variable" | ||
}, | ||
{ | ||
code: ` | ||
$a: 1; | ||
$b: 2; | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected("$a"), | ||
description: "Two dollar variables" | ||
}, | ||
{ | ||
code: ` | ||
.b { | ||
$a: 1; | ||
} | ||
`, | ||
line: 3, | ||
column: 9, | ||
message: messages.rejected("$a"), | ||
description: "A dollar variable inside a class selector" | ||
}, | ||
{ | ||
code: ` | ||
%b { | ||
$a: 1; | ||
} | ||
`, | ||
line: 3, | ||
column: 9, | ||
message: messages.rejected("$a"), | ||
description: "A dollar variable inside a placeholder selector" | ||
}, | ||
{ | ||
code: ` | ||
@mixin test() { | ||
$a: 1; | ||
} | ||
`, | ||
line: 3, | ||
column: 9, | ||
message: messages.rejected("$a"), | ||
description: "A dollar variable inside a @mixin" | ||
}, | ||
{ | ||
code: ` | ||
.b { | ||
.c { | ||
$a: 1; | ||
} | ||
} | ||
`, | ||
line: 4, | ||
column: 11, | ||
message: messages.rejected("$a"), | ||
description: "Nested dollar variable" | ||
} | ||
] | ||
}); |
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,33 @@ | ||
import { utils } from "stylelint"; | ||
import { namespace } from "../../utils"; | ||
|
||
export const ruleName = namespace("no-dollar-variables"); | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
rejected: variable => `Unexpected dollar variable ${variable}` | ||
}); | ||
|
||
export default function(value) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { | ||
actual: value | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkDecls(decl => { | ||
if (decl.prop[0] !== "$") { | ||
return; | ||
} | ||
|
||
utils.report({ | ||
message: messages.rejected(decl.prop), | ||
node: decl, | ||
result, | ||
ruleName | ||
}); | ||
}); | ||
}; | ||
} |