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 e2ef997 commit 8c26a40
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 271 deletions.
22 changes: 15 additions & 7 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2085,9 +2085,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 @@ -2101,10 +2105,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
41 changes: 26 additions & 15 deletions 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, NamespaceDeclaration, FunctionDeclaration} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration} 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 @@ -368,10 +368,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 @@ -380,13 +378,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 @@ -1456,10 +1461,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 @@ -1469,13 +1472,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
21 changes: 13 additions & 8 deletions 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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, NamespaceDeclaration, FunctionDeclaration} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -363,10 +363,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 @@ -375,13 +373,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
16 changes: 7 additions & 9 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,13 @@ 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,
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 @@ -249,7 +248,8 @@ export namespace JsLeftPadded {
JSMETHOD_DECLARATION_DEFAULT_VALUE,
NAMESPACE_DECLARATION_KEYWORD_TYPE,
SCOPED_VARIABLE_DECLARATIONS_SCOPE,
JS_IMPORT_SPECIFIER_IMPORT_TYPE
JS_IMPORT_SPECIFIER_IMPORT_TYPE,
JSFOR_OF_LOOP_AWAIT,
}
}
export namespace JsRightPadded {
Expand All @@ -265,13 +265,11 @@ export namespace JsRightPadded {
JSVARIABLE_DECLARATIONS_VARIABLES,
NAMESPACE_DECLARATION_NAME,
JSMETHOD_INVOCATION_SELECT,
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 8c26a40

Please sign in to comment.