Skip to content

Commit bb91e97

Browse files
committed
feat: add no-important rule
1 parent 8c3bf34 commit bb91e97

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import noEmptyBlocks from "./rules/no-empty-blocks.js";
1313
import noDuplicateImports from "./rules/no-duplicate-imports.js";
1414
import noInvalidProperties from "./rules/no-invalid-properties.js";
1515
import noInvalidAtRules from "./rules/no-invalid-at-rules.js";
16+
import noImportant from "./rules/no-important.js";
1617

1718
//-----------------------------------------------------------------------------
1819
// Plugin
@@ -29,6 +30,7 @@ const plugin = {
2930
rules: {
3031
"no-empty-blocks": noEmptyBlocks,
3132
"no-duplicate-imports": noDuplicateImports,
33+
"no-important": noImportant,
3234
"no-invalid-at-rules": noInvalidAtRules,
3335
"no-invalid-properties": noInvalidProperties,
3436
},
@@ -38,6 +40,7 @@ const plugin = {
3840
rules: /** @type {const} */ ({
3941
"css/no-empty-blocks": "error",
4042
"css/no-duplicate-imports": "error",
43+
"css/no-important": "error",
4144
"css/no-invalid-at-rules": "error",
4245
"css/no-invalid-properties": "error",
4346
}),

src/rules/no-important.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @fileoverview Rule to prevent !important in CSS.
3+
* @author Yann Bertrand
4+
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Rule Definition
8+
//-----------------------------------------------------------------------------
9+
10+
export default {
11+
meta: {
12+
type: /** @type {const} */ ('problem'),
13+
14+
docs: {
15+
description: 'Disallow important annotations',
16+
recommended: true,
17+
},
18+
19+
messages: {
20+
unexpectedImportant: 'Unexpected !important annotation found.',
21+
},
22+
},
23+
24+
create(context) {
25+
return {
26+
'Declaration[important=true]'(node) {
27+
context.report({
28+
loc: {
29+
start: node.loc.start,
30+
end: {
31+
line: node.loc.start.line,
32+
column: node.loc.start.column + node.property.length,
33+
},
34+
},
35+
messageId: 'unexpectedImportant',
36+
});
37+
},
38+
};
39+
},
40+
};

tests/rules/no-important.test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @fileoverview Tests for no-important rule.
3+
* @author Yann Bertrand
4+
*/
5+
6+
//------------------------------------------------------------------------------
7+
// Imports
8+
//------------------------------------------------------------------------------
9+
10+
import rule from '../../src/rules/no-important.js';
11+
import css from '../../src/index.js';
12+
import { RuleTester } from 'eslint';
13+
14+
//------------------------------------------------------------------------------
15+
// Tests
16+
//------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester({
19+
plugins: {
20+
css,
21+
},
22+
language: 'css/css',
23+
});
24+
25+
ruleTester.run('no-important', rule, {
26+
valid: [
27+
'a { color: red; }',
28+
'a { color: red; background-color: blue; }',
29+
'a { color: red; transition: none; }',
30+
'body { --custom-property: red; }',
31+
'body { padding: 0; }',
32+
'a { color: red; -moz-transition: bar }',
33+
'@font-face { font-weight: 100 400 }',
34+
'@property --foo { syntax: "*"; inherits: false; }',
35+
],
36+
invalid: [
37+
{
38+
code: 'a { color: red !important; }',
39+
errors: [
40+
{
41+
messageId: 'unexpectedImportant',
42+
line: 1,
43+
column: 5,
44+
endLine: 1,
45+
endColumn: 10,
46+
},
47+
],
48+
},
49+
{
50+
code: '.link { width: 100% !important }',
51+
errors: [
52+
{
53+
messageId: 'unexpectedImportant',
54+
line: 1,
55+
column: 9,
56+
endLine: 1,
57+
endColumn: 14,
58+
},
59+
],
60+
},
61+
{
62+
code: 'a .link { padding: 10px 20px 30px 40px !important }',
63+
errors: [
64+
{
65+
messageId: 'unexpectedImportant',
66+
line: 1,
67+
column: 11,
68+
endLine: 1,
69+
endColumn: 18,
70+
},
71+
],
72+
},
73+
],
74+
});

0 commit comments

Comments
 (0)