@apdesign/code-style-react 1.0.7 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@apdesign/code-style-react",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.9",
|
4
4
|
"scripts": {},
|
5
5
|
"bin": {
|
6
6
|
"apdesign-code-style": "cli.js"
|
@@ -20,6 +20,7 @@
|
|
20
20
|
"dependencies": {
|
21
21
|
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
22
22
|
"@typescript-eslint/parser": "^6.21.0",
|
23
|
+
"css-color-names": "^1.0.1",
|
23
24
|
"eslint": "^8.57.1",
|
24
25
|
"eslint-config-airbnb": "^19.0.4",
|
25
26
|
"eslint-config-prettier": "^9.1.0",
|
@@ -1,97 +1,34 @@
|
|
1
1
|
const stylelint = require('stylelint');
|
2
2
|
const valueParser = require('postcss-value-parser');
|
3
|
+
const cssColorNames = require('css-color-names');
|
4
|
+
const cssColorKeywords = Object.keys(cssColorNames);
|
3
5
|
|
4
6
|
const ruleName = 'custom/color-must-use-variable';
|
5
7
|
const messages = stylelint.utils.ruleMessages(ruleName, {
|
6
8
|
expected: (val) => `Expected color "${val}" to be a color variable`,
|
7
9
|
});
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
'
|
12
|
-
|
13
|
-
'green',
|
14
|
-
'black',
|
15
|
-
'white',
|
16
|
-
'gray',
|
17
|
-
'aqua',
|
18
|
-
'fuchsia',
|
19
|
-
'lime',
|
20
|
-
'maroon',
|
21
|
-
'navy',
|
22
|
-
'olive',
|
23
|
-
'purple',
|
24
|
-
'silver',
|
25
|
-
'teal',
|
26
|
-
'yellow',
|
27
|
-
'orange',
|
28
|
-
'pink',
|
29
|
-
'brown',
|
30
|
-
'cyan',
|
31
|
-
'magenta',
|
32
|
-
'gold',
|
33
|
-
'azure',
|
34
|
-
];
|
11
|
+
const colorWordRegex = new RegExp(
|
12
|
+
`^(#(?:[0-9a-fA-F]{3,8})|(?:${cssColorKeywords.join('|')}))$`,
|
13
|
+
'i',
|
14
|
+
);
|
35
15
|
|
36
|
-
|
37
|
-
const
|
38
|
-
'
|
39
|
-
|
40
|
-
'background-color',
|
41
|
-
'border',
|
42
|
-
'border-color',
|
43
|
-
'border-top-color',
|
44
|
-
'border-right-color',
|
45
|
-
'border-bottom-color',
|
46
|
-
'border-left-color',
|
47
|
-
'outline',
|
48
|
-
'outline-color',
|
49
|
-
'text-decoration-color',
|
50
|
-
'box-shadow',
|
51
|
-
'text-shadow',
|
52
|
-
'column-rule-color',
|
53
|
-
'caret-color',
|
54
|
-
'fill',
|
55
|
-
'stroke',
|
56
|
-
'stop-color',
|
57
|
-
];
|
58
|
-
|
59
|
-
const containsVarFunction = (node) => {
|
16
|
+
const isColorWord = (node) => node.type === 'word' && colorWordRegex.test(node.value);
|
17
|
+
const isHardcodedFunctionColor = (node) =>
|
18
|
+
node.type === 'function' && /^(rgb|rgba|hsl|hsla|lab|lch)$/i.test(node.value);
|
19
|
+
const containsVar = (node) => {
|
60
20
|
if (node.type === 'function' && node.value === 'var') return true;
|
21
|
+
if (node.type === 'word' && (node.value.startsWith('@') || node.value.startsWith('$')))
|
22
|
+
return true;
|
61
23
|
if (node.nodes && node.nodes.length) {
|
62
|
-
return node.nodes.some(
|
24
|
+
return node.nodes.some(containsVar);
|
63
25
|
}
|
64
26
|
return false;
|
65
27
|
};
|
66
28
|
|
67
29
|
module.exports = stylelint.createPlugin(ruleName, (primaryOption, secondaryOptions = {}) => {
|
68
|
-
// 合并自定义颜色关键字和默认颜色关键字(去重)
|
69
|
-
const cssColorKeywords = Array.from(
|
70
|
-
new Set([...(secondaryOptions.cssColorKeywords || []), ...defaultCssColorKeywords]),
|
71
|
-
);
|
72
|
-
|
73
|
-
// 生成颜色匹配正则
|
74
|
-
const colorWordRegex = new RegExp(
|
75
|
-
`^(#(?:[0-9a-fA-F]{3,8})|(?:${cssColorKeywords.join('|')}))$`,
|
76
|
-
'i',
|
77
|
-
);
|
78
|
-
|
79
|
-
// 判断是否为硬编码颜色文字,如 #fff 或 red
|
80
|
-
const isColorWord = (node) => node.type === 'word' && colorWordRegex.test(node.value);
|
81
|
-
|
82
|
-
// 判断是否为硬编码颜色函数,如 rgb(), hsl() 等
|
83
|
-
const isHardcodedFunctionColor = (node) =>
|
84
|
-
node.type === 'function' && /^(rgb|rgba|hsl|hsla|lab|lch)$/i.test(node.value);
|
85
|
-
|
86
|
-
// 合并自定义属性和默认属性(去重)
|
87
|
-
const propsToCheck = Array.from(
|
88
|
-
new Set([...(secondaryOptions.properties || []), ...defaultPropsToCheck]),
|
89
|
-
);
|
90
|
-
|
91
30
|
return (root, result) => {
|
92
31
|
root.walkDecls((decl) => {
|
93
|
-
if (!propsToCheck.includes(decl.prop)) return;
|
94
|
-
|
95
32
|
let parsedValue;
|
96
33
|
try {
|
97
34
|
parsedValue = valueParser(decl.value);
|
@@ -101,7 +38,7 @@ module.exports = stylelint.createPlugin(ruleName, (primaryOption, secondaryOptio
|
|
101
38
|
|
102
39
|
parsedValue.walk((node) => {
|
103
40
|
const nodeStr = valueParser.stringify(node);
|
104
|
-
if (
|
41
|
+
if (containsVar(node)) return; // 跳过包含 var() 的
|
105
42
|
|
106
43
|
if (isColorWord(node) || isHardcodedFunctionColor(node)) {
|
107
44
|
stylelint.utils.report({
|