diff --git a/src/rules/no-important.js b/src/rules/no-important.js index d4b79f1c..b877f011 100644 --- a/src/rules/no-important.js +++ b/src/rules/no-important.js @@ -24,7 +24,8 @@ import { findOffsets } from "../util.js"; // Helpers //----------------------------------------------------------------------------- -const importantPattern = /!(\s|\/\*.*?\*\/)*important/iu; +const importantPattern = /!\s*important/iu; +const commentPattern = /\/\*[\s\S]*?\*\//gu; const trailingWhitespacePattern = /\s*$/u; //----------------------------------------------------------------------------- @@ -55,8 +56,12 @@ export default { Declaration(node) { if (node.important) { const declarationText = context.sourceCode.getText(node); + const textWithoutComments = declarationText.replace( + commentPattern, + match => match.replace(/[^\n]/gu, " "), + ); const importantMatch = - importantPattern.exec(declarationText); + importantPattern.exec(textWithoutComments); const { lineOffset: startLineOffset, diff --git a/tests/rules/no-important.test.js b/tests/rules/no-important.test.js index d89875a5..9dce5e4d 100644 --- a/tests/rules/no-important.test.js +++ b/tests/rules/no-important.test.js @@ -47,6 +47,9 @@ ruleTester.run("no-important", rule, { "@keyframes important { from { margin: 1px; } }", "@-webkit-keyframes important { from { margin: 1px; } }", "@-WEBKIT-KEYFRAMES important { from { margin: 1px; } }", + "a { color: red /* !important */; }", + "a { color: /* !important */ red; }", + "a { color: red; /* !important */ background: blue; }", ], invalid: [ { @@ -535,5 +538,231 @@ ruleTester.run("no-important", rule, { }, ], }, + { + code: "a { color: red /* !important */ !important; }", + errors: [ + { + messageId: "unexpectedImportant", + line: 1, + column: 33, + endLine: 1, + endColumn: 43, + suggestions: [ + { + messageId: "removeImportant", + output: "a { color: red /* !important */; }", + }, + ], + }, + ], + }, + { + code: dedent` + a { + color: red /* !important */ + !important; + } + `, + errors: [ + { + messageId: "unexpectedImportant", + line: 3, + column: 3, + endLine: 3, + endColumn: 13, + suggestions: [ + { + messageId: "removeImportant", + output: dedent` + a { + color: red /* !important */; + } + `, + }, + ], + }, + ], + }, + { + code: "a { color: red !/* !important */important; }", + errors: [ + { + messageId: "unexpectedImportant", + line: 1, + column: 16, + endLine: 1, + endColumn: 42, + suggestions: [ + { + messageId: "removeImportant", + output: "a { color: red; }", + }, + ], + }, + ], + }, + { + code: "a { color: red ! /* !important */ important; }", + errors: [ + { + messageId: "unexpectedImportant", + line: 1, + column: 16, + endLine: 1, + endColumn: 44, + suggestions: [ + { + messageId: "removeImportant", + output: "a { color: red; }", + }, + ], + }, + ], + }, + { + code: dedent` + a { + color: red + !/* !important */important; + } + `, + errors: [ + { + messageId: "unexpectedImportant", + line: 3, + column: 3, + endLine: 3, + endColumn: 29, + suggestions: [ + { + messageId: "removeImportant", + output: dedent` + a { + color: red; + } + `, + }, + ], + }, + ], + }, + { + code: dedent` + a { + color: red + ! /* !important */ important; + } + `, + errors: [ + { + messageId: "unexpectedImportant", + line: 3, + column: 3, + endLine: 3, + endColumn: 31, + suggestions: [ + { + messageId: "removeImportant", + output: dedent` + a { + color: red; + } + `, + }, + ], + }, + ], + }, + { + code: "a { color: red /* !important */ /* !important */ !important; }", + errors: [ + { + messageId: "unexpectedImportant", + line: 1, + column: 50, + endLine: 1, + endColumn: 60, + suggestions: [ + { + messageId: "removeImportant", + output: "a { color: red /* !important */ /* !important */; }", + }, + ], + }, + ], + }, + { + code: dedent` + a { + color: red /* !important */ /* !important + */ !important; + } + `, + errors: [ + { + messageId: "unexpectedImportant", + line: 3, + column: 5, + endLine: 3, + endColumn: 15, + suggestions: [ + { + messageId: "removeImportant", + output: dedent` + a { + color: red /* !important */ /* !important + */; + } + `, + }, + ], + }, + ], + }, + { + code: "a { color: red ! /* !important */ /* another comment */ /* a third comment */important; }", + errors: [ + { + messageId: "unexpectedImportant", + line: 1, + column: 16, + endLine: 1, + endColumn: 87, + suggestions: [ + { + messageId: "removeImportant", + output: "a { color: red; }", + }, + ], + }, + ], + }, + { + code: dedent` + a { + color: red ! /* !important */ /* another + comment */ /* a third comment */important; + } + `, + errors: [ + { + messageId: "unexpectedImportant", + line: 2, + column: 13, + endLine: 3, + endColumn: 43, + suggestions: [ + { + messageId: "removeImportant", + output: dedent` + a { + color: red; + } + `, + }, + ], + }, + ], + }, ], });