From b5ddcea06d73abd8e51832feccd4ae449741c0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Segersv=C3=A4rd?= Date: Wed, 30 Dec 2015 21:16:05 +0100 Subject: [PATCH] Ensure names are properly exported --- src/Module.js | 25 ++++++++++++++++--------- src/ast/Scope.js | 9 ++++----- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/Module.js b/src/Module.js index e1965ab..5ff129c 100644 --- a/src/Module.js +++ b/src/Module.js @@ -10,6 +10,7 @@ import SOURCEMAPPING_URL from './utils/sourceMappingURL.js'; import { SyntheticDefaultDeclaration, SyntheticNamespaceDeclaration } from './Declaration.js'; import { isFalsy, isTruthy } from './ast/conditions.js'; import { emptyBlockStatement } from './ast/create.js'; +import { extractNames } from './ast/Scope.js'; export default class Module { constructor ({ id, code, originalCode, ast, sourceMapChain, bundle }) { @@ -97,6 +98,7 @@ export default class Module { } // export { foo, bar, baz } + // export var { foo, bar } = ... // export var foo = 42; // export var a = 1, b = 2, c = 3; // export function foo () {} @@ -114,17 +116,18 @@ export default class Module { else { let declaration = node.declaration; - let name; - if ( declaration.type === 'VariableDeclaration' ) { - // export var foo = 42 - name = declaration.declarations[0].id.name; - } else { + declaration.declarations.forEach( decl => { + extractNames( decl.id ).forEach( localName => { + this.exports[ localName ] = { localName }; + }); + }); + } + else { // export function foo () {} - name = declaration.id.name; + const localName = declaration.id.name; + this.exports[ localName ] = { localName }; } - - this.exports[ name ] = { localName: name }; } } } @@ -497,10 +500,14 @@ export default class Module { // modify exports as necessary if ( statement.isExportDeclaration ) { // remove `export` from `export var foo = 42` + // TODO: can we do something simpler here? + // we just want to remove `export`, right? if ( statement.node.type === 'ExportNamedDeclaration' && statement.node.declaration.type === 'VariableDeclaration' ) { - const name = statement.node.declaration.declarations[0].id.name; + const name = extractNames( statement.node.declaration.declarations[ 0 ].id )[ 0 ]; const declaration = this.declarations[ name ]; + if ( !declaration ) throw new Error( `Missing declaration for ${name}!` ); + const end = declaration.isExported && declaration.isReassigned ? statement.node.declaration.declarations[0].start : statement.node.declaration.start; diff --git a/src/ast/Scope.js b/src/ast/Scope.js index 508bf31..75a6c53 100644 --- a/src/ast/Scope.js +++ b/src/ast/Scope.js @@ -8,7 +8,7 @@ const extractors = { ObjectPattern ( names, param ) { param.properties.forEach( prop => { - extractors[ prop.key.type ]( names, prop.key ); + extractors[ prop.value.type ]( names, prop.value ); }); }, @@ -23,13 +23,12 @@ const extractors = { }, AssignmentPattern ( names, param ) { - return extractors[ param.left.type ]( names, param.left ); + extractors[ param.left.type ]( names, param.left ); } }; -function extractNames ( param ) { - let names = []; - +export function extractNames ( param ) { + const names = []; extractors[ param.type ]( names, param ); return names; }