11package com .semmle .js .extractor ;
22
3+ import java .util .Arrays ;
4+ import java .util .Collections ;
5+ import java .util .HashMap ;
6+ import java .util .LinkedHashMap ;
7+ import java .util .LinkedHashSet ;
8+ import java .util .List ;
9+ import java .util .Map ;
10+ import java .util .Set ;
11+
312import com .semmle .js .ast .ArrayPattern ;
413import com .semmle .js .ast .BlockStatement ;
514import com .semmle .js .ast .CatchClause ;
5463import com .semmle .ts .ast .UnionTypeExpr ;
5564import com .semmle .util .trap .TrapWriter ;
5665import com .semmle .util .trap .TrapWriter .Label ;
57- import java .util .Arrays ;
58- import java .util .Collections ;
59- import java .util .HashMap ;
60- import java .util .LinkedHashMap ;
61- import java .util .LinkedHashSet ;
62- import java .util .List ;
63- import java .util .Map ;
64- import java .util .Set ;
6566
6667/** Class for maintaining scoping information during extraction. */
6768public class ScopeManager {
@@ -104,7 +105,7 @@ public Label lookupNamespace(String name) {
104105
105106 public ScopeManager (TrapWriter trapWriter , ECMAVersion ecmaVersion ) {
106107 this .trapWriter = trapWriter ;
107- this .toplevelScope = enterScope (0 , trapWriter .globalID ("global_scope" ), null );
108+ this .toplevelScope = enterScope (ScopeKind . global , trapWriter .globalID ("global_scope" ), null );
108109 this .ecmaVersion = ecmaVersion ;
109110 }
110111
@@ -115,12 +116,12 @@ public ScopeManager(TrapWriter trapWriter, ECMAVersion ecmaVersion) {
115116 * @param scopeLabel the label of the scope itself
116117 * @param scopeNodeLabel the label of the AST node inducing this scope; may be null
117118 */
118- public Scope enterScope (int scopeKind , Label scopeLabel , Label scopeNodeLabel ) {
119+ public Scope enterScope (ScopeKind scopeKind , Label scopeLabel , Label scopeNodeLabel ) {
119120 Label outerScopeLabel = curScope == null ? null : curScope .scopeLabel ;
120121
121122 curScope = new Scope (curScope , scopeLabel );
122123
123- trapWriter .addTuple ("scopes" , curScope .scopeLabel , scopeKind );
124+ trapWriter .addTuple ("scopes" , curScope .scopeLabel , scopeKind . getValue () );
124125 if (scopeNodeLabel != null )
125126 trapWriter .addTuple ("scopenodes" , scopeNodeLabel , curScope .scopeLabel );
126127 if (outerScopeLabel != null )
@@ -162,32 +163,32 @@ public Scope getToplevelScope() {
162163 return toplevelScope ;
163164 }
164165
165- private static final Map <String , Integer > scopeKinds = new LinkedHashMap <String , Integer >();
166+ private static final Map <String , ScopeKind > scopeKinds = new LinkedHashMap <String , ScopeKind >();
166167
167168 static {
168- scopeKinds .put ("Program" , 0 );
169- scopeKinds .put ("FunctionDeclaration" , 1 );
170- scopeKinds .put ("FunctionExpression" , 1 );
171- scopeKinds .put ("ArrowFunctionExpression" , 1 );
172- scopeKinds .put ("CatchClause" , 2 );
173- scopeKinds .put ("Module" , 3 );
174- scopeKinds .put ("BlockStatement" , 4 );
175- scopeKinds .put ("SwitchStatement" , 4 );
176- scopeKinds .put ("ForStatement" , 5 );
177- scopeKinds .put ("ForInStatement" , 6 );
178- scopeKinds .put ("ForOfStatement" , 6 );
179- scopeKinds .put ("ComprehensionBlock" , 7 );
180- scopeKinds .put ("LetStatement" , 4 );
181- scopeKinds .put ("LetExpression" , 4 );
182- scopeKinds .put ("ClassExpression" , 8 );
183- scopeKinds .put ("NamespaceDeclaration" , 9 );
184- scopeKinds .put ("ClassDeclaration" , 10 );
185- scopeKinds .put ("InterfaceDeclaration" , 11 );
186- scopeKinds .put ("TypeAliasDeclaration" , 12 );
187- scopeKinds .put ("MappedTypeExpr" , 13 );
188- scopeKinds .put ("EnumDeclaration" , 14 );
189- scopeKinds .put ("ExternalModuleDeclaration" , 15 );
190- scopeKinds .put ("ConditionalTypeExpr" , 16 );
169+ scopeKinds .put ("Program" , ScopeKind . global );
170+ scopeKinds .put ("FunctionDeclaration" , ScopeKind . function );
171+ scopeKinds .put ("FunctionExpression" , ScopeKind . function );
172+ scopeKinds .put ("ArrowFunctionExpression" , ScopeKind . function );
173+ scopeKinds .put ("CatchClause" , ScopeKind . catch_ );
174+ scopeKinds .put ("Module" , ScopeKind . module );
175+ scopeKinds .put ("BlockStatement" , ScopeKind . block );
176+ scopeKinds .put ("SwitchStatement" , ScopeKind . block );
177+ scopeKinds .put ("ForStatement" , ScopeKind . for_ );
178+ scopeKinds .put ("ForInStatement" , ScopeKind . forIn );
179+ scopeKinds .put ("ForOfStatement" , ScopeKind . forIn );
180+ scopeKinds .put ("ComprehensionBlock" , ScopeKind . comprehensionBlock );
181+ scopeKinds .put ("LetStatement" , ScopeKind . block );
182+ scopeKinds .put ("LetExpression" , ScopeKind . block );
183+ scopeKinds .put ("ClassExpression" , ScopeKind . classExpr );
184+ scopeKinds .put ("NamespaceDeclaration" , ScopeKind . namespace );
185+ scopeKinds .put ("ClassDeclaration" , ScopeKind . classDecl );
186+ scopeKinds .put ("InterfaceDeclaration" , ScopeKind . interface_ );
187+ scopeKinds .put ("TypeAliasDeclaration" , ScopeKind . typeAlias );
188+ scopeKinds .put ("MappedTypeExpr" , ScopeKind . mappedType );
189+ scopeKinds .put ("EnumDeclaration" , ScopeKind . enum_ );
190+ scopeKinds .put ("ExternalModuleDeclaration" , ScopeKind . externalModule );
191+ scopeKinds .put ("ConditionalTypeExpr" , ScopeKind . conditionalType );
191192 }
192193
193194 /**
0 commit comments