@react-native/eslint-plugin 0.87.0-rc.0 → 0.87.0-rc.2
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/__tests__/no-deep-imports-test.js +28 -2
- package/no-deep-imports.js +33 -11
- package/package.json +1 -1
- package/utils.js +7 -0
|
@@ -29,10 +29,10 @@ eslintTester.run('../no-deep-imports', rule, {
|
|
|
29
29
|
"import Foo from 'react-native-foo';",
|
|
30
30
|
"import Foo from 'react-native-foo/Foo';",
|
|
31
31
|
"import Foo from 'react/native/Foo';",
|
|
32
|
-
"import 'react-native/Libraries/Core/InitializeCore';",
|
|
33
|
-
"require('react-native/Libraries/Core/InitializeCore');",
|
|
34
32
|
"import Foo from 'react-native/src/fb_internal/Foo'",
|
|
35
33
|
"require('react-native/src/fb_internal/Foo')",
|
|
34
|
+
"import 'react-native/setup-env';",
|
|
35
|
+
"require('react-native/setup-env');",
|
|
36
36
|
],
|
|
37
37
|
invalid: [
|
|
38
38
|
{
|
|
@@ -125,5 +125,31 @@ eslintTester.run('../no-deep-imports', rule, {
|
|
|
125
125
|
],
|
|
126
126
|
output: null,
|
|
127
127
|
},
|
|
128
|
+
{
|
|
129
|
+
code: "import 'react-native/Libraries/Core/InitializeCore';",
|
|
130
|
+
errors: [
|
|
131
|
+
{
|
|
132
|
+
messageId: 'useReplacementSource',
|
|
133
|
+
data: {
|
|
134
|
+
importPath: 'react-native/Libraries/Core/InitializeCore',
|
|
135
|
+
replacementSource: 'react-native/setup-env',
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
output: "import 'react-native/setup-env';",
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
code: "require('react-native/Libraries/Core/InitializeCore');",
|
|
143
|
+
errors: [
|
|
144
|
+
{
|
|
145
|
+
messageId: 'useReplacementSource',
|
|
146
|
+
data: {
|
|
147
|
+
importPath: 'react-native/Libraries/Core/InitializeCore',
|
|
148
|
+
replacementSource: 'react-native/setup-env',
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
output: "require('react-native/setup-env');",
|
|
153
|
+
},
|
|
128
154
|
],
|
|
129
155
|
});
|
package/no-deep-imports.js
CHANGED
|
@@ -21,6 +21,8 @@ module.exports = {
|
|
|
21
21
|
messages: {
|
|
22
22
|
deepImport:
|
|
23
23
|
"'{{importPath}}' React Native deep imports are deprecated. Please use the top level import instead.",
|
|
24
|
+
useReplacementSource:
|
|
25
|
+
"'{{importPath}}' is deprecated. Please import '{{replacementSource}}' instead.",
|
|
24
26
|
},
|
|
25
27
|
schema: [],
|
|
26
28
|
fixable: 'code',
|
|
@@ -31,12 +33,14 @@ module.exports = {
|
|
|
31
33
|
ImportDeclaration(node) {
|
|
32
34
|
if (
|
|
33
35
|
!isDeepReactNativeImport(node.source) ||
|
|
34
|
-
isInitializeCoreImport(node.source) ||
|
|
35
36
|
isSecondaryEntryPoint(node.source) ||
|
|
36
37
|
isFbInternalImport(node.source)
|
|
37
38
|
) {
|
|
38
39
|
return;
|
|
39
40
|
}
|
|
41
|
+
if (reportReplacementSource(node.source)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
40
44
|
if (isDefaultImport(node)) {
|
|
41
45
|
const reactNativeSource = node.source.value.slice(
|
|
42
46
|
'react-native/'.length,
|
|
@@ -88,13 +92,16 @@ module.exports = {
|
|
|
88
92
|
CallExpression(node) {
|
|
89
93
|
if (
|
|
90
94
|
!isDeepRequire(node) ||
|
|
91
|
-
isInitializeCoreImport(node.arguments[0]) ||
|
|
92
95
|
isSecondaryEntryPoint(node.arguments[0]) ||
|
|
93
96
|
isFbInternalImport(node.arguments[0])
|
|
94
97
|
) {
|
|
95
98
|
return;
|
|
96
99
|
}
|
|
97
100
|
|
|
101
|
+
if (reportReplacementSource(node.arguments[0])) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
98
105
|
const parent = node.parent;
|
|
99
106
|
const importPath = node.arguments[0].value;
|
|
100
107
|
|
|
@@ -123,6 +130,26 @@ module.exports = {
|
|
|
123
130
|
},
|
|
124
131
|
};
|
|
125
132
|
|
|
133
|
+
function reportReplacementSource(source) {
|
|
134
|
+
const reactNativeSource = source.value.slice('react-native/'.length);
|
|
135
|
+
const mapping = publicAPIMapping[reactNativeSource];
|
|
136
|
+
if (!mapping || !mapping.replacementSource) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
context.report({
|
|
140
|
+
node: source,
|
|
141
|
+
messageId: 'useReplacementSource',
|
|
142
|
+
data: {
|
|
143
|
+
importPath: source.value,
|
|
144
|
+
replacementSource: mapping.replacementSource,
|
|
145
|
+
},
|
|
146
|
+
fix(fixer) {
|
|
147
|
+
return fixer.replaceText(source, `'${mapping.replacementSource}'`);
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
126
153
|
function getStandardReport(source) {
|
|
127
154
|
return {
|
|
128
155
|
node: source,
|
|
@@ -167,20 +194,15 @@ module.exports = {
|
|
|
167
194
|
return parts.length > 1 && parts[0] === 'react-native';
|
|
168
195
|
}
|
|
169
196
|
|
|
170
|
-
function isInitializeCoreImport(source) {
|
|
171
|
-
if (source.type !== 'Literal' || typeof source.value !== 'string') {
|
|
172
|
-
return false;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return source.value === 'react-native/Libraries/Core/InitializeCore';
|
|
176
|
-
}
|
|
177
|
-
|
|
178
197
|
function isSecondaryEntryPoint(source) {
|
|
179
198
|
if (source.type !== 'Literal' || typeof source.value !== 'string') {
|
|
180
199
|
return false;
|
|
181
200
|
}
|
|
182
201
|
|
|
183
|
-
return
|
|
202
|
+
return (
|
|
203
|
+
source.value === 'react-native/asset-registry' ||
|
|
204
|
+
source.value === 'react-native/setup-env'
|
|
205
|
+
);
|
|
184
206
|
}
|
|
185
207
|
|
|
186
208
|
function isFbInternalImport(source) {
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -37,6 +37,13 @@ const publicAPIMapping = {
|
|
|
37
37
|
default: 'experimental_LayoutConformance',
|
|
38
38
|
types: ['LayoutConformanceProps'],
|
|
39
39
|
},
|
|
40
|
+
'Libraries/Core/InitializeCore': {
|
|
41
|
+
// `InitializeCore` has no public named export; the deep import must be
|
|
42
|
+
// swapped for the `react-native/setup-env` entry point entirely.
|
|
43
|
+
default: null,
|
|
44
|
+
types: null,
|
|
45
|
+
replacementSource: 'react-native/setup-env',
|
|
46
|
+
},
|
|
40
47
|
'Libraries/Lists/FlatList': {
|
|
41
48
|
default: 'FlatList',
|
|
42
49
|
types: ['FlatListProps'],
|