Skip to content

Commit 4a7e0fe

Browse files
author
Max Schaefer
authored
Merge pull request #766 from asger-semmle/ts-compiler-3.2
TS: Support TypeScript 3.2
2 parents 5bc1792 + 19dab71 commit 4a7e0fe

31 files changed

+5052
-2687
lines changed

change-notes/1.20/extractor-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020

2121
* The extractor now supports additional [Flow](https://flow.org/) syntax.
2222
* The extractor now supports [Nullish Coalescing](https://github.com/tc39/proposal-nullish-coalescing) expressions.
23+
* The extractor now supports [TypeScript 3.2](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-2.html).
2324
* The TypeScript extractor now handles the control-flow of logical operators and destructuring assignments more accurately.

javascript/extractor/lib/typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "typescript-parser-wrapper",
33
"private": true,
44
"dependencies": {
5-
"typescript": "3.0.1"
5+
"typescript": "3.2.1"
66
},
77
"scripts": {
88
"build": "tsc --project tsconfig.json && rollup -c",

javascript/extractor/lib/typescript/src/type_table.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ export class TypeTable {
505505
if (flags === ts.TypeFlags.Never) {
506506
return "never";
507507
}
508+
if (flags === ts.TypeFlags.BigInt) {
509+
return "bigint";
510+
}
508511
if (flags & ts.TypeFlags.Null) {
509512
return "null";
510513
}
@@ -536,6 +539,11 @@ export class TypeTable {
536539
if (flags & ts.TypeFlags.StringLiteral) {
537540
return "strlit;" + (type as ts.LiteralType).value;
538541
}
542+
if (flags & ts.TypeFlags.BigIntLiteral) {
543+
let literalType = type as ts.LiteralType;
544+
let value = literalType.value as ts.PseudoBigInt;
545+
return "bigintlit;" + (value.negative ? "-" : "") + value.base10Value;
546+
}
539547
if (flags & ts.TypeFlags.Union) {
540548
let unionType = type as ts.UnionType;
541549
if (unionType.types.length === 0) {

javascript/extractor/lib/typescript/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,9 @@ tsutils@^2.12.1:
509509
dependencies:
510510
tslib "^1.8.1"
511511

512-
typescript@3.0.1:
513-
version "3.0.1"
514-
resolved "typescript-3.0.1.tgz#43738f29585d3a87575520a4b93ab6026ef11fdb"
512+
typescript@3.2.1:
513+
version "3.2.1"
514+
resolved "typescript-3.2.1.tgz#0b7a04b8cf3868188de914d9568bd030f0c56192"
515515

516516
wrappy@1:
517517
version "1.0.2"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Main {
4141
* such a way that it may produce different tuples for the same file under the same
4242
* {@link ExtractorConfig}.
4343
*/
44-
public static final String EXTRACTOR_VERSION = "2019-09-01";
44+
public static final String EXTRACTOR_VERSION = "2019-14-01";
4545

4646
public static final Pattern NEWLINE = Pattern.compile("\n");
4747

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

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

3+
import com.semmle.jcorn.TokenType;
34
import com.semmle.js.ast.DefaultVisitor;
45
import com.semmle.js.ast.INode;
56
import com.semmle.js.ast.Identifier;
@@ -64,6 +65,7 @@ public class TypeExprKinds {
6465
private static final int importVarTypeAccess = 32;
6566
private static final int optionalTypeExpr = 33;
6667
private static final int restTypeExpr = 34;
68+
private static final int bigintLiteralTypeExpr = 35;
6769

6870
public static int getTypeExprKind(final INode type, final IdContext idcontext) {
6971
Integer kind = type.accept(new DefaultVisitor<Void, Integer>() {
@@ -159,6 +161,7 @@ public Integer visit(InterfaceTypeExpr nd, Void c) {
159161

160162
@Override
161163
public Integer visit(Literal nd, Void c) {
164+
TokenType type = nd.getTokenType();
162165
if (nd.getValue() == null) {
163166
// We represent the null type as a keyword type in QL, but in the extractor AST
164167
// it is a Literal because the TypeScript AST does not distinguish those.
@@ -167,12 +170,14 @@ public Integer visit(Literal nd, Void c) {
167170
// - TypeScript documentation does not treat the null type as a literal type.
168171
// - There is an "undefined" type, but there is no "undefined" literal.
169172
return keywordTypeExpr;
170-
} else if (nd.getValue() instanceof String) {
173+
} else if (type == TokenType.string) {
171174
return stringLiteralTypeExpr;
172-
} else if (nd.getValue() instanceof Number) {
175+
} else if (type == TokenType.num) {
173176
return numberLiteralTypeExpr;
174-
} else if (nd.getValue() instanceof Boolean) {
177+
} else if (type == TokenType._true || type == TokenType._false) {
175178
return booeleanLiteralTypeExpr;
179+
} else if (type == TokenType.bigint) {
180+
return bigintLiteralTypeExpr;
176181
} else {
177182
throw new CatastrophicError("Unsupported literal type expression kind: " + nd.getValue().getClass());
178183
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ private Node convertNodeUntyped(JsonObject node, String defaultKind) throws Pars
369369
return convertAsExpression(node, loc);
370370
case "AwaitExpression":
371371
return convertAwaitExpression(node, loc);
372+
case "BigIntKeyword":
373+
return convertKeywordTypeExpr(node, loc, "bigint");
374+
case "BigIntLiteral":
375+
return convertBigIntLiteral(node, loc);
372376
case "BinaryExpression":
373377
return convertBinaryExpression(node, loc);
374378
case "Block":
@@ -822,6 +826,12 @@ private Node convertAwaitExpression(JsonObject node, SourceLocation loc) throws
822826
return new AwaitExpression(loc, convertChild(node, "expression"));
823827
}
824828

829+
private Node convertBigIntLiteral(JsonObject node, SourceLocation loc) throws ParseError {
830+
String text = node.get("text").getAsString();
831+
String value = text.substring(0, text.length() - 1); // Remove the 'n' suffix.
832+
return new Literal(loc, TokenType.bigint, value);
833+
}
834+
825835
private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws ParseError {
826836
Expression left = convertChild(node, "left");
827837
Expression right = convertChild(node, "right");

javascript/extractor/src/com/semmle/ts/extractor/TypeExtractor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class TypeExtractor {
3131
private static final int thisKind = 20;
3232
private static final int numberLiteralTypeKind = 21;
3333
private static final int stringLiteralTypeKind = 22;
34+
private static final int bigintLiteralTypeKind = 25;
3435

3536
static {
3637
tagToKind.put("any", 0);
@@ -57,6 +58,8 @@ public class TypeExtractor {
5758
tagToKind.put("numlit", numberLiteralTypeKind);
5859
tagToKind.put("strlit", stringLiteralTypeKind);
5960
tagToKind.put("unknown", 23);
61+
tagToKind.put("bigint", 24);
62+
tagToKind.put("bigintlit", bigintLiteralTypeKind);
6063
}
6164

6265
private static final Map<String, Integer> symbolKind = new LinkedHashMap<String, Integer>();
@@ -126,6 +129,7 @@ private void extractType(int id) {
126129

127130
case numberLiteralTypeKind:
128131
case stringLiteralTypeKind:
132+
case bigintLiteralTypeKind:
129133
firstChild = parts.length; // No children.
130134
// The string value may contain `;` so don't use the split().
131135
String value = contents.substring(parts[0].length() + 1);

0 commit comments

Comments
 (0)