Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add at-use-no-redundant-alias rule (#848)
Co-authored-by: Krister Kari <kristerkari@users.noreply.github.com>
- Loading branch information
1 parent
ae7a070
commit 5ca4ba2
Showing
6 changed files
with
195 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,44 @@ | ||
# at-use-no-redundant-alias | ||
|
||
Disallow redundant namespace aliases. | ||
|
||
```scss | ||
@use "foo" as foo; | ||
/** ↑ | ||
* Disallow this */ | ||
``` | ||
|
||
By default, the [module's namespace](https://sass-lang.com/documentation/at-rules/use/#loading-members) | ||
is the last component of the module’s URL. | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered warnings: | ||
|
||
```scss | ||
@use "foo" as foo; | ||
``` | ||
|
||
```scss | ||
@use "sass:math" as math; | ||
``` | ||
|
||
```scss | ||
@use "src/corners" as corners; | ||
``` | ||
|
||
The following patterns are _not_ considered warnings: | ||
|
||
```scss | ||
@use "foo" as bar; | ||
``` | ||
|
||
```scss | ||
@use "sass:math"; | ||
``` | ||
|
||
```scss | ||
@use "src/corners" as c; | ||
``` |
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,95 @@ | ||
"use strict"; | ||
|
||
const { messages, ruleName } = require(".."); | ||
|
||
testRule({ | ||
ruleName, | ||
config: [true], | ||
customSyntax: "postcss-scss", | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
@use "foo"; | ||
`, | ||
description: "Default namespace" | ||
}, | ||
{ | ||
code: ` | ||
@use "foo/bar" as foo; | ||
`, | ||
description: "Different namespace" | ||
}, | ||
{ | ||
code: ` | ||
@use "sass:math" as *; | ||
`, | ||
description: "Without namespace" | ||
}, | ||
{ | ||
code: ` | ||
@use "sass:math"; | ||
`, | ||
description: "No explicit namespace" | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
@use "foo" as foo; | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected, | ||
description: "Without url" | ||
}, | ||
{ | ||
code: ` | ||
@use "src/corners" as corners; | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected, | ||
description: "With url" | ||
}, | ||
{ | ||
code: ` | ||
@use "sass:math" as math; | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected, | ||
description: "Built-in module" | ||
}, | ||
{ | ||
code: ` | ||
@use "foo" as foo; | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected, | ||
description: "Without space" | ||
}, | ||
{ | ||
code: ` | ||
@use "foo" as foo with ($baz: 1px); | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected, | ||
description: "Configured" | ||
}, | ||
{ | ||
code: ` | ||
@use "sass:color" as color with ( | ||
$baz: 1px | ||
); | ||
`, | ||
line: 2, | ||
column: 7, | ||
message: messages.rejected, | ||
description: "Configured multiline" | ||
} | ||
] | ||
}); |
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,53 @@ | ||
"use strict"; | ||
|
||
const { utils } = require("stylelint"); | ||
const namespace = require("../../utils/namespace"); | ||
const ruleUrl = require("../../utils/ruleUrl"); | ||
|
||
const ruleName = namespace("at-use-no-redundant-alias"); | ||
|
||
const messages = utils.ruleMessages(ruleName, { | ||
rejected: "Unexpected redundant namespace." | ||
}); | ||
|
||
const meta = { | ||
url: ruleUrl(ruleName) | ||
}; | ||
|
||
function getDefaultNamespace(module) { | ||
return module.match(/([^/:]+)$/)[1].replace(/\.[^.]|["]+$/, ""); | ||
} | ||
|
||
function separateEachParams(paramString) { | ||
const parts = paramString.replace(/"/g, "").split(/\s+as\s+|\s+with\s+/); | ||
if (parts.length < 2) return; | ||
return parts; | ||
} | ||
|
||
function rule(actual) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { actual }); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
root.walkAtRules("use", decl => { | ||
const parts = separateEachParams(decl.params); | ||
if (parts && getDefaultNamespace(parts[0]) === parts[1]) { | ||
utils.report({ | ||
message: messages.rejected, | ||
node: decl, | ||
result, | ||
ruleName | ||
}); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
rule.ruleName = ruleName; | ||
rule.messages = messages; | ||
rule.meta = meta; | ||
|
||
module.exports = 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