Browse Source

remove node properties from statement

contingency-plan
Rich Harris 10 years ago
parent
commit
e9b69cb21e
  1. 18
      src/Bundle.js
  2. 21
      src/Module.js
  3. 3
      src/Statement.js
  4. 16
      src/ast/analyse.js

18
src/Bundle.js

@ -164,32 +164,32 @@ export default class Bundle {
const source = statement._source.clone().trim();
// modify exports as necessary
if ( /^Export/.test( statement.type ) ) {
if ( /^Export/.test( statement.node.type ) ) {
// skip `export { foo, bar, baz }`
if ( statement.type === 'ExportNamedDeclaration' && statement.specifiers.length ) {
if ( statement.node.type === 'ExportNamedDeclaration' && statement.node.specifiers.length ) {
return;
}
// remove `export` from `export var foo = 42`
if ( statement.type === 'ExportNamedDeclaration' && statement.declaration.type === 'VariableDeclaration' ) {
source.remove( statement.start, statement.declaration.start );
if ( statement.node.type === 'ExportNamedDeclaration' && statement.node.declaration.type === 'VariableDeclaration' ) {
source.remove( statement.node.start, statement.node.declaration.start );
}
// remove `export` from `export class Foo {...}` or `export default Foo`
// TODO default exports need different treatment
else if ( statement.declaration.id ) {
source.remove( statement.start, statement.declaration.start );
else if ( statement.node.declaration.id ) {
source.remove( statement.node.start, statement.node.declaration.start );
}
else if ( statement.type === 'ExportDefaultDeclaration' ) {
else if ( statement.node.type === 'ExportDefaultDeclaration' ) {
const module = statement._module;
const canonicalName = module.getCanonicalName( 'default' );
if ( statement.declaration.type === 'Identifier' && canonicalName === module.getCanonicalName( statement.declaration.name ) ) {
if ( statement.node.declaration.type === 'Identifier' && canonicalName === module.getCanonicalName( statement.node.declaration.name ) ) {
return;
}
source.overwrite( statement.start, statement.declaration.start, `var ${canonicalName} = ` );
source.overwrite( statement.node.start, statement.node.declaration.start, `var ${canonicalName} = ` );
}
else {

21
src/Module.js

@ -47,7 +47,8 @@ export default class Module {
this.imports = {};
this.exports = {};
this.statements.forEach( node => {
this.statements.forEach( statement => {
const node = statement.node;
let source;
// import foo from './foo';
@ -85,7 +86,8 @@ export default class Module {
const isDeclaration = /Declaration$/.test( node.declaration.type );
this.exports.default = {
node,
node, // TODO remove this
statement,
name: 'default',
localName: isDeclaration ? node.declaration.id.name : 'default',
isDeclaration
@ -134,7 +136,8 @@ export default class Module {
}
this.exports[ name ] = {
node,
node, // TODO remove
statement,
localName: name,
expression: declaration
};
@ -279,7 +282,7 @@ export default class Module {
if ( name === 'default' ) {
// TODO can we use this.definitions[ name ], as below?
statement = this.exports.default.node;
statement = this.exports.default.statement;
}
else {
@ -349,13 +352,13 @@ export default class Module {
if ( statement._included ) return;
// skip import declarations
if ( statement.type === 'ImportDeclaration' ) {
if ( statement.node.type === 'ImportDeclaration' ) {
// unless they're empty, in which case assume we're importing them for the side-effects
// THIS IS NOT FOOLPROOF. Probably need /*rollup: include */ or similar
if ( !statement.specifiers.length ) {
return this.bundle.fetchModule( statement.source.value, this.path )
if ( !statement.node.specifiers.length ) {
return this.bundle.fetchModule( statement.node.source.value, this.path )
.then( module => {
statement.module = module;
statement.module = module; // TODO what is this for? what does it do? why not _module?
return module.expandAllStatements();
})
.then( statements => {
@ -367,7 +370,7 @@ export default class Module {
}
// skip `export { foo, bar, baz }`
if ( statement.type === 'ExportNamedDeclaration' && statement.specifiers.length ) {
if ( statement.node.type === 'ExportNamedDeclaration' && statement.node.specifiers.length ) {
// but ensure they are defined, if this is the entry module
if ( isEntryModule ) {
return this.expandStatement( statement )

3
src/Statement.js

@ -2,9 +2,6 @@ import { keys } from './utils/object';
export default class Statement {
constructor ( node ) {
// TODO remove this in favour of this.node
keys( node ).forEach( key => this[ key ] = node[ key ] );
this.node = node;
}
}

16
src/ast/analyse.js

@ -33,13 +33,15 @@ export default function analyse ( ast, magicString, module ) {
module.statements.forEach( statement => {
currentTopLevelStatement = statement; // so we can attach scoping info
const node = statement.node;
Object.defineProperties( statement, {
_defines: { value: {} },
_modifies: { value: {} },
_dependsOn: { value: {} },
_included: { value: false, writable: true },
_module: { value: module },
_source: { value: magicString.snip( statement.start, statement.end ) }, // TODO don't use snip, it's a waste of memory
_source: { value: magicString.snip( node.start, node.end ) }, // TODO don't use snip, it's a waste of memory
_margin: { value: [ 0, 0 ] },
_leadingComments: { value: [] },
_trailingComment: { value: null, writable: true },
@ -55,16 +57,16 @@ export default function analyse ( ast, magicString, module ) {
// prevent comments inside the previous statement being
// appended to it
if ( previousStatement ) {
while ( comment && comment.start < previousStatement.end ) {
while ( comment && comment.start < previousStatement.node.end ) {
commentIndex += 1;
comment = module.comments[ commentIndex ];
}
}
if ( !comment || ( comment.end > statement.start ) ) break;
if ( !comment || ( comment.end > node.start ) ) break;
// attach any trailing comment to the previous statement
if ( trailing && !/\n/.test( magicString.slice( previousStatement.end, comment.start ) ) ) {
if ( trailing && !/\n/.test( magicString.slice( previousStatement.node.end, comment.start ) ) ) {
previousStatement._trailingComment = comment;
}
@ -78,8 +80,8 @@ export default function analyse ( ast, magicString, module ) {
} while ( module.comments[ commentIndex ] );
// determine margin
const previousEnd = previousStatement ? ( previousStatement._trailingComment || previousStatement ).end : 0;
const start = ( statement._leadingComments[0] || statement ).start;
const previousEnd = previousStatement ? ( previousStatement._trailingComment || previousStatement.node ).end : 0;
const start = ( statement._leadingComments[0] || node ).start;
const gap = magicString.original.slice( previousEnd, start );
const margin = gap.split( '\n' ).length;
@ -218,7 +220,7 @@ export default function analyse ( ast, magicString, module ) {
// TODO UpdateExpressions, method calls?
}
walk( statement, {
walk( statement.node, {
enter ( node, parent ) {
// skip imports
if ( /^Import/.test( node.type ) ) return this.skip();

Loading…
Cancel
Save