-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #570 from stof/rule_dollar_variable_no_namespace
Implement the dollar-variable-no-namespaced-assignment rule
- Loading branch information
Showing
5 changed files
with
97 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
24 changes: 24 additions & 0 deletions
24
src/rules/dollar-variable-no-namespaced-assignment/README.md
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,24 @@ | ||
# dollar-variable-no-namespaced-assignment | ||
|
||
Disallow assignment to namespaced variables. | ||
|
||
```scss | ||
a { imported.$foo: 1px; } | ||
/** ↑ | ||
* This assignment */ | ||
``` | ||
|
||
## Examples | ||
|
||
The following patterns are considered warnings: | ||
|
||
```scss | ||
imported.$foo: 1; | ||
``` | ||
|
||
The following patterns are *not* considered warnings: | ||
|
||
```scss | ||
a { b: imported.$foo-bar; } | ||
a { $foo: 0; } | ||
``` |
39 changes: 39 additions & 0 deletions
39
src/rules/dollar-variable-no-namespaced-assignment/__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,39 @@ | ||
import { messages, ruleName } from ".."; | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true], | ||
customSyntax: "postcss-scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
p { | ||
$foo: 10px; | ||
} | ||
`, | ||
description: "Non-namespaced assignment" | ||
}, | ||
{ | ||
code: ` | ||
p { | ||
a: imported.$foo; | ||
} | ||
`, | ||
description: "Namespaced usage" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
p { | ||
imported.$foo: 10px; | ||
} | ||
`, | ||
line: 3, | ||
message: messages.rejected, | ||
description: "Namespaced assignment" | ||
} | ||
] | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/rules/dollar-variable-no-namespaced-assignment/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,31 @@ | ||
import { utils } from "stylelint"; | ||
import { namespace } from "../../utils"; | ||
|
||
export const ruleName = namespace("dollar-variable-no-namespaced-assignment"); | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
rejected: "Unexpected assignment to a namespaced $ variable" | ||
}); | ||
|
||
export default function(actual) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { actual }); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkDecls(decl => { | ||
if (!/^[^$.]+\.\$./.test(decl.prop)) { | ||
return; | ||
} | ||
|
||
utils.report({ | ||
message: messages.rejected, | ||
node: decl, | ||
result, | ||
ruleName | ||
}); | ||
}); | ||
}; | ||
} |
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