Files
vf_react/eslint-local-rules/no-hardcoded-fui-colors.js
zdl 5a4e6d2a03 fix(eslint): 修复插件冲突和本地规则加载问题
- 移除重复的 @typescript-eslint 插件声明(react-app 已包含)
- 重命名 eslint-rules → eslint-local-rules(符合插件约定)
- 简化 TypeScript overrides,仅保留规则覆盖

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-25 13:00:40 +08:00

87 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* ESLint 规则:禁止在 Company 目录使用硬编码颜色
*
* 检测模式:
* - #D4AF37 (金色十六进制)
* - rgba(212, 175, 55, x) (金色 RGBA)
*
* 使用方式:
* 在 .eslintrc.js 中添加规则配置
*/
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: '禁止在 FUI 主题组件中使用硬编码颜色值',
category: 'Best Practices',
recommended: true,
},
messages: {
noHardcodedGoldRgba:
'避免硬编码金色 RGBA 值 "{{value}}"。请使用 alpha("gold", {{opacity}}) 或 fui.border() 等语义化 API。',
noHardcodedGoldHex:
'避免硬编码金色十六进制值 "{{value}}"。请使用 fui.gold 或 FUI_COLORS.gold[400]。',
},
schema: [],
},
create(context) {
const filename = context.getFilename();
// 仅在 src/views/Company 目录下生效
if (!filename.includes('src/views/Company')) {
return {};
}
// 排除主题定义文件
if (
filename.includes('/theme/') ||
filename.includes('/utils/colorUtils')
) {
return {};
}
// 金色 RGBA 正则 - 匹配 rgba(212, 175, 55, x)
const goldRgbaPattern =
/rgba\(\s*212\s*,\s*175\s*,\s*55\s*,\s*([\d.]+)\s*\)/i;
// 金色十六进制 - 匹配 #D4AF37不区分大小写
const goldHexPattern = /#D4AF37/i;
function checkValue(node, value) {
if (typeof value !== 'string') return;
// 检测金色十六进制
if (goldHexPattern.test(value)) {
context.report({
node,
messageId: 'noHardcodedGoldHex',
data: { value },
});
return;
}
// 检测金色 RGBA
const rgbaMatch = value.match(goldRgbaPattern);
if (rgbaMatch) {
const opacity = rgbaMatch[1];
context.report({
node,
messageId: 'noHardcodedGoldRgba',
data: { value, opacity },
});
}
}
return {
Literal(node) {
checkValue(node, node.value);
},
TemplateElement(node) {
checkValue(node, node.value.raw);
},
};
},
};