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 { SyntheticDefaultDeclaration, SyntheticNamespaceDeclaration } from './Declaration.js';
import { isFalsy, isTruthy } from './ast/conditions.js'; import { isFalsy, isTruthy } from './ast/conditions.js';
import { emptyBlockStatement } from './ast/create.js'; import { emptyBlockStatement } from './ast/create.js';
import { extractNames } from './ast/Scope.js';
export default class Module { export default class Module {
constructor ({ id, code, originalCode, ast, sourceMapChain, bundle }) { constructor ({ id, code, originalCode, ast, sourceMapChain, bundle }) {
@ -97,6 +98,7 @@ export default class Module {
} }
// export { foo, bar, baz } // export { foo, bar, baz }
// export var { foo, bar } = ...
// export var foo = 42; // export var foo = 42;
// export var a = 1, b = 2, c = 3; // export var a = 1, b = 2, c = 3;
// export function foo () {} // export function foo () {}
@ -114,17 +116,18 @@ export default class Module {
else { else {
let declaration = node.declaration; let declaration = node.declaration;
let name;
if ( declaration.type === 'VariableDeclaration' ) { if ( declaration.type === 'VariableDeclaration' ) {
// export var foo = 42 declaration.declarations.forEach( decl => {
name = declaration.declarations[0].id.name; extractNames( decl.id ).forEach( localName => {
} else { this.exports[ localName ] = { localName };
});
});
}
else {
// export function foo () {} // 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 // modify exports as necessary
if ( statement.isExportDeclaration ) { if ( statement.isExportDeclaration ) {
// remove `export` from `export var foo = 42` // 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' ) { 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 ]; const declaration = this.declarations[ name ];
if ( !declaration ) throw new Error( `Missing declaration for ${name}!` );
const end = declaration.isExported && declaration.isReassigned ? const end = declaration.isExported && declaration.isReassigned ?
statement.node.declaration.declarations[0].start : statement.node.declaration.declarations[0].start :
statement.node.declaration.start; statement.node.declaration.start;

9
src/ast/Scope.js

@ -8,7 +8,7 @@ const extractors = {
ObjectPattern ( names, param ) { ObjectPattern ( names, param ) {
param.properties.forEach( prop => { 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 ) { AssignmentPattern ( names, param ) {
return extractors[ param.left.type ]( names, param.left ); extractors[ param.left.type ]( names, param.left );
} }
}; };
function extractNames ( param ) { export function extractNames ( param ) {
let names = []; const names = [];
extractors[ param.type ]( names, param ); extractors[ param.type ]( names, param );
return names; return names;
} }

Loading…
Cancel
Save