@alveole/eslint-config 0.24.0 → 0.26.0
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 +1 -1
- package/rules/_shared.js +116 -1
package/package.json
CHANGED
package/rules/_shared.js
CHANGED
|
@@ -1,4 +1,119 @@
|
|
|
1
|
-
const flashListPlugin =
|
|
1
|
+
const flashListPlugin = {
|
|
2
|
+
rules: {
|
|
3
|
+
'prefer-flash-list': {
|
|
4
|
+
meta: {
|
|
5
|
+
type: 'suggestion',
|
|
6
|
+
fixable: 'code',
|
|
7
|
+
docs: {
|
|
8
|
+
description: 'Interdit FlatList (react-native) et impose FlashList (@shopify/flash-list).',
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
preferFlashList:
|
|
12
|
+
'N’utilisez pas FlatList depuis react-native. Utilisez FlashList depuis @shopify/flash-list.',
|
|
13
|
+
},
|
|
14
|
+
schema: [],
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
const sourceCode = context.sourceCode;
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
ImportDeclaration(node) {
|
|
21
|
+
if (node.source.value !== 'react-native') {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const flatListSpecifier = node.specifiers.find(
|
|
26
|
+
specifier =>
|
|
27
|
+
specifier.type === 'ImportSpecifier' &&
|
|
28
|
+
specifier.imported &&
|
|
29
|
+
specifier.imported.type === 'Identifier' &&
|
|
30
|
+
specifier.imported.name === 'FlatList',
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (!flatListSpecifier) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
context.report({
|
|
38
|
+
node: flatListSpecifier,
|
|
39
|
+
messageId: 'preferFlashList',
|
|
40
|
+
fix(fixer) {
|
|
41
|
+
const program = node.parent;
|
|
42
|
+
if (!program || program.type !== 'Program') {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const importDeclarations = program.body.filter(statement => statement.type === 'ImportDeclaration');
|
|
47
|
+
const flashListImport = importDeclarations.find(
|
|
48
|
+
statement => statement.source.value === '@shopify/flash-list',
|
|
49
|
+
);
|
|
50
|
+
const hasFlashListAlready =
|
|
51
|
+
!!flashListImport &&
|
|
52
|
+
flashListImport.specifiers.some(
|
|
53
|
+
specifier =>
|
|
54
|
+
specifier.type === 'ImportSpecifier' &&
|
|
55
|
+
specifier.imported &&
|
|
56
|
+
specifier.imported.type === 'Identifier' &&
|
|
57
|
+
specifier.imported.name === 'FlashList',
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const fixes = [];
|
|
61
|
+
|
|
62
|
+
if (node.specifiers.length === 1) {
|
|
63
|
+
if (hasFlashListAlready) {
|
|
64
|
+
fixes.push(fixer.remove(node));
|
|
65
|
+
} else {
|
|
66
|
+
fixes.push(fixer.replaceText(node, "import { FlashList } from '@shopify/flash-list';"));
|
|
67
|
+
}
|
|
68
|
+
return fixes;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let [start, end] = flatListSpecifier.range;
|
|
72
|
+
const tokenBefore = sourceCode.getTokenBefore(flatListSpecifier);
|
|
73
|
+
const tokenAfter = sourceCode.getTokenAfter(flatListSpecifier);
|
|
74
|
+
|
|
75
|
+
if (tokenAfter && tokenAfter.value === ',') {
|
|
76
|
+
end = tokenAfter.range[1];
|
|
77
|
+
} else if (tokenBefore && tokenBefore.value === ',') {
|
|
78
|
+
start = tokenBefore.range[0];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fixes.push(fixer.removeRange([start, end]));
|
|
82
|
+
|
|
83
|
+
if (!hasFlashListAlready) {
|
|
84
|
+
if (flashListImport) {
|
|
85
|
+
const hasNamedSpecifiers = flashListImport.specifiers.some(
|
|
86
|
+
specifier => specifier.type === 'ImportSpecifier',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
if (hasNamedSpecifiers) {
|
|
90
|
+
const namedSpecifiers = flashListImport.specifiers.filter(
|
|
91
|
+
specifier => specifier.type === 'ImportSpecifier',
|
|
92
|
+
);
|
|
93
|
+
const lastNamedSpecifier = namedSpecifiers[namedSpecifiers.length - 1];
|
|
94
|
+
fixes.push(fixer.insertTextAfter(lastNamedSpecifier, ', FlashList'));
|
|
95
|
+
} else if (flashListImport.specifiers.length > 0) {
|
|
96
|
+
const lastSpecifier = flashListImport.specifiers[flashListImport.specifiers.length - 1];
|
|
97
|
+
fixes.push(fixer.insertTextAfter(lastSpecifier, ', { FlashList }'));
|
|
98
|
+
} else {
|
|
99
|
+
fixes.push(
|
|
100
|
+
fixer.replaceText(flashListImport, "import { FlashList } from '@shopify/flash-list';"),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
fixes.push(fixer.insertTextAfter(node, "\nimport { FlashList } from '@shopify/flash-list';"));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return fixes;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
2
117
|
|
|
3
118
|
/** @type {import('eslint').Linter.Config} */
|
|
4
119
|
const config = {
|