|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +const file_watcher_1 = require("../utils/file-watcher"); |
| 4 | +let cssWatcher = null; |
| 5 | +const rule = { |
| 6 | + defaultOptions: [ |
| 7 | + { |
| 8 | + classAttributes: ['className', 'class', 'classList'], |
| 9 | + classFunctions: ['clsx', 'classNames', 'cx'], |
| 10 | + cssFiles: ['**/*.css'], |
| 11 | + ignore: ['**/node_modules/**', '**/dist/**', '**/out/**', '**/build/**'], |
| 12 | + }, |
| 13 | + ], |
| 14 | + meta: { |
| 15 | + type: 'problem', |
| 16 | + docs: { |
| 17 | + description: 'Ensure that CSS classes used in JS/TS files exist', |
| 18 | + recommended: 'recommended', |
| 19 | + }, |
| 20 | + messages: { |
| 21 | + unknownClass: "Unknown CSS class '{{className}}'", |
| 22 | + }, |
| 23 | + schema: [ |
| 24 | + { |
| 25 | + type: 'object', |
| 26 | + properties: { |
| 27 | + classAttributes: { |
| 28 | + type: 'array', |
| 29 | + items: { type: 'string' }, |
| 30 | + }, |
| 31 | + classFunctions: { |
| 32 | + type: 'array', |
| 33 | + items: { type: 'string' }, |
| 34 | + }, |
| 35 | + cssFiles: { |
| 36 | + type: 'array', |
| 37 | + items: { type: 'string' }, |
| 38 | + }, |
| 39 | + ignore: { |
| 40 | + type: 'array', |
| 41 | + items: { type: 'string' }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + additionalProperties: false, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + create(context) { |
| 49 | + const options = context.options[0]; |
| 50 | + // Initialize watcher if not already done |
| 51 | + if (!cssWatcher) { |
| 52 | + cssWatcher = new file_watcher_1.CssWatcher(options.cssFiles, options.ignore); |
| 53 | + } |
| 54 | + /** Helper to check if a class exists in our CSS files */ |
| 55 | + const validate = (className, node) => { |
| 56 | + // Ignore Tailwind classes with arbitrary values |
| 57 | + if (className.includes('[')) { |
| 58 | + return; |
| 59 | + } |
| 60 | + // Remove any Tailwind modifiers |
| 61 | + const baseClass = className.split(':').pop(); |
| 62 | + if (!(cssWatcher === null || cssWatcher === void 0 ? void 0 : cssWatcher.hasClass(baseClass))) { |
| 63 | + context.report({ |
| 64 | + node, |
| 65 | + messageId: 'unknownClass', |
| 66 | + data: { className: baseClass }, |
| 67 | + }); |
| 68 | + } |
| 69 | + }; |
| 70 | + /** Helper to validate class names in object expressions */ |
| 71 | + const validateObjectExpression = (objExpr) => { |
| 72 | + objExpr.properties.forEach((prop) => { |
| 73 | + if (prop.type === 'Property') { |
| 74 | + if (prop.key.type === 'Literal' && typeof prop.key.value === 'string') { |
| 75 | + validate(prop.key.value, prop); |
| 76 | + } |
| 77 | + else if (prop.key.type === 'Identifier') { |
| 78 | + validate(prop.key.name, prop); |
| 79 | + } |
| 80 | + } |
| 81 | + // We ignore SpreadElement as it can't contain class names directly |
| 82 | + }); |
| 83 | + }; |
| 84 | + return { |
| 85 | + // Check JSX className attributes |
| 86 | + JSXAttribute(node) { |
| 87 | + var _a, _b, _c; |
| 88 | + if (node.name.type === 'JSXIdentifier' && |
| 89 | + ((_a = options.classAttributes) === null || _a === void 0 ? void 0 : _a.includes(node.name.name))) { |
| 90 | + if (((_b = node.value) === null || _b === void 0 ? void 0 : _b.type) === 'Literal' && typeof node.value.value === 'string') { |
| 91 | + const classNames = node.value.value.split(/\s+/); |
| 92 | + classNames.forEach((className) => { |
| 93 | + validate(className, node); |
| 94 | + }); |
| 95 | + } |
| 96 | + else if (((_c = node.value) === null || _c === void 0 ? void 0 : _c.type) === 'JSXExpressionContainer') { |
| 97 | + const expr = node.value.expression; |
| 98 | + if (expr.type === 'ObjectExpression') { |
| 99 | + validateObjectExpression(expr); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + // Check class utility function calls |
| 105 | + CallExpression(node) { |
| 106 | + var _a; |
| 107 | + if (node.callee.type === 'Identifier' && |
| 108 | + ((_a = options.classFunctions) === null || _a === void 0 ? void 0 : _a.includes(node.callee.name))) { |
| 109 | + node.arguments.forEach((arg) => { |
| 110 | + if (arg.type === 'Literal' && typeof arg.value === 'string') { |
| 111 | + const classNames = arg.value.split(/\s+/); |
| 112 | + classNames.forEach((className) => { |
| 113 | + validate(className, arg); |
| 114 | + }); |
| 115 | + } |
| 116 | + else if (arg.type === 'ObjectExpression') { |
| 117 | + validateObjectExpression(arg); |
| 118 | + } |
| 119 | + }); |
| 120 | + } |
| 121 | + }, |
| 122 | + }; |
| 123 | + }, |
| 124 | +}; |
| 125 | +exports.default = rule; |
0 commit comments