Skip to content

Commit

Permalink
reimplemented for-in/of loop with additional Control class
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Rodionov committed Nov 21, 2024
1 parent f5206ce commit 6b3a4c9
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 265 deletions.
22 changes: 15 additions & 7 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2117,9 +2117,13 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
this.suffix(this.findChildNode(node, ts.SyntaxKind.ForKeyword)!),
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression)),
new JS.JSForInOfLoopControl(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
Markers.EMPTY,
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
),
this.rightPadded(
this.convert(node.statement),
this.semicolonPrefix(node.statement),
Expand All @@ -2133,10 +2137,14 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
this.suffix(this.findChildNode(node, ts.SyntaxKind.ForKeyword)!),
node.awaitModifier ? this.rightPadded(true, this.suffix(node.awaitModifier)) : this.rightPadded(false, Space.EMPTY),
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression)),
node.awaitModifier ? this.leftPadded(this.prefix(node.awaitModifier), true) : this.leftPadded(Space.EMPTY, false),
new JS.JSForInOfLoopControl(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.OpenParenToken)!),
Markers.EMPTY,
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
),
this.rightPadded(
this.convert(node.statement),
this.semicolonPrefix(node.statement),
Expand Down
39 changes: 25 additions & 14 deletions openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,8 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
jSForOfLoop = jSForOfLoop.withId(ctx.receiveValue(jSForOfLoop.id, ValueType.UUID)!);
jSForOfLoop = jSForOfLoop.withPrefix(ctx.receiveNode(jSForOfLoop.prefix, receiveSpace)!);
jSForOfLoop = jSForOfLoop.withMarkers(ctx.receiveNode(jSForOfLoop.markers, ctx.receiveMarkers)!);
jSForOfLoop = jSForOfLoop.withFor_suffix(ctx.receiveNode(jSForOfLoop.for_suffix, receiveSpace));
jSForOfLoop = jSForOfLoop.padding.withAwait(ctx.receiveNode(jSForOfLoop.padding.await, rightPaddedValueReceiver(ValueType.Primitive))!);
jSForOfLoop = jSForOfLoop.padding.withInitializer(ctx.receiveNode(jSForOfLoop.padding.initializer, receiveRightPaddedTree)!);
jSForOfLoop = jSForOfLoop.padding.withIterable(ctx.receiveNode(jSForOfLoop.padding.iterable, receiveRightPaddedTree)!);
jSForOfLoop = jSForOfLoop.padding.withAwait(ctx.receiveNode(jSForOfLoop.padding.await, leftPaddedValueReceiver(ValueType.Primitive))!);
jSForOfLoop = jSForOfLoop.withControl(ctx.receiveNode(jSForOfLoop.control, ctx.receiveTree)!);
jSForOfLoop = jSForOfLoop.padding.withBody(ctx.receiveNode(jSForOfLoop.padding.body, receiveRightPaddedTree)!);
return jSForOfLoop;
}
Expand All @@ -389,13 +387,20 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
jSForInLoop = jSForInLoop.withId(ctx.receiveValue(jSForInLoop.id, ValueType.UUID)!);
jSForInLoop = jSForInLoop.withPrefix(ctx.receiveNode(jSForInLoop.prefix, receiveSpace)!);
jSForInLoop = jSForInLoop.withMarkers(ctx.receiveNode(jSForInLoop.markers, ctx.receiveMarkers)!);
jSForInLoop = jSForInLoop.withFor_suffix(ctx.receiveNode(jSForInLoop.for_suffix, receiveSpace));
jSForInLoop = jSForInLoop.padding.withInitializer(ctx.receiveNode(jSForInLoop.padding.initializer, receiveRightPaddedTree)!);
jSForInLoop = jSForInLoop.padding.withIterable(ctx.receiveNode(jSForInLoop.padding.iterable, receiveRightPaddedTree)!);
jSForInLoop = jSForInLoop.withControl(ctx.receiveNode(jSForInLoop.control, ctx.receiveTree)!);
jSForInLoop = jSForInLoop.padding.withBody(ctx.receiveNode(jSForInLoop.padding.body, receiveRightPaddedTree)!);
return jSForInLoop;
}

public visitJSForInOfLoopControl(jSForInOfLoopControl: JSForInOfLoopControl, ctx: ReceiverContext): J {
jSForInOfLoopControl = jSForInOfLoopControl.withId(ctx.receiveValue(jSForInOfLoopControl.id, ValueType.UUID)!);
jSForInOfLoopControl = jSForInOfLoopControl.withPrefix(ctx.receiveNode(jSForInOfLoopControl.prefix, receiveSpace)!);
jSForInOfLoopControl = jSForInOfLoopControl.withMarkers(ctx.receiveNode(jSForInOfLoopControl.markers, ctx.receiveMarkers)!);
jSForInOfLoopControl = jSForInOfLoopControl.padding.withVariable(ctx.receiveNode(jSForInOfLoopControl.padding.variable, receiveRightPaddedTree)!);
jSForInOfLoopControl = jSForInOfLoopControl.padding.withIterable(ctx.receiveNode(jSForInOfLoopControl.padding.iterable, receiveRightPaddedTree)!);
return jSForInOfLoopControl;
}

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 @@ -1495,10 +1500,8 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode(null, receiveSpace),
ctx.receiveNode<JRightPadded<boolean>>(null, rightPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JLeftPadded<boolean>>(null, leftPaddedValueReceiver(ValueType.Primitive))!,
ctx.receiveNode<JSForInOfLoopControl>(null, ctx.receiveTree)!,
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!
);
}
Expand All @@ -1508,13 +1511,21 @@ class Factory implements ReceiverFactory {
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode(null, receiveSpace),
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JRightPadded<Expression>>(null, receiveRightPaddedTree)!,
ctx.receiveNode<JSForInOfLoopControl>(null, ctx.receiveTree)!,
ctx.receiveNode<JRightPadded<Statement>>(null, receiveRightPaddedTree)!
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration") {
return new NamespaceDeclaration(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
19 changes: 12 additions & 7 deletions openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,8 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendValue(jSForOfLoop, v => v.id, ValueType.UUID);
ctx.sendNode(jSForOfLoop, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jSForOfLoop, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jSForOfLoop, v => v.for_suffix, Visitor.sendSpace);
ctx.sendNode(jSForOfLoop, v => v.padding.await, Visitor.sendRightPadded(ValueType.Primitive));
ctx.sendNode(jSForOfLoop, v => v.padding.initializer, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(jSForOfLoop, v => v.padding.iterable, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(jSForOfLoop, v => v.padding.await, Visitor.sendLeftPadded(ValueType.Primitive));
ctx.sendNode(jSForOfLoop, v => v.control, ctx.sendTree);
ctx.sendNode(jSForOfLoop, v => v.padding.body, Visitor.sendRightPadded(ValueType.Tree));
return jSForOfLoop;
}
Expand All @@ -384,13 +382,20 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
ctx.sendValue(jSForInLoop, v => v.id, ValueType.UUID);
ctx.sendNode(jSForInLoop, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jSForInLoop, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jSForInLoop, v => v.for_suffix, Visitor.sendSpace);
ctx.sendNode(jSForInLoop, v => v.padding.initializer, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(jSForInLoop, v => v.padding.iterable, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(jSForInLoop, v => v.control, ctx.sendTree);
ctx.sendNode(jSForInLoop, v => v.padding.body, Visitor.sendRightPadded(ValueType.Tree));
return jSForInLoop;
}

public visitJSForInOfLoopControl(jSForInOfLoopControl: JSForInOfLoopControl, ctx: SenderContext): J {
ctx.sendValue(jSForInOfLoopControl, v => v.id, ValueType.UUID);
ctx.sendNode(jSForInOfLoopControl, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(jSForInOfLoopControl, v => v.markers, ctx.sendMarkers);
ctx.sendNode(jSForInOfLoopControl, v => v.padding.variable, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(jSForInOfLoopControl, v => v.padding.iterable, Visitor.sendRightPadded(ValueType.Tree));
return jSForInOfLoopControl;
}

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
11 changes: 6 additions & 5 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ export namespace JsSpace {
JSVARIABLE_DECLARATIONS_VARARGS,
JSVARIABLE_DECLARATIONS_JSNAMED_VARIABLE_PREFIX,
NAMESPACE_DECLARATION_PREFIX,
NAMESPACE_DECLARATION_KEYWORD_PREFIX,
JSMETHOD_DECLARATION_PREFIX,
FUNCTION_DECLARATION_PREFIX,
JSMETHOD_INVOCATION_PREFIX,
Expand All @@ -234,9 +233,9 @@ export namespace JsSpace {
INDEX_SIGNATURE_DECLARATION_PARAMETERS_SUFFIX,
INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION_PREFIX,
JSFOR_OF_LOOP_PREFIX,
JSFOR_OF_LOOP_FOR_SUFFIX,
JSFOR_IN_LOOP_FOR_SUFFIX,
JSFOR_IN_LOOP_PREFIX,
JSFOR_IN_OF_LOOP_CONTROL_PREFIX,
TYPE_QUERY_PREFIX,
}
}
export namespace JsLeftPadded {
Expand All @@ -259,6 +258,7 @@ export namespace JsLeftPadded {
SCOPED_VARIABLE_DECLARATIONS_SCOPE,
JS_IMPORT_SPECIFIER_IMPORT_TYPE,
INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION,
JSFOR_OF_LOOP_AWAIT,
}
}
export namespace JsRightPadded {
Expand All @@ -280,10 +280,11 @@ export namespace JsRightPadded {
JSFOR_OF_LOOP_AWAIT,
JSFOR_OF_LOOP_INITIALIZER,
JSFOR_OF_LOOP_ITERABLE,
JSFOR_IN_OF_LOOP_CONTROL_VARIABLE,
JSFOR_IN_OF_LOOP_CONTROL_ITERABLE,
JSFOR_OF_LOOP_BODY,
JSFOR_IN_LOOP_INITIALIZER,
JSFOR_IN_LOOP_ITERABLE,
JSFOR_IN_LOOP_BODY,
FUNCTION_TYPE_CONSTRUCTOR_TYPE,
}
}
export namespace JsContainer {
Expand Down
Loading

0 comments on commit 6b3a4c9

Please sign in to comment.