From 3d53c1b835cbe79f290d9bd2aced754df6e1aa86 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 18 Jan 2022 17:44:40 -0500 Subject: [PATCH 1/2] fix importSync scope collision This is a fix for #1078. I plan to add a test too. --- packages/macros/src/babel/macros-babel-plugin.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/macros/src/babel/macros-babel-plugin.ts b/packages/macros/src/babel/macros-babel-plugin.ts index 482aa280c..342f4e5b6 100644 --- a/packages/macros/src/babel/macros-babel-plugin.ts +++ b/packages/macros/src/babel/macros-babel-plugin.ts @@ -130,6 +130,9 @@ export default function main(context: typeof Babel): unknown { path.replaceWith(state.importUtil.import(path, specifier.value, '*')); state.calledIdentifiers.add(callee.node); } else { + if (path.scope.hasBinding('require')) { + path.scope.rename('require'); + } let r = t.identifier('require'); state.generatedRequires.add(r); path.replaceWith( From 89b04574db806fa1933c61329bf35c039f967cfc Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 18 Jan 2022 19:23:17 -0500 Subject: [PATCH 2/2] adding test coverage --- packages/macros/tests/babel/import-sync.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/macros/tests/babel/import-sync.test.ts b/packages/macros/tests/babel/import-sync.test.ts index c8bcc1bc8..6e15aafd8 100644 --- a/packages/macros/tests/babel/import-sync.test.ts +++ b/packages/macros/tests/babel/import-sync.test.ts @@ -15,6 +15,17 @@ describe('importSync', function () { expect(code).toMatch(/esc\(require\(['"]foo['"]\)\)/); expect(code).not.toMatch(/window/); }); + test('importSync leaves existing binding for require alone', () => { + let code = transform(` + import { importSync } from '@embroider/macros'; + import require from 'require'; + importSync('foo'); + require('x'); + `); + expect(code).toMatch(/esc\(require\(['"]foo['"]\)\)/); + expect(code).toMatch(/import _require from 'require'/); + expect(code).toMatch(/_require\(['"]x['"]\)/); + }); test('aliased importSync becomes require', () => { let code = transform(` import { importSync as i } from '@embroider/macros';