|
| 1 | +/** |
| 2 | + * @fileoverview Warns about `-` prefixed classnames using arbitrary values. |
| 3 | + * @author François Massart |
| 4 | + */ |
| 5 | + |
| 6 | +import { RuleCreator } from "@typescript-eslint/utils/eslint-utils"; |
| 7 | +import { RuleContext as TSESLintRuleContext } from "@typescript-eslint/utils/ts-eslint"; |
| 8 | + |
| 9 | +import urlCreator from "../url-creator"; |
| 10 | +import { joiner } from "../utils/joiner"; |
| 11 | +import { |
| 12 | + parsePluginSettings, |
| 13 | + PluginSettings, |
| 14 | +} from "../utils/parse-plugin-settings"; |
| 15 | +import { |
| 16 | + getBaseClassname, |
| 17 | + getModifiersPrefix, |
| 18 | +} from "../utils/parser/classname"; |
| 19 | +import { |
| 20 | + dissectAtomicNode, |
| 21 | + generateLocForClassname, |
| 22 | + getClassnamesFromValue, |
| 23 | +} from "../utils/parser/node"; |
| 24 | +import { defineVisitors, GenericRuleContext } from "../utils/parser/visitors"; |
| 25 | +import { |
| 26 | + AtomicNode, |
| 27 | + createScriptVisitors, |
| 28 | + createTemplateVisitors, |
| 29 | +} from "../utils/rule"; |
| 30 | + |
| 31 | +export { ESLintUtils } from "@typescript-eslint/utils"; |
| 32 | + |
| 33 | +export const RULE_NAME = "enforces-negative-arbitrary-values"; |
| 34 | + |
| 35 | +// Message IDs don't need to be prefixed, I just find it easier to keep track of them this way |
| 36 | +export type MessageIds = "fix:irregular-negative"; |
| 37 | + |
| 38 | +// eslint-disable-next-line @typescript-eslint/no-empty-object-type |
| 39 | +export type RuleOptions = {}; |
| 40 | + |
| 41 | +type Options = [RuleOptions]; |
| 42 | + |
| 43 | +type RuleContext = TSESLintRuleContext<MessageIds, Options>; |
| 44 | + |
| 45 | +// The Rule creator returns a function that is used to create a well-typed ESLint rule |
| 46 | +// The parameter passed into RuleCreator is a URL generator function. |
| 47 | +export const createRule = RuleCreator(urlCreator); |
| 48 | + |
| 49 | +const propertiesPattern = [ |
| 50 | + // inset, inset-x, inset-y, scale, scale-x, scale-y |
| 51 | + "((inset|scale)(?:-(?:x|y))?)", |
| 52 | + // simple properties |
| 53 | + "(m|top|right|bottom|left|z|order|tracking|indent|(backdrop-)?hue-rotate)|(space-(x|y))", |
| 54 | + // scroll-m, scroll-mx, scroll-my, scroll-mt, scroll-mb, scroll-ml, scroll-mr, scroll-ms, scroll-me, scroll-mbs, scroll-mbe |
| 55 | + "(scroll-m(?:x|y|s|e|bs|be|t|r|b|l|))", |
| 56 | + // skew, skew-x, skew-y, translate, translate-x, translate-y, rotate, rotate-x, rotate-y, rotate-z |
| 57 | + "((skew|translate|rotate)(?:-(?:x|y|z))?)", |
| 58 | +].join("|"); |
| 59 | +// Matches classnames that start with '-' and contain arbitrary values (e.g., -m-[10px]) |
| 60 | +const regexPattern = [ |
| 61 | + "^", |
| 62 | + "(?<negative>-)", |
| 63 | + "(?<property>(" + propertiesPattern + "))", |
| 64 | + String.raw`-\[(?<arbitraryValue>.*)\]`, |
| 65 | + "$", |
| 66 | +].join(""); |
| 67 | + |
| 68 | +const negativeArbitraryRegEx = new RegExp(regexPattern); |
| 69 | + |
| 70 | +const negativeArbitraryClassnames = ( |
| 71 | + context: RuleContext, |
| 72 | + settings: PluginSettings, |
| 73 | + options: RuleOptions, |
| 74 | + literals: Array<AtomicNode>, |
| 75 | +) => { |
| 76 | + // console.log(options); |
| 77 | + const genericContext = context as unknown as GenericRuleContext; |
| 78 | + for (const node of literals) { |
| 79 | + const { originalClassNamesValue, start, end, prefix, suffix } = |
| 80 | + dissectAtomicNode(node, genericContext); |
| 81 | + // Process the extracted classnames and report |
| 82 | + const { classNames, whitespaces, headSpace, tailSpace } = |
| 83 | + getClassnamesFromValue(originalClassNamesValue); |
| 84 | + for (const [index, targetClassName] of classNames.entries()) { |
| 85 | + const baseClass = getBaseClassname(targetClassName); |
| 86 | + const modifiers = getModifiersPrefix(targetClassName); |
| 87 | + const match = baseClass.match(negativeArbitraryRegEx); |
| 88 | + |
| 89 | + if (!match?.groups) continue; |
| 90 | + |
| 91 | + const patchedLoc = generateLocForClassname( |
| 92 | + node, |
| 93 | + targetClassName, |
| 94 | + originalClassNamesValue, |
| 95 | + genericContext, |
| 96 | + ); |
| 97 | + |
| 98 | + const { property = "", arbitraryValue = "" } = match.groups; |
| 99 | + const arbitraryValuePatched = arbitraryValue.startsWith("-") |
| 100 | + ? arbitraryValue.slice(1) |
| 101 | + : "-" + arbitraryValue; |
| 102 | + const patchedClass = `${modifiers}${property}-[${arbitraryValuePatched}]`; |
| 103 | + |
| 104 | + // Patch the problematic classname |
| 105 | + classNames[index] = patchedClass; |
| 106 | + |
| 107 | + // Generates the "cleaned" attribute value |
| 108 | + let patchedValue = joiner({ |
| 109 | + classNames, |
| 110 | + whitespaces, |
| 111 | + headSpace, |
| 112 | + tailSpace, |
| 113 | + validator: (candidate) => candidate !== targetClassName, |
| 114 | + }); |
| 115 | + patchedValue = prefix + patchedValue + suffix; |
| 116 | + |
| 117 | + context.report({ |
| 118 | + loc: patchedLoc, |
| 119 | + messageId: "fix:irregular-negative", |
| 120 | + data: { |
| 121 | + oldClassName: targetClassName, |
| 122 | + newClassName: patchedClass, |
| 123 | + }, |
| 124 | + fix: (fixer) => fixer.replaceTextRange([start, end], patchedValue), |
| 125 | + }); |
| 126 | + } |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +export const enforcesNegativeArbitraryValues = createRule<Options, MessageIds>({ |
| 131 | + name: RULE_NAME, |
| 132 | + meta: { |
| 133 | + docs: { |
| 134 | + description: |
| 135 | + "Warns about `-` prefixed classnames using arbitrary values.", |
| 136 | + }, |
| 137 | + hasSuggestions: false, |
| 138 | + messages: { |
| 139 | + "fix:irregular-negative": `Replace '{{oldClassName}}' by '{{newClassName}}'`, |
| 140 | + }, |
| 141 | + fixable: "code", |
| 142 | + // Schema is also parsed by `eslint-doc-generator` |
| 143 | + schema: [ |
| 144 | + { |
| 145 | + type: "object", |
| 146 | + properties: {}, |
| 147 | + additionalProperties: false, |
| 148 | + }, |
| 149 | + ], |
| 150 | + defaultOptions: [{}], |
| 151 | + type: "problem", |
| 152 | + }, |
| 153 | + /** |
| 154 | + * About `defaultOptions`: |
| 155 | + * - `defaultOptions` is not parsed to generate the documentation |
| 156 | + * - `defaultOptions` is used when options are NOT provided in the rules configuration |
| 157 | + * - If some configuration is provided as the second argument, `defaultOptions` is ignored completely (not merged) |
| 158 | + * - In other words, the `defaultOptions` is only used when the rule is used WITHOUT any configuration |
| 159 | + */ |
| 160 | + defaultOptions: [{}], |
| 161 | + create: (context, options) => { |
| 162 | + // Merged settings |
| 163 | + const settings = parsePluginSettings(context.settings); |
| 164 | + |
| 165 | + return defineVisitors( |
| 166 | + context as unknown as Readonly<GenericRuleContext>, |
| 167 | + // Template visitor is only used within Vue SFC files (inside <template> section). |
| 168 | + createTemplateVisitors( |
| 169 | + context, |
| 170 | + settings, |
| 171 | + options, |
| 172 | + negativeArbitraryClassnames, |
| 173 | + ), |
| 174 | + // Script visitor is used within both JSX and Vue SFC files (inside <script> section). |
| 175 | + createScriptVisitors( |
| 176 | + context, |
| 177 | + settings, |
| 178 | + options, |
| 179 | + negativeArbitraryClassnames, |
| 180 | + ), |
| 181 | + ); |
| 182 | + }, |
| 183 | +}); |
0 commit comments