From c33b3d2437459499b961163b07b01256d8e62259 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 14 Nov 2015 16:58:10 -0500 Subject: [PATCH] attach statements to all declarations --- src/Declaration.js | 4 ++-- src/Statement.js | 8 +------- src/ast/Scope.js | 5 +++-- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Declaration.js b/src/Declaration.js index bc343ea..f4d2e0d 100644 --- a/src/Declaration.js +++ b/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; diff --git a/src/Statement.js b/src/Statement.js index b4a707e..a9efc49 100644 --- a/src/Statement.js +++ b/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; diff --git a/src/ast/Scope.js b/src/ast/Scope.js index 1adcd47..508bf31 100644 --- a/src/ast/Scope.js +++ b/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 ); }); } }