From da44db99a4fadb7cdae742ed2e964ada4fbab5f2 Mon Sep 17 00:00:00 2001 From: Oleh Dokuka <5380167+OlegDokuka@users.noreply.github.com> Date: Fri, 22 Nov 2024 21:57:27 +0200 Subject: [PATCH] add array binding pattern (#157) * add array binding pattern * add fixes --- openrewrite/src/javascript/parser.ts | 14 +- openrewrite/src/javascript/remote/receiver.ts | 73 ++-- openrewrite/src/javascript/remote/sender.ts | 35 +- openrewrite/src/javascript/tree/extensions.ts | 6 +- .../src/javascript/tree/support_types.ts | 7 +- openrewrite/src/javascript/tree/tree.ts | 346 ++++++++++-------- openrewrite/src/javascript/visitor.ts | 47 ++- .../e2e/google-maps-services-js_files.test.ts | 3 +- .../parser/arrayBindingPattern.test.ts | 59 +++ .../parser/variableDeclarations.test.ts | 13 + .../javascript/remote/JavaScriptReceiver.java | 72 ++-- .../javascript/remote/JavaScriptSender.java | 36 +- .../remote/JavaScriptValidator.java | 24 +- .../javascript/JavaScriptIsoVisitor.java | 4 +- .../javascript/JavaScriptVisitor.java | 32 +- .../internal/JavaScriptPrinter.java | 30 +- .../org/openrewrite/javascript/tree/JS.java | 323 +++++++++------- .../javascript/tree/JsContainer.java | 3 +- .../javascript/tree/JsLeftPadded.java | 2 +- .../javascript/tree/JsRightPadded.java | 7 +- .../openrewrite/javascript/tree/JsSpace.java | 9 +- 21 files changed, 721 insertions(+), 424 deletions(-) create mode 100644 openrewrite/test/javascript/parser/arrayBindingPattern.test.ts diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index d883e465..1aeb2899 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -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, @@ -1380,8 +1386,6 @@ export class JavaScriptParserVisitor { this.convert(node.name), null ) : this.convert(node.name), - [], - null, node.initializer ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsToken)!), this.convert(node.initializer)) : null, this.mapVariableType(node), ); @@ -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) { diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index 22059f3f..867a07d7 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -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"; @@ -170,19 +170,6 @@ class Visitor extends JavaScriptVisitor { 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)!); @@ -456,6 +443,26 @@ class Visitor extends JavaScriptVisitor { 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)!); @@ -1277,25 +1284,11 @@ class Factory implements ReceiverFactory { ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveNode(null, ctx.receiveTree), - ctx.receiveNode>(null, receiveContainer)!, + ctx.receiveNode>(null, receiveContainer)!, ctx.receiveNode>(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>(null, receiveRightPaddedTree), - ctx.receiveNode(null, ctx.receiveTree)!, - ctx.receiveNodes(null, leftPaddedNodeReceiver(Space))!, - ctx.receiveNode(null, receiveSpace), - ctx.receiveNode>(null, receiveLeftPaddedTree), - ctx.receiveValue(null, ValueType.Object) - ); - } - if (type === "org.openrewrite.javascript.tree.JS$PropertyAssignment") { return new PropertyAssignment( ctx.receiveValue(null, ValueType.UUID)!, @@ -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>(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>(null, receiveRightPaddedTree), + ctx.receiveNode(null, ctx.receiveTree)!, + ctx.receiveNode>(null, receiveLeftPaddedTree), + ctx.receiveValue(null, ValueType.Object) + ); + } + if (type === "org.openrewrite.java.tree.J$AnnotatedType") { return new Java.AnnotatedType( ctx.receiveValue(null, ValueType.UUID)!, diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index f72ff2fd..51221b73 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -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"; @@ -165,19 +165,6 @@ class Visitor extends JavaScriptVisitor { 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); @@ -451,6 +438,26 @@ class Visitor extends JavaScriptVisitor { 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); diff --git a/openrewrite/src/javascript/tree/extensions.ts b/openrewrite/src/javascript/tree/extensions.ts index d877c66c..03fdbf2b 100644 --- a/openrewrite/src/javascript/tree/extensions.ts +++ b/openrewrite/src/javascript/tree/extensions.ts @@ -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"; @@ -10,7 +10,7 @@ export function getJavaType(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; @@ -29,7 +29,7 @@ export function withJavaType(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; diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index fc72ccab..fe0854a7 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -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, @@ -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 { @@ -259,6 +260,7 @@ export namespace JsLeftPadded { JS_IMPORT_SPECIFIER_IMPORT_TYPE, INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION, JSFOR_OF_LOOP_AWAIT, + BINDING_ELEMENT_INITIALIZER, } } export namespace JsRightPadded { @@ -266,7 +268,7 @@ export namespace JsRightPadded { 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, @@ -298,5 +300,6 @@ export namespace JsContainer { JSMETHOD_INVOCATION_ARGUMENTS, TYPE_LITERAL_MEMBERS, INDEX_SIGNATURE_DECLARATION_PARAMETERS, + ARRAY_BINDING_PATTERN_ELEMENTS, } } diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index dcf142d5..977f9f2c 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -1176,7 +1176,7 @@ export namespace JsBinary { @LstType("org.openrewrite.javascript.tree.JS$ObjectBindingDeclarations") export class ObjectBindingDeclarations extends JSMixin(Object) implements Expression, TypedTree { - public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeExpression: TypeTree | null, bindings: JContainer, initializer: JLeftPadded | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeExpression: TypeTree | null, bindings: JContainer, initializer: JLeftPadded | null) { super(); this._id = id; this._prefix = prefix; @@ -1248,13 +1248,13 @@ export class ObjectBindingDeclarations extends JSMixin(Object) implements Expres return typeExpression === this._typeExpression ? this : new ObjectBindingDeclarations(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, typeExpression, this._bindings, this._initializer); } - private readonly _bindings: JContainer; + private readonly _bindings: JContainer; - public get bindings(): ObjectBindingDeclarations.Binding[] { + public get bindings(): BindingElement[] { return this._bindings.elements; } - public withBindings(bindings: ObjectBindingDeclarations.Binding[]): ObjectBindingDeclarations { + public withBindings(bindings: BindingElement[]): ObjectBindingDeclarations { return this.padding.withBindings(JContainer.withElements(this._bindings, bindings)); } @@ -1283,10 +1283,10 @@ export class ObjectBindingDeclarations extends JSMixin(Object) implements Expres get padding() { const t = this; return new class { - public get bindings(): JContainer { + public get bindings(): JContainer { return t._bindings; } - public withBindings(bindings: JContainer): ObjectBindingDeclarations { + public withBindings(bindings: JContainer): ObjectBindingDeclarations { return t._bindings === bindings ? t : new ObjectBindingDeclarations(t._id, t._prefix, t._markers, t._leadingAnnotations, t._modifiers, t._typeExpression, bindings, t._initializer); } public get initializer(): JLeftPadded | null { @@ -1300,146 +1300,6 @@ export class ObjectBindingDeclarations extends JSMixin(Object) implements Expres } -export namespace ObjectBindingDeclarations { - @LstType("org.openrewrite.javascript.tree.JS$ObjectBindingDeclarations$Binding") - export class Binding extends JSMixin(Object) implements NameTree { - public constructor(id: UUID, prefix: Space, markers: Markers, propertyName: JRightPadded | null, name: TypedTree, dimensionsAfterName: JLeftPadded[], afterVararg: Space | null, initializer: JLeftPadded | null, variableType: JavaType.Variable | null) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._propertyName = propertyName; - this._name = name; - this._dimensionsAfterName = dimensionsAfterName; - this._afterVararg = afterVararg; - this._initializer = initializer; - this._variableType = variableType; - } - - private readonly _id: UUID; - - public get id(): UUID { - return this._id; - } - - public withId(id: UUID): ObjectBindingDeclarations.Binding { - return id === this._id ? this : new ObjectBindingDeclarations.Binding(id, this._prefix, this._markers, this._propertyName, this._name, this._dimensionsAfterName, this._afterVararg, this._initializer, this._variableType); - } - - private readonly _prefix: Space; - - public get prefix(): Space { - return this._prefix; - } - - public withPrefix(prefix: Space): ObjectBindingDeclarations.Binding { - return prefix === this._prefix ? this : new ObjectBindingDeclarations.Binding(this._id, prefix, this._markers, this._propertyName, this._name, this._dimensionsAfterName, this._afterVararg, this._initializer, this._variableType); - } - - private readonly _markers: Markers; - - public get markers(): Markers { - return this._markers; - } - - public withMarkers(markers: Markers): ObjectBindingDeclarations.Binding { - return markers === this._markers ? this : new ObjectBindingDeclarations.Binding(this._id, this._prefix, markers, this._propertyName, this._name, this._dimensionsAfterName, this._afterVararg, this._initializer, this._variableType); - } - - private readonly _propertyName: JRightPadded | null; - - public get propertyName(): Java.Identifier | null { - return this._propertyName === null ? null : this._propertyName.element; - } - - public withPropertyName(propertyName: Java.Identifier | null): ObjectBindingDeclarations.Binding { - return this.padding.withPropertyName(JRightPadded.withElement(this._propertyName, propertyName)); - } - - private readonly _name: TypedTree; - - public get name(): TypedTree { - return this._name; - } - - public withName(name: TypedTree): ObjectBindingDeclarations.Binding { - return name === this._name ? this : new ObjectBindingDeclarations.Binding(this._id, this._prefix, this._markers, this._propertyName, name, this._dimensionsAfterName, this._afterVararg, this._initializer, this._variableType); - } - - private readonly _dimensionsAfterName: JLeftPadded[]; - - public get dimensionsAfterName(): JLeftPadded[] { - return this._dimensionsAfterName; - } - - public withDimensionsAfterName(dimensionsAfterName: JLeftPadded[]): ObjectBindingDeclarations.Binding { - return dimensionsAfterName === this._dimensionsAfterName ? this : new ObjectBindingDeclarations.Binding(this._id, this._prefix, this._markers, this._propertyName, this._name, dimensionsAfterName, this._afterVararg, this._initializer, this._variableType); - } - - private readonly _afterVararg: Space | null; - - public get afterVararg(): Space | null { - return this._afterVararg; - } - - public withAfterVararg(afterVararg: Space | null): ObjectBindingDeclarations.Binding { - return afterVararg === this._afterVararg ? this : new ObjectBindingDeclarations.Binding(this._id, this._prefix, this._markers, this._propertyName, this._name, this._dimensionsAfterName, afterVararg, this._initializer, this._variableType); - } - - private readonly _initializer: JLeftPadded | null; - - public get initializer(): Expression | null { - return this._initializer === null ? null : this._initializer.element; - } - - public withInitializer(initializer: Expression | null): ObjectBindingDeclarations.Binding { - return this.padding.withInitializer(JLeftPadded.withElement(this._initializer, initializer)); - } - - private readonly _variableType: JavaType.Variable | null; - - public get variableType(): JavaType.Variable | null { - return this._variableType; - } - - public withVariableType(variableType: JavaType.Variable | null): ObjectBindingDeclarations.Binding { - return variableType === this._variableType ? this : new ObjectBindingDeclarations.Binding(this._id, this._prefix, this._markers, this._propertyName, this._name, this._dimensionsAfterName, this._afterVararg, this._initializer, variableType); - } - - public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { - return v.visitBinding(this, p); - } - - public get type(): JavaType | null { - return extensions.getJavaType(this); - } - - public withType(type: JavaType): ObjectBindingDeclarations.Binding { - return extensions.withJavaType(this, type); - } - - get padding() { - const t = this; - return new class { - public get propertyName(): JRightPadded | null { - return t._propertyName; - } - public withPropertyName(propertyName: JRightPadded | null): ObjectBindingDeclarations.Binding { - return t._propertyName === propertyName ? t : new ObjectBindingDeclarations.Binding(t._id, t._prefix, t._markers, propertyName, t._name, t._dimensionsAfterName, t._afterVararg, t._initializer, t._variableType); - } - public get initializer(): JLeftPadded | null { - return t._initializer; - } - public withInitializer(initializer: JLeftPadded | null): ObjectBindingDeclarations.Binding { - return t._initializer === initializer ? t : new ObjectBindingDeclarations.Binding(t._id, t._prefix, t._markers, t._propertyName, t._name, t._dimensionsAfterName, t._afterVararg, initializer, t._variableType); - } - } - } - - } - -} - @LstType("org.openrewrite.javascript.tree.JS$PropertyAssignment") export class PropertyAssignment extends JSMixin(Object) implements Statement, TypedTree { public constructor(id: UUID, prefix: Space, markers: Markers, name: JRightPadded, initializer: Expression | null) { @@ -3942,3 +3802,197 @@ export class IndexSignatureDeclaration extends JSMixin(Object) implements Statem } } + +@LstType("org.openrewrite.javascript.tree.JS$ArrayBindingPattern") +export class ArrayBindingPattern extends JSMixin(Object) implements Expression, TypedTree { + public constructor(id: UUID, prefix: Space, markers: Markers, elements: JContainer, _type: JavaType | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._elements = elements; + this._type = _type; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): ArrayBindingPattern { + return id === this._id ? this : new ArrayBindingPattern(id, this._prefix, this._markers, this._elements, this._type); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): ArrayBindingPattern { + return prefix === this._prefix ? this : new ArrayBindingPattern(this._id, prefix, this._markers, this._elements, this._type); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): ArrayBindingPattern { + return markers === this._markers ? this : new ArrayBindingPattern(this._id, this._prefix, markers, this._elements, this._type); + } + + private readonly _elements: JContainer; + + public get elements(): Expression[] { + return this._elements.elements; + } + + public withElements(elements: Expression[]): ArrayBindingPattern { + return this.padding.withElements(JContainer.withElements(this._elements, elements)); + } + + private readonly _type: JavaType | null; + + public get type(): JavaType | null { + return this._type; + } + + public withType(_type: JavaType | null): ArrayBindingPattern { + return _type === this._type ? this : new ArrayBindingPattern(this._id, this._prefix, this._markers, this._elements, _type); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitArrayBindingPattern(this, p); + } + + get padding() { + const t = this; + return new class { + public get elements(): JContainer { + return t._elements; + } + public withElements(elements: JContainer): ArrayBindingPattern { + return t._elements === elements ? t : new ArrayBindingPattern(t._id, t._prefix, t._markers, elements, t._type); + } + } + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$BindingElement") +export class BindingElement extends JSMixin(Object) implements Statement, Expression, TypeTree { + public constructor(id: UUID, prefix: Space, markers: Markers, propertyName: JRightPadded | null, name: TypedTree, initializer: JLeftPadded | null, variableType: JavaType.Variable | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._propertyName = propertyName; + this._name = name; + this._initializer = initializer; + this._variableType = variableType; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): BindingElement { + return id === this._id ? this : new BindingElement(id, this._prefix, this._markers, this._propertyName, this._name, this._initializer, this._variableType); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): BindingElement { + return prefix === this._prefix ? this : new BindingElement(this._id, prefix, this._markers, this._propertyName, this._name, this._initializer, this._variableType); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): BindingElement { + return markers === this._markers ? this : new BindingElement(this._id, this._prefix, markers, this._propertyName, this._name, this._initializer, this._variableType); + } + + private readonly _propertyName: JRightPadded | null; + + public get propertyName(): Expression | null { + return this._propertyName === null ? null : this._propertyName.element; + } + + public withPropertyName(propertyName: Expression | null): BindingElement { + return this.padding.withPropertyName(JRightPadded.withElement(this._propertyName, propertyName)); + } + + private readonly _name: TypedTree; + + public get name(): TypedTree { + return this._name; + } + + public withName(name: TypedTree): BindingElement { + return name === this._name ? this : new BindingElement(this._id, this._prefix, this._markers, this._propertyName, name, this._initializer, this._variableType); + } + + private readonly _initializer: JLeftPadded | null; + + public get initializer(): Expression | null { + return this._initializer === null ? null : this._initializer.element; + } + + public withInitializer(initializer: Expression | null): BindingElement { + return this.padding.withInitializer(JLeftPadded.withElement(this._initializer, initializer)); + } + + private readonly _variableType: JavaType.Variable | null; + + public get variableType(): JavaType.Variable | null { + return this._variableType; + } + + public withVariableType(variableType: JavaType.Variable | null): BindingElement { + return variableType === this._variableType ? this : new BindingElement(this._id, this._prefix, this._markers, this._propertyName, this._name, this._initializer, variableType); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitBindingElement(this, p); + } + + public get type(): JavaType | null { + return extensions.getJavaType(this); + } + + public withType(type: JavaType): BindingElement { + return extensions.withJavaType(this, type); + } + + get padding() { + const t = this; + return new class { + public get propertyName(): JRightPadded | null { + return t._propertyName; + } + public withPropertyName(propertyName: JRightPadded | null): BindingElement { + return t._propertyName === propertyName ? t : new BindingElement(t._id, t._prefix, t._markers, propertyName, t._name, t._initializer, t._variableType); + } + public get initializer(): JLeftPadded | null { + return t._initializer; + } + public withInitializer(initializer: JLeftPadded | null): BindingElement { + return t._initializer === initializer ? t : new BindingElement(t._id, t._prefix, t._markers, t._propertyName, t._name, initializer, t._variableType); + } + } + } + +} diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index cea5020d..179a1af2 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -1,7 +1,7 @@ import * as extensions from "./extensions"; import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core"; import {JS, isJavaScript, JsLeftPadded, JsRightPadded, JsContainer, JsSpace} from "./tree"; -import {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 {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/tree"; import {JavaVisitor} from "../java"; import * as Java from "../java/tree"; @@ -207,17 +207,6 @@ export class JavaScriptVisitor

extends JavaVisitor

{ return objectBindingDeclarations; } - public visitBinding(binding: ObjectBindingDeclarations.Binding, p: P): J | null { - binding = binding.withPrefix(this.visitJsSpace(binding.prefix, JsSpace.Location.OBJECT_BINDING_DECLARATIONS_BINDING_PREFIX, p)!); - binding = binding.withMarkers(this.visitMarkers(binding.markers, p)); - binding = binding.padding.withPropertyName(this.visitJsRightPadded(binding.padding.propertyName, JsRightPadded.Location.OBJECT_BINDING_DECLARATIONS_BINDING_PROPERTY_NAME, p)); - binding = binding.withName(this.visitAndCast(binding.name, p)!); - binding = binding.withDimensionsAfterName(ListUtils.map(binding.dimensionsAfterName, el => this.visitJsLeftPadded(el, JsLeftPadded.Location.OBJECT_BINDING_DECLARATIONS_BINDING_DIMENSIONS_AFTER_NAME, p))); - binding = binding.withAfterVararg(this.visitJsSpace(binding.afterVararg, JsSpace.Location.OBJECT_BINDING_DECLARATIONS_BINDING_AFTER_VARARG, p)); - binding = binding.padding.withInitializer(this.visitJsLeftPadded(binding.padding.initializer, JsLeftPadded.Location.OBJECT_BINDING_DECLARATIONS_BINDING_INITIALIZER, p)); - return binding; - } - public visitPropertyAssignment(propertyAssignment: PropertyAssignment, p: P): J | null { propertyAssignment = propertyAssignment.withPrefix(this.visitJsSpace(propertyAssignment.prefix, JsSpace.Location.PROPERTY_ASSIGNMENT_PREFIX, p)!); let tempStatement = this.visitStatement(propertyAssignment, p) as Statement; @@ -613,6 +602,40 @@ export class JavaScriptVisitor

extends JavaVisitor

{ return indexSignatureDeclaration; } + public visitArrayBindingPattern(arrayBindingPattern: ArrayBindingPattern, p: P): J | null { + arrayBindingPattern = arrayBindingPattern.withPrefix(this.visitJsSpace(arrayBindingPattern.prefix, JsSpace.Location.ARRAY_BINDING_PATTERN_PREFIX, p)!); + let tempExpression = this.visitExpression(arrayBindingPattern, p) as Expression; + if (!(tempExpression instanceof ArrayBindingPattern)) + { + return tempExpression; + } + arrayBindingPattern = tempExpression as ArrayBindingPattern; + arrayBindingPattern = arrayBindingPattern.withMarkers(this.visitMarkers(arrayBindingPattern.markers, p)); + arrayBindingPattern = arrayBindingPattern.padding.withElements(this.visitJsContainer(arrayBindingPattern.padding.elements, JsContainer.Location.ARRAY_BINDING_PATTERN_ELEMENTS, p)!); + return arrayBindingPattern; + } + + public visitBindingElement(bindingElement: BindingElement, p: P): J | null { + bindingElement = bindingElement.withPrefix(this.visitJsSpace(bindingElement.prefix, JsSpace.Location.BINDING_ELEMENT_PREFIX, p)!); + let tempStatement = this.visitStatement(bindingElement, p) as Statement; + if (!(tempStatement instanceof BindingElement)) + { + return tempStatement; + } + bindingElement = tempStatement as BindingElement; + let tempExpression = this.visitExpression(bindingElement, p) as Expression; + if (!(tempExpression instanceof BindingElement)) + { + return tempExpression; + } + bindingElement = tempExpression as BindingElement; + bindingElement = bindingElement.withMarkers(this.visitMarkers(bindingElement.markers, p)); + bindingElement = bindingElement.padding.withPropertyName(this.visitJsRightPadded(bindingElement.padding.propertyName, JsRightPadded.Location.BINDING_ELEMENT_PROPERTY_NAME, p)); + bindingElement = bindingElement.withName(this.visitAndCast(bindingElement.name, p)!); + bindingElement = bindingElement.padding.withInitializer(this.visitJsLeftPadded(bindingElement.padding.initializer, JsLeftPadded.Location.BINDING_ELEMENT_INITIALIZER, p)); + return bindingElement; + } + public visitJsLeftPadded(left: JLeftPadded | null, loc: JsLeftPadded.Location, p: P): JLeftPadded { return extensions.visitJsLeftPadded(this, left, loc, p); } diff --git a/openrewrite/test/javascript/e2e/google-maps-services-js_files.test.ts b/openrewrite/test/javascript/e2e/google-maps-services-js_files.test.ts index 8208514d..40b8dd67 100644 --- a/openrewrite/test/javascript/e2e/google-maps-services-js_files.test.ts +++ b/openrewrite/test/javascript/e2e/google-maps-services-js_files.test.ts @@ -119,8 +119,7 @@ describe('google-maps-services-js files tests', () => { }); test('reversegeocode.ts', () => { - rewriteRunWithOptions( - {expectUnknowns : false}, + rewriteRun( //language=typescript typeScript(` /** diff --git a/openrewrite/test/javascript/parser/arrayBindingPattern.test.ts b/openrewrite/test/javascript/parser/arrayBindingPattern.test.ts new file mode 100644 index 00000000..d21adb10 --- /dev/null +++ b/openrewrite/test/javascript/parser/arrayBindingPattern.test.ts @@ -0,0 +1,59 @@ +import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; + +describe('array binding pattern', () => { + beforeAll(() => connect()); + afterAll(() => disconnect()); + + test('empty', () => { + rewriteRun( + //language=typescript + typeScript(` + const [ ] = [10, 20, 30, 40, 50]; + `) + ); + }); + + test('simple destructuring', () => { + rewriteRun( + //language=typescript + typeScript(` + const [a, b, ...rest] = [10, 20, 30, 40, 50]; + `) + ); + }); + + test('advanced destructuring', () => { + rewriteRun( + //language=typescript + typeScript(` + const aDefault = ""; + const array = []; + const /*1*/ [/*2*/a/*3*/, /*4*/b/*5*/, /*6*/] /*7*/=/*8*/ array/*9*/;/*10*/ + const [a1/*12*/, /*11*/,/*3*/ b1] = array; + const [/*16*/a2 /*14*/=/*15*/ aDefault/*17*/, b2/*18*/] = array; + const [/*21*/a3, /*22*/b3, /*20*/... /*19*/rest/*23*/] = array; + const [a4, , b4, ...rest1] = array; + const [a5, b5, /*26*/.../*25*/{/*27*/ pop/*28*/,/*29*/ push /*30*/, /*31*/}] = array; + const [a6, b6, /*33*/.../*32*/[/*34*/c, d]/*35*/] = array; + `) + ); + }); + + test('destructuring with existing variables', () => { + rewriteRun( + //language=typescript + typeScript(` + let aDefault = 1; + let array = []; + let a, b, a1, b1, c, d, rest, pop, push; + [a, b] = array; + [a, , b] = array; + [a = aDefault, b] = array; + [a, b, ...rest] = array; + [a, , b, ...rest] = array; + [a, b, ...{ pop, push }] = array; + [a, b, ...[c, d]] = array; + `) + ); + }); +}); diff --git a/openrewrite/test/javascript/parser/variableDeclarations.test.ts b/openrewrite/test/javascript/parser/variableDeclarations.test.ts index 12ff1bdf..4106e5dd 100644 --- a/openrewrite/test/javascript/parser/variableDeclarations.test.ts +++ b/openrewrite/test/javascript/parser/variableDeclarations.test.ts @@ -115,6 +115,19 @@ describe('variable declaration mapping', () => { } = {} `), //language=typescript + typeScript(` + const key = "test"; + const obj = {}; + const aDefault = {}; + const bDefault = {}; + const { x, y } = obj; + const { a: a1, b: b1 } = obj; + const { z: a2 = aDefault, f = bDefault } = obj; + const { c, k, ...rest } = obj; + const { re: a12, rb: b12, ...rest1 } = obj; + const { [key]: a } = obj; + `), + //language=typescript typeScript(` /*1*/ const /*2*/ { /*3*/ a/*4*/ :/*5*/ aa /*6*/ = /*7*/ 10 /*8*/ , /*9*/ b /*10*/ : /*11*/ bb = { } /*12*/ , /*13*/ } = { a: 3 }; `) diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java index 611b1b18..f4d92c46 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java @@ -229,20 +229,6 @@ public JS.ObjectBindingDeclarations visitObjectBindingDeclarations(JS.ObjectBind return objectBindingDeclarations; } - @Override - public JS.ObjectBindingDeclarations.Binding visitBinding(JS.ObjectBindingDeclarations.Binding binding, ReceiverContext ctx) { - binding = binding.withId(ctx.receiveNonNullValue(binding.getId(), UUID.class)); - binding = binding.withPrefix(ctx.receiveNonNullNode(binding.getPrefix(), JavaScriptReceiver::receiveSpace)); - binding = binding.withMarkers(ctx.receiveNonNullNode(binding.getMarkers(), ctx::receiveMarkers)); - binding = binding.getPadding().withPropertyName(ctx.receiveNode(binding.getPadding().getPropertyName(), JavaScriptReceiver::receiveRightPaddedTree)); - binding = binding.withName(ctx.receiveNonNullNode(binding.getName(), ctx::receiveTree)); - binding = binding.withDimensionsAfterName(ctx.receiveNonNullNodes(binding.getDimensionsAfterName(), leftPaddedNodeReceiver(org.openrewrite.java.tree.Space.class))); - binding = binding.withAfterVararg(ctx.receiveNode(binding.getAfterVararg(), JavaScriptReceiver::receiveSpace)); - binding = binding.getPadding().withInitializer(ctx.receiveNode(binding.getPadding().getInitializer(), JavaScriptReceiver::receiveLeftPaddedTree)); - binding = binding.withVariableType(ctx.receiveValue(binding.getVariableType(), JavaType.Variable.class)); - return binding; - } - @Override public JS.PropertyAssignment visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, ReceiverContext ctx) { propertyAssignment = propertyAssignment.withId(ctx.receiveNonNullValue(propertyAssignment.getId(), UUID.class)); @@ -543,6 +529,28 @@ public JS.IndexSignatureDeclaration visitIndexSignatureDeclaration(JS.IndexSigna return indexSignatureDeclaration; } + @Override + public JS.ArrayBindingPattern visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, ReceiverContext ctx) { + arrayBindingPattern = arrayBindingPattern.withId(ctx.receiveNonNullValue(arrayBindingPattern.getId(), UUID.class)); + arrayBindingPattern = arrayBindingPattern.withPrefix(ctx.receiveNonNullNode(arrayBindingPattern.getPrefix(), JavaScriptReceiver::receiveSpace)); + arrayBindingPattern = arrayBindingPattern.withMarkers(ctx.receiveNonNullNode(arrayBindingPattern.getMarkers(), ctx::receiveMarkers)); + arrayBindingPattern = arrayBindingPattern.getPadding().withElements(ctx.receiveNonNullNode(arrayBindingPattern.getPadding().getElements(), JavaScriptReceiver::receiveContainer)); + arrayBindingPattern = arrayBindingPattern.withType(ctx.receiveValue(arrayBindingPattern.getType(), JavaType.class)); + return arrayBindingPattern; + } + + @Override + public JS.BindingElement visitBindingElement(JS.BindingElement bindingElement, ReceiverContext ctx) { + bindingElement = bindingElement.withId(ctx.receiveNonNullValue(bindingElement.getId(), UUID.class)); + bindingElement = bindingElement.withPrefix(ctx.receiveNonNullNode(bindingElement.getPrefix(), JavaScriptReceiver::receiveSpace)); + bindingElement = bindingElement.withMarkers(ctx.receiveNonNullNode(bindingElement.getMarkers(), ctx::receiveMarkers)); + bindingElement = bindingElement.getPadding().withPropertyName(ctx.receiveNode(bindingElement.getPadding().getPropertyName(), JavaScriptReceiver::receiveRightPaddedTree)); + bindingElement = bindingElement.withName(ctx.receiveNonNullNode(bindingElement.getName(), ctx::receiveTree)); + bindingElement = bindingElement.getPadding().withInitializer(ctx.receiveNode(bindingElement.getPadding().getInitializer(), JavaScriptReceiver::receiveLeftPaddedTree)); + bindingElement = bindingElement.withVariableType(ctx.receiveValue(bindingElement.getVariableType(), JavaType.Variable.class)); + return bindingElement; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class)); @@ -1399,20 +1407,6 @@ public T create(Class type, ReceiverContext ctx) { ); } - if (type == JS.ObjectBindingDeclarations.Binding.class) { - return (T) new JS.ObjectBindingDeclarations.Binding( - ctx.receiveNonNullValue(null, UUID.class), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNode(null, JavaScriptReceiver::receiveRightPaddedTree), - ctx.receiveNonNullNode(null, ctx::receiveTree), - ctx.receiveNonNullNodes(null, leftPaddedNodeReceiver(org.openrewrite.java.tree.Space.class)), - ctx.receiveNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNode(null, JavaScriptReceiver::receiveLeftPaddedTree), - ctx.receiveValue(null, JavaType.Variable.class) - ); - } - if (type == JS.PropertyAssignment.class) { return (T) new JS.PropertyAssignment( ctx.receiveNonNullValue(null, UUID.class), @@ -1713,6 +1707,28 @@ public T create(Class type, ReceiverContext ctx) { ); } + if (type == JS.ArrayBindingPattern.class) { + return (T) new JS.ArrayBindingPattern( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer), + ctx.receiveValue(null, JavaType.class) + ); + } + + if (type == JS.BindingElement.class) { + return (T) new JS.BindingElement( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNode(null, JavaScriptReceiver::receiveRightPaddedTree), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNode(null, JavaScriptReceiver::receiveLeftPaddedTree), + ctx.receiveValue(null, JavaType.Variable.class) + ); + } + if (type == J.AnnotatedType.class) { return (T) new J.AnnotatedType( ctx.receiveNonNullValue(null, UUID.class), diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java index f5ef8f4c..32d88967 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java @@ -212,20 +212,6 @@ public JS.ObjectBindingDeclarations visitObjectBindingDeclarations(JS.ObjectBind return objectBindingDeclarations; } - @Override - public JS.ObjectBindingDeclarations.Binding visitBinding(JS.ObjectBindingDeclarations.Binding binding, SenderContext ctx) { - ctx.sendValue(binding, JS.ObjectBindingDeclarations.Binding::getId); - ctx.sendNode(binding, JS.ObjectBindingDeclarations.Binding::getPrefix, JavaScriptSender::sendSpace); - ctx.sendNode(binding, JS.ObjectBindingDeclarations.Binding::getMarkers, ctx::sendMarkers); - ctx.sendNode(binding, e -> e.getPadding().getPropertyName(), JavaScriptSender::sendRightPadded); - ctx.sendNode(binding, JS.ObjectBindingDeclarations.Binding::getName, ctx::sendTree); - ctx.sendNodes(binding, JS.ObjectBindingDeclarations.Binding::getDimensionsAfterName, JavaScriptSender::sendLeftPadded, Function.identity()); - ctx.sendNode(binding, JS.ObjectBindingDeclarations.Binding::getAfterVararg, JavaScriptSender::sendSpace); - ctx.sendNode(binding, e -> e.getPadding().getInitializer(), JavaScriptSender::sendLeftPadded); - ctx.sendTypedValue(binding, JS.ObjectBindingDeclarations.Binding::getVariableType); - return binding; - } - @Override public JS.PropertyAssignment visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, SenderContext ctx) { ctx.sendValue(propertyAssignment, JS.PropertyAssignment::getId); @@ -526,6 +512,28 @@ public JS.IndexSignatureDeclaration visitIndexSignatureDeclaration(JS.IndexSigna return indexSignatureDeclaration; } + @Override + public JS.ArrayBindingPattern visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, SenderContext ctx) { + ctx.sendValue(arrayBindingPattern, JS.ArrayBindingPattern::getId); + ctx.sendNode(arrayBindingPattern, JS.ArrayBindingPattern::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(arrayBindingPattern, JS.ArrayBindingPattern::getMarkers, ctx::sendMarkers); + ctx.sendNode(arrayBindingPattern, e -> e.getPadding().getElements(), JavaScriptSender::sendContainer); + ctx.sendTypedValue(arrayBindingPattern, JS.ArrayBindingPattern::getType); + return arrayBindingPattern; + } + + @Override + public JS.BindingElement visitBindingElement(JS.BindingElement bindingElement, SenderContext ctx) { + ctx.sendValue(bindingElement, JS.BindingElement::getId); + ctx.sendNode(bindingElement, JS.BindingElement::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(bindingElement, JS.BindingElement::getMarkers, ctx::sendMarkers); + ctx.sendNode(bindingElement, e -> e.getPadding().getPropertyName(), JavaScriptSender::sendRightPadded); + ctx.sendNode(bindingElement, JS.BindingElement::getName, ctx::sendTree); + ctx.sendNode(bindingElement, e -> e.getPadding().getInitializer(), JavaScriptSender::sendLeftPadded); + ctx.sendTypedValue(bindingElement, JS.BindingElement::getVariableType); + return bindingElement; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.sendValue(annotatedType, J.AnnotatedType::getId); diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java index 5ffe373a..621c4e24 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java @@ -140,19 +140,11 @@ public JS.ObjectBindingDeclarations visitObjectBindingDeclarations(JS.ObjectBind ListUtils.map(objectBindingDeclarations.getLeadingAnnotations(), el -> visitAndValidate(el, J.Annotation.class, p)); ListUtils.map(objectBindingDeclarations.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p)); visitAndValidate(objectBindingDeclarations.getTypeExpression(), TypeTree.class, p); - visitAndValidate(objectBindingDeclarations.getBindings(), JS.ObjectBindingDeclarations.Binding.class, p); + visitAndValidate(objectBindingDeclarations.getBindings(), JS.BindingElement.class, p); visitAndValidate(objectBindingDeclarations.getInitializer(), Expression.class, p); return objectBindingDeclarations; } - @Override - public JS.ObjectBindingDeclarations.Binding visitBinding(JS.ObjectBindingDeclarations.Binding binding, P p) { - visitAndValidate(binding.getPropertyName(), J.Identifier.class, p); - visitAndValidate(binding.getName(), TypedTree.class, p); - visitAndValidate(binding.getInitializer(), Expression.class, p); - return binding; - } - @Override public JS.PropertyAssignment visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, P p) { visitAndValidate(propertyAssignment.getName(), Expression.class, p); @@ -348,6 +340,20 @@ public JS.IndexSignatureDeclaration visitIndexSignatureDeclaration(JS.IndexSigna return indexSignatureDeclaration; } + @Override + public JS.ArrayBindingPattern visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, P p) { + visitAndValidate(arrayBindingPattern.getElements(), Expression.class, p); + return arrayBindingPattern; + } + + @Override + public JS.BindingElement visitBindingElement(JS.BindingElement bindingElement, P p) { + visitAndValidate(bindingElement.getPropertyName(), Expression.class, p); + visitAndValidate(bindingElement.getName(), TypedTree.class, p); + visitAndValidate(bindingElement.getInitializer(), Expression.class, p); + return bindingElement; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, P p) { ListUtils.map(annotatedType.getAnnotations(), el -> visitAndValidate(el, J.Annotation.class, p)); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptIsoVisitor.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptIsoVisitor.java index ad1c02c5..7571b1f0 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptIsoVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptIsoVisitor.java @@ -44,8 +44,8 @@ public JS.Await visitAwait(JS.Await await, P p) { } @Override - public JS.ObjectBindingDeclarations.Binding visitBinding(JS.ObjectBindingDeclarations.Binding binding, P p) { - return (JS.ObjectBindingDeclarations.Binding) super.visitBinding(binding, p); + public JS.BindingElement visitBindingElement(JS.BindingElement binding, P p) { + return (JS.BindingElement) super.visitBindingElement(binding, p); } @Override diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java index c01effa3..220e942b 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -124,21 +124,15 @@ public J visitAwait(JS.Await await, P p) { return a; } - public J visitBinding(JS.ObjectBindingDeclarations.Binding binding, P p) { - JS.ObjectBindingDeclarations.Binding b = binding; - b = b.withPrefix(visitSpace(b.getPrefix(), JsSpace.Location.BINDING_PREFIX, p)); + public J visitBindingElement(JS.BindingElement binding, P p) { + JS.BindingElement b = binding; + b = b.withPrefix(visitSpace(b.getPrefix(), JsSpace.Location.BINDING_ELEMENT_PREFIX, p)); b = b.withMarkers(visitMarkers(b.getMarkers(), p)); b = b.withPropertyName(visitAndCast(b.getPropertyName(), p)); - b = b.withName(visitAndCast(b.getName(), p)); - b = b.withDimensionsAfterName( - ListUtils.map(b.getDimensionsAfterName(), - dim -> dim.withBefore(visitSpace(dim.getBefore(), Space.Location.DIMENSION_PREFIX, p)) - .withElement(visitSpace(dim.getElement(), Space.Location.DIMENSION, p)) - ) - ); + b = b.withName(Objects.requireNonNull(visitAndCast(b.getName(), p))); if (b.getPadding().getInitializer() != null) { b = b.getPadding().withInitializer(visitLeftPadded(b.getPadding().getInitializer(), - JsLeftPadded.Location.BINDING_INITIALIZER, p)); + JsLeftPadded.Location.BINDING_ELEMENT_INITIALIZER, p)); } b = b.withVariableType((JavaType.Variable) visitType(b.getVariableType(), p)); return b; @@ -315,7 +309,7 @@ public J visitObjectBindingDeclarations(JS.ObjectBindingDeclarations objectBindi o = o.getPadding().withBindings(visitContainer(o.getPadding().getBindings(), JsContainer.Location.BINDING_ELEMENT, p)); if (o.getPadding().getInitializer() != null) { o = o.getPadding().withInitializer(visitLeftPadded(o.getPadding().getInitializer(), - JsLeftPadded.Location.BINDING_INITIALIZER, p)); + JsLeftPadded.Location.BINDING_ELEMENT_INITIALIZER, p)); } return o; } @@ -859,4 +853,18 @@ public J visitJSForInOfLoopControl(JS.JSForInOfLoopControl jsForInOfLoopControl, c = c.getPadding().withIterable(visitRightPadded(c.getPadding().getIterable(), JsRightPadded.Location.FOR_CONTROL_ITER, p)); return c; } + + public J visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, P p) { + JS.ArrayBindingPattern c = arrayBindingPattern; + c = c.withPrefix(visitSpace(c.getPrefix(), JsSpace.Location.ARRAY_BINDING_PATTERN_PREFIX, p)); + c = c.withMarkers(visitMarkers(c.getMarkers(), p)); + Expression temp = (Expression) visitExpression(c, p); + if (!(temp instanceof JS.ArrayBindingPattern)) { + return temp; + } else { + c = (JS.ArrayBindingPattern) temp; + } + c = c.getPadding().withElements(visitContainer(c.getPadding().getElements(), JsContainer.Location.ARRAY_BINDING_PATTERN_ELEMENTS, p)); + return c; + } } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 57dcf035..4113eca5 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java @@ -119,24 +119,14 @@ public J visitAwait(JS.Await await, PrintOutputCapture

p) { } @Override - public J visitBinding(JS.ObjectBindingDeclarations.Binding binding, PrintOutputCapture

p) { - beforeSyntax(binding, JsSpace.Location.BINDING_PREFIX, p); - if (binding.getAfterVararg() != null) { - p.append("..."); - visitSpace(binding.getAfterVararg(), Space.Location.VARARGS, p); - } + public J visitBindingElement(JS.BindingElement binding, PrintOutputCapture

p) { + beforeSyntax(binding, JsSpace.Location.BINDING_ELEMENT_PREFIX, p); if (binding.getPropertyName() != null) { - visitRightPadded(binding.getPadding().getPropertyName(), JsRightPadded.Location.BINDING_PROPERTY_NAME_SUFFIX, p); + visitRightPadded(binding.getPadding().getPropertyName(), JsRightPadded.Location.BINDING_ELEMENT_PROPERTY_NAME, p); p.append(":"); } visit(binding.getName(), p); - for (JLeftPadded dimension : binding.getDimensionsAfterName()) { - visitSpace(dimension.getBefore(), Space.Location.DIMENSION_PREFIX, p); - p.append('['); - visitSpace(dimension.getElement(), Space.Location.DIMENSION, p); - p.append(']'); - } - visitLeftPadded("=", binding.getPadding().getInitializer(), JsLeftPadded.Location.BINDING_INITIALIZER, p); + visitLeftPadded("=", binding.getPadding().getInitializer(), JsLeftPadded.Location.BINDING_ELEMENT_INITIALIZER, p); afterSyntax(binding, p); return binding; } @@ -343,7 +333,7 @@ public J visitObjectBindingDeclarations(JS.ObjectBindingDeclarations objectBindi visit(objectBindingDeclarations.getTypeExpression(), p); visitContainer("{", objectBindingDeclarations.getPadding().getBindings(), JsContainer.Location.BINDING_ELEMENT, ",", "}", p); - visitLeftPadded("=", objectBindingDeclarations.getPadding().getInitializer(), JsLeftPadded.Location.BINDING_INITIALIZER, p); + visitLeftPadded("=", objectBindingDeclarations.getPadding().getInitializer(), JsLeftPadded.Location.BINDING_ELEMENT_INITIALIZER, p); afterSyntax(objectBindingDeclarations, p); return objectBindingDeclarations; } @@ -721,6 +711,16 @@ public J visitJSForInLoop(JS.JSForInLoop loop, PrintOutputCapture

p) { return loop; } + @Override + public J visitArrayBindingPattern(JS.ArrayBindingPattern abp, PrintOutputCapture

p) { + beforeSyntax(abp, JsSpace.Location.ARRAY_BINDING_PATTERN_PREFIX, p); + + visitContainer("[", abp.getPadding().getElements(), JsContainer.Location.ARRAY_BINDING_PATTERN_ELEMENTS, "," , "]", p); + + afterSyntax(abp, p); + return abp; + } + private class JavaScriptJavaPrinter extends JavaPrinter

{ @Override diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java index 0a6f5738..0635409d 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java @@ -932,6 +932,7 @@ public JsImport withInitializer(@Nullable JLeftPadded initializer) { } } } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @@ -1135,13 +1136,13 @@ final class ObjectBindingDeclarations implements JS, Expression, TypedTree { @Getter TypeTree typeExpression; - JContainer bindings; + JContainer bindings; - public List getBindings() { + public List getBindings() { return bindings.getElements(); } - public ObjectBindingDeclarations withBindings(List bindings) { + public ObjectBindingDeclarations withBindings(List bindings) { return getPadding().withBindings(JContainer.withElements(this.bindings, bindings)); } @@ -1195,122 +1196,6 @@ public ObjectBindingDeclarations withType(@Nullable JavaType type) { withTypeExpression(typeExpression.withType(type)); } - @SuppressWarnings("unchecked") - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) - @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) - @RequiredArgsConstructor - @AllArgsConstructor(access = AccessLevel.PRIVATE) - public static final class Binding implements JS, NameTree { - - @Nullable - @NonFinal - transient WeakReference padding; - - @With - @EqualsAndHashCode.Include - @Getter - UUID id; - - @With - @Getter - Space prefix; - - @With - @Getter - Markers markers; - - @Nullable - JRightPadded propertyName; - - public J.@Nullable Identifier getPropertyName() { - return propertyName == null ? null : propertyName.getElement(); - } - - public ObjectBindingDeclarations.Binding withPropertyName(J.@Nullable Identifier propertyName) { - return getPadding().withPropertyName(JRightPadded.withElement(this.propertyName, propertyName)); - } - - @With - @Getter - TypedTree name; - - @With - @Getter - List> dimensionsAfterName; - - @With - @Nullable - @Getter - Space afterVararg; - - @Nullable - JLeftPadded initializer; - - public @Nullable Expression getInitializer() { - return initializer == null ? null : initializer.getElement(); - } - - public ObjectBindingDeclarations.Binding withInitializer(@Nullable Expression initializer) { - return getPadding().withInitializer(JLeftPadded.withElement(this.initializer, initializer)); - } - - @With - @Getter - JavaType.@Nullable Variable variableType; - - @Override - public JavaType getType() { - return variableType != null ? variableType.getType() : null; - } - - @SuppressWarnings({"unchecked", "DataFlowIssue"}) - @Override - public ObjectBindingDeclarations.Binding withType(@Nullable JavaType type) { - return variableType != null ? withVariableType(variableType.withType(type)) : this; - } - - @Override - public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - return v.visitBinding(this, p); - } - - public ObjectBindingDeclarations.Binding.Padding getPadding() { - ObjectBindingDeclarations.Binding.Padding p; - if (this.padding == null) { - p = new ObjectBindingDeclarations.Binding.Padding(this); - this.padding = new WeakReference<>(p); - } else { - p = this.padding.get(); - if (p == null || p.t != this) { - p = new ObjectBindingDeclarations.Binding.Padding(this); - this.padding = new WeakReference<>(p); - } - } - return p; - } - - @RequiredArgsConstructor - public static class Padding { - private final ObjectBindingDeclarations.Binding t; - - public @Nullable JRightPadded getPropertyName() { - return t.propertyName; - } - - public ObjectBindingDeclarations.Binding withPropertyName(@Nullable JRightPadded propertyName) { - return t.propertyName == propertyName ? t : new ObjectBindingDeclarations.Binding(t.id, t.prefix, t.markers, propertyName, t.name, t.dimensionsAfterName, t.afterVararg, t.initializer, t.variableType); - } - - public @Nullable JLeftPadded getInitializer() { - return t.initializer; - } - - public ObjectBindingDeclarations.Binding withInitializer(@Nullable JLeftPadded initializer) { - return t.initializer == initializer ? t : new ObjectBindingDeclarations.Binding(t.id, t.prefix, t.markers, t.propertyName, t.name, t.dimensionsAfterName, t.afterVararg, initializer, t.variableType); - } - } - } - public boolean hasModifier(Modifier.Type modifier) { return Modifier.hasModifier(getModifiers(), modifier); } @@ -1334,11 +1219,11 @@ public ObjectBindingDeclarations.Padding getPadding() { public static class Padding { private final ObjectBindingDeclarations t; - public JContainer getBindings() { + public JContainer getBindings() { return t.bindings; } - public ObjectBindingDeclarations withBindings(JContainer bindings) { + public ObjectBindingDeclarations withBindings(JContainer bindings) { return t.bindings == bindings ? t : new ObjectBindingDeclarations(t.id, t.prefix, t.markers, t.leadingAnnotations, t.modifiers, t.typeExpression, bindings, t.initializer); } @@ -1374,7 +1259,9 @@ final class PropertyAssignment implements JS, Statement, TypedTree { JRightPadded name; - public Expression getName() { return name.getElement(); } + public Expression getName() { + return name.getElement(); + } public PropertyAssignment withName(Expression property) { return getPadding().withName(JRightPadded.withElement(this.name, property)); @@ -3268,7 +3155,7 @@ public NamespaceDeclaration withName(Expression expression) { @Override public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - return v.visitNamespaceDeclaration(this, p); + return v.visitNamespaceDeclaration(this, p); } @Transient @@ -3554,4 +3441,194 @@ public IndexSignatureDeclaration withTypeExpression(JLeftPadded type } } } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ArrayBindingPattern implements JS, Expression, TypedTree { + + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JContainer elements; + + public List getElements() { + return elements.getElements(); + } + + public ArrayBindingPattern withElements(List elements) { + return getPadding().withElements(JContainer.withElements(this.elements, elements)); + } + + @Nullable + @With + @Getter + JavaType type; + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitArrayBindingPattern(this, p); + } + + @Transient + @Override + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public ArrayBindingPattern.Padding getPadding() { + ArrayBindingPattern.Padding p; + if (this.padding == null) { + p = new ArrayBindingPattern.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.abp != this) { + p = new ArrayBindingPattern.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ArrayBindingPattern abp; + + public JContainer getElements() { + return abp.elements; + } + + public ArrayBindingPattern withElements(JContainer elements) { + return abp.elements == elements ? abp : new ArrayBindingPattern(abp.id, abp.prefix, abp.markers, elements, abp.type); + } + } + } + + @SuppressWarnings("unchecked") + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class BindingElement implements JS, Statement, Expression, TypeTree { + + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @Nullable + JRightPadded propertyName; + + public @Nullable Expression getPropertyName() { + return propertyName == null ? null : propertyName.getElement(); + } + + public BindingElement withPropertyName(@Nullable Expression propertyName) { + return getPadding().withPropertyName(JRightPadded.withElement(this.propertyName, propertyName)); + } + + @With + @Getter + TypedTree name; + + @Nullable + JLeftPadded initializer; + + public @Nullable Expression getInitializer() { + return initializer == null ? null : initializer.getElement(); + } + + public BindingElement withInitializer(@Nullable Expression initializer) { + return getPadding().withInitializer(JLeftPadded.withElement(this.initializer, initializer)); + } + + @With + @Getter + JavaType.@Nullable Variable variableType; + + @Override + public JavaType getType() { + return variableType != null ? variableType.getType() : null; + } + + @SuppressWarnings({"unchecked", "DataFlowIssue"}) + @Override + public BindingElement withType(@Nullable JavaType type) { + return variableType != null ? withVariableType(variableType.withType(type)) : this; + } + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitBindingElement(this, p); + } + + @Override + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final BindingElement t; + + public @Nullable JRightPadded getPropertyName() { + return t.propertyName; + } + + public BindingElement withPropertyName(@Nullable JRightPadded propertyName) { + return t.propertyName == propertyName ? t : new BindingElement(t.id, t.prefix, t.markers, propertyName, t.name, t.initializer, t.variableType); + } + + public @Nullable JLeftPadded getInitializer() { + return t.initializer; + } + + public BindingElement withInitializer(@Nullable JLeftPadded initializer) { + return t.initializer == initializer ? t : new BindingElement(t.id, t.prefix, t.markers, t.propertyName, t.name, initializer, t.variableType); + } + } + } } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java index 1e70e7c2..77963099 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java @@ -21,7 +21,7 @@ public class JsContainer { @Getter public enum Location { ARRAY_LITERAL_EXPRESSION(JsSpace.Location.ARRAY_LITERAL_ELEMENTS, JsRightPadded.Location.ARRAY_LITERAL_ELEMENT_SUFFIX), - BINDING_ELEMENT(JsSpace.Location.BINDING_ELEMENTS, JsRightPadded.Location.BINDING_ELEMENT_SUFFIX), + BINDING_ELEMENT(JsSpace.Location.BINDING_ELEMENTS, JsRightPadded.Location.BINDING_ELEMENT), EXPORT_ELEMENT(JsSpace.Location.EXPORT_ELEMENTS, JsRightPadded.Location.EXPORT_ELEMENT_SUFFIX), FUNCTION_TYPE_PARAMETER(JsSpace.Location.FUNCTION_TYPE_PARAMETERS, JsRightPadded.Location.FUNCTION_TYPE_PARAMETER_SUFFIX), IMPORT_ELEMENT(JsSpace.Location.IMPORT_ELEMENTS, JsRightPadded.Location.IMPORT_ELEMENT_SUFFIX), @@ -31,6 +31,7 @@ public enum Location { JSMETHOD_INVOCATION_ARGUMENTS(JsSpace.Location.JSTYPE_PARAMETERS, JsRightPadded.Location.JSTYPE_PARAMETER), TYPE_LITERAL_MEMBERS(JsSpace.Location.TYPE_LITERAL_MEMBERS_PREFIX, JsRightPadded.Location.TYPE_LITERAL_MEMBERS), INDEXED_SIGNATURE_DECLARATION_PARAMETERS(JsSpace.Location.INDEXED_SIGNATURE_DECLARATION_PARAMETERS_PREFIX, JsRightPadded.Location.INDEXED_SIGNATURE_DECLARATION_PARAMETERS), + ARRAY_BINDING_PATTERN_ELEMENTS(JsSpace.Location.ARRAY_BINDING_PATTERN_ELEMENTS_PREFIX, JsRightPadded.Location.ARRAY_BINDING_PATTERN_ELEMENTS), ; private final JsSpace.Location beforeLocation; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java index 8c3af657..09e6464a 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsLeftPadded.java @@ -21,7 +21,7 @@ public class JsLeftPadded { @Getter public enum Location { BINARY_OPERATOR(JsSpace.Location.BINARY_PREFIX), - BINDING_INITIALIZER(JsSpace.Location.BINDING_INITIALIZER_PREFIX), + BINDING_ELEMENT_INITIALIZER(JsSpace.Location.BINDING_INITIALIZER_PREFIX), EXPORT_INITIALIZER(JsSpace.Location.EXPORT_INITIALIZER_PREFIX), IMPORT_INITIALIZER(JsSpace.Location.IMPORT_INITIALIZER_PREFIX), TYPE_DECLARATION_INITIALIZER(JsSpace.Location.TYPE_DECLARATION_INITIALIZER_PREFIX), diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java index 9392e839..c78c622a 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsRightPadded.java @@ -22,8 +22,8 @@ public class JsRightPadded { public enum Location { ALIAS_PROPERTY_NAME(JsSpace.Location.ALIAS_PROPERTY_NAME_PREFIX), ARRAY_LITERAL_ELEMENT_SUFFIX(JsSpace.Location.ARRAY_LITERAL_SUFFIX), - BINDING_ELEMENT_SUFFIX(JsSpace.Location.BINDING_SUFFIX), - BINDING_PROPERTY_NAME_SUFFIX(JsSpace.Location.BINDING_PROPERTY_NAME_SUFFIX), + BINDING_ELEMENT(JsSpace.Location.BINDING_ELEMENT_SUFFIX), + BINDING_ELEMENT_PROPERTY_NAME(JsSpace.Location.BINDING_ELEMENT_PROPERTY_NAME_SUFFIX), EXPORT_ELEMENT_SUFFIX(JsSpace.Location.EXPORT_ELEMENT_SUFFIX), FUNCTION_TYPE_PARAMETER_SUFFIX(JsSpace.Location.FUNCTION_TYPE_SUFFIX), IMPORT_ELEMENT_SUFFIX(JsSpace.Location.IMPORT_ELEMENT_SUFFIX), @@ -44,7 +44,8 @@ public enum Location { FOR_CONTROL_VAR(JsSpace.Location.FOR_INIT_SUFFIX), FOR_CONTROL_ITER(JsSpace.Location.FOR_ITER_SUFFIX), FOR_BODY(JsSpace.Location.FOR_BODY_SUFFIX), - FUNCTION_TYPE_CONSTRUCTOR(JsSpace.Location.FUNCTION_TYPE_CONSTRUCTOR_SUFFIX) + FUNCTION_TYPE_CONSTRUCTOR(JsSpace.Location.FUNCTION_TYPE_CONSTRUCTOR_SUFFIX), + ARRAY_BINDING_PATTERN_ELEMENTS(JsSpace.Location.FOR_BODY_SUFFIX), ; private final JsSpace.Location afterLocation; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java index 3427f623..f17868db 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java @@ -27,9 +27,9 @@ public enum Location { BINARY_PREFIX, BINDING_ELEMENTS, BINDING_INITIALIZER_PREFIX, - BINDING_PREFIX, - BINDING_PROPERTY_NAME_SUFFIX, - BINDING_SUFFIX, + BINDING_ELEMENT_PREFIX, + BINDING_ELEMENT_PROPERTY_NAME_SUFFIX, + BINDING_ELEMENT_SUFFIX, DEFAULT_TYPE_PREFIX, DELETE_PREFIX, EXPORT_ELEMENTS, @@ -107,6 +107,9 @@ public enum Location { FOR_IN_LOOP_PREFIX, TYPE_QUERY_PREFIX, FUNCTION_TYPE_CONSTRUCTOR_SUFFIX, + ARRAY_BINDING_PATTERN_PREFIX, + ARRAY_BINDING_PATTERN_ELEMENTS_PREFIX, + ARRAY_BINDING_PATTERN_ELEMENTS_SUFFIX, } }