Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import-export-plugin: Rename potentially conflicting declarations at module scope (module, exports, require, global) #1385

Closed
wants to merge 1 commit into from
Closed
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 @@ -186,6 +186,61 @@ test('exports named members', () => {
compare([importExportPlugin], code, expected, opts);
});

test('renames existing `exports` declarations in module scope', () => {
const code = `
const exports = 'foo';
export const bar = 'bar';
console.log(exports, bar);
`;

const expected = `
Object.defineProperty(exports, '__esModule', {value: true});
const _exports = 'foo';
const bar = 'bar';
console.log(_exports, bar);
exports.bar = bar;
`;

compare([importExportPlugin], code, expected, opts);
});

test('handles an export named "exports"', () => {
const code = `
export const exports = {a: 'foo'};
`;

const expected = `
Object.defineProperty(exports, '__esModule', {value: true});
const _exports = {
a: 'foo',
};
exports.exports = _exports;
`;

compare([importExportPlugin], code, expected, opts);
});

test('allows mixed esm and cjs exports', () => {
const code = `
export const foo = 'foo';
exports.bar = 'bar';
module.exports.baz = 'baz';
export default class {}
`;

const expected = `
Object.defineProperty(exports, '__esModule', {value: true});
const foo = 'foo';
exports.bar = 'bar';
module.exports.baz = 'baz';
class _default {}
exports.default = _default;
exports.foo = foo;
`;

compare([importExportPlugin], code, expected, opts);
});

test('exports destructured named object members', () => {
const code = `
export const {foo,bar} = {foo: 'bar',bar: 'baz'};
Expand Down Expand Up @@ -266,6 +321,24 @@ test('enables module exporting when something is exported', () => {
`);
});

test('renames bindings', () => {
const code = `
const module = 'foo';
let exports = 'bar';
var global = 'baz';
const require = {};
`;

const expected = `
const _module = 'foo';
let _exports = 'bar';
var _global = 'baz';
const _require = {};
`;

compare([importExportPlugin], code, expected, opts);
});

test('supports `import {default as LocalName}`', () => {
const code = `
import {
Expand Down
8 changes: 8 additions & 0 deletions packages/metro-transform-plugins/src/import-export-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
// eslint-disable-next-line no-redeclare
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's
* LTI update could not be added via codemod */
function withLocation(node, loc) {

Check warning on line 143 in packages/metro-transform-plugins/src/import-export-plugin.js

View workflow job for this annotation

GitHub Actions / Type check, lint, smoke test

'withLocation' is already defined
if (Array.isArray(node)) {
return node.map(n => withLocation(n, loc));
}
Expand Down Expand Up @@ -495,6 +495,14 @@
state.imports = [];
state.importAll = t.identifier(state.opts.importAll);
state.importDefault = t.identifier(state.opts.importDefault);

// Rename declarations at module scope that might otherwise conflict
// with arguments we inject into the module factory.
// Note that it isn't necessary to rename importAll/importDefault
// because Metro already uses generateUid to generate unused names.
['module', 'global', 'exports', 'require'].forEach(name =>
path.scope.rename(name),
);
},

exit(path: NodePath<Program>, state: State): void {
Expand Down
Loading