From d62a55d11fd718e214624c8417ffdba5911029bc Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Thu, 12 Dec 2024 16:30:53 +0100 Subject: [PATCH] Fixed missed optional tokens in yield and method declaration --- openrewrite/src/javascript/parser.ts | 21 +++++-- openrewrite/src/javascript/remote/receiver.ts | 8 +-- openrewrite/src/javascript/remote/sender.ts | 2 +- .../src/javascript/tree/support_types.ts | 1 + openrewrite/src/javascript/tree/tree.ts | 29 ++++++--- openrewrite/src/javascript/visitor.ts | 3 +- .../test/javascript/parser/class.test.ts | 13 +++- .../test/javascript/parser/function.test.ts | 9 +++ .../test/javascript/parser/yield.test.ts | 20 ++++++ .../javascript/remote/JavaScriptReceiver.java | 8 +-- .../javascript/remote/JavaScriptSender.java | 2 +- .../remote/JavaScriptValidator.java | 2 +- .../javascript/JavaScriptVisitor.java | 3 +- .../internal/JavaScriptPrinter.java | 37 ++++------- .../org/openrewrite/javascript/tree/JS.java | 61 +++++++++++++++++-- .../javascript/tree/JsLeftPadded.java | 3 +- .../openrewrite/javascript/tree/JsSpace.java | 3 +- 17 files changed, 167 insertions(+), 58 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 139a0877..6a67380d 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1037,7 +1037,20 @@ export class JavaScriptParserVisitor { } visitMethodDeclaration(node: ts.MethodDeclaration) { - if (node.questionToken) { + if (node.questionToken || node.asteriskToken) { + let methodName = node.questionToken ? this.getOptionalUnary(node) : this.visit(node.name); + + if (node.asteriskToken) { + methodName = new JS.Unary( + randomId(), + this.prefix(node.asteriskToken), + Markers.EMPTY, + this.leftPadded(this.prefix(node.name), JS.Unary.Type.Asterisk), + methodName, + this.mapType(node) + ) + } + return new JS.JSMethodDeclaration( randomId(), this.prefix(node), @@ -1046,7 +1059,7 @@ export class JavaScriptParserVisitor { this.mapModifiers(node), this.mapTypeParametersAsObject(node), this.mapTypeInfo(node), - this.getOptionalUnary(node), + methodName, this.mapCommaSeparatedList(this.getParameterListNodes(node)), null, node.body ? this.convert(node.body) : null, @@ -2335,7 +2348,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - false, + node.asteriskToken ? this.leftPadded(this.prefix(node.asteriskToken), true) : this.leftPadded(Space.EMPTY, false), node.expression ? this.visit(node.expression) : null, this.mapType(node) ); @@ -2809,7 +2822,7 @@ export class JavaScriptParserVisitor { this.mapTypeParametersAsObject(node), this.mapCommaSeparatedList(this.getParameterListNodes(node)), this.mapTypeInfo(node), - this.convert(node.body!), + node.body ? this.convert(node.body) : null, this.mapMethodType(node) ); } diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index b8756e92..3a57947c 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -416,7 +416,7 @@ class Visitor extends JavaScriptVisitor { _yield = _yield.withId(ctx.receiveValue(_yield.id, ValueType.UUID)!); _yield = _yield.withPrefix(ctx.receiveNode(_yield.prefix, receiveSpace)!); _yield = _yield.withMarkers(ctx.receiveNode(_yield.markers, ctx.receiveMarkers)!); - _yield = _yield.withDelegated(ctx.receiveValue(_yield.delegated, ValueType.Primitive)!); + _yield = _yield.padding.withDelegated(ctx.receiveNode(_yield.padding.delegated, leftPaddedValueReceiver(ValueType.Primitive))!); _yield = _yield.withExpression(ctx.receiveNode(_yield.expression, ctx.receiveTree)); _yield = _yield.withType(ctx.receiveValue(_yield.type, ValueType.Object)); return _yield; @@ -519,7 +519,7 @@ class Visitor extends JavaScriptVisitor { functionDeclaration = functionDeclaration.withTypeParameters(ctx.receiveNode(functionDeclaration.typeParameters, ctx.receiveTree)); functionDeclaration = functionDeclaration.padding.withParameters(ctx.receiveNode(functionDeclaration.padding.parameters, receiveContainer)!); functionDeclaration = functionDeclaration.withReturnTypeExpression(ctx.receiveNode(functionDeclaration.returnTypeExpression, ctx.receiveTree)); - functionDeclaration = functionDeclaration.withBody(ctx.receiveNode(functionDeclaration.body, ctx.receiveTree)!); + functionDeclaration = functionDeclaration.withBody(ctx.receiveNode(functionDeclaration.body, ctx.receiveTree)); functionDeclaration = functionDeclaration.withType(ctx.receiveValue(functionDeclaration.type, ValueType.Object)); return functionDeclaration; } @@ -1740,7 +1740,7 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.UUID)!, ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveValue(null, ValueType.Primitive)!, + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, ctx.receiveNode(null, ctx.receiveTree), ctx.receiveValue(null, ValueType.Object) ); @@ -1852,7 +1852,7 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, ctx.receiveTree), ctx.receiveNode>(null, receiveContainer)!, ctx.receiveNode(null, ctx.receiveTree), - ctx.receiveNode(null, ctx.receiveTree)!, + ctx.receiveNode(null, ctx.receiveTree), ctx.receiveValue(null, ValueType.Object) ); } diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index b50de0ed..37208d52 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -411,7 +411,7 @@ class Visitor extends JavaScriptVisitor { ctx.sendValue(_yield, v => v.id, ValueType.UUID); ctx.sendNode(_yield, v => v.prefix, Visitor.sendSpace); ctx.sendNode(_yield, v => v.markers, ctx.sendMarkers); - ctx.sendValue(_yield, v => v.delegated, ValueType.Primitive); + ctx.sendNode(_yield, v => v.padding.delegated, Visitor.sendLeftPadded(ValueType.Primitive)); ctx.sendNode(_yield, v => v.expression, ctx.sendTree); ctx.sendTypedValue(_yield, v => v.type, ValueType.Object); return _yield; diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index eb6b417f..605346dd 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -303,6 +303,7 @@ export namespace JsLeftPadded { MAPPED_TYPE_HAS_READONLY, MAPPED_TYPE_HAS_QUESTION_TOKEN, ARROW_FUNCTION_BODY, + YIELD_DELEGATED, } } export namespace JsRightPadded { diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 734e8e87..35a9f423 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -3270,6 +3270,7 @@ export namespace Unary { Exclamation = 2, QuestionDot = 3, QuestionDotWithDot = 4, + Asterisk = 5, } @@ -3499,7 +3500,7 @@ export class Void extends JSMixin(Object) implements Expression, Statement { @LstType("org.openrewrite.javascript.tree.JS$Yield") export class Yield extends JSMixin(Object) implements Expression { - public constructor(id: UUID, prefix: Space, markers: Markers, delegated: boolean, expression: Expression | null, _type: JavaType | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, delegated: JLeftPadded, expression: Expression | null, _type: JavaType | null) { super(); this._id = id; this._prefix = prefix; @@ -3539,14 +3540,14 @@ export class Yield extends JSMixin(Object) implements Expression { return markers === this._markers ? this : new Yield(this._id, this._prefix, markers, this._delegated, this._expression, this._type); } - private readonly _delegated: boolean; + private readonly _delegated: JLeftPadded; public get delegated(): boolean { - return this._delegated; + return this._delegated.element; } public withDelegated(delegated: boolean): Yield { - return delegated === this._delegated ? this : new Yield(this._id, this._prefix, this._markers, delegated, this._expression, this._type); + return this.padding.withDelegated(this._delegated.withElement(delegated)); } private readonly _expression: Expression | null; @@ -3573,6 +3574,18 @@ export class Yield extends JSMixin(Object) implements Expression { return v.visitJsYield(this, p); } + get padding() { + const t = this; + return new class { + public get delegated(): JLeftPadded { + return t._delegated; + } + public withDelegated(delegated: JLeftPadded): Yield { + return t._delegated === delegated ? t : new Yield(t._id, t._prefix, t._markers, delegated, t._expression, t._type); + } + } + } + } @LstType("org.openrewrite.javascript.tree.JS$TypeInfo") @@ -4442,7 +4455,7 @@ export namespace NamespaceDeclaration { @LstType("org.openrewrite.javascript.tree.JS$FunctionDeclaration") export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree { - public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], asteriskToken: JLeftPadded, name: JLeftPadded, typeParameters: Java.TypeParameters | null, parameters: JContainer, returnTypeExpression: TypeTree | null, body: J, _type: JavaType | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], asteriskToken: JLeftPadded, name: JLeftPadded, typeParameters: Java.TypeParameters | null, parameters: JContainer, returnTypeExpression: TypeTree | null, body: J | null, _type: JavaType | null) { super(); this._id = id; this._prefix = prefix; @@ -4547,13 +4560,13 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E return returnTypeExpression === this._returnTypeExpression ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, returnTypeExpression, this._body, this._type); } - private readonly _body: J; + private readonly _body: J | null; - public get body(): J { + public get body(): J | null { return this._body; } - public withBody(body: J): FunctionDeclaration { + public withBody(body: J | null): FunctionDeclaration { return body === this._body ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, body, this._type); } diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index dbccf3fa..2ae55e73 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -577,6 +577,7 @@ export class JavaScriptVisitor

extends JavaVisitor

{ } _yield = tempExpression as Yield; _yield = _yield.withMarkers(this.visitMarkers(_yield.markers, p)); + _yield = _yield.padding.withDelegated(this.visitJsLeftPadded(_yield.padding.delegated, JsLeftPadded.Location.YIELD_DELEGATED, p)!); _yield = _yield.withExpression(this.visitAndCast(_yield.expression, p)); return _yield; } @@ -715,7 +716,7 @@ export class JavaScriptVisitor

extends JavaVisitor

{ functionDeclaration = functionDeclaration.withTypeParameters(this.visitAndCast(functionDeclaration.typeParameters, p)); functionDeclaration = functionDeclaration.padding.withParameters(this.visitJsContainer(functionDeclaration.padding.parameters, JsContainer.Location.FUNCTION_DECLARATION_PARAMETERS, p)!); functionDeclaration = functionDeclaration.withReturnTypeExpression(this.visitAndCast(functionDeclaration.returnTypeExpression, p)); - functionDeclaration = functionDeclaration.withBody(this.visitAndCast(functionDeclaration.body, p)!); + functionDeclaration = functionDeclaration.withBody(this.visitAndCast(functionDeclaration.body, p)); return functionDeclaration; } diff --git a/openrewrite/test/javascript/parser/class.test.ts b/openrewrite/test/javascript/parser/class.test.ts index 62cb2e34..a526d07d 100644 --- a/openrewrite/test/javascript/parser/class.test.ts +++ b/openrewrite/test/javascript/parser/class.test.ts @@ -469,7 +469,6 @@ describe('class mapping', () => { ); }); - test('new class with type arguments', () => { rewriteRun( //language=typescript @@ -490,4 +489,16 @@ describe('class mapping', () => { ); }); + test('static method with asterisk', () => { + rewriteRun( + //language=typescript + typeScript(` + class A { + public static/*a*/*/*b*/getMarkdownRestSnippets?(document: TextDocument): Generator { + } + } + `) + ); + }); + }); diff --git a/openrewrite/test/javascript/parser/function.test.ts b/openrewrite/test/javascript/parser/function.test.ts index f8dde7b8..c4afcb74 100644 --- a/openrewrite/test/javascript/parser/function.test.ts +++ b/openrewrite/test/javascript/parser/function.test.ts @@ -433,4 +433,13 @@ describe('function mapping', () => { `) ); }); + + test('empty body function', () => { + rewriteRun( + //language=typescript + typeScript(` + export function getHeader(headers: ResponseHeaders, name: string): ResponseHeaderValue; + `) + ); + }); }); diff --git a/openrewrite/test/javascript/parser/yield.test.ts b/openrewrite/test/javascript/parser/yield.test.ts index 495a174f..90684aaa 100644 --- a/openrewrite/test/javascript/parser/yield.test.ts +++ b/openrewrite/test/javascript/parser/yield.test.ts @@ -24,4 +24,24 @@ describe('yield mapping', () => { typeScript('yield* other') ); }); + + test('yield expression', () => { + rewriteRun( + //language=typescript + typeScript(` + DenseMatrix.prototype[Symbol.iterator] = function* () { + const recurse = function* (value, index) { + if (isArray(value)) { + for (let i = 0; i < value.length; i++) { + yield* recurse(value[i], index.concat(i)) + } + } else { + yield ({value, index}) + } + } + yield/*a*/* /*b*/recurse(this._data, []) + } + `) + ); + }); }); 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 b4117a31..075a8ef9 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 @@ -502,7 +502,7 @@ public JS.Yield visitYield(JS.Yield yield, ReceiverContext ctx) { yield = yield.withId(ctx.receiveNonNullValue(yield.getId(), UUID.class)); yield = yield.withPrefix(ctx.receiveNonNullNode(yield.getPrefix(), JavaScriptReceiver::receiveSpace)); yield = yield.withMarkers(ctx.receiveNonNullNode(yield.getMarkers(), ctx::receiveMarkers)); - yield = yield.withDelegated(ctx.receiveNonNullValue(yield.isDelegated(), boolean.class)); + yield = yield.getPadding().withDelegated(ctx.receiveNonNullNode(yield.getPadding().getDelegated(), leftPaddedValueReceiver(java.lang.Boolean.class))); yield = yield.withExpression(ctx.receiveNode(yield.getExpression(), ctx::receiveTree)); yield = yield.withType(ctx.receiveValue(yield.getType(), JavaType.class)); return yield; @@ -614,7 +614,7 @@ public JS.FunctionDeclaration visitFunctionDeclaration(JS.FunctionDeclaration fu functionDeclaration = functionDeclaration.withTypeParameters(ctx.receiveNode(functionDeclaration.getTypeParameters(), ctx::receiveTree)); functionDeclaration = functionDeclaration.getPadding().withParameters(ctx.receiveNonNullNode(functionDeclaration.getPadding().getParameters(), JavaScriptReceiver::receiveContainer)); functionDeclaration = functionDeclaration.withReturnTypeExpression(ctx.receiveNode(functionDeclaration.getReturnTypeExpression(), ctx::receiveTree)); - functionDeclaration = functionDeclaration.withBody(ctx.receiveNonNullNode(functionDeclaration.getBody(), ctx::receiveTree)); + functionDeclaration = functionDeclaration.withBody(ctx.receiveNode(functionDeclaration.getBody(), ctx::receiveTree)); functionDeclaration = functionDeclaration.withType(ctx.receiveValue(functionDeclaration.getType(), JavaType.class)); return functionDeclaration; } @@ -2015,7 +2015,7 @@ private static JS.Yield createJSYield(ReceiverContext ctx) { ctx.receiveNonNullValue(null, UUID.class), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNonNullValue(null, boolean.class), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), ctx.receiveNode(null, ctx::receiveTree), ctx.receiveValue(null, JavaType.class) ); @@ -2127,7 +2127,7 @@ private static JS.FunctionDeclaration createJSFunctionDeclaration(ReceiverContex ctx.receiveNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer), ctx.receiveNode(null, ctx::receiveTree), - ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNode(null, ctx::receiveTree), ctx.receiveValue(null, JavaType.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 dd643dfb..09b7d8cc 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 @@ -484,7 +484,7 @@ public JS.Yield visitYield(JS.Yield yield, SenderContext ctx) { ctx.sendValue(yield, JS.Yield::getId); ctx.sendNode(yield, JS.Yield::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(yield, JS.Yield::getMarkers, ctx::sendMarkers); - ctx.sendValue(yield, JS.Yield::isDelegated); + ctx.sendNode(yield, e -> e.getPadding().getDelegated(), JavaScriptSender::sendLeftPadded); ctx.sendNode(yield, JS.Yield::getExpression, ctx::sendTree); ctx.sendTypedValue(yield, JS.Yield::getType); return yield; 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 85ecef55..b78f8fa9 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 @@ -404,7 +404,7 @@ public JS.FunctionDeclaration visitFunctionDeclaration(JS.FunctionDeclaration fu visitAndValidate(functionDeclaration.getTypeParameters(), J.TypeParameters.class, p); visitAndValidate(functionDeclaration.getParameters(), Statement.class, p); visitAndValidate(functionDeclaration.getReturnTypeExpression(), TypeTree.class, p); - visitAndValidateNonNull(functionDeclaration.getBody(), J.class, p); + visitAndValidate(functionDeclaration.getBody(), J.class, p); return functionDeclaration; } 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 4bdcfb23..888f688c 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -867,6 +867,7 @@ public J visitYield(JS.Yield yield, P p) { } else { y = (JS.Yield) temp; } + y = y.getPadding().withDelegated(this.visitLeftPadded(y.getPadding().getDelegated(), JsLeftPadded.Location.JS_YIELD_DELEGATED, p)); y = y.withExpression(visitAndCast(y.getExpression(), p)); return y; } @@ -981,7 +982,7 @@ public J visitFunctionDeclaration(JS.FunctionDeclaration functionDeclaration, P f = f.withTypeParameters(visitAndCast(f.getTypeParameters(), p)); f = f.getPadding().withParameters(Objects.requireNonNull(visitContainer(f.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, p))); f = f.withReturnTypeExpression(visitAndCast(f.getReturnTypeExpression(), p)); - f = f.withBody(Objects.requireNonNull(visitAndCast(f.getBody(), p))); + f = f.withBody(visitAndCast(f.getBody(), p)); f = f.withType(visitType(f.getType(), p)); return f; } 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 bf96a0e1..445f9e22 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 @@ -640,6 +640,10 @@ public J visitUnary(JS.Unary unary, PrintOutputCapture

p) { visitSpace(unary.getPadding().getOperator().getBefore(), Space.Location.UNARY_OPERATOR, p); p.append("?."); break; + case Asterisk: + p.append("*"); + visitSpace(unary.getPadding().getOperator().getBefore(), Space.Location.UNARY_OPERATOR, p); + visit(unary.getExpression(), p); default: break; } @@ -681,9 +685,11 @@ public J visitYield(JS.Yield yield, PrintOutputCapture

p) { beforeSyntax(yield, JsSpace.Location.YIELD_PREFIX, p); p.append("yield"); + if (yield.isDelegated()) { - p.append("*"); + visitLeftPaddedBoolean("*", yield.getPadding().getDelegated(), JsLeftPadded.Location.JS_YIELD_DELEGATED, p); } + visit(yield.getExpression(), p); afterSyntax(yield, p); @@ -758,11 +764,6 @@ public J visitJSMethodDeclaration(JS.JSMethodDeclaration method, PrintOutputCapt visit(method.getLeadingAnnotations(), p); method.getModifiers().forEach(it -> delegate.visitModifier(it, p)); - Asterisk asterisk = method.getMarkers().findFirst(Asterisk.class).orElse(null); - if (asterisk != null) { - visitSpace(asterisk.getPrefix(), Space.Location.LANGUAGE_EXTENSION, p); - p.append("*"); - } visit(method.getName(), p); J.TypeParameters typeParameters = method.getTypeParameters(); @@ -809,7 +810,10 @@ public J visitFunctionDeclaration(JS.FunctionDeclaration functionDeclaration, Pr visit(functionDeclaration.getReturnTypeExpression(), p); } - visit(functionDeclaration.getBody(), p); + if (functionDeclaration.getBody() != null) { + visit(functionDeclaration.getBody(), p); + } + afterSyntax(functionDeclaration, p); return functionDeclaration; } @@ -1139,11 +1143,6 @@ public J visitMethodDeclaration(J.MethodDeclaration method, PrintOutputCapture

delegate.visitModifier(it, p)); - Asterisk asterisk = method.getMarkers().findFirst(Asterisk.class).orElse(null); - if (asterisk != null) { - visitSpace(asterisk.getPrefix(), Space.Location.LANGUAGE_EXTENSION, p); - p.append("*"); - } visit(method.getName(), p); J.TypeParameters typeParameters = method.getAnnotations().getTypeParameters(); @@ -1378,20 +1377,6 @@ public J visitVariable(J.VariableDeclarations.NamedVariable variable, PrintOutpu return variable; } - @Override - public J visitYield(J.Yield yield, PrintOutputCapture

p) { - beforeSyntax(yield, Space.Location.YIELD_PREFIX, p); - p.append("yield"); - Asterisk asterisk = yield.getMarkers().findFirst(Asterisk.class).orElse(null); - if (asterisk != null) { - visitSpace(asterisk.getPrefix(), Space.Location.LANGUAGE_EXTENSION, p); - p.append("*"); - } - visit(yield.getValue(), p); - afterSyntax(yield, p); - return yield; - } - protected void visitStatement(@Nullable JRightPadded paddedStat, JRightPadded.Location location, PrintOutputCapture

p) { if (paddedStat != null) { visit(paddedStat.getElement(), p); 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 848fc5b4..88671467 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 @@ -3031,7 +3031,8 @@ public enum Type { Optional, Exclamation, QuestionDot, - QuestionDotWithDot; + QuestionDotWithDot, + Asterisk; public boolean isModifying() { switch (this) { @@ -3261,24 +3262,46 @@ public CoordinateBuilder.Statement getCoordinates() { } } - @Getter @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor - @With + @AllArgsConstructor(access = AccessLevel.PRIVATE) final class Yield implements JS, Expression { + @Nullable + @NonFinal + transient WeakReference padding; + @EqualsAndHashCode.Include + @With + @Getter UUID id; + @With + @Getter Space prefix; + + @With + @Getter Markers markers; - boolean delegated; + JLeftPadded delegated; + + public boolean isDelegated() { + return delegated.getElement(); + } + + public JS.Yield withDelegated(boolean delegated) { + return getPadding().withDelegated(this.delegated.withElement(delegated)); + } + @With + @Getter @Nullable Expression expression; + @With + @Getter @Nullable JavaType type; @@ -3291,6 +3314,35 @@ public

J acceptJavaScript(JavaScriptVisitor

v, P p) { public CoordinateBuilder.Expression getCoordinates() { return new CoordinateBuilder.Expression(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 JS.Yield t; + + public JLeftPadded getDelegated() { + return t.delegated; + } + + public JS.Yield withDelegated(JLeftPadded delegated) { + return t.delegated == delegated ? t : new JS.Yield(t.id, t.prefix, t.markers, delegated, t.expression, t.type); + } + } + } @Getter @@ -4182,6 +4234,7 @@ public FunctionDeclaration withParameters(List parameters) { @Getter @With + @Nullable J body; @Getter 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 9782ab56..177291dc 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 @@ -51,7 +51,8 @@ public enum Location { MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE(JsSpace.Location.MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE_PREFIX), MAPPED_TYPE_READONLY(JsSpace.Location.MAPPED_TYPE_READONLY_PREFIX), MAPPED_TYPE_QUESTION_TOKEN(JsSpace.Location.MAPPED_TYPE_QUESTION_TOKEN_PREFIX), - LAMBDA_ARROW(JsSpace.Location.LAMBDA_ARROW_PREFIX); + LAMBDA_ARROW(JsSpace.Location.LAMBDA_ARROW_PREFIX), + JS_YIELD_DELEGATED(JsSpace.Location.JS_YIELD_DELEGATED_PREFIX); private final JsSpace.Location beforeLocation; 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 a271763b..40d279c5 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 @@ -164,6 +164,7 @@ public enum Location { MAPPED_TYPE_READONLY_PREFIX, MAPPED_TYPE_QUESTION_TOKEN_PREFIX, TYPE_TREE_EXPRESSION_PREFIX, - LAMBDA_ARROW_PREFIX; + LAMBDA_ARROW_PREFIX, + JS_YIELD_DELEGATED_PREFIX; } }