Skip to content

Commit bd62ec2

Browse files
committed
Support TS 5.3 import attributes (previously import assertions)
1 parent 1067dd9 commit bd62ec2

File tree

3 files changed

+255
-248
lines changed

3 files changed

+255
-248
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ public class TypeScriptASTConverter {
176176
private static final Pattern EXPORT_DECL_START =
177177
Pattern.compile("^export" + "(" + WHITESPACE_CHAR + "+default)?" + WHITESPACE_CHAR + "+");
178178
private static final Pattern TYPEOF_START = Pattern.compile("^typeof" + WHITESPACE_CHAR + "+");
179-
private static final Pattern ASSERT_START = Pattern.compile("^assert" + WHITESPACE_CHAR + "+");
179+
private static final Pattern IMPORT_ATTRIBUTE_START =
180+
Pattern.compile("^(assert|with)" + WHITESPACE_CHAR + "+");
180181
private static final Pattern WHITESPACE_END_PAREN =
181182
Pattern.compile("^" + WHITESPACE_CHAR + "*\\)");
182183

@@ -342,10 +343,10 @@ private Node convertNodeUntyped(JsonObject node, String defaultKind) throws Pars
342343
return convertArrowFunction(node, loc);
343344
case "AsExpression":
344345
return convertTypeAssertionExpression(node, loc);
345-
case "AssertClause":
346-
return convertAssertClause(node, loc);
347-
case "AssertEntry":
348-
return convertAssertEntry(node, loc);
346+
case "ImportAttributes":
347+
return convertImportAttributes(node, loc);
348+
case "ImportAttribute":
349+
return convertImportAttribute(node, loc);
349350
case "SatisfiesExpression":
350351
return convertSatisfiesExpression(node, loc);
351352
case "AwaitExpression":
@@ -2297,20 +2298,20 @@ private Node convertTypeAssertionExpression(JsonObject node, SourceLocation loc)
22972298
return new TypeAssertion(loc, convertChild(node, "expression"), type, false);
22982299
}
22992300

2300-
private Node convertAssertClause(JsonObject node, SourceLocation loc) throws ParseError {
2301+
private Node convertImportAttributes(JsonObject node, SourceLocation loc) throws ParseError {
23012302
List<Property> properties = new ArrayList<>();
23022303
for (INode child : convertChildren(node, "elements")) {
23032304
properties.add((Property) child);
23042305
}
2305-
// Adjust location to skip over the `assert` keyword.
2306-
Matcher m = ASSERT_START.matcher(loc.getSource());
2306+
// Adjust location to skip over the `with` or `assert` keyword.
2307+
Matcher m = IMPORT_ATTRIBUTE_START.matcher(loc.getSource());
23072308
if (m.find()) {
23082309
advance(loc, m.group(0));
23092310
}
23102311
return new ObjectExpression(loc, properties);
23112312
}
23122313

2313-
private Node convertAssertEntry(JsonObject node, SourceLocation loc) throws ParseError {
2314+
private Node convertImportAttribute(JsonObject node, SourceLocation loc) throws ParseError {
23142315
return new Property(
23152316
loc, convertChild(node, "key"), convertChild(node, "value"), "init", false, false);
23162317
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import "module" assert { type: "json" };
2-
import * as v1 from "module" assert { type: "json" };
3-
import { v2 } from "module" assert { type: "json" };
1+
import "module" with { type: "json" };
2+
import * as v1 from "module" with { type: "json" };
3+
import { v2 } from "module" with { type: "json" };
44
import v3 from "module" assert { type: "json" };
55

6-
export { v4 } from "module" assert { type: "json" };
7-
export * from "module" assert { type: "json" };
8-
export * as v5 from "module" assert { type: "json" };
6+
export { v4 } from "module" with { type: "json" };
7+
export * from "module" with { type: "json" };
8+
export * as v5 from "module" with { type: "json" };
99

10-
const v6 = import("module", { assert: { type: "json" } });
10+
const v6 = import("module", { "with": { type: "json" } });
1111

1212
import "module"; // missing semicolon
1313
assert({ type: "json" }); // function call, not import assertion

0 commit comments

Comments
 (0)