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

Commit f127983

Browse files
arodionovAndrii Rodionov
andauthored
Added WithStatement and JS.Try-Catch (#193)
Co-authored-by: Andrii Rodionov <[email protected]>
1 parent 8ac8033 commit f127983

File tree

17 files changed

+948
-9
lines changed

17 files changed

+948
-9
lines changed

openrewrite/src/javascript/parser.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,7 +2764,22 @@ export class JavaScriptParserVisitor {
27642764
}
27652765

27662766
visitWithStatement(node: ts.WithStatement) {
2767-
return this.visitUnknown(node);
2767+
return new JS.WithStatement(
2768+
randomId(),
2769+
this.prefix(node),
2770+
Markers.EMPTY,
2771+
new J.ControlParentheses(
2772+
randomId(),
2773+
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
2774+
Markers.EMPTY,
2775+
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
2776+
),
2777+
this.rightPadded(
2778+
this.convert(node.statement),
2779+
this.semicolonPrefix(node.statement),
2780+
node.statement.getChildAt(node.statement.getChildCount() - 1)?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
2781+
)
2782+
);
27682783
}
27692784

27702785
visitSwitchStatement(node: ts.SwitchStatement) {
@@ -2813,7 +2828,14 @@ export class JavaScriptParserVisitor {
28132828

28142829
visitTryStatement(node: ts.TryStatement) {
28152830
if (node.catchClause?.variableDeclaration?.name && !ts.isIdentifier(node.catchClause?.variableDeclaration?.name)) {
2816-
this.visitUnknown(node);
2831+
return new JS.JSTry(
2832+
randomId(),
2833+
this.prefix(node),
2834+
Markers.EMPTY,
2835+
this.visit(node.tryBlock),
2836+
this.visit(node.catchClause),
2837+
node.finallyBlock ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FinallyKeyword)!), this.visit(node.finallyBlock)) : null
2838+
);
28172839
}
28182840

28192841
return new J.Try(
@@ -3431,6 +3453,32 @@ export class JavaScriptParserVisitor {
34313453
}
34323454

34333455
visitCatchClause(node: ts.CatchClause) {
3456+
if (node.variableDeclaration?.name && !ts.isIdentifier(node.variableDeclaration?.name)) {
3457+
return new JS.JSTry.JSCatch(
3458+
randomId(),
3459+
this.prefix(node),
3460+
Markers.EMPTY,
3461+
new J.ControlParentheses(
3462+
randomId(),
3463+
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
3464+
Markers.EMPTY,
3465+
this.rightPadded(
3466+
new JS.JSVariableDeclarations(
3467+
randomId(),
3468+
this.prefix(node.variableDeclaration),
3469+
Markers.EMPTY,
3470+
[],
3471+
[],
3472+
this.mapTypeInfo(node.variableDeclaration),
3473+
null,
3474+
[this.rightPadded(this.visit(node.variableDeclaration), Space.EMPTY)]
3475+
),
3476+
this.suffix(node.variableDeclaration))
3477+
),
3478+
this.visit(node.block)
3479+
)
3480+
}
3481+
34343482
return new J.Try.Catch(
34353483
randomId(),
34363484
this.prefix(node),

openrewrite/src/javascript/remote/receiver.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
22
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
33
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
44
import {JavaScriptVisitor} from '..';
5-
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree';
5+
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree';
66
import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java";
77
import * as Java from "../../java/tree";
88

@@ -298,6 +298,15 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
298298
return statementExpression;
299299
}
300300

301+
public visitWithStatement(withStatement: WithStatement, ctx: ReceiverContext): J {
302+
withStatement = withStatement.withId(ctx.receiveValue(withStatement.id, ValueType.UUID)!);
303+
withStatement = withStatement.withPrefix(ctx.receiveNode(withStatement.prefix, receiveSpace)!);
304+
withStatement = withStatement.withMarkers(ctx.receiveNode(withStatement.markers, ctx.receiveMarkers)!);
305+
withStatement = withStatement.withExpression(ctx.receiveNode(withStatement.expression, ctx.receiveTree)!);
306+
withStatement = withStatement.padding.withBody(ctx.receiveNode(withStatement.padding.body, receiveRightPaddedTree)!);
307+
return withStatement;
308+
}
309+
301310
public visitTaggedTemplateExpression(taggedTemplateExpression: TaggedTemplateExpression, ctx: ReceiverContext): J {
302311
taggedTemplateExpression = taggedTemplateExpression.withId(ctx.receiveValue(taggedTemplateExpression.id, ValueType.UUID)!);
303312
taggedTemplateExpression = taggedTemplateExpression.withPrefix(ctx.receiveNode(taggedTemplateExpression.prefix, receiveSpace)!);
@@ -510,6 +519,25 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
510519
return jSForInOfLoopControl;
511520
}
512521

522+
public visitJSTry(jSTry: JSTry, ctx: ReceiverContext): J {
523+
jSTry = jSTry.withId(ctx.receiveValue(jSTry.id, ValueType.UUID)!);
524+
jSTry = jSTry.withPrefix(ctx.receiveNode(jSTry.prefix, receiveSpace)!);
525+
jSTry = jSTry.withMarkers(ctx.receiveNode(jSTry.markers, ctx.receiveMarkers)!);
526+
jSTry = jSTry.withBody(ctx.receiveNode(jSTry.body, ctx.receiveTree)!);
527+
jSTry = jSTry.withCatches(ctx.receiveNode(jSTry.catches, ctx.receiveTree)!);
528+
jSTry = jSTry.padding.withFinallie(ctx.receiveNode(jSTry.padding.finallie, receiveLeftPaddedTree));
529+
return jSTry;
530+
}
531+
532+
public visitJSTryJSCatch(jSCatch: JSTry.JSCatch, ctx: ReceiverContext): J {
533+
jSCatch = jSCatch.withId(ctx.receiveValue(jSCatch.id, ValueType.UUID)!);
534+
jSCatch = jSCatch.withPrefix(ctx.receiveNode(jSCatch.prefix, receiveSpace)!);
535+
jSCatch = jSCatch.withMarkers(ctx.receiveNode(jSCatch.markers, ctx.receiveMarkers)!);
536+
jSCatch = jSCatch.withParameter(ctx.receiveNode(jSCatch.parameter, ctx.receiveTree)!);
537+
jSCatch = jSCatch.withBody(ctx.receiveNode(jSCatch.body, ctx.receiveTree)!);
538+
return jSCatch;
539+
}
540+
513541
public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, ctx: ReceiverContext): J {
514542
namespaceDeclaration = namespaceDeclaration.withId(ctx.receiveValue(namespaceDeclaration.id, ValueType.UUID)!);
515543
namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNode(namespaceDeclaration.prefix, receiveSpace)!);
@@ -1629,6 +1657,16 @@ class Factory implements ReceiverFactory {
16291657
);
16301658
}
16311659

1660+
if (type === "org.openrewrite.javascript.tree.JS$WithStatement") {
1661+
return new WithStatement(
1662+
ctx.receiveValue(null, ValueType.UUID)!,
1663+
ctx.receiveNode(null, receiveSpace)!,
1664+
ctx.receiveNode(null, ctx.receiveMarkers)!,
1665+
ctx.receiveNode<Java.ControlParentheses<Expression>>(null, ctx.receiveTree)!,
1666+
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!
1667+
);
1668+
}
1669+
16321670
if (type === "org.openrewrite.javascript.tree.JS$TaggedTemplateExpression") {
16331671
return new TaggedTemplateExpression(
16341672
ctx.receiveValue(null, ValueType.UUID)!,
@@ -1862,6 +1900,27 @@ class Factory implements ReceiverFactory {
18621900
);
18631901
}
18641902

1903+
if (type === "org.openrewrite.javascript.tree.JS$JSTry") {
1904+
return new JSTry(
1905+
ctx.receiveValue(null, ValueType.UUID)!,
1906+
ctx.receiveNode(null, receiveSpace)!,
1907+
ctx.receiveNode(null, ctx.receiveMarkers)!,
1908+
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!,
1909+
ctx.receiveNode<JSTry.JSCatch>(null, ctx.receiveTree)!,
1910+
ctx.receiveNode<JLeftPadded<Java.Block>>(null, receiveLeftPaddedTree)
1911+
);
1912+
}
1913+
1914+
if (type === "org.openrewrite.javascript.tree.JS$JSTry$JSCatch") {
1915+
return new JSTry.JSCatch(
1916+
ctx.receiveValue(null, ValueType.UUID)!,
1917+
ctx.receiveNode(null, receiveSpace)!,
1918+
ctx.receiveNode(null, ctx.receiveMarkers)!,
1919+
ctx.receiveNode<Java.ControlParentheses<JSVariableDeclarations>>(null, ctx.receiveTree)!,
1920+
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!
1921+
);
1922+
}
1923+
18651924
if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration") {
18661925
return new NamespaceDeclaration(
18671926
ctx.receiveValue(null, ValueType.UUID)!,

openrewrite/src/javascript/remote/sender.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
22
import {Cursor, ListUtils, Tree} from '../../core';
33
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
44
import {JavaScriptVisitor} from '..';
5-
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree';
5+
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, TrailingTokenStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, WithStatement, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, JSTry, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree';
66
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
77
import * as Java from "../../java/tree";
88

@@ -293,6 +293,15 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
293293
return statementExpression;
294294
}
295295

296+
public visitWithStatement(withStatement: WithStatement, ctx: SenderContext): J {
297+
ctx.sendValue(withStatement, v => v.id, ValueType.UUID);
298+
ctx.sendNode(withStatement, v => v.prefix, Visitor.sendSpace);
299+
ctx.sendNode(withStatement, v => v.markers, ctx.sendMarkers);
300+
ctx.sendNode(withStatement, v => v.expression, ctx.sendTree);
301+
ctx.sendNode(withStatement, v => v.padding.body, Visitor.sendRightPadded(ValueType.Tree));
302+
return withStatement;
303+
}
304+
296305
public visitTaggedTemplateExpression(taggedTemplateExpression: TaggedTemplateExpression, ctx: SenderContext): J {
297306
ctx.sendValue(taggedTemplateExpression, v => v.id, ValueType.UUID);
298307
ctx.sendNode(taggedTemplateExpression, v => v.prefix, Visitor.sendSpace);
@@ -505,6 +514,25 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
505514
return jSForInOfLoopControl;
506515
}
507516

517+
public visitJSTry(jSTry: JSTry, ctx: SenderContext): J {
518+
ctx.sendValue(jSTry, v => v.id, ValueType.UUID);
519+
ctx.sendNode(jSTry, v => v.prefix, Visitor.sendSpace);
520+
ctx.sendNode(jSTry, v => v.markers, ctx.sendMarkers);
521+
ctx.sendNode(jSTry, v => v.body, ctx.sendTree);
522+
ctx.sendNode(jSTry, v => v.catches, ctx.sendTree);
523+
ctx.sendNode(jSTry, v => v.padding.finallie, Visitor.sendLeftPadded(ValueType.Tree));
524+
return jSTry;
525+
}
526+
527+
public visitJSTryJSCatch(jSCatch: JSTry.JSCatch, ctx: SenderContext): J {
528+
ctx.sendValue(jSCatch, v => v.id, ValueType.UUID);
529+
ctx.sendNode(jSCatch, v => v.prefix, Visitor.sendSpace);
530+
ctx.sendNode(jSCatch, v => v.markers, ctx.sendMarkers);
531+
ctx.sendNode(jSCatch, v => v.parameter, ctx.sendTree);
532+
ctx.sendNode(jSCatch, v => v.body, ctx.sendTree);
533+
return jSCatch;
534+
}
535+
508536
public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, ctx: SenderContext): J {
509537
ctx.sendValue(namespaceDeclaration, v => v.id, ValueType.UUID);
510538
ctx.sendNode(namespaceDeclaration, v => v.prefix, Visitor.sendSpace);

openrewrite/src/javascript/tree/support_types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ export namespace JsSpace {
261261
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX,
262262
TYPE_TREE_EXPRESSION_PREFIX,
263263
TRAILING_TOKEN_STATEMENT_PREFIX,
264+
JSTRY_PREFIX,
265+
JSTRY_JSCATCH_PREFIX,
266+
WITH_STATEMENT_PREFIX,
264267
}
265268
}
266269
export namespace JsLeftPadded {
@@ -306,6 +309,7 @@ export namespace JsLeftPadded {
306309
ARROW_FUNCTION_BODY,
307310
YIELD_DELEGATED,
308311
FUNCTION_TYPE_CONSTRUCTOR_TYPE,
312+
JSTRY_FINALLIE,
309313
}
310314
}
311315
export namespace JsRightPadded {
@@ -334,6 +338,7 @@ export namespace JsRightPadded {
334338
MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER,
335339
MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE,
336340
TRAILING_TOKEN_STATEMENT_EXPRESSION,
341+
WITH_STATEMENT_BODY,
337342
}
338343
}
339344
export namespace JsContainer {

0 commit comments

Comments
 (0)