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

Skip to content

Commit 4b5a861

Browse files
committed
JS: Add TopLevelKind enum
1 parent 9b99f56 commit 4b5a861

7 files changed

Lines changed: 41 additions & 21 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,9 +2137,9 @@ public Label visit(AngularPipeRef nd, Context c) {
21372137
}
21382138

21392139
public List<ParseError> extract(
2140-
Node root, Platform platform, SourceType sourceType, int toplevelKind) {
2140+
Node root, Platform platform, SourceType sourceType, TopLevelKind toplevelKind) {
21412141
lexicalExtractor.getMetrics().startPhase(ExtractionPhase.ASTExtractor_extract);
2142-
trapwriter.addTuple("toplevels", toplevelLabel, toplevelKind);
2142+
trapwriter.addTuple("toplevels", toplevelLabel, toplevelKind.getValue());
21432143
locationManager.emitNodeLocation(root, toplevelLabel);
21442144

21452145
V visitor = new V(platform, sourceType);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public class FileSnippet {
1111
private Path originalFile;
1212
private int line;
1313
private int column;
14-
private int topLevelKind;
14+
private TopLevelKind topLevelKind;
1515
private SourceType sourceType;
1616

17-
public FileSnippet(Path originalFile, int line, int column, int topLevelKind, SourceType sourceType) {
17+
public FileSnippet(Path originalFile, int line, int column, TopLevelKind topLevelKind, SourceType sourceType) {
1818
this.originalFile = originalFile;
1919
this.line = line;
2020
this.column = column;
@@ -34,7 +34,7 @@ public int getColumn() {
3434
return column;
3535
}
3636

37-
public int getTopLevelKind() {
37+
public TopLevelKind getTopLevelKind() {
3838
return topLevelKind;
3939
}
4040

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void handleElement(Element elt) {
7373
RowColumnVector contentStart = content.getRowColumnVector();
7474
snippetLoC =
7575
extractSnippet(
76-
1,
76+
TopLevelKind.inlineScript,
7777
config.withSourceType(sourceType),
7878
scopeManager,
7979
textualExtractor,
@@ -96,7 +96,7 @@ public void handleElement(Element elt) {
9696
if (JS_ATTRIBUTE.matcher(attr.getName()).matches() || isAngularTemplateAttributeName(attr.getName())) {
9797
snippetLoC =
9898
extractSnippet(
99-
2,
99+
TopLevelKind.eventHandler,
100100
config,
101101
scopeManager,
102102
textualExtractor,
@@ -108,7 +108,7 @@ public void handleElement(Element elt) {
108108
source = source.substring(11);
109109
snippetLoC =
110110
extractSnippet(
111-
3,
111+
TopLevelKind.javascriptUrl,
112112
config,
113113
scopeManager,
114114
textualExtractor,
@@ -233,7 +233,7 @@ private String getAttributeValueLC(Element elt, String attr) {
233233
}
234234

235235
private LoCInfo extractSnippet(
236-
int toplevelKind,
236+
TopLevelKind toplevelKind,
237237
ExtractorConfig config,
238238
ScopeManager scopeManager,
239239
TextualExtractor textualExtractor,

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.semmle.js.extractor;
22

3+
import java.util.ArrayList;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
37
import com.semmle.js.ast.Comment;
48
import com.semmle.js.ast.Node;
59
import com.semmle.js.ast.Token;
@@ -14,9 +18,6 @@
1418
import com.semmle.util.exception.UserError;
1519
import com.semmle.util.trap.TrapWriter;
1620
import com.semmle.util.trap.TrapWriter.Label;
17-
import java.util.ArrayList;
18-
import java.util.regex.Matcher;
19-
import java.util.regex.Pattern;
2021

2122
/**
2223
* Extractor for populating JavaScript source code, including AST information, lexical information
@@ -36,7 +37,7 @@ public JSExtractor(ExtractorConfig config) {
3637
Pattern.compile("(?m)^([ \t]*)(import|export|goog\\.module)\\b");
3738

3839
public Pair<Label, LoCInfo> extract(
39-
TextualExtractor textualExtractor, String source, int toplevelKind, ScopeManager scopeManager)
40+
TextualExtractor textualExtractor, String source, TopLevelKind toplevelKind, ScopeManager scopeManager)
4041
throws ParseError {
4142
// if the file starts with `{ "<string>":` it won't parse as JavaScript; try parsing as JSON
4243
// instead
@@ -84,7 +85,7 @@ public SourceType establishSourceType(String source, boolean allowLeadingWS) {
8485
public Pair<Label, LoCInfo> extract(
8586
TextualExtractor textualExtractor,
8687
String source,
87-
int toplevelKind,
88+
TopLevelKind toplevelKind,
8889
ScopeManager scopeManager,
8990
SourceType sourceType,
9091
JSParser.Result parserRes)
@@ -121,7 +122,7 @@ public Pair<Label, LoCInfo> extract(
121122
ASTExtractor scriptExtractor = new ASTExtractor(lexicalExtractor, null);
122123
toplevelLabel = scriptExtractor.getToplevelLabel();
123124

124-
trapwriter.addTuple("toplevels", toplevelLabel, toplevelKind);
125+
trapwriter.addTuple("toplevels", toplevelLabel, toplevelKind.getValue());
125126
locationManager.emitSnippetLocation(toplevelLabel, 1, 1, 1, 1);
126127
loc = new LoCInfo(0, 0);
127128
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
import java.io.BufferedReader;
44
import java.io.File;
5-
import java.io.FileNotFoundException;
65
import java.io.FileReader;
76
import java.io.IOException;
8-
import java.util.concurrent.ConcurrentMap;
97
import java.util.Optional;
8+
import java.util.concurrent.ConcurrentMap;
109

1110
import com.google.gson.Gson;
1211
import com.google.gson.JsonSyntaxException;
13-
1412
import com.semmle.js.extractor.ExtractorConfig.Platform;
1513
import com.semmle.js.extractor.ExtractorConfig.SourceType;
1614
import com.semmle.js.parser.ParseError;
@@ -84,7 +82,7 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
8482
LoCInfo loc;
8583
try {
8684
Pair<Label, LoCInfo> res =
87-
new JSExtractor(config).extract(textualExtractor, source, 0, scopeManager);
85+
new JSExtractor(config).extract(textualExtractor, source, TopLevelKind.script, scopeManager);
8886
toplevelLabel = res.fst();
8987
loc = res.snd();
9088
} catch (ParseError e) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.semmle.js.extractor;
2+
3+
/**
4+
* A kind of top-level, corresponding to the <code>@toplevel</code> type in the dbscheme.
5+
*/
6+
public enum TopLevelKind {
7+
script(0),
8+
inlineScript(1),
9+
eventHandler(2),
10+
javascriptUrl(3);
11+
12+
private int value;
13+
14+
private TopLevelKind(int value) {
15+
this.value = value;
16+
}
17+
18+
/** Returns the value identifying this toplevel kind in the database. */
19+
public int getValue() {
20+
return value;
21+
}
22+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.semmle.js.extractor.ExtractorConfig.ECMAVersion;
66
import com.semmle.js.extractor.ExtractorConfig.SourceType;
77
import com.semmle.js.parser.JSParser.Result;
8-
import com.semmle.ts.extractor.TypeScriptParser;
98
import com.semmle.js.parser.ParseError;
109

1110
public class TypeScriptExtractor implements IExtractor {
@@ -28,7 +27,7 @@ public LoCInfo extract(TextualExtractor textualExtractor) {
2827
try {
2928
FileSnippet snippet = state.getSnippets().get(sourceFile.toPath());
3029
SourceType sourceType = snippet != null ? snippet.getSourceType() : jsExtractor.establishSourceType(source, false);
31-
int toplevelKind = snippet != null ? snippet.getTopLevelKind() : 0;
30+
TopLevelKind toplevelKind = snippet != null ? snippet.getTopLevelKind() : TopLevelKind.script;
3231
return jsExtractor.extract(textualExtractor, source, toplevelKind, scopeManager, sourceType, res).snd();
3332
} catch (ParseError e) {
3433
e.setPosition(locationManager.translatePosition(e.getPosition()));

0 commit comments

Comments
 (0)