From a7161689e467452621b9b2fa453d87646a94da06 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 11 Sep 2016 11:05:27 -0700 Subject: [PATCH] Correctly deshadow re-assigned module functions Fixes #910. --- src/ast/scopes/ModuleScope.js | 13 +++++++------ .../namespacing-in-sub-functions/_config.js | 8 ++++++++ test/function/namespacing-in-sub-functions/main.js | 11 +++++++++++ .../namespacing-in-sub-functions/problematicFunc.js | 5 +++++ 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 test/function/namespacing-in-sub-functions/_config.js create mode 100644 test/function/namespacing-in-sub-functions/main.js create mode 100644 test/function/namespacing-in-sub-functions/problematicFunc.js diff --git a/src/ast/scopes/ModuleScope.js b/src/ast/scopes/ModuleScope.js index f451d20..cdc2c93 100644 --- a/src/ast/scopes/ModuleScope.js +++ b/src/ast/scopes/ModuleScope.js @@ -19,14 +19,15 @@ export default class ModuleScope extends Scope { forOwn( this.module.imports, specifier => { if ( specifier.module.isExternal ) return; - if ( specifier.name === '*' ) { - specifier.module.getExports().forEach( name => { - names.set( name, true ); - }); - } else { + specifier.module.getExports().forEach( name => { + names.set(name); + }); + if ( specifier.name !== '*' ) { const declaration = specifier.module.traceExport( specifier.name ); const name = declaration.getName( true ); - if ( name !== specifier.name ) names.set( declaration.getName( true ) ); + if ( name !== specifier.name ) { + names.set(declaration.getName( true )); + } } }); diff --git a/test/function/namespacing-in-sub-functions/_config.js b/test/function/namespacing-in-sub-functions/_config.js new file mode 100644 index 0000000..025b117 --- /dev/null +++ b/test/function/namespacing-in-sub-functions/_config.js @@ -0,0 +1,8 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'correctly namespaces sub-functions (#910)', + exports: function ( exports ) { + assert.equal( exports, 'foobar' ); + } +}; diff --git a/test/function/namespacing-in-sub-functions/main.js b/test/function/namespacing-in-sub-functions/main.js new file mode 100644 index 0000000..ac9d9da --- /dev/null +++ b/test/function/namespacing-in-sub-functions/main.js @@ -0,0 +1,11 @@ +import { problematicFunc as otherFunc } from './problematicFunc'; +function innerFunc() { + function problematicFunc () { + return otherFunc(); + } + return problematicFunc(); +} + +var res = innerFunc(); + +export default res; \ No newline at end of file diff --git a/test/function/namespacing-in-sub-functions/problematicFunc.js b/test/function/namespacing-in-sub-functions/problematicFunc.js new file mode 100644 index 0000000..1fbecc0 --- /dev/null +++ b/test/function/namespacing-in-sub-functions/problematicFunc.js @@ -0,0 +1,5 @@ +function problematicFunc() { + return 'foobar'; +} + +export { problematicFunc }; \ No newline at end of file