@apdesign/code-style-react 1.2.3 → 2.0.1

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.
@@ -1,39 +1,39 @@
1
- const path = require('path');
2
-
3
- module.exports = {
4
- extends: ['stylelint-config-standard'],
5
- plugins: [
6
- 'stylelint-less',
7
- 'stylelint-scss',
8
- path.join(__dirname, 'rules/color-must-use-variable.js'),
9
- ],
10
- overrides: [
11
- {
12
- files: ['**/*.less'],
13
- customSyntax: 'postcss-less',
14
- },
15
- {
16
- files: ['**/*.scss', '**/*.sass'],
17
- customSyntax: 'postcss-scss',
18
- },
19
- ],
20
- rules: {
21
- 'selector-class-pattern': '^[a-z][a-zA-Z0-9-]*$',
22
- 'selector-pseudo-class-no-unknown': [
23
- true,
24
- {
25
- ignorePseudoClasses: ['global'],
26
- },
27
- ],
28
-
29
- 'media-feature-range-notation': null,
30
- 'property-no-vendor-prefix': null,
31
-
32
- 'custom/color-must-use-variable': [
33
- true,
34
- {
35
- severity: 'warning',
36
- },
37
- ],
38
- },
39
- };
1
+ const path = require('node:path');
2
+
3
+ module.exports = {
4
+ extends: ['stylelint-config-standard'],
5
+ plugins: [
6
+ 'stylelint-less',
7
+ 'stylelint-scss',
8
+ path.join(__dirname, 'rules/color-must-use-variable.js'),
9
+ ],
10
+ overrides: [
11
+ {
12
+ files: ['**/*.less'],
13
+ customSyntax: 'postcss-less',
14
+ },
15
+ {
16
+ files: ['**/*.scss', '**/*.sass'],
17
+ customSyntax: 'postcss-scss',
18
+ },
19
+ ],
20
+ rules: {
21
+ 'selector-class-pattern': '^[a-z][a-zA-Z0-9-]*$',
22
+ 'selector-pseudo-class-no-unknown': [
23
+ true,
24
+ {
25
+ ignorePseudoClasses: ['global'],
26
+ },
27
+ ],
28
+
29
+ 'media-feature-range-notation': null,
30
+ 'property-no-vendor-prefix': null,
31
+
32
+ 'custom/color-must-use-variable': [
33
+ true,
34
+ {
35
+ severity: 'warning',
36
+ },
37
+ ],
38
+ },
39
+ };
@@ -1,61 +1,63 @@
1
- const stylelint = require('stylelint');
2
- const valueParser = require('postcss-value-parser');
3
- const cssColorNames = require('css-color-names');
4
- const cssColorKeywords = Object.keys(cssColorNames);
5
-
6
- const ruleName = 'custom/color-must-use-variable';
7
- const messages = stylelint.utils.ruleMessages(ruleName, {
8
- expected: (val) => `Expected color "${val}" to be a color variable`,
9
- });
10
-
11
- const colorWordRegex = new RegExp(
12
- `^(#(?:[0-9a-fA-F]{3,8})|(?:${cssColorKeywords.join('|')}))$`,
13
- 'i',
14
- );
15
-
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) => {
20
- if (node.type === 'function' && node.value === 'var') return true;
21
- if (node.type === 'word' && (node.value.startsWith('@') || node.value.startsWith('$')))
22
- return true;
23
- if (node.nodes && node.nodes.length) {
24
- return node.nodes.some(containsVar);
25
- }
26
- return false;
27
- };
28
-
29
- module.exports = stylelint.createPlugin(ruleName, (primaryOption, secondaryOptions = {}) => {
30
- return (root, result) => {
31
- root.walkDecls((decl) => {
32
- let parsedValue;
33
- try {
34
- parsedValue = valueParser(decl.value);
35
- } catch (e) {
36
- return; // 解析失败跳过
37
- }
38
-
39
- parsedValue.walk((node) => {
40
- const nodeStr = valueParser.stringify(node);
41
- if (containsVar(node)) return; // 跳过包含 var() 的
42
-
43
- if (isColorWord(node) || isHardcodedFunctionColor(node)) {
44
- stylelint.utils.report({
45
- ruleName,
46
- result,
47
- node: decl,
48
- message: messages.expected(nodeStr),
49
- word: nodeStr,
50
- index: decl.value.indexOf(nodeStr),
51
- endIndex: decl.value.indexOf(nodeStr) + nodeStr.length,
52
- severity: secondaryOptions.severity || 'error',
53
- });
54
- }
55
- });
56
- });
57
- };
58
- });
59
-
60
- module.exports.ruleName = ruleName;
61
- module.exports.messages = messages;
1
+ const stylelint = require('stylelint');
2
+ const valueParser = require('postcss-value-parser');
3
+ const cssColorNames = require('css-color-names');
4
+ const cssColorKeywords = Object.keys(cssColorNames);
5
+
6
+ const ruleName = 'custom/color-must-use-variable';
7
+ const messages = stylelint.utils.ruleMessages(ruleName, {
8
+ expected: (val) => `Expected color "${val}" to be a color variable`,
9
+ });
10
+
11
+ const colorWordRegex = new RegExp(
12
+ `^(#(?:[0-9a-fA-F]{3,8})|(?:${cssColorKeywords.join('|')}))$`,
13
+ 'i',
14
+ );
15
+
16
+ const colorFunctionRegex = /^(rgb|rgba|hsl|hsla|lab|lch)$/i;
17
+
18
+ const isColorWord = (node) => node.type === 'word' && colorWordRegex.test(node.value);
19
+ const isHardcodedFunctionColor = (node) =>
20
+ node.type === 'function' && colorFunctionRegex.test(node.value);
21
+ const containsVar = (node) => {
22
+ if (node.type === 'function' && node.value === 'var') return true;
23
+ if (node.type === 'word' && (node.value.startsWith('@') || node.value.startsWith('$')))
24
+ return true;
25
+ if (node.nodes?.length) {
26
+ return node.nodes.some(containsVar);
27
+ }
28
+ return false;
29
+ };
30
+
31
+ module.exports = stylelint.createPlugin(ruleName, (_primaryOption, secondaryOptions = {}) => {
32
+ return (root, result) => {
33
+ root.walkDecls((decl) => {
34
+ let parsedValue;
35
+ try {
36
+ parsedValue = valueParser(decl.value);
37
+ } catch (_e) {
38
+ return; // 解析失败跳过
39
+ }
40
+
41
+ parsedValue.walk((node) => {
42
+ const nodeStr = valueParser.stringify(node);
43
+ if (containsVar(node)) return; // 跳过包含 var()
44
+
45
+ if (isColorWord(node) || isHardcodedFunctionColor(node)) {
46
+ stylelint.utils.report({
47
+ ruleName,
48
+ result,
49
+ node: decl,
50
+ message: messages.expected(nodeStr),
51
+ word: nodeStr,
52
+ index: decl.value.indexOf(nodeStr),
53
+ endIndex: decl.value.indexOf(nodeStr) + nodeStr.length,
54
+ severity: secondaryOptions.severity || 'error',
55
+ });
56
+ }
57
+ });
58
+ });
59
+ };
60
+ });
61
+
62
+ module.exports.ruleName = ruleName;
63
+ module.exports.messages = messages;
@@ -1,107 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- env: {
4
- browser: true,
5
- node: true,
6
- es2021: true,
7
- },
8
- parser: '@typescript-eslint/parser',
9
- extends: [
10
- 'airbnb',
11
- 'airbnb/hooks',
12
- 'plugin:@typescript-eslint/recommended',
13
- 'plugin:import/errors',
14
- 'plugin:import/warnings',
15
- 'prettier',
16
- ],
17
- plugins: ['@typescript-eslint', 'import', 'jsx-a11y', 'react', 'react-hooks'],
18
- rules: {
19
- 'arrow-body-style': 'off',
20
-
21
- camelcase: 'off',
22
- 'class-methods-use-this': 'off',
23
-
24
- 'dot-notation': 'warn',
25
-
26
- 'import/order': 'warn',
27
- 'import/first': 'off',
28
- 'import/extensions': 'off',
29
- 'import/prefer-default-export': 'off',
30
- 'import/newline-after-import': 'warn',
31
- 'react/jsx-curly-brace-presence': 'warn',
32
-
33
- 'jsx-a11y/click-events-have-key-events': 'off',
34
- 'jsx-a11y/no-static-element-interactions': 'off',
35
- 'jsx-a11y/no-noninteractive-element-interactions': 'off',
36
-
37
- 'linebreak-style': 'off',
38
- 'lines-between-class-members': 'warn',
39
-
40
- 'max-classes-per-file': 'off',
41
-
42
- 'no-undef': 'off',
43
- 'no-plusplus': 'off',
44
-
45
- 'prefer-template': 'warn',
46
-
47
- 'react/function-component-definition': 'off',
48
- 'react-hooks/exhaustive-deps': 'off',
49
- 'react/prop-types': 'off',
50
- 'react/react-in-jsx-scope': 'off',
51
- 'react/require-default-props': 'off',
52
- 'react/jsx-boolean-value': 'off',
53
- 'react/jsx-filename-extension': 'off',
54
- 'react/jsx-props-no-spreading': 'off',
55
- 'react/self-closing-comp': 'off',
56
- 'react/jsx-no-useless-fragment': 'off',
57
- 'react/no-array-index-key': 'warn',
58
-
59
- 'spaced-comment': 'warn',
60
-
61
- '@typescript-eslint/no-explicit-any': 'warn',
62
-
63
- 'no-use-before-define': 'off',
64
- '@typescript-eslint/no-use-before-define': ['warn'],
65
-
66
- 'no-unused-vars': 'off',
67
- '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
68
-
69
- 'no-useless-constructor': 'off',
70
- '@typescript-eslint/no-useless-constructor': 'warn',
71
-
72
- 'no-shadow': 'off',
73
- '@typescript-eslint/no-shadow': 'warn',
74
-
75
- 'no-empty-function': ['warn', { allow: ['constructors', 'arrowFunctions'] }],
76
- 'no-param-reassign': ['warn', { props: true, ignorePropertyModificationsFor: ['draft'] }],
77
- 'no-restricted-syntax': [
78
- 'warn',
79
- {
80
- selector: "CallExpression[callee.name='useMemo']",
81
- message: 'Avoid using useMemo unless caching a computed value improves performance.',
82
- },
83
- {
84
- selector: "CallExpression[callee.name='useCallback']",
85
- message:
86
- 'Avoid using useCallback unless you need to cache a function to prevent unnecessary re-renders.',
87
- },
88
- ],
89
- },
90
- parserOptions: {
91
- sourceType: 'module',
92
- },
93
- ignorePatterns: ['dist/', 'node_modules/', '*.d.ts', 'mock', '.eslintrc.cjs'],
94
- settings: {
95
- react: {
96
- version: 'detect',
97
- },
98
- 'import/resolver': {
99
- alias: {
100
- map: [
101
- ['@', './src'], // 将 '@' 映射到 'src' 目录
102
- ],
103
- extensions: ['.ts', '.tsx', '.js', '.jsx', '.d.ts'],
104
- },
105
- },
106
- },
107
- };
@@ -1,107 +0,0 @@
1
- module.exports = {
2
- root: true,
3
- env: {
4
- browser: true,
5
- node: true,
6
- es2021: true,
7
- },
8
- parser: '@typescript-eslint/parser',
9
- extends: [
10
- 'airbnb',
11
- 'airbnb/hooks',
12
- 'plugin:@typescript-eslint/recommended',
13
- 'plugin:import/errors',
14
- 'plugin:import/warnings',
15
- 'prettier',
16
- ],
17
- plugins: ['@typescript-eslint', 'import', 'jsx-a11y', 'react', 'react-hooks'],
18
- rules: {
19
- 'arrow-body-style': 'off',
20
-
21
- camelcase: 'off',
22
- 'class-methods-use-this': 'off',
23
-
24
- 'dot-notation': 'warn',
25
-
26
- 'import/order': 'warn',
27
- 'import/first': 'off',
28
- 'import/extensions': 'off',
29
- 'import/prefer-default-export': 'off',
30
- 'import/newline-after-import': 'warn',
31
-
32
- 'jsx-a11y/click-events-have-key-events': 'off',
33
- 'jsx-a11y/no-static-element-interactions': 'off',
34
- 'jsx-a11y/no-noninteractive-element-interactions': 'off',
35
-
36
- 'linebreak-style': 'off',
37
- 'lines-between-class-members': 'warn',
38
-
39
- 'max-classes-per-file': 'off',
40
-
41
- 'no-undef': 'off',
42
- 'no-plusplus': 'off',
43
-
44
- 'prefer-template': 'warn',
45
-
46
- 'react/function-component-definition': 'off',
47
- 'react-hooks/exhaustive-deps': 'off',
48
- 'react/prop-types': 'off',
49
- 'react/react-in-jsx-scope': 'off',
50
- 'react/require-default-props': 'off',
51
- 'react/jsx-boolean-value': 'off',
52
- 'react/jsx-filename-extension': 'off',
53
- 'react/jsx-props-no-spreading': 'off',
54
- 'react/self-closing-comp': 'off',
55
- 'react/jsx-no-useless-fragment': 'off',
56
- 'react/no-array-index-key': 'warn',
57
- 'react/jsx-curly-brace-presence': 'warn',
58
-
59
- 'spaced-comment': 'warn',
60
-
61
- '@typescript-eslint/no-explicit-any': 'warn',
62
-
63
- 'no-use-before-define': 'off',
64
- '@typescript-eslint/no-use-before-define': ['warn'],
65
-
66
- 'no-unused-vars': 'off',
67
- '@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
68
-
69
- 'no-useless-constructor': 'off',
70
- '@typescript-eslint/no-useless-constructor': 'error',
71
-
72
- 'no-shadow': 'off',
73
- '@typescript-eslint/no-shadow': 'warn',
74
-
75
- 'no-empty-function': ['warn', { allow: ['constructors', 'arrowFunctions'] }],
76
- 'no-param-reassign': ['warn', { props: true, ignorePropertyModificationsFor: ['draft'] }],
77
- 'no-restricted-syntax': [
78
- 'warn',
79
- {
80
- selector: "CallExpression[callee.name='useMemo']",
81
- message: 'Avoid using useMemo unless caching a computed value improves performance.',
82
- },
83
- {
84
- selector: "CallExpression[callee.name='useCallback']",
85
- message:
86
- 'Avoid using useCallback unless you need to cache a function to prevent unnecessary re-renders.',
87
- },
88
- ],
89
- },
90
- parserOptions: {
91
- sourceType: 'module',
92
- },
93
- ignorePatterns: ['dist/', 'node_modules/', '*.d.ts', 'mock', '.eslintrc.cjs'],
94
- settings: {
95
- react: {
96
- version: 'detect',
97
- },
98
- 'import/resolver': {
99
- alias: {
100
- map: [
101
- ['@', './src'], // 将 '@' 映射到 'src' 目录
102
- ],
103
- extensions: ['.ts', '.tsx', '.js', '.jsx', '.d.ts'],
104
- },
105
- },
106
- },
107
- };