|
| 1 | +/** |
| 2 | + * @fileoverview Rule to disallow `!important` flags. |
| 3 | + * @author thecalamiity |
| 4 | + * @author Yann Bertrand |
| 5 | + */ |
| 6 | + |
| 7 | +//----------------------------------------------------------------------------- |
| 8 | +// Imports |
| 9 | +//----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +import { findOffsets } from "../util.js"; |
| 12 | + |
| 13 | +//----------------------------------------------------------------------------- |
| 14 | +// Type Definitions |
| 15 | +//----------------------------------------------------------------------------- |
| 16 | + |
| 17 | +/** |
| 18 | + * @import { CSSRuleDefinition } from "../types.js" |
| 19 | + * @typedef {"unexpectedImportant"} NoImportantMessageIds |
| 20 | + * @typedef {CSSRuleDefinition<{ RuleOptions: [], MessageIds: NoImportantMessageIds }>} NoImportantRuleDefinition |
| 21 | + */ |
| 22 | + |
| 23 | +//----------------------------------------------------------------------------- |
| 24 | +// Rule Definition |
| 25 | +//----------------------------------------------------------------------------- |
| 26 | + |
| 27 | +/** @type {NoImportantRuleDefinition} */ |
| 28 | +export default { |
| 29 | + meta: { |
| 30 | + type: "problem", |
| 31 | + |
| 32 | + docs: { |
| 33 | + description: "Disallow !important flags", |
| 34 | + recommended: true, |
| 35 | + url: "https://github.com/eslint/css/blob/main/docs/rules/no-important.md", |
| 36 | + }, |
| 37 | + |
| 38 | + messages: { |
| 39 | + unexpectedImportant: "Unexpected !important flag found.", |
| 40 | + }, |
| 41 | + }, |
| 42 | + |
| 43 | + create(context) { |
| 44 | + const importantPattern = /!(\s|\/\*.*?\*\/)*important/iu; |
| 45 | + |
| 46 | + return { |
| 47 | + Declaration(node) { |
| 48 | + if (node.important) { |
| 49 | + const declarationText = context.sourceCode.getText(node); |
| 50 | + const importantMatch = |
| 51 | + importantPattern.exec(declarationText); |
| 52 | + |
| 53 | + const { |
| 54 | + lineOffset: startLineOffset, |
| 55 | + columnOffset: startColumnOffset, |
| 56 | + } = findOffsets(declarationText, importantMatch.index); |
| 57 | + |
| 58 | + const { |
| 59 | + lineOffset: endLineOffset, |
| 60 | + columnOffset: endColumnOffset, |
| 61 | + } = findOffsets( |
| 62 | + declarationText, |
| 63 | + importantMatch.index + importantMatch[0].length, |
| 64 | + ); |
| 65 | + |
| 66 | + const nodeStartLine = node.loc.start.line; |
| 67 | + const nodeStartColumn = node.loc.start.column; |
| 68 | + const startLine = nodeStartLine + startLineOffset; |
| 69 | + const endLine = nodeStartLine + endLineOffset; |
| 70 | + const startColumn = |
| 71 | + (startLine === nodeStartLine ? nodeStartColumn : 1) + |
| 72 | + startColumnOffset; |
| 73 | + const endColumn = |
| 74 | + (endLine === nodeStartLine ? nodeStartColumn : 1) + |
| 75 | + endColumnOffset; |
| 76 | + |
| 77 | + context.report({ |
| 78 | + loc: { |
| 79 | + start: { |
| 80 | + line: startLine, |
| 81 | + column: startColumn, |
| 82 | + }, |
| 83 | + end: { |
| 84 | + line: endLine, |
| 85 | + column: endColumn, |
| 86 | + }, |
| 87 | + }, |
| 88 | + messageId: "unexpectedImportant", |
| 89 | + }); |
| 90 | + } |
| 91 | + }, |
| 92 | + }; |
| 93 | + }, |
| 94 | +}; |
0 commit comments