|
| 1 | +/** |
| 2 | + * @fileoverview Forbid using arbitrary values in classnames. |
| 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 { |
| 11 | + parsePluginSettings, |
| 12 | + PluginSettings, |
| 13 | +} from "../utils/parse-plugin-settings"; |
| 14 | +import { getBaseClassname } from "../utils/parser/classname"; |
| 15 | +import { |
| 16 | + dissectAtomicNode, |
| 17 | + generateLocForClassname, |
| 18 | + getClassnamesFromValue, |
| 19 | +} from "../utils/parser/node"; |
| 20 | +import { defineVisitors, GenericRuleContext } from "../utils/parser/visitors"; |
| 21 | +import { |
| 22 | + AtomicNode, |
| 23 | + createScriptVisitors, |
| 24 | + createTemplateVisitors, |
| 25 | +} from "../utils/rule"; |
| 26 | + |
| 27 | +export { ESLintUtils } from "@typescript-eslint/utils"; |
| 28 | + |
| 29 | +export const RULE_NAME = "no-arbitrary-value"; |
| 30 | + |
| 31 | +// Message IDs don't need to be prefixed, I just find it easier to keep track of them this way |
| 32 | +export type MessageIds = "issue:arbitrary-value"; |
| 33 | + |
| 34 | +// eslint-disable-next-line @typescript-eslint/no-empty-object-type |
| 35 | +export type RuleOptions = {}; |
| 36 | + |
| 37 | +type Options = [RuleOptions]; |
| 38 | + |
| 39 | +type RuleContext = TSESLintRuleContext<MessageIds, Options>; |
| 40 | + |
| 41 | +// The Rule creator returns a function that is used to create a well-typed ESLint rule |
| 42 | +// The parameter passed into RuleCreator is a URL generator function. |
| 43 | +export const createRule = RuleCreator(urlCreator); |
| 44 | + |
| 45 | +// Matches classnames that contain arbitrary values (e.g., m-[5px]) |
| 46 | +const regexPattern = /^[-a-z]+-\[.*\]$/; |
| 47 | + |
| 48 | +const arbitraryClassnames = ( |
| 49 | + context: RuleContext, |
| 50 | + settings: PluginSettings, |
| 51 | + options: RuleOptions, |
| 52 | + literals: Array<AtomicNode>, |
| 53 | +) => { |
| 54 | + // console.log(options); |
| 55 | + const genericContext = context as unknown as GenericRuleContext; |
| 56 | + for (const node of literals) { |
| 57 | + const { originalClassNamesValue } = dissectAtomicNode(node, genericContext); |
| 58 | + // Process the extracted classnames and report |
| 59 | + const { classNames } = getClassnamesFromValue(originalClassNamesValue); |
| 60 | + for (const targetClassName of classNames) { |
| 61 | + const baseClass = getBaseClassname(targetClassName); |
| 62 | + const match = regexPattern.test(baseClass); |
| 63 | + |
| 64 | + if (!match) continue; |
| 65 | + |
| 66 | + const patchedLoc = generateLocForClassname( |
| 67 | + node, |
| 68 | + targetClassName, |
| 69 | + originalClassNamesValue, |
| 70 | + genericContext, |
| 71 | + ); |
| 72 | + |
| 73 | + context.report({ |
| 74 | + loc: patchedLoc, |
| 75 | + messageId: "issue:arbitrary-value", |
| 76 | + data: { |
| 77 | + className: targetClassName, |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +export const noArbitraryValue = createRule<Options, MessageIds>({ |
| 85 | + name: RULE_NAME, |
| 86 | + meta: { |
| 87 | + docs: { |
| 88 | + description: "Forbid using arbitrary values in classnames.", |
| 89 | + }, |
| 90 | + hasSuggestions: false, |
| 91 | + messages: { |
| 92 | + "issue:arbitrary-value": "Arbitrary value detected in '{{className}}'", |
| 93 | + }, |
| 94 | + // Schema is also parsed by `eslint-doc-generator` |
| 95 | + schema: [ |
| 96 | + { |
| 97 | + type: "object", |
| 98 | + properties: {}, |
| 99 | + additionalProperties: false, |
| 100 | + }, |
| 101 | + ], |
| 102 | + defaultOptions: [{}], |
| 103 | + type: "problem", |
| 104 | + }, |
| 105 | + /** |
| 106 | + * About `defaultOptions`: |
| 107 | + * - `defaultOptions` is not parsed to generate the documentation |
| 108 | + * - `defaultOptions` is used when options are NOT provided in the rules configuration |
| 109 | + * - If some configuration is provided as the second argument, `defaultOptions` is ignored completely (not merged) |
| 110 | + * - In other words, the `defaultOptions` is only used when the rule is used WITHOUT any configuration |
| 111 | + */ |
| 112 | + defaultOptions: [{}], |
| 113 | + create: (context, options) => { |
| 114 | + // Merged settings |
| 115 | + const settings = parsePluginSettings(context.settings); |
| 116 | + |
| 117 | + return defineVisitors( |
| 118 | + context as unknown as Readonly<GenericRuleContext>, |
| 119 | + // Template visitor is only used within Vue SFC files (inside <template> section). |
| 120 | + createTemplateVisitors(context, settings, options, arbitraryClassnames), |
| 121 | + // Script visitor is used within both JSX and Vue SFC files (inside <script> section). |
| 122 | + createScriptVisitors(context, settings, options, arbitraryClassnames), |
| 123 | + ); |
| 124 | + }, |
| 125 | +}); |
| 126 | + |
| 127 | +// TODO: option or new rule to disallow number values like `border-1` in favor of `border-preset1` |
0 commit comments