11package com .semmle .ts .extractor ;
22
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-
93import com .google .gson .JsonArray ;
104import com .google .gson .JsonElement ;
115import com .google .gson .JsonNull ;
145139import com .semmle .ts .ast .ParenthesizedTypeExpr ;
146140import com .semmle .ts .ast .PredicateTypeExpr ;
147141import com .semmle .ts .ast .RestTypeExpr ;
142+ import com .semmle .ts .ast .SatisfiesExpr ;
148143import com .semmle .ts .ast .TemplateLiteralTypeExpr ;
149144import com .semmle .ts .ast .TupleTypeExpr ;
150145import com .semmle .ts .ast .TypeAliasDeclaration ;
151146import com .semmle .ts .ast .TypeAssertion ;
152- import com .semmle .ts .ast .SatisfiesExpr ;
153147import com .semmle .ts .ast .TypeParameter ;
154148import com .semmle .ts .ast .TypeofTypeExpr ;
155149import com .semmle .ts .ast .UnaryTypeExpr ;
156150import com .semmle .ts .ast .UnionTypeExpr ;
157151import com .semmle .util .collections .CollectionUtil ;
158152import 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 ;
159158
160159/**
161160 * Utility class for converting a <a
@@ -177,7 +176,8 @@ public class TypeScriptASTConverter {
177176 private static final Pattern EXPORT_DECL_START =
178177 Pattern .compile ("^export" + "(" + WHITESPACE_CHAR + "+default)?" + WHITESPACE_CHAR + "+" );
179178 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 + "+" );
181181 private static final Pattern WHITESPACE_END_PAREN =
182182 Pattern .compile ("^" + WHITESPACE_CHAR + "*\\ )" );
183183
@@ -343,10 +343,10 @@ private Node convertNodeUntyped(JsonObject node, String defaultKind) throws Pars
343343 return convertArrowFunction (node , loc );
344344 case "AsExpression" :
345345 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 );
350350 case "SatisfiesExpression" :
351351 return convertSatisfiesExpression (node , loc );
352352 case "AwaitExpression" :
@@ -877,8 +877,10 @@ private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws
877877 }
878878 }
879879
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" ));
882884 return new StaticInitializer (loc , body );
883885 }
884886
@@ -893,7 +895,8 @@ private Node convertBreakStatement(JsonObject node, SourceLocation loc) throws P
893895 private Node convertCallExpression (JsonObject node , SourceLocation loc ) throws ParseError {
894896 List <Expression > arguments = convertChildren (node , "arguments" );
895897 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 );
897900 }
898901 Expression callee = convertChild (node , "expression" );
899902 List <ITypeExpression > typeArguments = convertChildrenAsTypes (node , "typeArguments" );
@@ -1198,16 +1201,16 @@ private Node convertExportAssignment(JsonObject node, SourceLocation loc) throws
11981201
11991202 private Node convertExportDeclaration (JsonObject node , SourceLocation loc ) throws ParseError {
12001203 Literal source = tryConvertChild (node , "moduleSpecifier" , Literal .class );
1201- Expression assertion = convertChild (node , "assertClause " );
1204+ Expression attributes = convertChild (node , "attributes " );
12021205 if (hasChild (node , "exportClause" )) {
12031206 boolean hasTypeKeyword = node .get ("isTypeOnly" ).getAsBoolean ();
12041207 List <ExportSpecifier > specifiers =
12051208 hasKind (node .get ("exportClause" ), "NamespaceExport" )
12061209 ? Collections .singletonList (convertChild (node , "exportClause" ))
12071210 : 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 );
12091212 } else {
1210- return new ExportAllDeclaration (loc , source , assertion );
1213+ return new ExportAllDeclaration (loc , source , attributes );
12111214 }
12121215 }
12131216
@@ -1238,7 +1241,8 @@ private Node convertExpressionWithTypeArguments(JsonObject node, SourceLocation
12381241
12391242 private Node convertExternalModuleReference (JsonObject node , SourceLocation loc )
12401243 throws ParseError {
1241- ExternalModuleReference moduleRef = new ExternalModuleReference (loc , convertChild (node , "expression" ));
1244+ ExternalModuleReference moduleRef =
1245+ new ExternalModuleReference (loc , convertChild (node , "expression" ));
12421246 attachSymbolInformation (moduleRef , node );
12431247 return moduleRef ;
12441248 }
@@ -1389,7 +1393,7 @@ private Node convertImportClause(JsonObject node, SourceLocation loc) throws Par
13891393
13901394 private Node convertImportDeclaration (JsonObject node , SourceLocation loc ) throws ParseError {
13911395 Literal src = tryConvertChild (node , "moduleSpecifier" , Literal .class );
1392- Expression assertion = convertChild (node , "assertClause " );
1396+ Expression attributes = convertChild (node , "attributes " );
13931397 List <ImportSpecifier > specifiers = new ArrayList <>();
13941398 boolean hasTypeKeyword = false ;
13951399 if (hasChild (node , "importClause" )) {
@@ -1407,7 +1411,8 @@ private Node convertImportDeclaration(JsonObject node, SourceLocation loc) throw
14071411 }
14081412 hasTypeKeyword = importClause .get ("isTypeOnly" ).getAsBoolean ();
14091413 }
1410- ImportDeclaration importDecl = new ImportDeclaration (loc , specifiers , src , assertion , hasTypeKeyword );
1414+ ImportDeclaration importDecl =
1415+ new ImportDeclaration (loc , specifiers , src , attributes , hasTypeKeyword );
14111416 attachSymbolInformation (importDecl , node );
14121417 return importDecl ;
14131418 }
@@ -1558,7 +1563,9 @@ private Node convertJsxAttribute(JsonObject node, SourceLocation loc) throws Par
15581563 nameNode = nameNode .get ("name" ).getAsJsonObject ();
15591564 }
15601565 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
15621569 }
15631570
15641571 private Node convertJsxClosingElement (JsonObject node , SourceLocation loc ) throws ParseError {
@@ -1649,8 +1656,10 @@ private Node convertLiteralType(JsonObject node, SourceLocation loc) throws Pars
16491656 }
16501657 }
16511658 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 ());
16541663 }
16551664 return literal ;
16561665 }
@@ -2254,15 +2263,16 @@ private Node convertTupleType(JsonObject node, SourceLocation loc) throws ParseE
22542263 for (JsonElement element : node .get ("elements" ).getAsJsonArray ()) {
22552264 Identifier id = null ;
22562265 if (getKind (element ).equals ("NamedTupleMember" )) {
2257- id = (Identifier )convertNode (element .getAsJsonObject ().get ("name" ).getAsJsonObject ());
2266+ id = (Identifier ) convertNode (element .getAsJsonObject ().get ("name" ).getAsJsonObject ());
22582267 }
22592268 names .add (id );
22602269 }
22612270
22622271 return new TupleTypeExpr (loc , convertChildrenAsTypes (node , "elements" ), names );
22632272 }
22642273
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`.
22662276 private Node convertNamedTupleMember (JsonObject node , SourceLocation loc ) throws ParseError {
22672277 return convertChild (node , "type" );
22682278 }
@@ -2288,27 +2298,22 @@ private Node convertTypeAssertionExpression(JsonObject node, SourceLocation loc)
22882298 return new TypeAssertion (loc , convertChild (node , "expression" ), type , false );
22892299 }
22902300
2291- private Node convertAssertClause (JsonObject node , SourceLocation loc ) throws ParseError {
2301+ private Node convertImportAttributes (JsonObject node , SourceLocation loc ) throws ParseError {
22922302 List <Property > properties = new ArrayList <>();
22932303 for (INode child : convertChildren (node , "elements" )) {
2294- properties .add ((Property )child );
2304+ properties .add ((Property ) child );
22952305 }
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 ());
22982308 if (m .find ()) {
22992309 advance (loc , m .group (0 ));
23002310 }
23012311 return new ObjectExpression (loc , properties );
23022312 }
23032313
2304- private Node convertAssertEntry (JsonObject node , SourceLocation loc ) throws ParseError {
2314+ private Node convertImportAttribute (JsonObject node , SourceLocation loc ) throws ParseError {
23052315 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 );
23122317 }
23132318
23142319 private Node convertSatisfiesExpression (JsonObject node , SourceLocation loc ) throws ParseError {
@@ -2490,7 +2495,8 @@ private Node fixExports(SourceLocation loc, Node decl) {
24902495 advance (loc , skipped );
24912496 // capture group 1 is `default`, if present
24922497 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 );
24942500 return new ExportDefaultDeclaration (outerLoc , decl );
24952501 }
24962502 return decl ;
@@ -2586,8 +2592,9 @@ private Iterable<JsonElement> getModifiers(JsonObject node) {
25862592 }
25872593
25882594 /**
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.
25912598 */
25922599 private JsonObject getModifier (JsonObject node , String modKind ) {
25932600 for (JsonElement mod : getModifiers (node ))
@@ -2597,8 +2604,8 @@ private JsonObject getModifier(JsonObject node, String modKind) {
25972604 }
25982605
25992606 /**
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.
26022609 */
26032610 private boolean hasModifier (JsonObject node , String modKind ) {
26042611 return getModifier (node , modKind ) != null ;
@@ -2639,8 +2646,8 @@ private int getMemberModifierKeywords(JsonObject node) {
26392646 }
26402647
26412648 /**
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.
26442651 */
26452652 private boolean hasFlag (JsonObject node , String flagName ) {
26462653 int flagId = metadata .getNodeFlagId (flagName );
@@ -2683,7 +2690,7 @@ private boolean hasKind(JsonElement node, String kind) {
26832690 }
26842691
26852692 /**
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"},
26872694 * {@code "const"}, or {@code "using"}.
26882695 */
26892696 private String getDeclarationKind (JsonObject declarationList ) {
0 commit comments