Skip to content

Commit 788c0f9

Browse files
committed
JS: Refactor metadata class a bit
1 parent ddab13a commit 788c0f9

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

javascript/extractor/src/com/semmle/js/parser/TypeScriptASTConverter.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ private Node convertMappedType(JsonObject node, SourceLocation loc) throws Parse
15991599
private Node convertMetaProperty(JsonObject node, SourceLocation loc) throws ParseError {
16001600
Position metaStart = loc.getStart();
16011601
String keywordKind =
1602-
metadata.getSyntaxKinds().get(node.getAsJsonPrimitive("keywordToken").getAsInt() + "").getAsString();
1602+
metadata.getSyntaxKindName(node.getAsJsonPrimitive("keywordToken").getAsInt());
16031603
String identifier = keywordKind.equals("ImportKeyword") ? "import" : "new";
16041604
Position metaEnd =
16051605
new Position(
@@ -1977,7 +1977,7 @@ private Node convertPrefixUnaryExpression(JsonObject node, SourceLocation loc) t
19771977

19781978
private String getOperator(JsonObject node) throws ParseError {
19791979
int operatorId = node.get("operator").getAsInt();
1980-
switch (metadata.getSyntaxKindMap().get(operatorId)) {
1980+
switch (metadata.getSyntaxKindName(operatorId)) {
19811981
case "PlusPlusToken":
19821982
return "++";
19831983
case "MinusMinusToken":
@@ -2201,7 +2201,7 @@ private Node convertTypeOfExpression(JsonObject node, SourceLocation loc) throws
22012201
}
22022202

22032203
private Node convertTypeOperator(JsonObject node, SourceLocation loc) throws ParseError {
2204-
String operator = metadata.getSyntaxKinds().get("" + node.get("operator").getAsInt()).getAsString();
2204+
String operator = metadata.getSyntaxKindName(node.get("operator").getAsInt());
22052205
if (operator.equals("KeyOfKeyword")) {
22062206
return new UnaryTypeExpr(loc, UnaryTypeExpr.Kind.Keyof, convertChildAsType(node, "type"));
22072207
}
@@ -2519,12 +2519,7 @@ private int getMemberModifierKeywords(JsonObject node) {
25192519
* <tt>ts.NodeFlags</tt> in enum.
25202520
*/
25212521
private boolean hasFlag(JsonObject node, String flagName) {
2522-
JsonElement flagDescriptor = this.metadata.getNodeFlags().get(flagName);
2523-
if (flagDescriptor == null) {
2524-
throw new RuntimeException(
2525-
"Incompatible version of TypeScript installed. Missing node flag " + flagName);
2526-
}
2527-
int flagId = flagDescriptor.getAsInt();
2522+
int flagId = metadata.getNodeFlagId(flagName);
25282523
JsonElement flags = node.get("flags");
25292524
if (flags instanceof JsonPrimitive) {
25302525
return (flags.getAsInt() & flagId) != 0;
@@ -2534,12 +2529,7 @@ private boolean hasFlag(JsonObject node, String flagName) {
25342529

25352530
/** Gets the numeric value of the syntax kind enum with the given name. */
25362531
private int getSyntaxKind(String syntaxKind) {
2537-
JsonElement descriptor = this.metadata.getSyntaxKinds().get(syntaxKind);
2538-
if (descriptor == null) {
2539-
throw new RuntimeException(
2540-
"Incompatible version of TypeScript installed. Missing syntax kind " + syntaxKind);
2541-
}
2542-
return descriptor.getAsInt();
2532+
return metadata.getSyntaxKindId(syntaxKind);
25432533
}
25442534

25452535
/** Check whether a node has a child with a given name. */
@@ -2563,7 +2553,7 @@ private String getKind(JsonElement node) {
25632553
if (node instanceof JsonObject) {
25642554
JsonElement kind = ((JsonObject) node).get("kind");
25652555
if (kind instanceof JsonPrimitive && ((JsonPrimitive) kind).isNumber())
2566-
return metadata.getSyntaxKindMap().get(kind.getAsInt());
2556+
return metadata.getSyntaxKindName(kind.getAsInt());
25672557
}
25682558
return null;
25692559
}

javascript/extractor/src/com/semmle/js/parser/TypeScriptParserMetadata.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
public class TypeScriptParserMetadata {
1818
private final JsonObject nodeFlags;
1919
private final JsonObject syntaxKinds;
20-
private final Map<Integer, String> nodeFlagMap = new LinkedHashMap<>();
2120
private final Map<Integer, String> syntaxKindMap = new LinkedHashMap<>();
2221

2322
public TypeScriptParserMetadata(JsonObject metadata) {
2423
this.nodeFlags = metadata.get("nodeFlags").getAsJsonObject();
2524
this.syntaxKinds = metadata.get("syntaxKinds").getAsJsonObject();
26-
makeEnumIdMap(getNodeFlags(), getNodeFlagMap());
27-
makeEnumIdMap(getSyntaxKinds(), getSyntaxKindMap());
25+
makeEnumIdMap(syntaxKinds, syntaxKindMap);
2826
}
2927

3028
/** Builds a mapping from ID to name given a TypeScript enum object. */
@@ -38,30 +36,41 @@ private void makeEnumIdMap(JsonObject enumObject, Map<Integer, String> idToName)
3836
}
3937

4038
/**
41-
* Returns the <code>NodeFlags</code> enum object from the TypeScript API.
39+
* Returns the logical name associated with syntax kind ID <code>id</code>,
40+
* or throws an exception if it does not exist.
4241
*/
43-
public JsonObject getNodeFlags() {
44-
return nodeFlags;
45-
}
46-
47-
/**
48-
* Returns the <code>SyntaxKind</code> enum object from the TypeScript API.
49-
*/
50-
public JsonObject getSyntaxKinds() {
51-
return syntaxKinds;
42+
String getSyntaxKindName(int id) {
43+
String name = syntaxKindMap.get(id);
44+
if (name == null) {
45+
throw new RuntimeException(
46+
"Incompatible version of TypeScript installed. Missing syntax kind ID " + id);
47+
}
48+
return name;
5249
}
5350

5451
/**
55-
* Returns the mapping from node flag bit to its name.
52+
* Returns the syntax kind ID corresponding to the logical name <code>name</code>,
53+
* or throws an exception if it does not exist.
5654
*/
57-
public Map<Integer, String> getNodeFlagMap() {
58-
return nodeFlagMap;
55+
int getSyntaxKindId(String name) {
56+
JsonElement elm = syntaxKinds.get(name);
57+
if (elm == null) {
58+
throw new RuntimeException(
59+
"Incompatible version of TypeScript installed. Missing syntax kind " + name);
60+
}
61+
return elm.getAsInt();
5962
}
6063

6164
/**
62-
* Returns the mapping from syntax kind ID to its name.
65+
* Returns the NodeFlag ID from the logical name <code>name</code>
66+
* or throws an exception if it does not exist.
6367
*/
64-
public Map<Integer, String> getSyntaxKindMap() {
65-
return syntaxKindMap;
68+
int getNodeFlagId(String name) {
69+
JsonElement elm = nodeFlags.get(name);
70+
if (elm == null) {
71+
throw new RuntimeException(
72+
"Incompatible version of TypeScript installed. Missing node flag " + name);
73+
}
74+
return elm.getAsInt();
6675
}
6776
}

0 commit comments

Comments
 (0)