Skip to content

Commit

Permalink
add array binding pattern (#157)
Browse files Browse the repository at this point in the history
* add array binding pattern

* add fixes
  • Loading branch information
OlegDokuka authored Nov 22, 2024
1 parent c8c2d47 commit da44db9
Show file tree
Hide file tree
Showing 21 changed files with 721 additions and 424 deletions.
14 changes: 9 additions & 5 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,11 +1363,17 @@ export class JavaScriptParserVisitor {
}

visitArrayBindingPattern(node: ts.ArrayBindingPattern) {
return this.visitUnknown(node);
return new JS.ArrayBindingPattern(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapCommaSeparatedList(node.getChildren(this.sourceFile)),
this.mapType(node)
);
}

visitBindingElement(node: ts.BindingElement) {
return new JS.ObjectBindingDeclarations.Binding(
return new JS.BindingElement(
randomId(),
this.prefix(node),
Markers.EMPTY,
Expand All @@ -1380,8 +1386,6 @@ export class JavaScriptParserVisitor {
this.convert<J.Expression>(node.name),
null
) : this.convert<TypedTree>(node.name),
[],
null,
node.initializer ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsToken)!), this.convert<J.Expression>(node.initializer)) : null,
this.mapVariableType(node),
);
Expand Down Expand Up @@ -1994,7 +1998,7 @@ export class JavaScriptParserVisitor {
}

visitOmittedExpression(node: ts.OmittedExpression) {
return this.visitUnknown(node);
return this.newJEmpty(this.prefix(node));
}

visitExpressionWithTypeArguments(node: ts.ExpressionWithTypeArguments) {
Expand Down
73 changes: 44 additions & 29 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, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, 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, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} 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 @@ -170,19 +170,6 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return objectBindingDeclarations;
}

public visitBinding(binding: ObjectBindingDeclarations.Binding, ctx: ReceiverContext): J {
binding = binding.withId(ctx.receiveValue(binding.id, ValueType.UUID)!);
binding = binding.withPrefix(ctx.receiveNode(binding.prefix, receiveSpace)!);
binding = binding.withMarkers(ctx.receiveNode(binding.markers, ctx.receiveMarkers)!);
binding = binding.padding.withPropertyName(ctx.receiveNode(binding.padding.propertyName, receiveRightPaddedTree));
binding = binding.withName(ctx.receiveNode(binding.name, ctx.receiveTree)!);
binding = binding.withDimensionsAfterName(ctx.receiveNodes(binding.dimensionsAfterName, leftPaddedNodeReceiver(Space))!);
binding = binding.withAfterVararg(ctx.receiveNode(binding.afterVararg, receiveSpace));
binding = binding.padding.withInitializer(ctx.receiveNode(binding.padding.initializer, receiveLeftPaddedTree));
binding = binding.withVariableType(ctx.receiveValue(binding.variableType, ValueType.Object));
return binding;
}

public visitPropertyAssignment(propertyAssignment: PropertyAssignment, ctx: ReceiverContext): J {
propertyAssignment = propertyAssignment.withId(ctx.receiveValue(propertyAssignment.id, ValueType.UUID)!);
propertyAssignment = propertyAssignment.withPrefix(ctx.receiveNode(propertyAssignment.prefix, receiveSpace)!);
Expand Down Expand Up @@ -456,6 +443,26 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return indexSignatureDeclaration;
}

public visitArrayBindingPattern(arrayBindingPattern: ArrayBindingPattern, ctx: ReceiverContext): J {
arrayBindingPattern = arrayBindingPattern.withId(ctx.receiveValue(arrayBindingPattern.id, ValueType.UUID)!);
arrayBindingPattern = arrayBindingPattern.withPrefix(ctx.receiveNode(arrayBindingPattern.prefix, receiveSpace)!);
arrayBindingPattern = arrayBindingPattern.withMarkers(ctx.receiveNode(arrayBindingPattern.markers, ctx.receiveMarkers)!);
arrayBindingPattern = arrayBindingPattern.padding.withElements(ctx.receiveNode(arrayBindingPattern.padding.elements, receiveContainer)!);
arrayBindingPattern = arrayBindingPattern.withType(ctx.receiveValue(arrayBindingPattern.type, ValueType.Object));
return arrayBindingPattern;
}

public visitBindingElement(bindingElement: BindingElement, ctx: ReceiverContext): J {
bindingElement = bindingElement.withId(ctx.receiveValue(bindingElement.id, ValueType.UUID)!);
bindingElement = bindingElement.withPrefix(ctx.receiveNode(bindingElement.prefix, receiveSpace)!);
bindingElement = bindingElement.withMarkers(ctx.receiveNode(bindingElement.markers, ctx.receiveMarkers)!);
bindingElement = bindingElement.padding.withPropertyName(ctx.receiveNode(bindingElement.padding.propertyName, receiveRightPaddedTree));
bindingElement = bindingElement.withName(ctx.receiveNode(bindingElement.name, ctx.receiveTree)!);
bindingElement = bindingElement.padding.withInitializer(ctx.receiveNode(bindingElement.padding.initializer, receiveLeftPaddedTree));
bindingElement = bindingElement.withVariableType(ctx.receiveValue(bindingElement.variableType, ValueType.Object));
return bindingElement;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: ReceiverContext): J {
annotatedType = annotatedType.withId(ctx.receiveValue(annotatedType.id, ValueType.UUID)!);
annotatedType = annotatedType.withPrefix(ctx.receiveNode(annotatedType.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1277,25 +1284,11 @@ class Factory implements ReceiverFactory {
ctx.receiveNodes<Java.Annotation>(null, ctx.receiveTree)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree),
ctx.receiveNode<JContainer<ObjectBindingDeclarations.Binding>>(null, receiveContainer)!,
ctx.receiveNode<JContainer<BindingElement>>(null, receiveContainer)!,
ctx.receiveNode<JLeftPadded<Expression>>(null, receiveLeftPaddedTree)
);
}

if (type === "org.openrewrite.javascript.tree.JS$ObjectBindingDeclarations$Binding") {
return new ObjectBindingDeclarations.Binding(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<JRightPadded<Java.Identifier>>(null, receiveRightPaddedTree),
ctx.receiveNode<TypedTree>(null, ctx.receiveTree)!,
ctx.receiveNodes(null, leftPaddedNodeReceiver(Space))!,
ctx.receiveNode(null, receiveSpace),
ctx.receiveNode<JLeftPadded<Expression>>(null, receiveLeftPaddedTree),
ctx.receiveValue(null, ValueType.Object)
);
}

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

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

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

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
35 changes: 21 additions & 14 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, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, 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, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSMethodInvocation, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -165,19 +165,6 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return objectBindingDeclarations;
}

public visitBinding(binding: ObjectBindingDeclarations.Binding, ctx: SenderContext): J {
ctx.sendValue(binding, v => v.id, ValueType.UUID);
ctx.sendNode(binding, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(binding, v => v.markers, ctx.sendMarkers);
ctx.sendNode(binding, v => v.padding.propertyName, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(binding, v => v.name, ctx.sendTree);
ctx.sendNodes(binding, v => v.dimensionsAfterName, Visitor.sendLeftPadded(ValueType.Object), t => t);
ctx.sendNode(binding, v => v.afterVararg, Visitor.sendSpace);
ctx.sendNode(binding, v => v.padding.initializer, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendTypedValue(binding, v => v.variableType, ValueType.Object);
return binding;
}

public visitPropertyAssignment(propertyAssignment: PropertyAssignment, ctx: SenderContext): J {
ctx.sendValue(propertyAssignment, v => v.id, ValueType.UUID);
ctx.sendNode(propertyAssignment, v => v.prefix, Visitor.sendSpace);
Expand Down Expand Up @@ -451,6 +438,26 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return indexSignatureDeclaration;
}

public visitArrayBindingPattern(arrayBindingPattern: ArrayBindingPattern, ctx: SenderContext): J {
ctx.sendValue(arrayBindingPattern, v => v.id, ValueType.UUID);
ctx.sendNode(arrayBindingPattern, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(arrayBindingPattern, v => v.markers, ctx.sendMarkers);
ctx.sendNode(arrayBindingPattern, v => v.padding.elements, Visitor.sendContainer(ValueType.Tree));
ctx.sendTypedValue(arrayBindingPattern, v => v.type, ValueType.Object);
return arrayBindingPattern;
}

public visitBindingElement(bindingElement: BindingElement, ctx: SenderContext): J {
ctx.sendValue(bindingElement, v => v.id, ValueType.UUID);
ctx.sendNode(bindingElement, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(bindingElement, v => v.markers, ctx.sendMarkers);
ctx.sendNode(bindingElement, v => v.padding.propertyName, Visitor.sendRightPadded(ValueType.Tree));
ctx.sendNode(bindingElement, v => v.name, ctx.sendTree);
ctx.sendNode(bindingElement, v => v.padding.initializer, Visitor.sendLeftPadded(ValueType.Tree));
ctx.sendTypedValue(bindingElement, v => v.variableType, ValueType.Object);
return bindingElement;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: SenderContext): J {
ctx.sendValue(annotatedType, v => v.id, ValueType.UUID);
ctx.sendNode(annotatedType, v => v.prefix, Visitor.sendSpace);
Expand Down
6 changes: 3 additions & 3 deletions openrewrite/src/javascript/tree/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {J, JavaType} from "../../java";
import {Alias, ObjectBindingDeclarations, TypeOperator, Void} from "./tree";
import {Alias, BindingElement, ObjectBindingDeclarations, TypeOperator, Void} from "./tree";
import {ExpressionStatement, StatementExpression} from "./support_types";
import * as java_extensions from "../../java/tree/extensions";

Expand All @@ -10,7 +10,7 @@ export function getJavaType<T extends J>(expr: T): JavaType | null {
return expr.expression.type;
} else if (expr instanceof ObjectBindingDeclarations) {
return expr.typeExpression != null ? expr.typeExpression.type : null;
} else if (expr instanceof ObjectBindingDeclarations.Binding) {
} else if (expr instanceof BindingElement) {
return expr.variableType != null ? expr.variableType.type : null;
} else if (expr instanceof StatementExpression) {
return null;
Expand All @@ -29,7 +29,7 @@ export function withJavaType<T>(expr: T, type: JavaType): T {
return expr.withExpression(expr.expression.withType(type)) as T;
} else if (expr instanceof ObjectBindingDeclarations) {
return (expr.typeExpression != null ? expr.withTypeExpression(expr.typeExpression.withType(type)) : null) as T;
} else if (expr instanceof ObjectBindingDeclarations.Binding) {
} else if (expr instanceof BindingElement) {
return (expr.variableType != null ? expr.withVariableType(expr.variableType.withType(type)) : null) as T;
} else if (expr instanceof StatementExpression) {
return expr as T;
Expand Down
7 changes: 5 additions & 2 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export namespace JsSpace {
JS_IMPORT_SPECIFIER_PREFIX,
JS_IMPORT_SPECIFIER_IMPORT_TYPE_PREFIX,
OBJECT_BINDING_DECLARATIONS_BINDING_AFTER_VARARG,
OBJECT_BINDING_DECLARATIONS_BINDING_PREFIX,
BINDING_ELEMENT_PREFIX,
OBJECT_BINDING_DECLARATIONS_PREFIX,
PROPERTY_ASSIGNMENT_PREFIX,
SCOPED_VARIABLE_DECLARATIONS_PREFIX,
Expand Down Expand Up @@ -236,6 +236,7 @@ export namespace JsSpace {
JSFOR_IN_LOOP_PREFIX,
JSFOR_IN_OF_LOOP_CONTROL_PREFIX,
TYPE_QUERY_PREFIX,
ARRAY_BINDING_PATTERN_PREFIX,
}
}
export namespace JsLeftPadded {
Expand All @@ -259,14 +260,15 @@ export namespace JsLeftPadded {
JS_IMPORT_SPECIFIER_IMPORT_TYPE,
INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION,
JSFOR_OF_LOOP_AWAIT,
BINDING_ELEMENT_INITIALIZER,
}
}
export namespace JsRightPadded {
export enum Location {
ALIAS_PROPERTY_NAME,
COMPILATION_UNIT_STATEMENTS,
JS_IMPORT_NAME,
OBJECT_BINDING_DECLARATIONS_BINDING_PROPERTY_NAME,
BINDING_ELEMENT_PROPERTY_NAME,
PROPERTY_ASSIGNMENT_NAME,
SCOPED_VARIABLE_DECLARATIONS_VARIABLES,
TEMPLATE_EXPRESSION_TAG,
Expand Down Expand Up @@ -298,5 +300,6 @@ export namespace JsContainer {
JSMETHOD_INVOCATION_ARGUMENTS,
TYPE_LITERAL_MEMBERS,
INDEX_SIGNATURE_DECLARATION_PARAMETERS,
ARRAY_BINDING_PATTERN_ELEMENTS,
}
}
Loading

0 comments on commit da44db9

Please sign in to comment.