Skip to content

Commit

Permalink
Added WithStatement and JS.Try-Catch (#193)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Rodionov <[email protected]>
  • Loading branch information
arodionov and Andrii Rodionov authored Jan 13, 2025
1 parent 8ac8033 commit f127983
Show file tree
Hide file tree
Showing 17 changed files with 948 additions and 9 deletions.
52 changes: 50 additions & 2 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,22 @@ export class JavaScriptParserVisitor {
}

visitWithStatement(node: ts.WithStatement) {
return this.visitUnknown(node);
return new JS.WithStatement(
randomId(),
this.prefix(node),
Markers.EMPTY,
new J.ControlParentheses(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
Markers.EMPTY,
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
),
this.rightPadded(
this.convert(node.statement),
this.semicolonPrefix(node.statement),
node.statement.getChildAt(node.statement.getChildCount() - 1)?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
)
);
}

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

visitTryStatement(node: ts.TryStatement) {
if (node.catchClause?.variableDeclaration?.name && !ts.isIdentifier(node.catchClause?.variableDeclaration?.name)) {
this.visitUnknown(node);
return new JS.JSTry(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.visit(node.tryBlock),
this.visit(node.catchClause),
node.finallyBlock ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FinallyKeyword)!), this.visit(node.finallyBlock)) : null
);
}

return new J.Try(
Expand Down Expand Up @@ -3431,6 +3453,32 @@ export class JavaScriptParserVisitor {
}

visitCatchClause(node: ts.CatchClause) {
if (node.variableDeclaration?.name && !ts.isIdentifier(node.variableDeclaration?.name)) {
return new JS.JSTry.JSCatch(
randomId(),
this.prefix(node),
Markers.EMPTY,
new J.ControlParentheses(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
Markers.EMPTY,
this.rightPadded(
new JS.JSVariableDeclarations(
randomId(),
this.prefix(node.variableDeclaration),
Markers.EMPTY,
[],
[],
this.mapTypeInfo(node.variableDeclaration),
null,
[this.rightPadded(this.visit(node.variableDeclaration), Space.EMPTY)]
),
this.suffix(node.variableDeclaration))
),
this.visit(node.block)
)
}

return new J.Try.Catch(
randomId(),
this.prefix(node),
Expand Down
61 changes: 60 additions & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
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';
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';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -298,6 +298,15 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return statementExpression;
}

public visitWithStatement(withStatement: WithStatement, ctx: ReceiverContext): J {
withStatement = withStatement.withId(ctx.receiveValue(withStatement.id, ValueType.UUID)!);
withStatement = withStatement.withPrefix(ctx.receiveNode(withStatement.prefix, receiveSpace)!);
withStatement = withStatement.withMarkers(ctx.receiveNode(withStatement.markers, ctx.receiveMarkers)!);
withStatement = withStatement.withExpression(ctx.receiveNode(withStatement.expression, ctx.receiveTree)!);
withStatement = withStatement.padding.withBody(ctx.receiveNode(withStatement.padding.body, receiveRightPaddedTree)!);
return withStatement;
}

public visitTaggedTemplateExpression(taggedTemplateExpression: TaggedTemplateExpression, ctx: ReceiverContext): J {
taggedTemplateExpression = taggedTemplateExpression.withId(ctx.receiveValue(taggedTemplateExpression.id, ValueType.UUID)!);
taggedTemplateExpression = taggedTemplateExpression.withPrefix(ctx.receiveNode(taggedTemplateExpression.prefix, receiveSpace)!);
Expand Down Expand Up @@ -510,6 +519,25 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return jSForInOfLoopControl;
}

public visitJSTry(jSTry: JSTry, ctx: ReceiverContext): J {
jSTry = jSTry.withId(ctx.receiveValue(jSTry.id, ValueType.UUID)!);
jSTry = jSTry.withPrefix(ctx.receiveNode(jSTry.prefix, receiveSpace)!);
jSTry = jSTry.withMarkers(ctx.receiveNode(jSTry.markers, ctx.receiveMarkers)!);
jSTry = jSTry.withBody(ctx.receiveNode(jSTry.body, ctx.receiveTree)!);
jSTry = jSTry.withCatches(ctx.receiveNode(jSTry.catches, ctx.receiveTree)!);
jSTry = jSTry.padding.withFinallie(ctx.receiveNode(jSTry.padding.finallie, receiveLeftPaddedTree));
return jSTry;
}

public visitJSTryJSCatch(jSCatch: JSTry.JSCatch, ctx: ReceiverContext): J {
jSCatch = jSCatch.withId(ctx.receiveValue(jSCatch.id, ValueType.UUID)!);
jSCatch = jSCatch.withPrefix(ctx.receiveNode(jSCatch.prefix, receiveSpace)!);
jSCatch = jSCatch.withMarkers(ctx.receiveNode(jSCatch.markers, ctx.receiveMarkers)!);
jSCatch = jSCatch.withParameter(ctx.receiveNode(jSCatch.parameter, ctx.receiveTree)!);
jSCatch = jSCatch.withBody(ctx.receiveNode(jSCatch.body, ctx.receiveTree)!);
return jSCatch;
}

public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, ctx: ReceiverContext): J {
namespaceDeclaration = namespaceDeclaration.withId(ctx.receiveValue(namespaceDeclaration.id, ValueType.UUID)!);
namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNode(namespaceDeclaration.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1629,6 +1657,16 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$WithStatement") {
return new WithStatement(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<Java.ControlParentheses<Expression>>(null, ctx.receiveTree)!,
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!
);
}

if (type === "org.openrewrite.javascript.tree.JS$TaggedTemplateExpression") {
return new TaggedTemplateExpression(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down Expand Up @@ -1862,6 +1900,27 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$JSTry") {
return new JSTry(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!,
ctx.receiveNode<JSTry.JSCatch>(null, ctx.receiveTree)!,
ctx.receiveNode<JLeftPadded<Java.Block>>(null, receiveLeftPaddedTree)
);
}

if (type === "org.openrewrite.javascript.tree.JS$JSTry$JSCatch") {
return new JSTry.JSCatch(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<Java.ControlParentheses<JSVariableDeclarations>>(null, ctx.receiveTree)!,
ctx.receiveNode<Java.Block>(null, ctx.receiveTree)!
);
}

if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration") {
return new NamespaceDeclaration(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
30 changes: 29 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Cursor, ListUtils, Tree} from '../../core';
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
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';
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';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -293,6 +293,15 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return statementExpression;
}

public visitWithStatement(withStatement: WithStatement, ctx: SenderContext): J {
ctx.sendValue(withStatement, v => v.id, ValueType.UUID);
ctx.sendNode(withStatement, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(withStatement, v => v.markers, ctx.sendMarkers);
ctx.sendNode(withStatement, v => v.expression, ctx.sendTree);
ctx.sendNode(withStatement, v => v.padding.body, Visitor.sendRightPadded(ValueType.Tree));
return withStatement;
}

public visitTaggedTemplateExpression(taggedTemplateExpression: TaggedTemplateExpression, ctx: SenderContext): J {
ctx.sendValue(taggedTemplateExpression, v => v.id, ValueType.UUID);
ctx.sendNode(taggedTemplateExpression, v => v.prefix, Visitor.sendSpace);
Expand Down Expand Up @@ -505,6 +514,25 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return jSForInOfLoopControl;
}

public visitJSTry(jSTry: JSTry, ctx: SenderContext): J {
ctx.sendValue(jSTry, v => v.id, ValueType.UUID);
ctx.sendNode(jSTry, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jSTry, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jSTry, v => v.body, ctx.sendTree);
ctx.sendNode(jSTry, v => v.catches, ctx.sendTree);
ctx.sendNode(jSTry, v => v.padding.finallie, Visitor.sendLeftPadded(ValueType.Tree));
return jSTry;
}

public visitJSTryJSCatch(jSCatch: JSTry.JSCatch, ctx: SenderContext): J {
ctx.sendValue(jSCatch, v => v.id, ValueType.UUID);
ctx.sendNode(jSCatch, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jSCatch, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jSCatch, v => v.parameter, ctx.sendTree);
ctx.sendNode(jSCatch, v => v.body, ctx.sendTree);
return jSCatch;
}

public visitNamespaceDeclaration(namespaceDeclaration: NamespaceDeclaration, ctx: SenderContext): J {
ctx.sendValue(namespaceDeclaration, v => v.id, ValueType.UUID);
ctx.sendNode(namespaceDeclaration, v => v.prefix, Visitor.sendSpace);
Expand Down
5 changes: 5 additions & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ export namespace JsSpace {
MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX,
TYPE_TREE_EXPRESSION_PREFIX,
TRAILING_TOKEN_STATEMENT_PREFIX,
JSTRY_PREFIX,
JSTRY_JSCATCH_PREFIX,
WITH_STATEMENT_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down Expand Up @@ -306,6 +309,7 @@ export namespace JsLeftPadded {
ARROW_FUNCTION_BODY,
YIELD_DELEGATED,
FUNCTION_TYPE_CONSTRUCTOR_TYPE,
JSTRY_FINALLIE,
}
}
export namespace JsRightPadded {
Expand Down Expand Up @@ -334,6 +338,7 @@ export namespace JsRightPadded {
MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER,
MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE,
TRAILING_TOKEN_STATEMENT_EXPRESSION,
WITH_STATEMENT_BODY,
}
}
export namespace JsContainer {
Expand Down
Loading

0 comments on commit f127983

Please sign in to comment.