-
Notifications
You must be signed in to change notification settings - Fork 94
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 #371 from paulgv/add-no-duplicate-mixin-rule
Add no-duplicate-mixins rule
- Loading branch information
Showing
7 changed files
with
251 additions
and
6 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,83 @@ | ||
# no-duplicate-mixins | ||
|
||
Disallow duplicate mixins within a stylesheet. | ||
|
||
```scss | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-default { | ||
font-size: 18px; | ||
} | ||
/** ↑ | ||
* These are duplicates */ | ||
``` | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered violations: | ||
|
||
```scss | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-default { | ||
font-size: 18px; | ||
} | ||
``` | ||
|
||
```scss | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-sm { | ||
font-size: 14px; | ||
} | ||
@mixin font-size-default { | ||
font-size: 18px; | ||
} | ||
``` | ||
|
||
```scss | ||
@mixin font-size { | ||
font-size: 16px; | ||
} | ||
@mixin font-size($var) { | ||
font-size: $var; | ||
} | ||
``` | ||
|
||
```scss | ||
@mixin font-size($property, $value) { | ||
#{$property}: $value; | ||
} | ||
@mixin font-size($var) { | ||
font-size: $var; | ||
} | ||
``` | ||
|
||
```scss | ||
@mixin font-size { | ||
color: blue; | ||
} | ||
|
||
.b { | ||
@mixin font-size { | ||
color: red; | ||
} | ||
@include font-size; | ||
} | ||
``` | ||
|
||
The following patterns are _not_ considered violations: | ||
|
||
```scss | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-lg { | ||
font-size: 18px; | ||
} | ||
``` |
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,110 @@ | ||
import rule, { ruleName, messages } from ".."; | ||
|
||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
syntax: "scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
`, | ||
description: "A single mixin." | ||
}, | ||
{ | ||
code: ` | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-lg { | ||
font-size: 18px; | ||
} | ||
`, | ||
description: "Two mixins with different names." | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-default { | ||
font-size: 18px; | ||
} | ||
`, | ||
column: 7, | ||
line: 5, | ||
message: messages.rejected("font-size-default"), | ||
description: "Two mixins with the same names." | ||
}, | ||
{ | ||
code: ` | ||
@mixin font-size-default { | ||
font-size: 16px; | ||
} | ||
@mixin font-size-sm { | ||
font-size: 14px; | ||
} | ||
@mixin font-size-default { | ||
font-size: 18px; | ||
} | ||
`, | ||
column: 7, | ||
line: 8, | ||
message: messages.rejected("font-size-default"), | ||
description: "Three mixins including two with the same names." | ||
}, | ||
{ | ||
code: ` | ||
@mixin font-size { | ||
font-size: 16px; | ||
} | ||
@mixin font-size($var) { | ||
font-size: $var; | ||
} | ||
`, | ||
column: 7, | ||
line: 5, | ||
message: messages.rejected("font-size"), | ||
description: | ||
"Two mixins with the same names including one accepting arguments." | ||
}, | ||
{ | ||
code: ` | ||
@mixin font-size($property, $value) { | ||
#{$property}: $value; | ||
} | ||
@mixin font-size($var) { | ||
font-size: $var; | ||
} | ||
`, | ||
column: 7, | ||
line: 5, | ||
message: messages.rejected("font-size"), | ||
description: "Two mixins with the same names accepting arguments." | ||
}, | ||
{ | ||
code: ` | ||
@mixin font-size { | ||
color: blue; | ||
} | ||
.b { | ||
@mixin font-size { | ||
color: red; | ||
} | ||
@include font-size; | ||
} | ||
`, | ||
column: 9, | ||
line: 7, | ||
message: messages.rejected("font-size"), | ||
description: "Two mixins with the same names accepting arguments." | ||
} | ||
] | ||
}); |
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,43 @@ | ||
import { utils } from "stylelint"; | ||
import { atRuleBaseName, namespace } from "../../utils"; | ||
|
||
export const ruleName = namespace("no-duplicate-mixins"); | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
rejected: mixin => `Unexpected duplicate mixin ${mixin}`, | ||
}); | ||
|
||
export default function(value) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { | ||
actual: value, | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
const mixins = {}; | ||
|
||
root.walkAtRules(decl => { | ||
const isMixin = decl.name === "mixin"; | ||
|
||
if (!isMixin) { | ||
return; | ||
} | ||
|
||
const mixinName = atRuleBaseName(decl); | ||
|
||
if (mixins[mixinName]) { | ||
utils.report({ | ||
message: messages.rejected(mixinName), | ||
node: decl, | ||
result, | ||
ruleName, | ||
}); | ||
} | ||
|
||
mixins[mixinName] = true; | ||
}); | ||
}; | ||
} |
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,9 @@ | ||
/** | ||
* Get an at rule's base name | ||
* | ||
* @param {AtRule} atRule | ||
* @return {string} The name | ||
*/ | ||
export default function(atRule) { | ||
return atRule.params.replace(/\([^)]*\)/, "").trim(); | ||
} |
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