Skip to content

Commit e7209d1

Browse files
authored
Merge pull request #7216 from erik-krogh/ts45
JS: Add support for TypeScript 4.5
2 parents df482a9 + 3145e8f commit e7209d1

File tree

35 files changed

+3360
-240
lines changed

35 files changed

+3360
-240
lines changed

docs/codeql/support/reusables/versions-compilers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
JavaScript,ECMAScript 2021 or lower,Not applicable,"``.js``, ``.jsx``, ``.mjs``, ``.es``, ``.es6``, ``.htm``, ``.html``, ``.xhtm``, ``.xhtml``, ``.vue``, ``.hbs``, ``.ejs``, ``.njk``, ``.json``, ``.yaml``, ``.yml``, ``.raml``, ``.xml`` [6]_"
2424
Python,"2.7, 3.5, 3.6, 3.7, 3.8, 3.9",Not applicable,``.py``
2525
Ruby [7]_,"up to 3.02",Not applicable,"``.rb``, ``.erb``, ``.gemspec``, ``Gemfile``"
26-
TypeScript [8]_,"2.6-4.4",Standard TypeScript compiler,"``.ts``, ``.tsx``"
26+
TypeScript [8]_,"2.6-4.5",Standard TypeScript compiler,"``.ts``, ``.tsx``"
2727

2828
.. container:: footnote-group
2929

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* TypeScript 4.5 is now supported.

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": "4.4.2"
5+
"typescript": "4.5.2"
66
},
77
"scripts": {
88
"build": "tsc --project tsconfig.json",

javascript/extractor/lib/typescript/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
version "12.7.11"
77
resolved node-12.7.11.tgz#be879b52031cfb5d295b047f5462d8ef1a716446
88

9-
typescript@4.4.2:
10-
version "4.4.2"
11-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.2.tgz#6d618640d430e3569a1dfb44f7d7e600ced3ee86"
12-
integrity sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==
9+
typescript@4.5.2:
10+
version "4.5.2"
11+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.5.2.tgz#8ac1fba9f52256fdb06fb89e4122fa6a346c2998"
12+
integrity sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==

javascript/extractor/src/com/semmle/jcorn/Parser.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,15 @@ protected Expression parseExprAtom(DestructuringErrors refDestructuringErrors) {
16461646
node = new ThisExpression(new SourceLocation(this.startLoc));
16471647
this.next();
16481648
return this.finishNode(node);
1649+
} else if (this.type == TokenType.pound) {
1650+
Position startLoc = this.startLoc;
1651+
// there is only one case where this is valid, and that is "Ergonomic brand checks for Private Fields", i.e. `#name in obj`.
1652+
Identifier id = parseIdent(true);
1653+
String op = String.valueOf(this.value);
1654+
if (!op.equals("in")) {
1655+
this.unexpected(startLoc);
1656+
}
1657+
return this.parseExprOp(id, this.start, startLoc, -1, false);
16491658
} else if (this.type == TokenType.name) {
16501659
Position startLoc = this.startLoc;
16511660
Identifier id = this.parseIdent(this.type != TokenType.name);
@@ -3314,9 +3323,6 @@ protected MemberDefinition<?> parseClassPropertyBody(
33143323
if (pi.kind.equals("set") && node.getValue().hasRest())
33153324
this.raiseRecoverable(params.get(params.size() - 1), "Setter cannot use rest params");
33163325
}
3317-
if (pi.key instanceof Identifier && ((Identifier)pi.key).getName().startsWith("#")) {
3318-
raiseRecoverable(pi.key, "Only fields, not methods, can be declared private.");
3319-
}
33203326
return node;
33213327
}
33223328

javascript/extractor/src/com/semmle/js/ast/ImportSpecifier.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,25 @@
1010
*/
1111
public class ImportSpecifier extends Expression {
1212
private final Identifier imported, local;
13+
private final boolean isTypeOnly;
1314

1415
public ImportSpecifier(SourceLocation loc, Identifier imported, Identifier local) {
15-
this("ImportSpecifier", loc, imported, local);
16+
this(loc, imported, local, false);
17+
}
18+
19+
public ImportSpecifier(SourceLocation loc, Identifier imported, Identifier local, boolean isTypeOnly) {
20+
this("ImportSpecifier", loc, imported, local, isTypeOnly);
1621
}
1722

1823
public ImportSpecifier(String type, SourceLocation loc, Identifier imported, Identifier local) {
24+
this(type, loc, imported, local, false);
25+
}
26+
27+
private ImportSpecifier(String type, SourceLocation loc, Identifier imported, Identifier local, boolean isTypeOnly) {
1928
super(type, loc);
2029
this.imported = imported;
2130
this.local = local == imported ? new NodeCopier().copy(local) : local;
31+
this.isTypeOnly = isTypeOnly;
2232
}
2333

2434
public Identifier getImported() {
@@ -33,4 +43,8 @@ public Identifier getLocal() {
3343
public <C, R> R accept(Visitor<C, R> v, C c) {
3444
return v.visit(this, c);
3545
}
46+
47+
public boolean hasTypeKeyword() {
48+
return isTypeOnly;
49+
}
3650
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,15 @@ public Label visit(AssignmentExpression nd, Context c) {
847847
@Override
848848
public Label visit(BinaryExpression nd, Context c) {
849849
Label key = super.visit(nd, c);
850-
visit(nd.getLeft(), key, 0, true);
850+
if (nd.getOperator().equals("in") && nd.getLeft() instanceof Identifier && ((Identifier)nd.getLeft()).getName().startsWith("#")) {
851+
// this happens with Ergonomic brand checks for Private Fields (see https://github.com/tc39/proposal-private-fields-in-in).
852+
// it's the only case where private field identifiers are used not as a field.
853+
visit(nd.getLeft(), key, 0, IdContext.LABEL, true);
854+
} else {
855+
visit(nd.getLeft(), key, 0, true);
856+
}
851857
visit(nd.getRight(), key, 1, true);
858+
852859
extractRegxpFromBinop(nd, c);
853860
return key;
854861
}
@@ -1805,7 +1812,10 @@ public Label visit(ImportDeclaration nd, Context c) {
18051812
public Label visit(ImportSpecifier nd, Context c) {
18061813
Label lbl = super.visit(nd, c);
18071814
visit(nd.getImported(), lbl, 0, IdContext.LABEL);
1808-
visit(nd.getLocal(), lbl, 1, c.idcontext);
1815+
visit(nd.getLocal(), lbl, 1, nd.hasTypeKeyword() ? IdContext.TYPE_ONLY_IMPORT : c.idcontext);
1816+
if (nd.hasTypeKeyword()) {
1817+
trapwriter.addTuple("has_type_keyword", lbl);
1818+
}
18091819
return lbl;
18101820
}
18111821

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class Main {
4343
* A version identifier that should be updated every time the extractor changes in such a way that
4444
* it may produce different tuples for the same file under the same {@link ExtractorConfig}.
4545
*/
46-
public static final String EXTRACTOR_VERSION = "2021-10-28";
46+
public static final String EXTRACTOR_VERSION = "2021-11-23";
4747

4848
public static final Pattern NEWLINE = Pattern.compile("\n");
4949

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,8 @@ private Node convertImportSpecifier(JsonObject node, SourceLocation loc) throws
14061406
boolean hasImported = hasChild(node, "propertyName");
14071407
Identifier imported = convertChild(node, hasImported ? "propertyName" : "name");
14081408
Identifier local = convertChild(node, "name");
1409-
return new ImportSpecifier(loc, imported, local);
1409+
boolean isTypeOnly = node.get("isTypeOnly").getAsBoolean() == true;
1410+
return new ImportSpecifier(loc, imported, local, isTypeOnly);
14101411
}
14111412

14121413
private Node convertImportType(JsonObject node, SourceLocation loc) throws ParseError {

0 commit comments

Comments
 (0)