Skip to content

Commit

Permalink
Merge pull request #570 from stof/rule_dollar_variable_no_namespace
Browse files Browse the repository at this point in the history
Implement the dollar-variable-no-namespaced-assignment rule
  • Loading branch information
kristerkari authored Dec 21, 2021
2 parents 6ff4d1e + 22fee73 commit a591252
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Please also see the [example configs](./docs/examples/) for special cases.
- [`dollar-variable-empty-line-before`](./src/rules/dollar-variable-empty-line-before/README.md): Require a single empty line or disallow empty lines before `$`-variable declarations (Autofixable).
- [`dollar-variable-first-in-block`](./src/rules/dollar-variable-first-in-block/README.md): Require for variables to be put first in a block (a rule or in root).
- [`dollar-variable-no-missing-interpolation`](./src/rules/dollar-variable-no-missing-interpolation/README.md): Disallow Sass variables that are used without interpolation with CSS features that use custom identifiers.
- [`dollar-variable-no-namespaced-assignment`](./src/rules/dollar-variable-no-namespaced-assignment/README.md): Disallow assignment to namespaced Sass variables.
- [`dollar-variable-pattern`](./src/rules/dollar-variable-pattern/README.md): Specify a pattern for Sass-like variables.

### `%`-placeholder
Expand Down
24 changes: 24 additions & 0 deletions src/rules/dollar-variable-no-namespaced-assignment/README.md
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; }
```
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 src/rules/dollar-variable-no-namespaced-assignment/index.js
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
});
});
};
}
2 changes: 2 additions & 0 deletions src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import dollarVariableEmptyLineAfter from "./dollar-variable-empty-line-after";
import dollarVariableEmptyLineBefore from "./dollar-variable-empty-line-before";
import dollarVariableFirstInBlock from "./dollar-variable-first-in-block";
import dollarVariableNoMissingInterpolation from "./dollar-variable-no-missing-interpolation";
import dollarVariableNoNamespacedAssignment from "./dollar-variable-no-namespaced-assignment";
import dollarVariablePattern from "./dollar-variable-pattern";
import doubleSlashCommentEmptyLineBefore from "./double-slash-comment-empty-line-before";
import doubleSlashCommentInline from "./double-slash-comment-inline";
Expand Down Expand Up @@ -93,6 +94,7 @@ export default {
"dollar-variable-empty-line-before": dollarVariableEmptyLineBefore,
"dollar-variable-first-in-block": dollarVariableFirstInBlock,
"dollar-variable-no-missing-interpolation": dollarVariableNoMissingInterpolation,
"dollar-variable-no-namespaced-assignment": dollarVariableNoNamespacedAssignment,
"dollar-variable-pattern": dollarVariablePattern,
"double-slash-comment-empty-line-before": doubleSlashCommentEmptyLineBefore,
"double-slash-comment-inline": doubleSlashCommentInline,
Expand Down

0 comments on commit a591252

Please sign in to comment.