mirror of
https://github.com/github/codeql.git
synced 2026-04-25 00:35:20 +02:00
JS: Refactor metadata class a bit
This commit is contained in:
@@ -1599,7 +1599,7 @@ public class TypeScriptASTConverter {
|
||||
private Node convertMetaProperty(JsonObject node, SourceLocation loc) throws ParseError {
|
||||
Position metaStart = loc.getStart();
|
||||
String keywordKind =
|
||||
metadata.getSyntaxKinds().get(node.getAsJsonPrimitive("keywordToken").getAsInt() + "").getAsString();
|
||||
metadata.getSyntaxKindName(node.getAsJsonPrimitive("keywordToken").getAsInt());
|
||||
String identifier = keywordKind.equals("ImportKeyword") ? "import" : "new";
|
||||
Position metaEnd =
|
||||
new Position(
|
||||
@@ -1977,7 +1977,7 @@ public class TypeScriptASTConverter {
|
||||
|
||||
private String getOperator(JsonObject node) throws ParseError {
|
||||
int operatorId = node.get("operator").getAsInt();
|
||||
switch (metadata.getSyntaxKindMap().get(operatorId)) {
|
||||
switch (metadata.getSyntaxKindName(operatorId)) {
|
||||
case "PlusPlusToken":
|
||||
return "++";
|
||||
case "MinusMinusToken":
|
||||
@@ -2201,7 +2201,7 @@ public class TypeScriptASTConverter {
|
||||
}
|
||||
|
||||
private Node convertTypeOperator(JsonObject node, SourceLocation loc) throws ParseError {
|
||||
String operator = metadata.getSyntaxKinds().get("" + node.get("operator").getAsInt()).getAsString();
|
||||
String operator = metadata.getSyntaxKindName(node.get("operator").getAsInt());
|
||||
if (operator.equals("KeyOfKeyword")) {
|
||||
return new UnaryTypeExpr(loc, UnaryTypeExpr.Kind.Keyof, convertChildAsType(node, "type"));
|
||||
}
|
||||
@@ -2519,12 +2519,7 @@ public class TypeScriptASTConverter {
|
||||
* <tt>ts.NodeFlags</tt> in enum.
|
||||
*/
|
||||
private boolean hasFlag(JsonObject node, String flagName) {
|
||||
JsonElement flagDescriptor = this.metadata.getNodeFlags().get(flagName);
|
||||
if (flagDescriptor == null) {
|
||||
throw new RuntimeException(
|
||||
"Incompatible version of TypeScript installed. Missing node flag " + flagName);
|
||||
}
|
||||
int flagId = flagDescriptor.getAsInt();
|
||||
int flagId = metadata.getNodeFlagId(flagName);
|
||||
JsonElement flags = node.get("flags");
|
||||
if (flags instanceof JsonPrimitive) {
|
||||
return (flags.getAsInt() & flagId) != 0;
|
||||
@@ -2534,12 +2529,7 @@ public class TypeScriptASTConverter {
|
||||
|
||||
/** Gets the numeric value of the syntax kind enum with the given name. */
|
||||
private int getSyntaxKind(String syntaxKind) {
|
||||
JsonElement descriptor = this.metadata.getSyntaxKinds().get(syntaxKind);
|
||||
if (descriptor == null) {
|
||||
throw new RuntimeException(
|
||||
"Incompatible version of TypeScript installed. Missing syntax kind " + syntaxKind);
|
||||
}
|
||||
return descriptor.getAsInt();
|
||||
return metadata.getSyntaxKindId(syntaxKind);
|
||||
}
|
||||
|
||||
/** Check whether a node has a child with a given name. */
|
||||
@@ -2563,7 +2553,7 @@ public class TypeScriptASTConverter {
|
||||
if (node instanceof JsonObject) {
|
||||
JsonElement kind = ((JsonObject) node).get("kind");
|
||||
if (kind instanceof JsonPrimitive && ((JsonPrimitive) kind).isNumber())
|
||||
return metadata.getSyntaxKindMap().get(kind.getAsInt());
|
||||
return metadata.getSyntaxKindName(kind.getAsInt());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -17,14 +17,12 @@ import com.google.gson.JsonPrimitive;
|
||||
public class TypeScriptParserMetadata {
|
||||
private final JsonObject nodeFlags;
|
||||
private final JsonObject syntaxKinds;
|
||||
private final Map<Integer, String> nodeFlagMap = new LinkedHashMap<>();
|
||||
private final Map<Integer, String> syntaxKindMap = new LinkedHashMap<>();
|
||||
|
||||
public TypeScriptParserMetadata(JsonObject metadata) {
|
||||
this.nodeFlags = metadata.get("nodeFlags").getAsJsonObject();
|
||||
this.syntaxKinds = metadata.get("syntaxKinds").getAsJsonObject();
|
||||
makeEnumIdMap(getNodeFlags(), getNodeFlagMap());
|
||||
makeEnumIdMap(getSyntaxKinds(), getSyntaxKindMap());
|
||||
makeEnumIdMap(syntaxKinds, syntaxKindMap);
|
||||
}
|
||||
|
||||
/** Builds a mapping from ID to name given a TypeScript enum object. */
|
||||
@@ -38,30 +36,41 @@ public class TypeScriptParserMetadata {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the <code>NodeFlags</code> enum object from the TypeScript API.
|
||||
* Returns the logical name associated with syntax kind ID <code>id</code>,
|
||||
* or throws an exception if it does not exist.
|
||||
*/
|
||||
public JsonObject getNodeFlags() {
|
||||
return nodeFlags;
|
||||
String getSyntaxKindName(int id) {
|
||||
String name = syntaxKindMap.get(id);
|
||||
if (name == null) {
|
||||
throw new RuntimeException(
|
||||
"Incompatible version of TypeScript installed. Missing syntax kind ID " + id);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the <code>SyntaxKind</code> enum object from the TypeScript API.
|
||||
* Returns the syntax kind ID corresponding to the logical name <code>name</code>,
|
||||
* or throws an exception if it does not exist.
|
||||
*/
|
||||
public JsonObject getSyntaxKinds() {
|
||||
return syntaxKinds;
|
||||
int getSyntaxKindId(String name) {
|
||||
JsonElement elm = syntaxKinds.get(name);
|
||||
if (elm == null) {
|
||||
throw new RuntimeException(
|
||||
"Incompatible version of TypeScript installed. Missing syntax kind " + name);
|
||||
}
|
||||
return elm.getAsInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mapping from node flag bit to its name.
|
||||
* Returns the NodeFlag ID from the logical name <code>name</code>
|
||||
* or throws an exception if it does not exist.
|
||||
*/
|
||||
public Map<Integer, String> getNodeFlagMap() {
|
||||
return nodeFlagMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mapping from syntax kind ID to its name.
|
||||
*/
|
||||
public Map<Integer, String> getSyntaxKindMap() {
|
||||
return syntaxKindMap;
|
||||
int getNodeFlagId(String name) {
|
||||
JsonElement elm = nodeFlags.get(name);
|
||||
if (elm == null) {
|
||||
throw new RuntimeException(
|
||||
"Incompatible version of TypeScript installed. Missing node flag " + name);
|
||||
}
|
||||
return elm.getAsInt();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user