Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default [
| :--------------------------------------------------------------- | :------------------------------- | :-------------: |
| [`no-duplicate-imports`](./docs/rules/no-duplicate-imports.md) | Disallow duplicate @import rules | yes |
| [`no-empty-blocks`](./docs/rules/no-empty-blocks.md) | Disallow empty blocks | yes |
| [`no-important`](./docs/rules/no-important.md) | Disallow important annotations | yes |
| [`no-invalid-at-rules`](./docs/rules/no-invalid-at-rules.md) | Disallow invalid at-rules | yes |
| [`no-invalid-properties`](./docs/rules/no-invalid-properties.md) | Disallow invalid properties | yes |

Expand Down
21 changes: 21 additions & 0 deletions docs/rules/no-important.md
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.
Copy link
Member

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.


## Rule Details

Examples of incorrect code:

```css
a {
color: red !important;
}

a .link {
font-size: padding: 10px 20px 30px 40px !important;
}
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import noEmptyBlocks from "./rules/no-empty-blocks.js";
import noDuplicateImports from "./rules/no-duplicate-imports.js";
import noInvalidProperties from "./rules/no-invalid-properties.js";
import noInvalidAtRules from "./rules/no-invalid-at-rules.js";
import noImportant from "./rules/no-important.js";

//-----------------------------------------------------------------------------
// Plugin
Expand All @@ -29,6 +30,7 @@ const plugin = {
rules: {
"no-empty-blocks": noEmptyBlocks,
"no-duplicate-imports": noDuplicateImports,
"no-important": noImportant,
"no-invalid-at-rules": noInvalidAtRules,
"no-invalid-properties": noInvalidProperties,
},
Expand All @@ -38,6 +40,7 @@ const plugin = {
rules: /** @type {const} */ ({
"css/no-empty-blocks": "error",
"css/no-duplicate-imports": "error",
"css/no-important": "error",
"css/no-invalid-at-rules": "error",
"css/no-invalid-properties": "error",
}),
Expand Down
41 changes: 41 additions & 0 deletions src/rules/no-important.js
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description: "Disallow important annotations",
description: "Disallow !important annotations",

recommended: true,
},

messages: {
forbiddenImportantUsage: "!important annotations are not allowed.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we shorten this to unexpectedImportant?

And we generally like to be a bit gentler with the messages:

Suggested change
forbiddenImportantUsage: "!important annotations are not allowed.",
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:
Copy link
Member

Choose a reason for hiding this comment

The 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 !important and set that as the location of the error.

node.loc.start.column + node.property.length,
},
},
messageId: "forbiddenImportantUsage",
});
},
};
},
};
89 changes: 89 additions & 0 deletions tests/rules/no-important.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @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; }",
Copy link
Member

Choose a reason for hiding this comment

The 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 { --custom-property: red; }",
"body { --custom-property: red; }",
"body { --custom-property: important; }",

"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: "forbiddenImportantUsage",
data: {
property: "color",
value: "red !important",
expected: "red",
},
Comment on lines +42 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data doesn't apply because you're not passing any data to context.report() in the rule. Please remove all data entries from this file.

line: 1,
column: 5,
endLine: 1,
endColumn: 10,
},
],
},
{
code: ".link { width: 100% !important }",
errors: [
{
messageId: "forbiddenImportantUsage",
data: {
property: "width",
value: "100% !important",
expected: "100%",
},
line: 1,
column: 9,
endLine: 1,
endColumn: 14,
},
],
},
{
code: "a .link { padding: 10px 20px 30px 40px !important }",
errors: [
{
messageId: "forbiddenImportantUsage",
data: {
property: "padding",
value: "10px 20px 30px 40px !important",
expected: "10px 20px 30px 40px",
},
line: 1,
column: 11,
endLine: 1,
endColumn: 18,
},
],
},
],
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test that has multiple properties with !important to ensure we're catching them all?