-
Notifications
You must be signed in to change notification settings - Fork 92
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
1 parent
9aa4449
commit 8eda4dc
Showing
4 changed files
with
187 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# at-use-no-redundant-alias | ||
|
||
By default, the module's namespace is just the last component of the module’s URL. | ||
This rule is to disallow redundant namespace aliases. | ||
|
||
```scss | ||
@use "foo" as foo; | ||
/** ↑ | ||
* Disallow this */ | ||
``` | ||
|
||
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: "Unnecessary explicit namespace" | ||
}); | ||
|
||
const meta = { | ||
url: ruleUrl(ruleName) | ||
}; | ||
|
||
function getDefaultNamespace(module) { | ||
return module.match(/([^/:]+)$/)[1].replace(/\.[^.]|["]+$/, ""); | ||
} | ||
|
||
function separateEachParams(paramString) { | ||
const parts = paramString.replace(/["]/, "").split(/\s+as\s+|\s+with\s+/); | ||
Check failure Code scanning / CodeQL Incomplete string escaping or encoding High
This replaces only the first occurrence of /["]/.
|
||
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 && parts[1] === getDefaultNamespace(parts[0])) { | ||
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