Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplemented for-in/of loop #153

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2113,19 +2113,36 @@ export class JavaScriptParserVisitor {
}

visitForInStatement(node: ts.ForInStatement) {
return this.visitUnknown(node);
return new JS.JSForInLoop(
randomId(),
this.prefix(node),
Markers.EMPTY,
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),
node.statement.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY
)
);
}

visitForOfStatement(node: ts.ForOfStatement) {
return new J.ForEachLoop(
return new JS.JSForOfLoop(
randomId(),
this.prefix(node),
Markers.EMPTY,
new J.ForEachLoop.Control(
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), Space.EMPTY),
this.rightPadded(this.visit(node.initializer), this.prefix(node.initializer)),
this.rightPadded(this.visit(node.expression), this.suffix(node.expression))
),
this.rightPadded(
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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} 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, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} 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 @@ -373,6 +373,34 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return jSMethodInvocation;
}

public visitJSForOfLoop(jSForOfLoop: JSForOfLoop, ctx: ReceiverContext): J {
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.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;
}

public visitJSForInLoop(jSForInLoop: JSForInLoop, ctx: ReceiverContext): J {
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.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 @@ -1467,6 +1495,37 @@ class Factory implements ReceiverFactory {
);
}

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

if (type === "org.openrewrite.javascript.tree.JS$JSForInLoop") {
return new JSForInLoop(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
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
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, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} 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, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -368,6 +368,34 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return jSMethodInvocation;
}

public visitJSForOfLoop(jSForOfLoop: JSForOfLoop, ctx: SenderContext): J {
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.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;
}

public visitJSForInLoop(jSForInLoop: JSForInLoop, ctx: SenderContext): J {
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.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
14 changes: 13 additions & 1 deletion 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 @@ -233,6 +232,10 @@ export namespace JsSpace {
INDEX_SIGNATURE_DECLARATION_PARAMETERS_PREFIX,
INDEX_SIGNATURE_DECLARATION_PARAMETERS_SUFFIX,
INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION_PREFIX,
JSFOR_OF_LOOP_PREFIX,
JSFOR_IN_LOOP_PREFIX,
JSFOR_IN_OF_LOOP_CONTROL_PREFIX,
TYPE_QUERY_PREFIX,
}
}
export namespace JsLeftPadded {
Expand All @@ -255,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 @@ -273,6 +277,14 @@ export namespace JsRightPadded {
JSMETHOD_INVOCATION_SELECT,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
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_BODY,
FUNCTION_TYPE_CONSTRUCTOR_TYPE,
}
}
export namespace JsContainer {
Expand Down
Loading