-
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add no-important rule #23
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# no-important | ||
|
||
Disallow `!important` annotations. | ||
|
||
## Background | ||
|
||
Needing !important indicates there may be a larger underlying issue. | ||
|
||
## Rule Details | ||
|
||
Examples of incorrect code: | ||
|
||
```css | ||
a { | ||
color: red !important; | ||
} | ||
|
||
a .link { | ||
font-size: padding: 10px 20px 30px 40px !important; | ||
} | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add "Prior Art" section as in other rules. https://stylelint.io/user-guide/rules/declaration-no-important/ |
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will just highlight the property name. I think it would be better to find the location of the |
||
node.loc.start.column + node.property.length, | ||
}, | ||
}, | ||
messageId: "unexpectedImportant", | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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; }", | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also add a test with just the word "important":
Suggested change
|
||||||||
"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, | ||||||||
}, | ||||||||
], | ||||||||
}, | ||||||||
], | ||||||||
}); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add a test that has multiple properties with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need more of a description here. 😄 Please see other rule docs for examples.