|
| 1 | +/** |
| 2 | + * Disallow raw `btn-*` and `btn-v2-*` class strings in JSX className / |
| 3 | + * `classed(...)` / `classNames(...)` / `cn(...)` calls. |
| 4 | + * |
| 5 | + * The token classes are an implementation detail of <Button> / <ButtonV2> |
| 6 | + * and the buttons.ts / buttons-v2.ts Tailwind plugins. Hand-stamping |
| 7 | + * them in className strings bypasses every variant guarantee |
| 8 | + * (typography, focus ring, disabled state, theme contract). |
| 9 | + * |
| 10 | + * Use the component instead: |
| 11 | + * |
| 12 | + * // BAD |
| 13 | + * <a className="btn btn-primary">Click</a> |
| 14 | + * |
| 15 | + * // GOOD |
| 16 | + * <Button tag="a" variant={ButtonVariant.Primary}>Click</Button> |
| 17 | + */ |
| 18 | + |
| 19 | +const RAW_BUTTON_CLASS_PATTERN = /(?:^|\s)(btn(?:-v2)?(?:-[a-zA-Z]+)*)\b/; |
| 20 | + |
| 21 | +const collectStringPieces = (node, out = []) => { |
| 22 | + if (!node) { |
| 23 | + return out; |
| 24 | + } |
| 25 | + switch (node.type) { |
| 26 | + case 'Literal': |
| 27 | + if (typeof node.value === 'string') { |
| 28 | + out.push({ value: node.value, node }); |
| 29 | + } |
| 30 | + return out; |
| 31 | + case 'TemplateLiteral': |
| 32 | + node.quasis.forEach((quasi) => { |
| 33 | + out.push({ value: quasi.value.cooked || quasi.value.raw, node: quasi }); |
| 34 | + }); |
| 35 | + node.expressions.forEach((expr) => collectStringPieces(expr, out)); |
| 36 | + return out; |
| 37 | + case 'TemplateElement': |
| 38 | + out.push({ value: node.value.cooked || node.value.raw, node }); |
| 39 | + return out; |
| 40 | + case 'JSXExpressionContainer': |
| 41 | + return collectStringPieces(node.expression, out); |
| 42 | + case 'CallExpression': |
| 43 | + node.arguments.forEach((arg) => collectStringPieces(arg, out)); |
| 44 | + return out; |
| 45 | + case 'ConditionalExpression': |
| 46 | + collectStringPieces(node.consequent, out); |
| 47 | + collectStringPieces(node.alternate, out); |
| 48 | + return out; |
| 49 | + case 'LogicalExpression': |
| 50 | + collectStringPieces(node.left, out); |
| 51 | + collectStringPieces(node.right, out); |
| 52 | + return out; |
| 53 | + case 'ArrayExpression': |
| 54 | + node.elements.forEach((el) => collectStringPieces(el, out)); |
| 55 | + return out; |
| 56 | + case 'ObjectExpression': |
| 57 | + node.properties.forEach((prop) => { |
| 58 | + if (prop.type === 'Property' && prop.key) { |
| 59 | + collectStringPieces(prop.key, out); |
| 60 | + } |
| 61 | + }); |
| 62 | + return out; |
| 63 | + default: |
| 64 | + return out; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +const reportMatches = (context, pieces) => { |
| 69 | + pieces.forEach(({ value, node }) => { |
| 70 | + if (!value || typeof value !== 'string') { |
| 71 | + return; |
| 72 | + } |
| 73 | + const match = value.match(RAW_BUTTON_CLASS_PATTERN); |
| 74 | + if (!match) { |
| 75 | + return; |
| 76 | + } |
| 77 | + context.report({ |
| 78 | + node, |
| 79 | + message: |
| 80 | + `Raw button class "${match[1]}" is not allowed. ` + |
| 81 | + 'Use the <Button> or <ButtonV2> component with a `variant` prop.', |
| 82 | + }); |
| 83 | + }); |
| 84 | +}; |
| 85 | + |
| 86 | +const isClassNameAttribute = (node) => |
| 87 | + node.name && /^class(Name)?$/.test(node.name.name); |
| 88 | + |
| 89 | +const CLASSNAME_HELPERS = new Set([ |
| 90 | + 'classNames', |
| 91 | + 'classnames', |
| 92 | + 'cn', |
| 93 | + 'clsx', |
| 94 | + 'classed', |
| 95 | + 'twMerge', |
| 96 | +]); |
| 97 | + |
| 98 | +module.exports = { |
| 99 | + meta: { |
| 100 | + type: 'problem', |
| 101 | + docs: { |
| 102 | + description: |
| 103 | + 'Disallow raw `btn-*` / `btn-v2-*` class strings; use <Button> instead.', |
| 104 | + recommended: false, |
| 105 | + }, |
| 106 | + schema: [], |
| 107 | + }, |
| 108 | + create(context) { |
| 109 | + return { |
| 110 | + JSXAttribute(node) { |
| 111 | + if (!isClassNameAttribute(node) || !node.value) { |
| 112 | + return; |
| 113 | + } |
| 114 | + const pieces = collectStringPieces(node.value); |
| 115 | + reportMatches(context, pieces); |
| 116 | + }, |
| 117 | + CallExpression(node) { |
| 118 | + const callee = node.callee; |
| 119 | + const calleeName = |
| 120 | + (callee.type === 'Identifier' && callee.name) || |
| 121 | + (callee.type === 'MemberExpression' && |
| 122 | + callee.property && |
| 123 | + callee.property.name); |
| 124 | + if (!calleeName || !CLASSNAME_HELPERS.has(calleeName)) { |
| 125 | + return; |
| 126 | + } |
| 127 | + const pieces = []; |
| 128 | + node.arguments.forEach((arg) => collectStringPieces(arg, pieces)); |
| 129 | + reportMatches(context, pieces); |
| 130 | + }, |
| 131 | + }; |
| 132 | + }, |
| 133 | +}; |
0 commit comments