Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ test('deep cjs import', () => {
`);
});

test('does not warn for a shadowed require function', () => {
const code = `
function load(require) {
return require('react-native/Libraries/Components/View/View');
}
`;

expect(transform(code, [rnDeepImportsWarningPlugin])).not.toContain(
'console.warn',
);
});

test('warns for a dynamic import when require is shadowed', () => {
const code = `
function load(require) {
return import('react-native/Libraries/Utilities/Platform');
}
`;

expect(transform(code, [rnDeepImportsWarningPlugin])).toContain(
"Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Utilities/Platform').",
);
});

test('multiple deep imports', () => {
const code = `
import View from 'react-native/Libraries/Components/View/View';
Expand Down Expand Up @@ -73,6 +97,16 @@ test('deep reexport', () => {
`);
});

test('deep export all', () => {
const code = `
export * from 'react-native/Libraries/Utilities/Platform';
`;

expect(transform(code, [rnDeepImportsWarningPlugin])).toContain(
"console.warn(\"Deep imports from the 'react-native' package are deprecated ('react-native/Libraries/Utilities/Platform'). Source: path/to/project/foo.js 2:4\");",
);
});

test('import from other package', () => {
const code = `
import {foo} from 'react-native-foo';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ function withLocation(node, loc) {
return node;
}

function recordDeepExport(path, state) {
const source = path.node.source;

if (source && isDeepReactNativeImport(source.value)) {
const loc = path.node.loc;
state.export.push({source: source.value, loc});
}
}

module.exports = ({types: t}) => ({
name: 'warn-on-deep-imports',
visitor: {
Expand All @@ -61,7 +70,9 @@ module.exports = ({types: t}) => ({
const args = path.get('arguments');

if (
callee.isIdentifier({name: 'require'}) &&
((callee.isIdentifier({name: 'require'}) &&
path.scope.getBinding('require') == null) ||
callee.node.type === 'Import') &&
args.length === 1 &&
args[0].isStringLiteral()
) {
Expand All @@ -73,14 +84,8 @@ module.exports = ({types: t}) => ({
}
}
},
ExportNamedDeclaration(path, state) {
const source = path.node.source;

if (source && isDeepReactNativeImport(source.value)) {
const loc = path.node.loc;
state.export.push({source: source.value, loc});
}
},
ExportNamedDeclaration: recordDeepExport,
ExportAllDeclaration: recordDeepExport,
Program: {
enter(path, state) {
state.require = [];
Expand Down
Loading