mirror of
https://github.com/github/codeql.git
synced 2026-07-20 10:48:17 +02:00
TS: Support const type assertions
This commit is contained in:
@@ -345,7 +345,7 @@ public class TypeScriptASTConverter {
|
||||
case "ArrowFunction":
|
||||
return convertArrowFunction(node, loc);
|
||||
case "AsExpression":
|
||||
return convertAsExpression(node, loc);
|
||||
return convertTypeAssertionExpression(node, loc);
|
||||
case "AwaitExpression":
|
||||
return convertAwaitExpression(node, loc);
|
||||
case "BigIntKeyword":
|
||||
@@ -797,11 +797,6 @@ public class TypeScriptASTConverter {
|
||||
convertChildAsType(node, "type"));
|
||||
}
|
||||
|
||||
private Node convertAsExpression(JsonObject node, SourceLocation loc) throws ParseError {
|
||||
return new TypeAssertion(
|
||||
loc, convertChild(node, "expression"), convertChildAsType(node, "type"), true);
|
||||
}
|
||||
|
||||
private Node convertAwaitExpression(JsonObject node, SourceLocation loc) throws ParseError {
|
||||
return new AwaitExpression(loc, convertChild(node, "expression"));
|
||||
}
|
||||
@@ -2115,8 +2110,12 @@ public class TypeScriptASTConverter {
|
||||
|
||||
private Node convertTypeAssertionExpression(JsonObject node, SourceLocation loc)
|
||||
throws ParseError {
|
||||
return new TypeAssertion(
|
||||
loc, convertChild(node, "expression"), convertChildAsType(node, "type"), false);
|
||||
ITypeExpression type = convertChildAsType(node, "type");
|
||||
// `T as const` is extracted as a cast to the keyword type `const`.
|
||||
if (type instanceof Identifier && ((Identifier) type).getName().equals("const")) {
|
||||
type = new KeywordTypeExpr(type.getLoc(), "const");
|
||||
}
|
||||
return new TypeAssertion(loc, convertChild(node, "expression"), type, false);
|
||||
}
|
||||
|
||||
private Node convertTypeLiteral(JsonObject obj, SourceLocation loc) throws ParseError {
|
||||
|
||||
@@ -585,6 +585,9 @@ class TypeExpr extends ExprOrType, @typeexpr {
|
||||
/** Holds if this is the `bigint` type. */
|
||||
predicate isBigInt() { none() }
|
||||
|
||||
/** Holds if this is the `const` keyword, occurding in a type assertion such as `x as const`. */
|
||||
predicate isConstKeyword() { none() }
|
||||
|
||||
/** Gets this type expression, with any surrounding parentheses removed. */
|
||||
override TypeExpr stripParens() { result = this }
|
||||
|
||||
@@ -638,6 +641,8 @@ private class KeywordTypeExpr extends @keywordtypeexpr, TypeExpr {
|
||||
override predicate isUnknownKeyword() { getName() = "unknown" }
|
||||
|
||||
override predicate isBigInt() { getName() = "bigint" }
|
||||
|
||||
override predicate isConstKeyword() { getName() = "const" }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
test_ConstKeyword
|
||||
| tst.ts:1:19:1:23 | const |
|
||||
| tst.ts:2:10:2:14 | const |
|
||||
test_ConstTypeAssertion
|
||||
| tst.ts:1:9:1:23 | [1, 2] as const |
|
||||
| tst.ts:2:9:2:21 | <const>[1, 2] |
|
||||
@@ -0,0 +1,9 @@
|
||||
import javascript
|
||||
|
||||
query predicate test_ConstKeyword(TypeExpr t) {
|
||||
t.isConstKeyword()
|
||||
}
|
||||
|
||||
query predicate test_ConstTypeAssertion(TypeAssertion t) {
|
||||
t.getTypeAnnotation().isConstKeyword()
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
var x = [1, 2] as const;
|
||||
var x = <const>[1, 2];
|
||||
@@ -80,6 +80,16 @@
|
||||
| tst.ts:38:5:38:24 | tupleWithRestElement | [number, ...string[]] |
|
||||
| tst.ts:39:5:39:36 | tupleWi ... lements | [number, string?, ...number[]] |
|
||||
| tst.ts:40:5:40:15 | unknownType | unknown |
|
||||
| tst.ts:42:5:42:21 | constArrayLiteral | readonly [1, 2] |
|
||||
| tst.ts:42:25:42:30 | [1, 2] | readonly [1, 2] |
|
||||
| tst.ts:42:25:42:39 | [1, 2] as const | readonly [1, 2] |
|
||||
| tst.ts:42:26:42:26 | 1 | 1 |
|
||||
| tst.ts:42:29:42:29 | 2 | 2 |
|
||||
| tst.ts:43:5:43:22 | constObjectLiteral | { readonly foo: "foo"; } |
|
||||
| tst.ts:43:26:43:39 | { foo: "foo" } | { readonly foo: "foo"; } |
|
||||
| tst.ts:43:26:43:48 | { foo: ... s const | { readonly foo: "foo"; } |
|
||||
| tst.ts:43:28:43:30 | foo | "foo" |
|
||||
| tst.ts:43:33:43:37 | "foo" | "foo" |
|
||||
| type_alias.ts:3:5:3:5 | b | boolean |
|
||||
| type_definition_objects.ts:1:13:1:17 | dummy | typeof dummy.ts |
|
||||
| type_definition_objects.ts:1:24:1:32 | "./dummy" | any |
|
||||
|
||||
@@ -68,6 +68,8 @@
|
||||
| tst.ts:39:60:39:65 | number | number |
|
||||
| tst.ts:39:60:39:67 | number[] | number[] |
|
||||
| tst.ts:40:18:40:24 | unknown | unknown |
|
||||
| tst.ts:42:35:42:39 | const | any |
|
||||
| tst.ts:43:44:43:48 | const | any |
|
||||
| type_alias.ts:1:6:1:6 | B | boolean |
|
||||
| type_alias.ts:1:10:1:16 | boolean | boolean |
|
||||
| type_alias.ts:3:8:3:8 | B | boolean |
|
||||
|
||||
@@ -8,3 +8,9 @@
|
||||
| tst.ts:39:5:39:36 | tupleWi ... lements | [number, string?, ...number[]] | 0 | number | 1 | number |
|
||||
| tst.ts:39:5:39:36 | tupleWi ... lements | [number, string?, ...number[]] | 1 | string | 1 | number |
|
||||
| tst.ts:39:5:39:36 | tupleWi ... lements | [number, string?, ...number[]] | 2 | number | 1 | number |
|
||||
| tst.ts:42:5:42:21 | constArrayLiteral | readonly [1, 2] | 0 | 1 | 2 | no-rest |
|
||||
| tst.ts:42:5:42:21 | constArrayLiteral | readonly [1, 2] | 1 | 2 | 2 | no-rest |
|
||||
| tst.ts:42:25:42:30 | [1, 2] | readonly [1, 2] | 0 | 1 | 2 | no-rest |
|
||||
| tst.ts:42:25:42:30 | [1, 2] | readonly [1, 2] | 1 | 2 | 2 | no-rest |
|
||||
| tst.ts:42:25:42:39 | [1, 2] as const | readonly [1, 2] | 0 | 1 | 2 | no-rest |
|
||||
| tst.ts:42:25:42:39 | [1, 2] as const | readonly [1, 2] | 1 | 2 | 2 | no-rest |
|
||||
|
||||
@@ -37,4 +37,7 @@ let tupleWithOptionalElement: [number, string, number?];
|
||||
let emptyTuple: [];
|
||||
let tupleWithRestElement: [number, ...string[]];
|
||||
let tupleWithOptionalAndRestElements: [number, string?, ...number[]];
|
||||
let unknownType: unknown;
|
||||
let unknownType: unknown;
|
||||
|
||||
let constArrayLiteral = [1, 2] as const;
|
||||
let constObjectLiteral = { foo: "foo" } as const;
|
||||
|
||||
Reference in New Issue
Block a user