Skip to content

Commit d7eb944

Browse files
committed
Complete deep import warning coverage
1 parent 2fa055f commit d7eb944

2 files changed

Lines changed: 48 additions & 9 deletions

File tree

packages/react-native-babel-preset/src/__tests__/plugin-warn-on-deep-imports-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ test('deep cjs import', () => {
4646
`);
4747
});
4848

49+
test('does not warn for a shadowed require function', () => {
50+
const code = `
51+
function load(require) {
52+
return require('react-native/Libraries/Components/View/View');
53+
}
54+
`;
55+
56+
expect(transform(code, [rnDeepImportsWarningPlugin])).not.toContain(
57+
'console.warn',
58+
);
59+
});
60+
61+
test('warns for a dynamic import when require is shadowed', () => {
62+
const code = `
63+
function load(require) {
64+
return import('react-native/Libraries/Utilities/Platform');
65+
}
66+
`;
67+
68+
expect(transform(code, [rnDeepImportsWarningPlugin])).toContain(
69+
"Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Utilities/Platform').",
70+
);
71+
});
72+
4973
test('multiple deep imports', () => {
5074
const code = `
5175
import View from 'react-native/Libraries/Components/View/View';
@@ -73,6 +97,16 @@ test('deep reexport', () => {
7397
`);
7498
});
7599

100+
test('deep export all', () => {
101+
const code = `
102+
export * from 'react-native/Libraries/Utilities/Platform';
103+
`;
104+
105+
expect(transform(code, [rnDeepImportsWarningPlugin])).toContain(
106+
"console.warn(\"Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Utilities/Platform'). Source: path/to/project/foo.js 2:4\");",
107+
);
108+
});
109+
76110
test('import from other package', () => {
77111
const code = `
78112
import {foo} from 'react-native-foo';

packages/react-native-babel-preset/src/plugin-warn-on-deep-imports.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ function withLocation(node, loc) {
4545
return node;
4646
}
4747

48+
function recordDeepExport(path, state) {
49+
const source = path.node.source;
50+
51+
if (source && isDeepReactNativeImport(source.value)) {
52+
const loc = path.node.loc;
53+
state.export.push({source: source.value, loc});
54+
}
55+
}
56+
4857
module.exports = ({types: t}) => ({
4958
name: 'warn-on-deep-imports',
5059
visitor: {
@@ -61,7 +70,9 @@ module.exports = ({types: t}) => ({
6170
const args = path.get('arguments');
6271

6372
if (
64-
callee.isIdentifier({name: 'require'}) &&
73+
((callee.isIdentifier({name: 'require'}) &&
74+
path.scope.getBinding('require') == null) ||
75+
callee.node.type === 'Import') &&
6576
args.length === 1 &&
6677
args[0].isStringLiteral()
6778
) {
@@ -73,14 +84,8 @@ module.exports = ({types: t}) => ({
7384
}
7485
}
7586
},
76-
ExportNamedDeclaration(path, state) {
77-
const source = path.node.source;
78-
79-
if (source && isDeepReactNativeImport(source.value)) {
80-
const loc = path.node.loc;
81-
state.export.push({source: source.value, loc});
82-
}
83-
},
87+
ExportNamedDeclaration: recordDeepExport,
88+
ExportAllDeclaration: recordDeepExport,
8489
Program: {
8590
enter(path, state) {
8691
state.require = [];

0 commit comments

Comments
 (0)