/** * frontend/eslint-rules/no-legacy-admin-controls.js * 用途:管理端 Vue 里禁止再写原生表单控件、旧按钮样式和 lucide 图标。 * 依赖:vue-eslint-parser 提供的 VElement、VAttribute,以及脚本里的 ImportDeclaration。 * 逻辑:由 eslint.config.js 以 hxops/no-legacy-admin-controls 挂上。当前严重级别为 off。 * 改为 error 的步骤见 docs/ui/ELEMENT_MIGRATION.md §9。 */ const LEGACY_TAGS = new Set(['button', 'input', 'select', 'textarea']); const LEGACY_CLASSES = [ 'btn-primary', 'btn-secondary', 'btn-ghost', 'btn-danger', 'input-field', 'select-field', ]; /** * 判断属性是不是 class 或 :class。 * 核心逻辑:静态 class 的 key 是 VIdentifier;绑定 class 的 key 是 VDirectiveKey,参数名才是 class。 * @param key vue-eslint-parser 的属性键节点 * @returns 是 class 绑定则为 true */ function isClassAttribute(key) { if (key?.type === 'VIdentifier') return key.name === 'class'; return key?.type === 'VDirectiveKey' && key.argument?.name === 'class'; } /** @type {import('eslint').Rule.RuleModule} */ const rule = { meta: { type: 'problem', docs: { description: '管理端界面使用 Element Plus,不再新增原生控件和旧样式类', }, schema: [], messages: { tag: '管理端请使用 Element Plus,不要新增 <{{name}}>。见 docs/ui/ELEMENT_MIGRATION.md。', klass: '管理端请使用 Element Plus,不要新增样式类 {{name}}。见 docs/ui/ELEMENT_MIGRATION.md。', lucide: '管理端按钮图标请使用 @element-plus/icons-vue,不要新增 lucide-vue-next。', }, }, create(context) { const templateVisitor = { VElement(node) { const name = node.rawName || node.name; if (!LEGACY_TAGS.has(name)) return; context.report({ node, messageId: 'tag', data: { name } }); }, VAttribute(node) { if (!isClassAttribute(node.key)) return; const raw = context.sourceCode.getText(node); for (const name of LEGACY_CLASSES) { if (!raw.includes(name)) continue; context.report({ node, messageId: 'klass', data: { name } }); } }, }; const scriptVisitor = { ImportDeclaration(node) { if (node.source?.value !== 'lucide-vue-next') return; context.report({ node, messageId: 'lucide' }); }, }; // 模板节点不会出现在脚本的遍历里,必须交给 vue-eslint-parser 分开访问。 const defineTemplateBodyVisitor = context.sourceCode.parserServices?.defineTemplateBodyVisitor; if (typeof defineTemplateBodyVisitor === 'function') { return defineTemplateBodyVisitor(templateVisitor, scriptVisitor); } return scriptVisitor; }, }; export default rule;