Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit faad466

Browse files
committed
JS: Add ScopeKind enum
1 parent 07cfcee commit faad466

3 files changed

Lines changed: 72 additions & 36 deletions

File tree

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ public Label visit(Program nd, Context c) {
704704
+ locationManager.getStartLine()
705705
+ ","
706706
+ locationManager.getStartColumn());
707-
scopeManager.enterScope(3, moduleScopeKey, toplevelLabel);
707+
scopeManager.enterScope(ScopeKind.module, moduleScopeKey, toplevelLabel);
708708
scopeManager.addVariables(
709709
sourceType.getPredefinedLocals(platform, locationManager.getSourceFileExtension()));
710710
trapwriter.addTuple("is_module", toplevelLabel);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.semmle.js.extractor;
2+
3+
/**
4+
* A kind of scope, corresponding to the <code>@scope</code> type in the dbscheme.
5+
*/
6+
public enum ScopeKind {
7+
global(0),
8+
function(1),
9+
catch_(2),
10+
module(3),
11+
block(4),
12+
for_(5),
13+
forIn(6),
14+
comprehensionBlock(7),
15+
classExpr(8),
16+
namespace(9),
17+
classDecl(10),
18+
interface_(11),
19+
typeAlias(12),
20+
mappedType(13),
21+
enum_(14),
22+
externalModule(15),
23+
conditionalType(16);
24+
25+
private int value;
26+
27+
private ScopeKind(int value) {
28+
this.value = value;
29+
}
30+
31+
/** Returns the value identifying this scope kind in the database. */
32+
public int getValue() {
33+
return value;
34+
}
35+
}

javascript/extractor/src/com/semmle/js/extractor/ScopeManager.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package 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+
312
import com.semmle.js.ast.ArrayPattern;
413
import com.semmle.js.ast.BlockStatement;
514
import com.semmle.js.ast.CatchClause;
@@ -54,14 +63,6 @@
5463
import com.semmle.ts.ast.UnionTypeExpr;
5564
import com.semmle.util.trap.TrapWriter;
5665
import 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. */
6768
public 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

Comments
 (0)