Browse Source

attach statements to all declarations

better-aggressive
Rich-Harris 9 years ago
parent
commit
c33b3d2437
  1. 4
      src/Declaration.js
  2. 8
      src/Statement.js
  3. 5
      src/ast/Scope.js

4
src/Declaration.js

@ -2,7 +2,7 @@ import { blank, keys } from './utils/object.js';
import run from './utils/run.js';
export default class Declaration {
constructor ( node, isParam ) {
constructor ( node, isParam, statement ) {
if ( node ) {
if ( node.type === 'FunctionDeclaration' ) {
this.isFunctionDeclaration = true;
@ -13,7 +13,7 @@ export default class Declaration {
}
}
this.statement = null;
this.statement = statement;
this.name = null;
this.isParam = isParam;

8
src/Statement.js

@ -39,7 +39,7 @@ export default class Statement {
this.end = end;
this.next = null; // filled in later
this.scope = new Scope();
this.scope = new Scope({ statement: this });
this.references = [];
this.stringLiteralRanges = [];
@ -61,12 +61,6 @@ export default class Statement {
// attach scopes
attachScopes( this );
// attach statement to each top-level declaration,
// so we can mark statements easily
this.scope.eachDeclaration( ( name, declaration ) => {
declaration.statement = this;
});
// find references
const statement = this;
let { module, references, scope, stringLiteralRanges } = this;

5
src/ast/Scope.js

@ -39,6 +39,7 @@ export default class Scope {
options = options || {};
this.parent = options.parent;
this.statement = options.statement || this.parent.statement;
this.isBlockScope = !!options.block;
this.isTopLevel = !this.parent || ( this.parent.isTopLevel && this.isBlockScope );
@ -47,7 +48,7 @@ export default class Scope {
if ( options.params ) {
options.params.forEach( param => {
extractNames( param ).forEach( name => {
this.declarations[ name ] = new Declaration( param, true );
this.declarations[ name ] = new Declaration( param, true, this.statement );
});
});
}
@ -60,7 +61,7 @@ export default class Scope {
this.parent.addDeclaration( node, isBlockDeclaration, isVar );
} else {
extractNames( node.id ).forEach( name => {
this.declarations[ name ] = new Declaration( node );
this.declarations[ name ] = new Declaration( node, false, this.statement );
});
}
}

Loading…
Cancel
Save