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

21
src/Module.js

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

3
src/Statement.js

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

16
src/ast/analyse.js

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

Loading…
Cancel
Save