1
1
package com .semmle .ts .extractor ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Collections ;
5
- import java .util .List ;
6
- import java .util .regex .Matcher ;
7
- import java .util .regex .Pattern ;
8
-
9
3
import com .google .gson .JsonArray ;
10
4
import com .google .gson .JsonElement ;
11
5
import com .google .gson .JsonNull ;
145
139
import com .semmle .ts .ast .ParenthesizedTypeExpr ;
146
140
import com .semmle .ts .ast .PredicateTypeExpr ;
147
141
import com .semmle .ts .ast .RestTypeExpr ;
142
+ import com .semmle .ts .ast .SatisfiesExpr ;
148
143
import com .semmle .ts .ast .TemplateLiteralTypeExpr ;
149
144
import com .semmle .ts .ast .TupleTypeExpr ;
150
145
import com .semmle .ts .ast .TypeAliasDeclaration ;
151
146
import com .semmle .ts .ast .TypeAssertion ;
152
- import com .semmle .ts .ast .SatisfiesExpr ;
153
147
import com .semmle .ts .ast .TypeParameter ;
154
148
import com .semmle .ts .ast .TypeofTypeExpr ;
155
149
import com .semmle .ts .ast .UnaryTypeExpr ;
156
150
import com .semmle .ts .ast .UnionTypeExpr ;
157
151
import com .semmle .util .collections .CollectionUtil ;
158
152
import com .semmle .util .data .IntList ;
153
+ import java .util .ArrayList ;
154
+ import java .util .Collections ;
155
+ import java .util .List ;
156
+ import java .util .regex .Matcher ;
157
+ import java .util .regex .Pattern ;
159
158
160
159
/**
161
160
* Utility class for converting a <a
@@ -177,7 +176,8 @@ public class TypeScriptASTConverter {
177
176
private static final Pattern EXPORT_DECL_START =
178
177
Pattern .compile ("^export" + "(" + WHITESPACE_CHAR + "+default)?" + WHITESPACE_CHAR + "+" );
179
178
private static final Pattern TYPEOF_START = Pattern .compile ("^typeof" + WHITESPACE_CHAR + "+" );
180
- 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 + "+" );
181
181
private static final Pattern WHITESPACE_END_PAREN =
182
182
Pattern .compile ("^" + WHITESPACE_CHAR + "*\\ )" );
183
183
@@ -343,10 +343,10 @@ private Node convertNodeUntyped(JsonObject node, String defaultKind) throws Pars
343
343
return convertArrowFunction (node , loc );
344
344
case "AsExpression" :
345
345
return convertTypeAssertionExpression (node , loc );
346
- case "AssertClause " :
347
- return convertAssertClause (node , loc );
348
- case "AssertEntry " :
349
- return convertAssertEntry (node , loc );
346
+ case "ImportAttributes " :
347
+ return convertImportAttributes (node , loc );
348
+ case "ImportAttribute " :
349
+ return convertImportAttribute (node , loc );
350
350
case "SatisfiesExpression" :
351
351
return convertSatisfiesExpression (node , loc );
352
352
case "AwaitExpression" :
@@ -877,8 +877,10 @@ private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws
877
877
}
878
878
}
879
879
880
- private Node convertStaticInitializerBlock (JsonObject node , SourceLocation loc ) throws ParseError {
881
- BlockStatement body = new BlockStatement (loc , convertChildren (node .get ("body" ).getAsJsonObject (), "statements" ));
880
+ private Node convertStaticInitializerBlock (JsonObject node , SourceLocation loc )
881
+ throws ParseError {
882
+ BlockStatement body =
883
+ new BlockStatement (loc , convertChildren (node .get ("body" ).getAsJsonObject (), "statements" ));
882
884
return new StaticInitializer (loc , body );
883
885
}
884
886
@@ -893,7 +895,8 @@ private Node convertBreakStatement(JsonObject node, SourceLocation loc) throws P
893
895
private Node convertCallExpression (JsonObject node , SourceLocation loc ) throws ParseError {
894
896
List <Expression > arguments = convertChildren (node , "arguments" );
895
897
if (arguments .size () >= 1 && hasKind (node .get ("expression" ), "ImportKeyword" )) {
896
- return new DynamicImport (loc , arguments .get (0 ), arguments .size () > 1 ? arguments .get (1 ) : null );
898
+ return new DynamicImport (
899
+ loc , arguments .get (0 ), arguments .size () > 1 ? arguments .get (1 ) : null );
897
900
}
898
901
Expression callee = convertChild (node , "expression" );
899
902
List <ITypeExpression > typeArguments = convertChildrenAsTypes (node , "typeArguments" );
@@ -1198,16 +1201,16 @@ private Node convertExportAssignment(JsonObject node, SourceLocation loc) throws
1198
1201
1199
1202
private Node convertExportDeclaration (JsonObject node , SourceLocation loc ) throws ParseError {
1200
1203
Literal source = tryConvertChild (node , "moduleSpecifier" , Literal .class );
1201
- Expression assertion = convertChild (node , "assertClause " );
1204
+ Expression attributes = convertChild (node , "attributes " );
1202
1205
if (hasChild (node , "exportClause" )) {
1203
1206
boolean hasTypeKeyword = node .get ("isTypeOnly" ).getAsBoolean ();
1204
1207
List <ExportSpecifier > specifiers =
1205
1208
hasKind (node .get ("exportClause" ), "NamespaceExport" )
1206
1209
? Collections .singletonList (convertChild (node , "exportClause" ))
1207
1210
: convertChildren (node .get ("exportClause" ).getAsJsonObject (), "elements" );
1208
- return new ExportNamedDeclaration (loc , null , specifiers , source , assertion , hasTypeKeyword );
1211
+ return new ExportNamedDeclaration (loc , null , specifiers , source , attributes , hasTypeKeyword );
1209
1212
} else {
1210
- return new ExportAllDeclaration (loc , source , assertion );
1213
+ return new ExportAllDeclaration (loc , source , attributes );
1211
1214
}
1212
1215
}
1213
1216
@@ -1238,7 +1241,8 @@ private Node convertExpressionWithTypeArguments(JsonObject node, SourceLocation
1238
1241
1239
1242
private Node convertExternalModuleReference (JsonObject node , SourceLocation loc )
1240
1243
throws ParseError {
1241
- ExternalModuleReference moduleRef = new ExternalModuleReference (loc , convertChild (node , "expression" ));
1244
+ ExternalModuleReference moduleRef =
1245
+ new ExternalModuleReference (loc , convertChild (node , "expression" ));
1242
1246
attachSymbolInformation (moduleRef , node );
1243
1247
return moduleRef ;
1244
1248
}
@@ -1389,7 +1393,7 @@ private Node convertImportClause(JsonObject node, SourceLocation loc) throws Par
1389
1393
1390
1394
private Node convertImportDeclaration (JsonObject node , SourceLocation loc ) throws ParseError {
1391
1395
Literal src = tryConvertChild (node , "moduleSpecifier" , Literal .class );
1392
- Expression assertion = convertChild (node , "assertClause " );
1396
+ Expression attributes = convertChild (node , "attributes " );
1393
1397
List <ImportSpecifier > specifiers = new ArrayList <>();
1394
1398
boolean hasTypeKeyword = false ;
1395
1399
if (hasChild (node , "importClause" )) {
@@ -1407,7 +1411,8 @@ private Node convertImportDeclaration(JsonObject node, SourceLocation loc) throw
1407
1411
}
1408
1412
hasTypeKeyword = importClause .get ("isTypeOnly" ).getAsBoolean ();
1409
1413
}
1410
- ImportDeclaration importDecl = new ImportDeclaration (loc , specifiers , src , assertion , hasTypeKeyword );
1414
+ ImportDeclaration importDecl =
1415
+ new ImportDeclaration (loc , specifiers , src , attributes , hasTypeKeyword );
1411
1416
attachSymbolInformation (importDecl , node );
1412
1417
return importDecl ;
1413
1418
}
@@ -1558,7 +1563,9 @@ private Node convertJsxAttribute(JsonObject node, SourceLocation loc) throws Par
1558
1563
nameNode = nameNode .get ("name" ).getAsJsonObject ();
1559
1564
}
1560
1565
return new JSXAttribute (
1561
- loc , convertJSXName (((Expression )convertNode (nameNode , null ))), convertChild (node , "initializer" )); // 2
1566
+ loc ,
1567
+ convertJSXName (((Expression ) convertNode (nameNode , null ))),
1568
+ convertChild (node , "initializer" )); // 2
1562
1569
}
1563
1570
1564
1571
private Node convertJsxClosingElement (JsonObject node , SourceLocation loc ) throws ParseError {
@@ -1649,8 +1656,10 @@ private Node convertLiteralType(JsonObject node, SourceLocation loc) throws Pars
1649
1656
}
1650
1657
}
1651
1658
if (literal instanceof TemplateLiteral ) {
1652
- // A LiteralType containing a NoSubstitutionTemplateLiteral must produce a TemplateLiteralTypeExpr
1653
- return new TemplateLiteralTypeExpr (literal .getLoc (), new ArrayList <>(), ((TemplateLiteral )literal ).getQuasis ());
1659
+ // A LiteralType containing a NoSubstitutionTemplateLiteral must produce a
1660
+ // TemplateLiteralTypeExpr
1661
+ return new TemplateLiteralTypeExpr (
1662
+ literal .getLoc (), new ArrayList <>(), ((TemplateLiteral ) literal ).getQuasis ());
1654
1663
}
1655
1664
return literal ;
1656
1665
}
@@ -2254,15 +2263,16 @@ private Node convertTupleType(JsonObject node, SourceLocation loc) throws ParseE
2254
2263
for (JsonElement element : node .get ("elements" ).getAsJsonArray ()) {
2255
2264
Identifier id = null ;
2256
2265
if (getKind (element ).equals ("NamedTupleMember" )) {
2257
- id = (Identifier )convertNode (element .getAsJsonObject ().get ("name" ).getAsJsonObject ());
2266
+ id = (Identifier ) convertNode (element .getAsJsonObject ().get ("name" ).getAsJsonObject ());
2258
2267
}
2259
2268
names .add (id );
2260
2269
}
2261
2270
2262
2271
return new TupleTypeExpr (loc , convertChildrenAsTypes (node , "elements" ), names );
2263
2272
}
2264
2273
2265
- // This method just does a trivial forward to the type. The names have already been extracted in `convertTupleType`.
2274
+ // This method just does a trivial forward to the type. The names have already been extracted in
2275
+ // `convertTupleType`.
2266
2276
private Node convertNamedTupleMember (JsonObject node , SourceLocation loc ) throws ParseError {
2267
2277
return convertChild (node , "type" );
2268
2278
}
@@ -2288,27 +2298,22 @@ private Node convertTypeAssertionExpression(JsonObject node, SourceLocation loc)
2288
2298
return new TypeAssertion (loc , convertChild (node , "expression" ), type , false );
2289
2299
}
2290
2300
2291
- private Node convertAssertClause (JsonObject node , SourceLocation loc ) throws ParseError {
2301
+ private Node convertImportAttributes (JsonObject node , SourceLocation loc ) throws ParseError {
2292
2302
List <Property > properties = new ArrayList <>();
2293
2303
for (INode child : convertChildren (node , "elements" )) {
2294
- properties .add ((Property )child );
2304
+ properties .add ((Property ) child );
2295
2305
}
2296
- // Adjust location to skip over the `assert` keyword.
2297
- 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 ());
2298
2308
if (m .find ()) {
2299
2309
advance (loc , m .group (0 ));
2300
2310
}
2301
2311
return new ObjectExpression (loc , properties );
2302
2312
}
2303
2313
2304
- private Node convertAssertEntry (JsonObject node , SourceLocation loc ) throws ParseError {
2314
+ private Node convertImportAttribute (JsonObject node , SourceLocation loc ) throws ParseError {
2305
2315
return new Property (
2306
- loc ,
2307
- convertChild (node , "key" ),
2308
- convertChild (node , "value" ),
2309
- "init" ,
2310
- false ,
2311
- false );
2316
+ loc , convertChild (node , "key" ), convertChild (node , "value" ), "init" , false , false );
2312
2317
}
2313
2318
2314
2319
private Node convertSatisfiesExpression (JsonObject node , SourceLocation loc ) throws ParseError {
@@ -2490,7 +2495,8 @@ private Node fixExports(SourceLocation loc, Node decl) {
2490
2495
advance (loc , skipped );
2491
2496
// capture group 1 is `default`, if present
2492
2497
if (m .group (1 ) == null )
2493
- return new ExportNamedDeclaration (outerLoc , (Statement ) decl , new ArrayList <>(), null , null );
2498
+ return new ExportNamedDeclaration (
2499
+ outerLoc , (Statement ) decl , new ArrayList <>(), null , null );
2494
2500
return new ExportDefaultDeclaration (outerLoc , decl );
2495
2501
}
2496
2502
return decl ;
@@ -2586,8 +2592,9 @@ private Iterable<JsonElement> getModifiers(JsonObject node) {
2586
2592
}
2587
2593
2588
2594
/**
2589
- * Returns a specific modifier from the given node (or <code>null</code> if absent), as defined by its
2590
- * <code>modifiers</code> property and the <code>kind</code> property of the modifier AST node.
2595
+ * Returns a specific modifier from the given node (or <code>null</code> if absent), as defined by
2596
+ * its <code>modifiers</code> property and the <code>kind</code> property of the modifier AST
2597
+ * node.
2591
2598
*/
2592
2599
private JsonObject getModifier (JsonObject node , String modKind ) {
2593
2600
for (JsonElement mod : getModifiers (node ))
@@ -2597,8 +2604,8 @@ private JsonObject getModifier(JsonObject node, String modKind) {
2597
2604
}
2598
2605
2599
2606
/**
2600
- * Check whether a node has a particular modifier, as defined by its <code>modifiers</code> property
2601
- * and the <code>kind</code> property of the modifier AST node.
2607
+ * Check whether a node has a particular modifier, as defined by its <code>modifiers</code>
2608
+ * property and the <code>kind</code> property of the modifier AST node.
2602
2609
*/
2603
2610
private boolean hasModifier (JsonObject node , String modKind ) {
2604
2611
return getModifier (node , modKind ) != null ;
@@ -2639,8 +2646,8 @@ private int getMemberModifierKeywords(JsonObject node) {
2639
2646
}
2640
2647
2641
2648
/**
2642
- * Check whether a node has a particular flag, as defined by its <code>flags</code> property and the
2643
- * <code>ts.NodeFlags</code> in enum.
2649
+ * Check whether a node has a particular flag, as defined by its <code>flags</code> property and
2650
+ * the <code>ts.NodeFlags</code> in enum.
2644
2651
*/
2645
2652
private boolean hasFlag (JsonObject node , String flagName ) {
2646
2653
int flagId = metadata .getNodeFlagId (flagName );
@@ -2683,7 +2690,7 @@ private boolean hasKind(JsonElement node, String kind) {
2683
2690
}
2684
2691
2685
2692
/**
2686
- * Gets the declaration kind of the given node, which is one of {@code "var"}, {@code "let"},
2693
+ * Gets the declaration kind of the given node, which is one of {@code "var"}, {@code "let"},
2687
2694
* {@code "const"}, or {@code "using"}.
2688
2695
*/
2689
2696
private String getDeclarationKind (JsonObject declarationList ) {
0 commit comments