-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add no-duplicate-imports rule (#4)
* feat: Add no-duplicate-imports rule * Update src/rules/no-duplicate-imports.js Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/rules/no-duplicate-imports.md Co-authored-by: Milos Djermanovic <[email protected]> * Update docs/rules/no-duplicate-imports.md Co-authored-by: Milos Djermanovic <[email protected]> * Add rule to recommended and docs * Add rule to plugin --------- Co-authored-by: Milos Djermanovic <[email protected]>
- Loading branch information
1 parent
c9424e5
commit 8d4558b
Showing
5 changed files
with
188 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# no-duplicate-imports | ||
|
||
Disallow duplicate `@import` rules. | ||
|
||
## Background | ||
|
||
CSS files can import rules from other CSS files using the `@import` rule and specifying a URL from which to load. In a file with many `@import` rules it can be difficult to see if you've accidentally imported the same URL twice, for example: | ||
|
||
```css | ||
@import url(a.css); | ||
@import "b.css"; | ||
@import url("c.css"); | ||
@import "a.css"; | ||
``` | ||
|
||
There is no reason to import the same URL twice, so this is a mistake. | ||
|
||
## Rule Details | ||
|
||
This rule warns when it finds an `@import` rule that imports the same URL as a previous `@import` rule. This includes all of the URL forms (`"a.css"`, `url("a.css")`, and `url(a.css)`). | ||
|
||
Examples of incorrect code: | ||
|
||
```css | ||
@import url(a.css); | ||
@import "b.css"; | ||
@import url("c.css"); | ||
|
||
/* duplicates */ | ||
@import "a.css"; | ||
@import url(b.css); | ||
@import "c.css"; | ||
``` | ||
|
||
## When Not to Use It | ||
|
||
If you aren't concerned with duplicate `@import` rules, you can safely disable this rule. | ||
|
||
## Prior Art | ||
|
||
- [`no-duplicate-at-import-rules`](https://stylelint.io/user-guide/rules/no-duplicate-at-import-rules) |
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,39 @@ | ||
/** | ||
* @fileoverview Rule to prevent duplicate imports in CSS. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
|
||
export default { | ||
meta: { | ||
type: "problem", | ||
|
||
docs: { | ||
description: "Disallow duplicate @import rules.", | ||
recommended: true, | ||
}, | ||
|
||
messages: { | ||
duplicateImport: "Unexpected duplicate @import rule for {{url}}.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const imports = new Set(); | ||
|
||
return { | ||
"Atrule[name=import]"(node) { | ||
const url = node.prelude.children[0].value; | ||
|
||
if (imports.has(url)) { | ||
context.report({ | ||
loc: node.loc, | ||
messageId: "duplicateImport", | ||
data: { url }, | ||
}); | ||
} else { | ||
imports.add(url); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,101 @@ | ||
/** | ||
* @fileoverview Tests for no-duplicate-imports rule. | ||
* @author Nicholas C. Zakas | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Imports | ||
//------------------------------------------------------------------------------ | ||
|
||
import rule from "../../src/rules/no-duplicate-imports.js"; | ||
import css from "../../src/index.js"; | ||
import { RuleTester } from "eslint"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
plugins: { | ||
css, | ||
}, | ||
language: "css/css", | ||
}); | ||
|
||
ruleTester.run("no-duplicate-imports", rule, { | ||
valid: [ | ||
"@import url('x.css');", | ||
"@import url('x.css'); @import url('y.css');", | ||
"@import 'x.css'; @import url('y.css'); @import 'z.css';", | ||
], | ||
invalid: [ | ||
{ | ||
code: "@import url('x.css');\n@import url('x.css');", | ||
errors: [ | ||
{ | ||
messageId: "duplicateImport", | ||
data: { url: "x.css" }, | ||
line: 2, | ||
column: 1, | ||
endLine: 2, | ||
endColumn: 22, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "@import url('x.css');\n@import 'x.css';", | ||
errors: [ | ||
{ | ||
messageId: "duplicateImport", | ||
data: { url: "x.css" }, | ||
line: 2, | ||
column: 1, | ||
endLine: 2, | ||
endColumn: 17, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "@import url('x.css');\n@import 'x.css';\n@import 'x.css';", | ||
errors: [ | ||
{ | ||
messageId: "duplicateImport", | ||
data: { url: "x.css" }, | ||
line: 2, | ||
column: 1, | ||
endLine: 2, | ||
endColumn: 17, | ||
}, | ||
{ | ||
messageId: "duplicateImport", | ||
data: { url: "x.css" }, | ||
line: 3, | ||
column: 1, | ||
endLine: 3, | ||
endColumn: 17, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "@import url('x.css');\n@import 'x.css';\n@import url('y.css');\n@import 'y.css';", | ||
errors: [ | ||
{ | ||
messageId: "duplicateImport", | ||
data: { url: "x.css" }, | ||
line: 2, | ||
column: 1, | ||
endLine: 2, | ||
endColumn: 17, | ||
}, | ||
{ | ||
messageId: "duplicateImport", | ||
data: { url: "y.css" }, | ||
line: 4, | ||
column: 1, | ||
endLine: 4, | ||
endColumn: 17, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |