Skip to content

Commit 0e3369f

Browse files
authored
Merge pull request github#14484 from aibaars/ts53-js
JS: Support import attributes
2 parents 80c5e1e + a4d0ef6 commit 0e3369f

File tree

18 files changed

+2734
-1418
lines changed

18 files changed

+2734
-1418
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ protected ExportDeclaration parseExportRest(SourceLocation exportStart, Set<Stri
314314
this.parseExportSpecifiersMaybe(specifiers, exports);
315315
}
316316
Literal source = (Literal) this.parseExportFrom(specifiers, null, true);
317-
Expression assertion = this.parseImportOrExportAssertionAndSemicolon();
318-
return this.finishNode(new ExportNamedDeclaration(exportStart, null, specifiers, source, assertion));
317+
Expression attributes = this.parseImportOrExportAttributesAndSemicolon();
318+
return this.finishNode(
319+
new ExportNamedDeclaration(exportStart, null, specifiers, source, attributes));
319320
}
320321

321322
return super.parseExportRest(exportStart, exports);
@@ -331,8 +332,9 @@ protected ExportDeclaration parseExportAll(
331332
List<ExportSpecifier> specifiers = CollectionUtil.makeList(nsSpec);
332333
this.parseExportSpecifiersMaybe(specifiers, exports);
333334
Literal source = (Literal) this.parseExportFrom(specifiers, null, true);
334-
Expression assertion = this.parseImportOrExportAssertionAndSemicolon();
335-
return this.finishNode(new ExportNamedDeclaration(exportStart, null, specifiers, source, assertion));
335+
Expression attributes = this.parseImportOrExportAttributesAndSemicolon();
336+
return this.finishNode(
337+
new ExportNamedDeclaration(exportStart, null, specifiers, source, attributes));
336338
}
337339

338340
return super.parseExportAll(exportStart, starLoc, exports);

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,7 +3447,7 @@ protected ExportDeclaration parseExportRest(SourceLocation loc, Set<String> expo
34473447
Statement declaration;
34483448
List<ExportSpecifier> specifiers;
34493449
Expression source = null;
3450-
Expression assertion = null;
3450+
Expression attributes = null;
34513451
if (this.shouldParseExportStatement()) {
34523452
declaration = this.parseStatement(true, false);
34533453
if (declaration == null) return null;
@@ -3463,10 +3463,10 @@ protected ExportDeclaration parseExportRest(SourceLocation loc, Set<String> expo
34633463
declaration = null;
34643464
specifiers = this.parseExportSpecifiers(exports);
34653465
source = parseExportFrom(specifiers, source, false);
3466-
assertion = parseImportOrExportAssertionAndSemicolon();
3466+
attributes = parseImportOrExportAttributesAndSemicolon();
34673467
}
34683468
return this.finishNode(
3469-
new ExportNamedDeclaration(loc, declaration, specifiers, (Literal) source, assertion));
3469+
new ExportNamedDeclaration(loc, declaration, specifiers, (Literal) source, attributes));
34703470
}
34713471

34723472
/** Parses the 'from' clause of an export, not including the assertion or semicolon. */
@@ -3494,8 +3494,8 @@ protected Expression parseExportFrom(
34943494
protected ExportDeclaration parseExportAll(
34953495
SourceLocation loc, Position starLoc, Set<String> exports) {
34963496
Expression source = parseExportFrom(null, null, true);
3497-
Expression assertion = parseImportOrExportAssertionAndSemicolon();
3498-
return this.finishNode(new ExportAllDeclaration(loc, (Literal) source, assertion));
3497+
Expression attributes = parseImportOrExportAttributesAndSemicolon();
3498+
return this.finishNode(new ExportAllDeclaration(loc, (Literal) source, attributes));
34993499
}
35003500

35013501
private void checkExport(Set<String> exports, String name, Position pos) {
@@ -3560,10 +3560,12 @@ protected Statement parseImport(Position startLoc) {
35603560
return parseImportRest(loc);
35613561
}
35623562

3563-
protected Expression parseImportOrExportAssertionAndSemicolon() {
3563+
protected Expression parseImportOrExportAttributesAndSemicolon() {
35643564
Expression result = null;
35653565
if (!this.eagerlyTrySemicolon()) {
3566-
this.expectContextual("assert");
3566+
if (!this.eatContextual("assert")) {
3567+
this.expect(TokenType._with);
3568+
}
35673569
result = this.parseObj(false, null);
35683570
this.semicolon();
35693571
}
@@ -3583,9 +3585,9 @@ protected ImportDeclaration parseImportRest(SourceLocation loc) {
35833585
if (this.type != TokenType.string) this.unexpected();
35843586
source = (Literal) this.parseExprAtom(null);
35853587
}
3586-
Expression assertion = this.parseImportOrExportAssertionAndSemicolon();
3588+
Expression attributes = this.parseImportOrExportAttributesAndSemicolon();
35873589
if (specifiers == null) return null;
3588-
return this.finishNode(new ImportDeclaration(loc, specifiers, source, assertion));
3590+
return this.finishNode(new ImportDeclaration(loc, specifiers, source, attributes));
35893591
}
35903592

35913593
// Parses a comma-separated list of module imports.

javascript/extractor/src/com/semmle/jcorn/flow/FlowParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,12 +943,12 @@ protected ExportDeclaration parseExportRest(SourceLocation loc, Set<String> expo
943943
// `export type { foo, bar };`
944944
List<ExportSpecifier> specifiers = this.parseExportSpecifiers(exports);
945945
this.parseExportFrom(specifiers, null, false);
946-
this.parseImportOrExportAssertionAndSemicolon();
946+
this.parseImportOrExportAttributesAndSemicolon();
947947
return null;
948948
} else if (this.eat(TokenType.star)) {
949949
if (this.eatContextual("as")) this.parseIdent(true);
950950
this.parseExportFrom(null, null, true);
951-
this.parseImportOrExportAssertionAndSemicolon();
951+
this.parseImportOrExportAttributesAndSemicolon();
952952
return null;
953953
} else {
954954
// `export type Foo = Bar;`

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ public Expression getSource() {
1414
return source;
1515
}
1616

17-
/** Returns the second "argument" provided to the import, such as <code>{ assert: { type: "json" }}</code>. */
17+
/**
18+
* Returns the second "argument" provided to the import, such as <code>{ "with": { type: "json" }}
19+
* </code>.
20+
*/
1821
public Expression getAttributes() {
1922
return attributes;
2023
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
*/
1010
public class ExportAllDeclaration extends ExportDeclaration {
1111
private final Literal source;
12-
private final Expression assertion;
12+
private final Expression attributes;
1313

14-
public ExportAllDeclaration(SourceLocation loc, Literal source, Expression assertion) {
14+
public ExportAllDeclaration(SourceLocation loc, Literal source, Expression attributes) {
1515
super("ExportAllDeclaration", loc);
1616
this.source = source;
17-
this.assertion = assertion;
17+
this.attributes = attributes;
1818
}
1919

2020
public Literal getSource() {
2121
return source;
2222
}
2323

24-
public Expression getAssertion() {
25-
return assertion;
24+
public Expression getAttributes() {
25+
return attributes;
2626
}
2727

2828
@Override

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,30 @@ public class ExportNamedDeclaration extends ExportDeclaration {
1515
private final Statement declaration;
1616
private final List<ExportSpecifier> specifiers;
1717
private final Literal source;
18-
private final Expression assertion;
18+
private final Expression attributes;
1919
private final boolean hasTypeKeyword;
2020

2121
public ExportNamedDeclaration(
22-
SourceLocation loc, Statement declaration, List<ExportSpecifier> specifiers, Literal source, Expression assertion) {
23-
this(loc, declaration, specifiers, source, assertion, false);
22+
SourceLocation loc,
23+
Statement declaration,
24+
List<ExportSpecifier> specifiers,
25+
Literal source,
26+
Expression attributes) {
27+
this(loc, declaration, specifiers, source, attributes, false);
2428
}
2529

2630
public ExportNamedDeclaration(
27-
SourceLocation loc, Statement declaration, List<ExportSpecifier> specifiers, Literal source,
28-
Expression assertion, boolean hasTypeKeyword) {
31+
SourceLocation loc,
32+
Statement declaration,
33+
List<ExportSpecifier> specifiers,
34+
Literal source,
35+
Expression attributes,
36+
boolean hasTypeKeyword) {
2937
super("ExportNamedDeclaration", loc);
3038
this.declaration = declaration;
3139
this.specifiers = specifiers;
3240
this.source = source;
33-
this.assertion = assertion;
41+
this.attributes = attributes;
3442
this.hasTypeKeyword = hasTypeKeyword;
3543
}
3644

@@ -59,9 +67,12 @@ public <C, R> R accept(Visitor<C, R> v, C c) {
5967
return v.visit(this, c);
6068
}
6169

62-
/** Returns the expression after the <code>assert</code> keyword, if any, such as <code>{ type: "json" }</code>. */
63-
public Expression getAssertion() {
64-
return assertion;
70+
/**
71+
* Returns the expression after the <code>with</code> keyword, if any, such as <code>
72+
* { type: "json" }</code>.
73+
*/
74+
public Expression getAttributes() {
75+
return attributes;
6576
}
6677

6778
/** Returns true if this is an <code>export type</code> declaration. */

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.semmle.js.ast;
22

3-
import java.util.List;
4-
53
import com.semmle.ts.ast.INodeWithSymbol;
4+
import java.util.List;
65

76
/**
87
* An import declaration, which can be of one of the following forms:
@@ -23,21 +22,27 @@ public class ImportDeclaration extends Statement implements INodeWithSymbol {
2322
/** The module from which declarations are imported. */
2423
private final Literal source;
2524

26-
private final Expression assertion;
25+
private final Expression attributes;
2726

2827
private int symbol = -1;
2928

3029
private boolean hasTypeKeyword;
3130

32-
public ImportDeclaration(SourceLocation loc, List<ImportSpecifier> specifiers, Literal source, Expression assertion) {
33-
this(loc, specifiers, source, assertion, false);
31+
public ImportDeclaration(
32+
SourceLocation loc, List<ImportSpecifier> specifiers, Literal source, Expression attributes) {
33+
this(loc, specifiers, source, attributes, false);
3434
}
3535

36-
public ImportDeclaration(SourceLocation loc, List<ImportSpecifier> specifiers, Literal source, Expression assertion, boolean hasTypeKeyword) {
36+
public ImportDeclaration(
37+
SourceLocation loc,
38+
List<ImportSpecifier> specifiers,
39+
Literal source,
40+
Expression attributes,
41+
boolean hasTypeKeyword) {
3742
super("ImportDeclaration", loc);
3843
this.specifiers = specifiers;
3944
this.source = source;
40-
this.assertion = assertion;
45+
this.attributes = attributes;
4146
this.hasTypeKeyword = hasTypeKeyword;
4247
}
4348

@@ -49,9 +54,12 @@ public List<ImportSpecifier> getSpecifiers() {
4954
return specifiers;
5055
}
5156

52-
/** Returns the expression after the <code>assert</code> keyword, if any, such as <code>{ type: "json" }</code>. */
53-
public Expression getAssertion() {
54-
return assertion;
57+
/**
58+
* Returns the expression after the <code>with</code> keyword, if any, such as <code>
59+
* { type: "json" }</code>.
60+
*/
61+
public Expression getAttributes() {
62+
return attributes;
5563
}
5664

5765
@Override

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

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

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
import com.semmle.js.ast.jsx.JSXAttribute;
74
import com.semmle.js.ast.jsx.JSXClosingElement;
85
import com.semmle.js.ast.jsx.JSXElement;
@@ -42,16 +39,18 @@
4239
import com.semmle.ts.ast.ParenthesizedTypeExpr;
4340
import com.semmle.ts.ast.PredicateTypeExpr;
4441
import com.semmle.ts.ast.RestTypeExpr;
42+
import com.semmle.ts.ast.SatisfiesExpr;
4543
import com.semmle.ts.ast.TemplateLiteralTypeExpr;
4644
import com.semmle.ts.ast.TupleTypeExpr;
4745
import com.semmle.ts.ast.TypeAliasDeclaration;
4846
import com.semmle.ts.ast.TypeAssertion;
49-
import com.semmle.ts.ast.SatisfiesExpr;
5047
import com.semmle.ts.ast.TypeParameter;
5148
import com.semmle.ts.ast.TypeofTypeExpr;
5249
import com.semmle.ts.ast.UnaryTypeExpr;
5350
import com.semmle.ts.ast.UnionTypeExpr;
5451
import com.semmle.util.data.IntList;
52+
import java.util.ArrayList;
53+
import java.util.List;
5554

5655
/** Deep cloning of AST nodes. */
5756
public class NodeCopier implements Visitor<Void, INode> {
@@ -429,7 +428,8 @@ public TemplateLiteral visit(TemplateLiteral nd, Void q) {
429428

430429
@Override
431430
public TemplateLiteralTypeExpr visit(TemplateLiteralTypeExpr nd, Void q) {
432-
return new TemplateLiteralTypeExpr(visit(nd.getLoc()), copy(nd.getExpressions()), copy(nd.getQuasis()));
431+
return new TemplateLiteralTypeExpr(
432+
visit(nd.getLoc()), copy(nd.getExpressions()), copy(nd.getQuasis()));
433433
}
434434

435435
@Override
@@ -523,7 +523,8 @@ public MetaProperty visit(MetaProperty nd, Void c) {
523523

524524
@Override
525525
public ExportAllDeclaration visit(ExportAllDeclaration nd, Void c) {
526-
return new ExportAllDeclaration(visit(nd.getLoc()), copy(nd.getSource()), copy(nd.getAssertion()));
526+
return new ExportAllDeclaration(
527+
visit(nd.getLoc()), copy(nd.getSource()), copy(nd.getAttributes()));
527528
}
528529

529530
@Override
@@ -538,7 +539,7 @@ public ExportNamedDeclaration visit(ExportNamedDeclaration nd, Void c) {
538539
copy(nd.getDeclaration()),
539540
copy(nd.getSpecifiers()),
540541
copy(nd.getSource()),
541-
copy(nd.getAssertion()));
542+
copy(nd.getAttributes()));
542543
}
543544

544545
@Override
@@ -559,7 +560,11 @@ public ExportSpecifier visit(ExportSpecifier nd, Void c) {
559560
@Override
560561
public ImportDeclaration visit(ImportDeclaration nd, Void c) {
561562
return new ImportDeclaration(
562-
visit(nd.getLoc()), copy(nd.getSpecifiers()), copy(nd.getSource()), copy(nd.getAssertion()), nd.hasTypeKeyword());
563+
visit(nd.getLoc()),
564+
copy(nd.getSpecifiers()),
565+
copy(nd.getSource()),
566+
copy(nd.getAttributes()),
567+
nd.hasTypeKeyword());
563568
}
564569

565570
@Override
@@ -725,7 +730,8 @@ public INode visit(ParenthesizedTypeExpr nd, Void c) {
725730

726731
@Override
727732
public INode visit(TupleTypeExpr nd, Void c) {
728-
return new TupleTypeExpr(visit(nd.getLoc()), copy(nd.getElementTypes()), copy(nd.getElementNames()));
733+
return new TupleTypeExpr(
734+
visit(nd.getLoc()), copy(nd.getElementTypes()), copy(nd.getElementNames()));
729735
}
730736

731737
@Override
@@ -787,9 +793,7 @@ public INode visit(TypeAssertion nd, Void c) {
787793
@Override
788794
public INode visit(SatisfiesExpr nd, Void c) {
789795
return new SatisfiesExpr(
790-
visit(nd.getLoc()),
791-
copy(nd.getExpression()),
792-
copy(nd.getTypeAnnotation()));
796+
visit(nd.getLoc()), copy(nd.getExpression()), copy(nd.getTypeAnnotation()));
793797
}
794798

795799
@Override
@@ -907,7 +911,8 @@ public INode visit(XMLDotDotExpression nd, Void c) {
907911

908912
@Override
909913
public INode visit(GeneratedCodeExpr nd, Void c) {
910-
return new GeneratedCodeExpr(visit(nd.getLoc()), nd.getOpeningDelimiter(), nd.getClosingDelimiter(), nd.getBody());
914+
return new GeneratedCodeExpr(
915+
visit(nd.getLoc()), nd.getOpeningDelimiter(), nd.getClosingDelimiter(), nd.getBody());
911916
}
912917

913918
@Override

0 commit comments

Comments
 (0)