Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit addea35

Browse files
arodionovAndrii Rodionov
andauthored
Updates for ExpressionWithTypeArguments, TaggedTemplateExpression and TypeQuery (#177)
Fixes for: - trailing comma - types in models Co-authored-by: Andrii Rodionov <andriih@moderne.io>
1 parent 6f0af81 commit addea35

File tree

27 files changed

+345
-78
lines changed

27 files changed

+345
-78
lines changed

openrewrite/src/javascript/parser.ts

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ export class JavaScriptParserVisitor {
263263
| ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration
264264
| ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration
265265
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression
266-
| ts.ConstructorTypeNode) {
266+
| ts.ConstructorTypeNode | ts.TypeParameterDeclaration) {
267267
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)
268268
|| ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node)
269269
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node)
270270
|| ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node)
271-
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node)) {
271+
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node) || ts.isTypeParameterDeclaration(node)) {
272272
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
273273
}
274274
else if (ts.isExportAssignment(node)) {
@@ -648,7 +648,7 @@ export class JavaScriptParserVisitor {
648648
this.prefix(node),
649649
Markers.EMPTY,
650650
[],
651-
[],
651+
this.mapModifiers(node),
652652
this.visit(node.name),
653653
(node.constraint || node.default) ?
654654
new JContainer(
@@ -771,17 +771,8 @@ export class JavaScriptParserVisitor {
771771
let _arguments: JContainer<J.Expression> | null = null;
772772

773773
if (ts.isCallExpression(node.expression)) {
774-
let callExpression: J.MethodInvocation = this.convert(node.expression);
775-
annotationType = callExpression.select === null ? callExpression.name :
776-
new J.FieldAccess(
777-
randomId(),
778-
callExpression.prefix,
779-
Markers.EMPTY,
780-
callExpression.select,
781-
this.leftPadded(callExpression.padding.select!.after, callExpression.name),
782-
callExpression.type
783-
);
784-
_arguments = callExpression.padding.arguments;
774+
annotationType = this.convert(node.expression.expression);
775+
_arguments = this.mapCommaSeparatedList(node.expression.getChildren(this.sourceFile).slice(-3))
785776
} else if (ts.isIdentifier(node.expression)) {
786777
annotationType = this.convert(node.expression);
787778
} else if (ts.isPropertyAccessExpression(node.expression)) {
@@ -1333,7 +1324,8 @@ export class JavaScriptParserVisitor {
13331324
this.mapTypeParametersAsObject(node),
13341325
new JContainer(
13351326
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
1336-
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p))),
1327+
node.parameters.map(p => this.rightPadded(this.visit(p), this.suffix(p)))
1328+
.concat(node.parameters.hasTrailingComma ? this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)) : []),
13371329
Markers.EMPTY),
13381330
this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsGreaterThanToken)!),
13391331
this.convert(node.type),
@@ -1363,6 +1355,7 @@ export class JavaScriptParserVisitor {
13631355
this.prefix(node),
13641356
Markers.EMPTY,
13651357
this.convert(node.exprName),
1358+
node.typeArguments ? this.mapTypeArguments(this.suffix(node.exprName), node.typeArguments) : null,
13661359
this.mapType(node)
13671360
)
13681361
}
@@ -1899,36 +1892,19 @@ export class JavaScriptParserVisitor {
18991892
if (ts.isIdentifier(node.expression) && !node.questionDotToken) {
19001893
select = null;
19011894
name = this.convert(node.expression);
1902-
} else if (ts.isPropertyAccessExpression(node.expression)) {
1903-
select = this.rightPadded(
1904-
node.expression.questionDotToken ?
1905-
new JS.Unary(
1906-
randomId(),
1907-
Space.EMPTY,
1908-
Markers.EMPTY,
1909-
this.leftPadded(this.suffix(node.expression.expression), JS.Unary.Type.QuestionDot),
1910-
this.visit(node.expression.expression),
1911-
this.mapType(node)
1912-
) :
1913-
this.convert<J.Expression>(node.expression.expression),
1914-
this.prefix(node.expression.getChildAt(1, this.sourceFile))
1915-
);
1916-
name = this.convert(node.expression.name);
1895+
} else if (node.questionDotToken) {
1896+
select = this.rightPadded(new JS.Unary(
1897+
randomId(),
1898+
Space.EMPTY,
1899+
Markers.EMPTY,
1900+
this.leftPadded(this.suffix(node.expression), JS.Unary.Type.QuestionDotWithDot),
1901+
this.visit(node.expression),
1902+
this.mapType(node)
1903+
),
1904+
Space.EMPTY
1905+
)
19171906
} else {
1918-
if (node.questionDotToken) {
1919-
select = this.rightPadded(new JS.Unary(
1920-
randomId(),
1921-
Space.EMPTY,
1922-
Markers.EMPTY,
1923-
this.leftPadded(this.suffix(node.expression), JS.Unary.Type.QuestionDotWithDot),
1924-
this.visit(node.expression),
1925-
this.mapType(node)
1926-
),
1927-
Space.EMPTY
1928-
)
1929-
} else {
1930-
select = this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
1931-
}
1907+
select = this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
19321908
}
19331909

19341910
return new J.MethodInvocation(
@@ -1975,7 +1951,7 @@ export class JavaScriptParserVisitor {
19751951
Markers.EMPTY,
19761952
this.rightPadded(this.visit(node.tag), this.suffix(node.tag)),
19771953
node.typeArguments ? this.mapTypeArguments(Space.EMPTY, node.typeArguments) : null,
1978-
this.visit(node.template),
1954+
this.convert(node.template),
19791955
this.mapType(node)
19801956
)
19811957
}
@@ -2561,8 +2537,8 @@ export class JavaScriptParserVisitor {
25612537
}
25622538

25632539
visitIfStatement(node: ts.IfStatement) {
2564-
const semicolonAfterThen = node.thenStatement.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken;
2565-
const semicolonAfterElse = node.elseStatement?.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken;
2540+
const semicolonAfterThen = (node.thenStatement?.kind != ts.SyntaxKind.IfStatement) && (node.thenStatement.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken);
2541+
const semicolonAfterElse = (node.elseStatement?.kind != ts.SyntaxKind.IfStatement) && (node.elseStatement?.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken);
25662542
return new J.If(
25672543
randomId(),
25682544
this.prefix(node),
@@ -2641,9 +2617,9 @@ export class JavaScriptParserVisitor {
26412617
(ts.isVariableDeclarationList(node.initializer) ? this.rightPadded(this.visit(node.initializer), Space.EMPTY) :
26422618
this.rightPadded(new ExpressionStatement(randomId(), this.visit(node.initializer)), this.suffix(node.initializer.getLastToken()!))) :
26432619
this.rightPadded(this.newJEmpty(), this.suffix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!))], // to handle for (/*_*/; ; );
2644-
node.condition ? this.rightPadded(ts.isStatement(node.condition) ? this.visit(node.condition) : new ExpressionStatement(randomId(), this.visit(node.condition)), this.suffix(node.condition.getLastToken()!)) :
2620+
node.condition ? this.rightPadded(ts.isStatement(node.condition) ? this.visit(node.condition) : new ExpressionStatement(randomId(), this.visit(node.condition)), this.suffix(node.condition)) :
26452621
this.rightPadded(this.newJEmpty(), this.suffix(this.findChildNode(node, ts.SyntaxKind.SemicolonToken)!)), // to handle for ( ;/*_*/; );
2646-
[node.incrementor ? this.rightPadded(ts.isStatement(node.incrementor) ? this.visit(node.incrementor) : new ExpressionStatement(randomId(), this.visit(node.incrementor)), this.suffix(node.incrementor.getLastToken()!)) :
2622+
[node.incrementor ? this.rightPadded(ts.isStatement(node.incrementor) ? this.visit(node.incrementor) : new ExpressionStatement(randomId(), this.visit(node.incrementor)), this.suffix(node.incrementor)) :
26472623
this.rightPadded(this.newJEmpty(this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!)), Space.EMPTY)], // to handle for ( ; ;/*_*/);
26482624
),
26492625
this.rightPadded(
@@ -3435,7 +3411,7 @@ export class JavaScriptParserVisitor {
34353411
this.prefix(node),
34363412
Markers.EMPTY,
34373413
[],
3438-
node.name ? this.convert(node.name) : this.mapIdentifier(node, ""),
3414+
node.name ? ts.isStringLiteral(node.name) ? this.mapIdentifier(node.name, node.name.getText()) : this.convert(node.name) : this.mapIdentifier(node, ""),
34393415
node.initializer ? new J.NewClass(
34403416
randomId(),
34413417
this.suffix(node.name),
@@ -3789,6 +3765,7 @@ export class JavaScriptParserVisitor {
37893765
Markers.EMPTY,
37903766
[],
37913767
typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp)))
3768+
.concat(typeParameters.hasTrailingComma ? this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.GreaterThanToken)!)) : []),
37923769
);
37933770
}
37943771

openrewrite/src/javascript/remote/receiver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
353353
typeQuery = typeQuery.withPrefix(ctx.receiveNode(typeQuery.prefix, receiveSpace)!);
354354
typeQuery = typeQuery.withMarkers(ctx.receiveNode(typeQuery.markers, ctx.receiveMarkers)!);
355355
typeQuery = typeQuery.withTypeExpression(ctx.receiveNode(typeQuery.typeExpression, ctx.receiveTree)!);
356+
typeQuery = typeQuery.padding.withTypeArguments(ctx.receiveNode(typeQuery.padding.typeArguments, receiveContainer));
356357
typeQuery = typeQuery.withType(ctx.receiveValue(typeQuery.type, ValueType.Object));
357358
return typeQuery;
358359
}
@@ -1429,7 +1430,7 @@ class Factory implements ReceiverFactory {
14291430
ctx.receiveValue(null, ValueType.UUID)!,
14301431
ctx.receiveNode(null, receiveSpace)!,
14311432
ctx.receiveNode(null, ctx.receiveMarkers)!,
1432-
ctx.receiveNode<NameTree>(null, ctx.receiveTree)!,
1433+
ctx.receiveNode<J>(null, ctx.receiveTree)!,
14331434
ctx.receiveNode<JContainer<Expression>>(null, receiveContainer),
14341435
ctx.receiveValue(null, ValueType.Object)
14351436
);
@@ -1614,7 +1615,7 @@ class Factory implements ReceiverFactory {
16141615
ctx.receiveNode(null, ctx.receiveMarkers)!,
16151616
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree),
16161617
ctx.receiveNode<JContainer<Expression>>(null, receiveContainer),
1617-
ctx.receiveNode<TemplateExpression>(null, ctx.receiveTree)!,
1618+
ctx.receiveNode<Expression>(null, ctx.receiveTree)!,
16181619
ctx.receiveValue(null, ValueType.Object)
16191620
);
16201621
}
@@ -1679,6 +1680,7 @@ class Factory implements ReceiverFactory {
16791680
ctx.receiveNode(null, receiveSpace)!,
16801681
ctx.receiveNode(null, ctx.receiveMarkers)!,
16811682
ctx.receiveNode<TypeTree>(null, ctx.receiveTree)!,
1683+
ctx.receiveNode<JContainer<Expression>>(null, receiveContainer),
16821684
ctx.receiveValue(null, ValueType.Object)
16831685
);
16841686
}

openrewrite/src/javascript/remote/sender.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
348348
ctx.sendNode(typeQuery, v => v.prefix, Visitor.sendSpace);
349349
ctx.sendNode(typeQuery, v => v.markers, ctx.sendMarkers);
350350
ctx.sendNode(typeQuery, v => v.typeExpression, ctx.sendTree);
351+
ctx.sendNode(typeQuery, v => v.padding.typeArguments, Visitor.sendContainer(ValueType.Tree));
351352
ctx.sendTypedValue(typeQuery, v => v.type, ValueType.Object);
352353
return typeQuery;
353354
}

openrewrite/src/javascript/tree/support_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,5 +353,6 @@ export namespace JsContainer {
353353
IMPORT_TYPE_TYPE_ARGUMENTS,
354354
NAMED_EXPORTS_ELEMENTS,
355355
MAPPED_TYPE_VALUE_TYPE,
356+
TYPE_QUERY_TYPE_ARGUMENTS,
356357
}
357358
}

0 commit comments

Comments
 (0)