|
| 1 | +import { isPandaAttribute, isPandaProp } from '../utils/helpers' |
| 2 | +import { type Rule, createRule } from '../utils' |
| 3 | +import { isIdentifier, isJSXExpressionContainer, isLiteral, isTemplateLiteral, type Node } from '../utils/nodes' |
| 4 | + |
| 5 | +// Check if the string ends with '!' with optional whitespace before it |
| 6 | +const exclamationRegex = /\s*!$/ |
| 7 | +// Check if the string ends with '!important' with optional whitespace before it and after, but not within '!important' |
| 8 | +const importantRegex = /\s*!important\s*$/ |
| 9 | + |
| 10 | +export const RULE_NAME = 'no-important' |
| 11 | + |
| 12 | +const rule: Rule = createRule({ |
| 13 | + name: RULE_NAME, |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: |
| 17 | + 'Disallow usage of important keyword. Prioroitize specificity for a maintainable and predictable styling structure.', |
| 18 | + }, |
| 19 | + messages: { |
| 20 | + important: |
| 21 | + 'Avoid using the !important keyword. Refactor your code to prioritize specificity for predictable styling.', |
| 22 | + remove: 'Remove the `{{keyword}}` keyword.', |
| 23 | + }, |
| 24 | + type: 'suggestion', |
| 25 | + hasSuggestions: true, |
| 26 | + schema: [], |
| 27 | + }, |
| 28 | + defaultOptions: [], |
| 29 | + create(context) { |
| 30 | + const removeQuotes = ([start, end]: readonly [number, number]) => [start + 1, end - 1] as const |
| 31 | + |
| 32 | + const hasImportantKeyword = (value?: string) => { |
| 33 | + if (!value) return false |
| 34 | + return exclamationRegex.test(value) || importantRegex.test(value) |
| 35 | + } |
| 36 | + |
| 37 | + const removeImportantKeyword = (input: string) => { |
| 38 | + if (exclamationRegex.test(input)) { |
| 39 | + // Remove trailing '!' |
| 40 | + return { fixed: input.replace(exclamationRegex, ''), keyword: '!' } |
| 41 | + } else if (importantRegex.test(input)) { |
| 42 | + // Remove '!important' with optional whitespace |
| 43 | + return { fixed: input.replace(importantRegex, ''), keyword: '!important' } |
| 44 | + } else { |
| 45 | + // No match, return the original string |
| 46 | + return { fixed: input, keyword: null } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const handleLiteral = (node: Node) => { |
| 51 | + if (!isLiteral(node)) return |
| 52 | + if (!hasImportantKeyword(node.value?.toString())) return |
| 53 | + |
| 54 | + sendReport(node) |
| 55 | + } |
| 56 | + |
| 57 | + const handleTemplateLiteral = (node: Node) => { |
| 58 | + if (!isTemplateLiteral(node)) return |
| 59 | + if (node.expressions.length > 0) return |
| 60 | + if (!hasImportantKeyword(node.quasis[0].value.raw)) return |
| 61 | + |
| 62 | + sendReport(node.quasis[0], node.quasis[0].value.raw) |
| 63 | + } |
| 64 | + |
| 65 | + const sendReport = (node: any, _value?: string) => { |
| 66 | + const value = _value ?? node.value?.toString() |
| 67 | + const { keyword, fixed } = removeImportantKeyword(value) |
| 68 | + |
| 69 | + return context.report({ |
| 70 | + node, |
| 71 | + messageId: 'important', |
| 72 | + suggest: [ |
| 73 | + { |
| 74 | + messageId: 'remove', |
| 75 | + data: { keyword }, |
| 76 | + fix: (fixer) => { |
| 77 | + return fixer.replaceTextRange(removeQuotes(node.range), fixed) |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + JSXAttribute(node) { |
| 86 | + if (!node.value) return |
| 87 | + if (!isPandaProp(node, context)) return |
| 88 | + |
| 89 | + handleLiteral(node.value) |
| 90 | + |
| 91 | + if (!isJSXExpressionContainer(node.value)) return |
| 92 | + |
| 93 | + handleLiteral(node.value.expression) |
| 94 | + handleTemplateLiteral(node.value.expression) |
| 95 | + }, |
| 96 | + |
| 97 | + Property(node) { |
| 98 | + if (!isIdentifier(node.key)) return |
| 99 | + if (!isLiteral(node.value) && !isTemplateLiteral(node.value)) return |
| 100 | + if (!isPandaAttribute(node, context)) return |
| 101 | + |
| 102 | + handleLiteral(node.value) |
| 103 | + handleTemplateLiteral(node.value) |
| 104 | + }, |
| 105 | + } |
| 106 | + }, |
| 107 | +}) |
| 108 | + |
| 109 | +export default rule |
0 commit comments