-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
8c3bf34
commit 6440128
Showing
3 changed files
with
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* @fileoverview Rule to prevent !important in CSS. | ||
* @author Yann Bertrand | ||
*/ | ||
|
||
//----------------------------------------------------------------------------- | ||
// Rule Definition | ||
//----------------------------------------------------------------------------- | ||
|
||
export default { | ||
meta: { | ||
type: /** @type {const} */ ("problem"), | ||
|
||
docs: { | ||
description: "Disallow !important annotations", | ||
recommended: true, | ||
}, | ||
|
||
messages: { | ||
unexpectedImportant: "Unexpected !important annotation found.", | ||
}, | ||
}, | ||
|
||
create(context) { | ||
return { | ||
"Declaration[important=true]"(node) { | ||
context.report({ | ||
loc: { | ||
start: node.loc.start, | ||
end: { | ||
line: node.loc.start.line, | ||
column: | ||
node.loc.start.column + node.property.length, | ||
}, | ||
}, | ||
messageId: "unexpectedImportant", | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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,74 @@ | ||
/** | ||
* @fileoverview Tests for no-important rule. | ||
* @author Yann Bertrand | ||
*/ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Imports | ||
//------------------------------------------------------------------------------ | ||
|
||
import rule from "../../src/rules/no-important.js"; | ||
import css from "../../src/index.js"; | ||
import { RuleTester } from "eslint"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
plugins: { | ||
css, | ||
}, | ||
language: "css/css", | ||
}); | ||
|
||
ruleTester.run("no-important", rule, { | ||
valid: [ | ||
"a { color: red; }", | ||
"a { color: red; background-color: blue; }", | ||
"a { color: red; transition: none; }", | ||
"body { --custom-property: red; }", | ||
"body { padding: 0; }", | ||
"a { color: red; -moz-transition: bar }", | ||
"@font-face { font-weight: 100 400 }", | ||
'@property --foo { syntax: "*"; inherits: false; }', | ||
], | ||
invalid: [ | ||
{ | ||
code: "a { color: red !important; }", | ||
errors: [ | ||
{ | ||
messageId: "unexpectedImportant", | ||
line: 1, | ||
column: 5, | ||
endLine: 1, | ||
endColumn: 10, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ".link { width: 100% !important }", | ||
errors: [ | ||
{ | ||
messageId: "unexpectedImportant", | ||
line: 1, | ||
column: 9, | ||
endLine: 1, | ||
endColumn: 14, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "a .link { padding: 10px 20px 30px 40px !important }", | ||
errors: [ | ||
{ | ||
messageId: "unexpectedImportant", | ||
line: 1, | ||
column: 11, | ||
endLine: 1, | ||
endColumn: 18, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |