Browse Source

Ensure names are properly exported

gh-438-b
Oskar Segersvärd 9 years ago
parent
commit
b5ddcea06d
  1. 25
      src/Module.js
  2. 9
      src/ast/Scope.js

25
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;

9
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;
}

Loading…
Cancel
Save