From c43556bcc2dbc1fa6c807d118d04f3552cf14550 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 2 Dec 2020 07:28:34 +0100 Subject: [PATCH 1/2] Always respect synthetic namespaces in namespace reexports --- src/Module.ts | 30 +++++++++---------- .../reexport-from-synthetic/_config.js | 14 +++++++++ .../samples/reexport-from-synthetic/main.js | 2 ++ .../reexport-from-synthetic/reexport.js | 1 + .../reexport-from-synthetic/synthetic.js | 1 + 5 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 test/function/samples/reexport-from-synthetic/_config.js create mode 100644 test/function/samples/reexport-from-synthetic/main.js create mode 100644 test/function/samples/reexport-from-synthetic/reexport.js create mode 100644 test/function/samples/reexport-from-synthetic/synthetic.js diff --git a/src/Module.ts b/src/Module.ts index 8510b24920d..ea2c09dd3df 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -539,24 +539,24 @@ export default class Module { } } - // we don't want to create shims when we are just - // probing export * modules for exports - if (!isExportAllSearch) { - if (this.info.syntheticNamedExports) { - let syntheticExport = this.syntheticExports.get(name); - if (!syntheticExport) { - const syntheticNamespace = this.getSyntheticNamespace(); - syntheticExport = new SyntheticNamedExportVariable( - this.astContext, - name, - syntheticNamespace - ); - this.syntheticExports.set(name, syntheticExport); - return syntheticExport; - } + if (this.info.syntheticNamedExports) { + let syntheticExport = this.syntheticExports.get(name); + if (!syntheticExport) { + const syntheticNamespace = this.getSyntheticNamespace(); + syntheticExport = new SyntheticNamedExportVariable( + this.astContext, + name, + syntheticNamespace + ); + this.syntheticExports.set(name, syntheticExport); return syntheticExport; } + return syntheticExport; + } + // we don't want to create shims when we are just + // probing export * modules for exports + if (!isExportAllSearch) { if (this.options.shimMissingExports) { this.shimMissingExport(name); return this.exportShimVariable; diff --git a/test/function/samples/reexport-from-synthetic/_config.js b/test/function/samples/reexport-from-synthetic/_config.js new file mode 100644 index 00000000000..18cfd06f36a --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/_config.js @@ -0,0 +1,14 @@ +module.exports = { + description: 'handles reexporting a synthetic namespace from a non-synthetic module', + options: { + plugins: [ + { + transform(code, id) { + if (id.endsWith('synthetic.js')) { + return { syntheticNamedExports: '__synth' }; + } + } + } + ] + } +}; diff --git a/test/function/samples/reexport-from-synthetic/main.js b/test/function/samples/reexport-from-synthetic/main.js new file mode 100644 index 00000000000..203a76d25f4 --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/main.js @@ -0,0 +1,2 @@ +import { q } from './reexport.js'; +assert.equal(q, 42); diff --git a/test/function/samples/reexport-from-synthetic/reexport.js b/test/function/samples/reexport-from-synthetic/reexport.js new file mode 100644 index 00000000000..18f74a64559 --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/reexport.js @@ -0,0 +1 @@ +export * from './synthetic.js'; diff --git a/test/function/samples/reexport-from-synthetic/synthetic.js b/test/function/samples/reexport-from-synthetic/synthetic.js new file mode 100644 index 00000000000..e5407b28f96 --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/synthetic.js @@ -0,0 +1 @@ +export const __synth = { q: 42 }; From 8aef56e86b8cd52b730ff2bacdee23d419421c08 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Tue, 19 Jan 2021 06:05:18 +0100 Subject: [PATCH 2/2] Favour explicit named exports over synthetic ones for multiple namespace exports --- src/Module.ts | 15 +++++++++++++-- .../samples/reexport-from-synthetic/_config.js | 6 ++---- .../samples/reexport-from-synthetic/main.js | 6 ++++-- .../samples/reexport-from-synthetic/reexport.js | 3 ++- .../samples/reexport-from-synthetic/synthetic.js | 1 - .../samples/reexport-from-synthetic/synthetic1.js | 2 ++ .../samples/reexport-from-synthetic/synthetic2.js | 2 ++ 7 files changed, 25 insertions(+), 10 deletions(-) delete mode 100644 test/function/samples/reexport-from-synthetic/synthetic.js create mode 100644 test/function/samples/reexport-from-synthetic/synthetic1.js create mode 100644 test/function/samples/reexport-from-synthetic/synthetic2.js diff --git a/src/Module.ts b/src/Module.ts index ea2c09dd3df..0509a209ae0 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -527,6 +527,7 @@ export default class Module { } if (name !== 'default') { + let foundSyntheticDeclaration: SyntheticNamedExportVariable | null = null; for (const module of this.exportAllModules) { const declaration = getVariableForExportNameRecursive( module, @@ -535,7 +536,17 @@ export default class Module { searchedNamesAndModules ); - if (declaration) return declaration; + if (declaration) { + if (!(declaration instanceof SyntheticNamedExportVariable)) { + return declaration; + } + if (!foundSyntheticDeclaration) { + foundSyntheticDeclaration = declaration; + } + } + } + if (foundSyntheticDeclaration) { + return foundSyntheticDeclaration; } } @@ -623,7 +634,7 @@ export default class Module { this.addModulesToImportDescriptions(this.importDescriptions); this.addModulesToImportDescriptions(this.reexportDescriptions); for (const name in this.exports) { - if (name !== 'default') { + if (name !== 'default' && name !== this.info.syntheticNamedExports) { this.exportsAll[name] = this.id; } } diff --git a/test/function/samples/reexport-from-synthetic/_config.js b/test/function/samples/reexport-from-synthetic/_config.js index 18cfd06f36a..7a0aa62df7a 100644 --- a/test/function/samples/reexport-from-synthetic/_config.js +++ b/test/function/samples/reexport-from-synthetic/_config.js @@ -3,10 +3,8 @@ module.exports = { options: { plugins: [ { - transform(code, id) { - if (id.endsWith('synthetic.js')) { - return { syntheticNamedExports: '__synth' }; - } + transform() { + return { syntheticNamedExports: '__synth' }; } } ] diff --git a/test/function/samples/reexport-from-synthetic/main.js b/test/function/samples/reexport-from-synthetic/main.js index 203a76d25f4..be276abded9 100644 --- a/test/function/samples/reexport-from-synthetic/main.js +++ b/test/function/samples/reexport-from-synthetic/main.js @@ -1,2 +1,4 @@ -import { q } from './reexport.js'; -assert.equal(q, 42); +import { synth, explicit1, explicit2 } from './reexport.js'; +assert.strictEqual(synth, 1); +assert.strictEqual(explicit1, 2); +assert.strictEqual(explicit2, 4); diff --git a/test/function/samples/reexport-from-synthetic/reexport.js b/test/function/samples/reexport-from-synthetic/reexport.js index 18f74a64559..abb3502ee80 100644 --- a/test/function/samples/reexport-from-synthetic/reexport.js +++ b/test/function/samples/reexport-from-synthetic/reexport.js @@ -1 +1,2 @@ -export * from './synthetic.js'; +export * from './synthetic1.js'; +export * from './synthetic2.js'; diff --git a/test/function/samples/reexport-from-synthetic/synthetic.js b/test/function/samples/reexport-from-synthetic/synthetic.js deleted file mode 100644 index e5407b28f96..00000000000 --- a/test/function/samples/reexport-from-synthetic/synthetic.js +++ /dev/null @@ -1 +0,0 @@ -export const __synth = { q: 42 }; diff --git a/test/function/samples/reexport-from-synthetic/synthetic1.js b/test/function/samples/reexport-from-synthetic/synthetic1.js new file mode 100644 index 00000000000..614026ef6a0 --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/synthetic1.js @@ -0,0 +1,2 @@ +export const __synth = { synth: 1 }; +export const explicit1 = 2; diff --git a/test/function/samples/reexport-from-synthetic/synthetic2.js b/test/function/samples/reexport-from-synthetic/synthetic2.js new file mode 100644 index 00000000000..31e5c1e86af --- /dev/null +++ b/test/function/samples/reexport-from-synthetic/synthetic2.js @@ -0,0 +1,2 @@ +export const __synth = { synth: 3 }; +export const explicit2 = 4;