-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path.eslintrc.js
More file actions
110 lines (99 loc) · 2.55 KB
/
.eslintrc.js
File metadata and controls
110 lines (99 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// https://docs.expo.dev/guides/using-eslint/
const forbiddenRule = require('./forbidden-rule');
/**
* 深度比较两个值是否相等
* @template T
* @param {T} a
* @param {T} b
* @returns {boolean}
*/
function deepEqual(a, b) {
if (a === b) return true;
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
return false;
}
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (let key of keysA) {
if (!keysB.includes(key)) return false;
if (!deepEqual(a[key], b[key])) return false;
}
return true;
}
/**
* 将目录路径转换为递归 glob:
* - a/b -> a/b/**\/\*
* - a/b/ -> a/b/**\/\*
* - a/b.tsx -> a/b.tsx
*
* @param {string} input - 原始路径
* @returns {string} 转换后的路径
*/
function normalizeToRecursiveGlob(input) {
const normalized = input.replace(/\/+$/, '');
const lastSegment = normalized.split('/').pop() || '';
const looksLikeFile = /\.[^/]+$/.test(lastSegment);
if (looksLikeFile) {
return normalized;
}
return `${normalized}/**/*`;
}
module.exports = {
root: true,
extends: ['@react-native', 'expo', 'prettier', 'plugin:react/jsx-runtime'],
plugins: ['prettier'],
rules: {
'prettier/prettier': [
'warn',
{
endOfLine: 'auto',
},
],
'comma-dangle': [
'warn',
{
arrays: 'always-multiline',
objects: 'always-multiline',
imports: 'always-multiline',
exports: 'always-multiline',
functions: 'only-multiline',
},
],
'no-restricted-imports': [
'error',
{
patterns: forbiddenRule.flatMap(item =>
item.names.map(name => ({
group: [item.source],
importNames: [name],
message: item.message,
})),
),
},
],
},
overrides: forbiddenRule
.filter(item => item.allowIn?.length)
.flatMap(item =>
item.allowIn.map(allowPath => ({
files: normalizeToRecursiveGlob(allowPath),
rules: {
'no-restricted-imports': [
'error',
{
patterns: forbiddenRule
.filter(rule => !deepEqual(rule, item)) // 排除当前规则,保留其他规则
.flatMap(rule =>
rule.names.map(name => ({
group: [rule.source],
importNames: [name],
message: rule.message,
})),
),
},
],
},
})),
),
};