UNPKG

786 BJavaScriptView Raw
1export default class Scope {
2 constructor ( options ) {
3 options = options || {};
4
5 this.parent = options.parent;
6 this.depth = this.parent ? this.parent.depth + 1 : 0;
7 this.names = options.params || [];
8 this.isBlockScope = !!options.block;
9 }
10
11 add ( name, isBlockDeclaration ) {
12 if ( !isBlockDeclaration && this.isBlockScope ) {
13 // it's a `var` or function declaration, and this
14 // is a block scope, so we need to go up
15 this.parent.add( name, isBlockDeclaration );
16 } else {
17 this.names.push( name );
18 }
19 }
20
21 contains ( name ) {
22 return !!this.findDefiningScope( name );
23 }
24
25 findDefiningScope ( name ) {
26 if ( ~this.names.indexOf( name ) ) {
27 return this;
28 }
29
30 if ( this.parent ) {
31 return this.parent.findDefiningScope( name );
32 }
33
34 return null;
35 }
36}
\No newline at end of file