From e1dc0a2a5ac60c2f96e2e5a4dafc596ab630e8a0 Mon Sep 17 00:00:00 2001 From: Oleh Dokuka Date: Tue, 12 Nov 2024 13:01:35 +0200 Subject: [PATCH 1/2] wip --- openrewrite/src/javascript/parser.ts | 9 +- openrewrite/src/javascript/remote/receiver.ts | 21 ++++- openrewrite/src/javascript/remote/sender.ts | 10 +- openrewrite/src/javascript/tree/tree.ts | 94 ++++++++++++++++--- openrewrite/src/javascript/visitor.ts | 8 +- .../test/javascript/parser/namespace.test.ts | 49 ++++++++++ .../javascript/remote/JavaScriptReceiver.java | 22 ++++- .../javascript/remote/JavaScriptSender.java | 11 ++- .../javascript/JavaScriptVisitor.java | 16 +++- .../internal/JavaScriptPrinter.java | 11 ++- .../org/openrewrite/javascript/tree/JS.java | 51 +++++++++- 11 files changed, 270 insertions(+), 32 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index c365efd3..ccbc929a 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1994,14 +1994,17 @@ export class JavaScriptParserVisitor { visitModuleDeclaration(node: ts.ModuleDeclaration) { const body = this.visit(node.body as ts.Node); - const namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword); + let namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword); + const kindType = namespaceKeyword ? JS.NamespaceDeclaration.Kind.Type.Namespace : JS.NamespaceDeclaration.Kind.Type.Module + namespaceKeyword ??= this.findChildNode(node, ts.SyntaxKind.ModuleKeyword); + const kind = new JS.NamespaceDeclaration.Kind(randomId(), this.prefix(namespaceKeyword!), Markers.EMPTY, kindType) if (body instanceof JS.NamespaceDeclaration) { return new JS.NamespaceDeclaration( randomId(), Space.EMPTY, Markers.EMPTY, this.mapModifiers(node), - namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY, + kind, this.rightPadded( new J.FieldAccess( randomId(), @@ -2025,7 +2028,7 @@ export class JavaScriptParserVisitor { node.parent.kind === ts.SyntaxKind.ModuleBlock ? this.prefix(node) : Space.EMPTY, Markers.EMPTY, this.mapModifiers(node), - namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY, + kind, this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess body // J.Block ); diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index afa776c0..00827c5c 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -345,12 +345,20 @@ class Visitor extends JavaScriptVisitor { namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNode(namespaceDeclaration.prefix, receiveSpace)!); namespaceDeclaration = namespaceDeclaration.withMarkers(ctx.receiveNode(namespaceDeclaration.markers, ctx.receiveMarkers)!); namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNodes(namespaceDeclaration.modifiers, ctx.receiveTree)!); - namespaceDeclaration = namespaceDeclaration.withNamespace(ctx.receiveNode(namespaceDeclaration.namespace, receiveSpace)!); + namespaceDeclaration = namespaceDeclaration.withKind(ctx.receiveNode(namespaceDeclaration.kind, ctx.receiveTree)!); namespaceDeclaration = namespaceDeclaration.padding.withName(ctx.receiveNode(namespaceDeclaration.padding.name, receiveRightPaddedTree)!); namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)!); return namespaceDeclaration; } + public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, ctx: ReceiverContext): J { + kind = kind.withId(ctx.receiveValue(kind.id, ValueType.UUID)!); + kind = kind.withPrefix(ctx.receiveNode(kind.prefix, receiveSpace)!); + kind = kind.withMarkers(ctx.receiveNode(kind.markers, ctx.receiveMarkers)!); + kind = kind.withType(ctx.receiveValue(kind.type, ValueType.Enum)!); + return kind; + } + 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)!); @@ -1370,12 +1378,21 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveNodes(null, ctx.receiveTree)!, - ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveTree)!, ctx.receiveNode>(null, receiveRightPaddedTree)!, ctx.receiveNode(null, ctx.receiveTree)! ); } + if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration$Kind") { + return new NamespaceDeclaration.Kind( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveValue(null, ValueType.Enum)! + ); + } + 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 a0b1620f..de0177ee 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -340,12 +340,20 @@ class Visitor extends JavaScriptVisitor { ctx.sendNode(namespaceDeclaration, v => v.prefix, Visitor.sendSpace); ctx.sendNode(namespaceDeclaration, v => v.markers, ctx.sendMarkers); ctx.sendNodes(namespaceDeclaration, v => v.modifiers, ctx.sendTree, t => t.id); - ctx.sendNode(namespaceDeclaration, v => v.namespace, Visitor.sendSpace); + ctx.sendNode(namespaceDeclaration, v => v.kind, ctx.sendTree); ctx.sendNode(namespaceDeclaration, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree)); ctx.sendNode(namespaceDeclaration, v => v.body, ctx.sendTree); return namespaceDeclaration; } + public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, ctx: SenderContext): J { + ctx.sendValue(kind, v => v.id, ValueType.UUID); + ctx.sendNode(kind, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(kind, v => v.markers, ctx.sendMarkers); + ctx.sendValue(kind, v => v.type, ValueType.Enum); + return kind; + } + 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/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 30898b83..fbbc9151 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -2838,13 +2838,13 @@ export class JSMethodDeclaration extends JSMixin(Object) implements Statement, T @LstType("org.openrewrite.javascript.tree.JS$NamespaceDeclaration") export class NamespaceDeclaration extends JSMixin(Object) implements Statement { - public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], namespace: Space, name: JRightPadded, body: Java.Block) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], kind: NamespaceDeclaration.Kind, name: JRightPadded, body: Java.Block) { super(); this._id = id; this._prefix = prefix; this._markers = markers; this._modifiers = modifiers; - this._namespace = namespace; + this._kind = kind; this._name = name; this._body = body; } @@ -2856,7 +2856,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withId(id: UUID): NamespaceDeclaration { - return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._namespace, this._name, this._body); + return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._kind, this._name, this._body); } private readonly _prefix: Space; @@ -2866,7 +2866,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withPrefix(prefix: Space): NamespaceDeclaration { - return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._namespace, this._name, this._body); + return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._kind, this._name, this._body); } private readonly _markers: Markers; @@ -2876,7 +2876,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withMarkers(markers: Markers): NamespaceDeclaration { - return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._namespace, this._name, this._body); + return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._kind, this._name, this._body); } private readonly _modifiers: Java.Modifier[]; @@ -2886,17 +2886,13 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withModifiers(modifiers: Java.Modifier[]): NamespaceDeclaration { - return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, this._namespace, this._name, this._body); + return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, this._kind, this._name, this._body); } - private readonly _namespace: Space; + private readonly _kind: NamespaceDeclaration.Kind; - public get namespace(): Space { - return this._namespace; - } - - public withNamespace(namespace: Space): NamespaceDeclaration { - return namespace === this._namespace ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, namespace, this._name, this._body); + public withKind(kind: NamespaceDeclaration.Kind): NamespaceDeclaration { + return kind === this._kind ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, kind, this._name, this._body); } private readonly _name: JRightPadded; @@ -2916,7 +2912,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withBody(body: Java.Block): NamespaceDeclaration { - return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._namespace, this._name, body); + return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._kind, this._name, body); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { @@ -2930,9 +2926,77 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { return t._name; } public withName(name: JRightPadded): NamespaceDeclaration { - return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._namespace, name, t._body); + return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._kind, name, t._body); } } } } + +export namespace NamespaceDeclaration { + @LstType("org.openrewrite.javascript.tree.JS$NamespaceDeclaration$Kind") + export class Kind extends JSMixin(Object) { + public constructor(id: UUID, prefix: Space, markers: Markers, _type: NamespaceDeclaration.Kind.Type) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._type = _type; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): NamespaceDeclaration.Kind { + return id === this._id ? this : new NamespaceDeclaration.Kind(id, this._prefix, this._markers, this._type); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): NamespaceDeclaration.Kind { + return prefix === this._prefix ? this : new NamespaceDeclaration.Kind(this._id, prefix, this._markers, this._type); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): NamespaceDeclaration.Kind { + return markers === this._markers ? this : new NamespaceDeclaration.Kind(this._id, this._prefix, markers, this._type); + } + + private readonly _type: NamespaceDeclaration.Kind.Type; + + public get type(): NamespaceDeclaration.Kind.Type { + return this._type; + } + + public withType(_type: NamespaceDeclaration.Kind.Type): NamespaceDeclaration.Kind { + return _type === this._type ? this : new NamespaceDeclaration.Kind(this._id, this._prefix, this._markers, _type); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitNamespaceDeclarationKind(this, p); + } + + } + + export namespace Kind { + export enum Type { + Namespace = 0, + Module = 1, + + } + + } + +} diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index 2493620d..f12acfbd 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -452,12 +452,18 @@ export class JavaScriptVisitor

extends JavaVisitor

{ namespaceDeclaration = tempStatement as NamespaceDeclaration; namespaceDeclaration = namespaceDeclaration.withMarkers(this.visitMarkers(namespaceDeclaration.markers, p)); namespaceDeclaration = namespaceDeclaration.withModifiers(ListUtils.map(namespaceDeclaration.modifiers, el => this.visitAndCast(el, p))); - namespaceDeclaration = namespaceDeclaration.withNamespace(this.visitJsSpace(namespaceDeclaration.namespace, JsSpace.Location.NAMESPACE_DECLARATION_NAMESPACE, p)!); + namespaceDeclaration = namespaceDeclaration.withKind(this.visitAndCast(namespaceDeclaration.kind, p)!); namespaceDeclaration = namespaceDeclaration.padding.withName(this.visitJsRightPadded(namespaceDeclaration.padding.name, JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p)!); namespaceDeclaration = namespaceDeclaration.withBody(this.visitAndCast(namespaceDeclaration.body, p)!); return namespaceDeclaration; } + public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, p: P): J | null { + kind = kind.withPrefix(this.visitJsSpace(kind.prefix, JsSpace.Location.NAMESPACE_DECLARATION_KIND_PREFIX, p)!); + kind = kind.withMarkers(this.visitMarkers(kind.markers, p)); + return kind; + } + public visitJsLeftPadded(left: JLeftPadded | null, loc: JsLeftPadded.Location, p: P): JLeftPadded { return extensions.visitJsLeftPadded(this, left, loc, p); } diff --git a/openrewrite/test/javascript/parser/namespace.test.ts b/openrewrite/test/javascript/parser/namespace.test.ts index b389cd5f..adddf211 100644 --- a/openrewrite/test/javascript/parser/namespace.test.ts +++ b/openrewrite/test/javascript/parser/namespace.test.ts @@ -181,4 +181,53 @@ describe('namespace mapping', () => { `) ); }); + + + test('complex test with namaspaces and modules', () => { + rewriteRun( + //language=typescript + typeScript(` + export var asdf = 123; + + module porting { + var foo = {}; + foo.bar = 123; // error : property 'bar' does not exist on \`{}\` + foo.bas = 'hello'; // error : property 'bas' does not exist on \`{}\` + } + + module assert { + interface Foo { + bar: number; + bas: string; + } + + var foo = {} as Foo; + foo.bar = 123; + foo.bas = 'hello'; + } + + module sdfsdfsdf { + var foo: any; + var bar = foo; // bar is now of type "string" + } + + + namespace doubleAssertion { + + function handler1(event: Event) { + let mouseEvent = event as MouseEvent; + } + + function handler2(event: Event) { + let element = event as HTMLElement; // Error : Neither 'Event' not type 'HTMLElement' is assignable to the other + } + + function handler(event: Event) { + let element = event as any as HTMLElement; // Okay! + } + } + + `) + ); + }); }); 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 100cf56c..3443e9e6 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 @@ -422,12 +422,21 @@ public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNonNullNode(namespaceDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace)); namespaceDeclaration = namespaceDeclaration.withMarkers(ctx.receiveNonNullNode(namespaceDeclaration.getMarkers(), ctx::receiveMarkers)); namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNonNullNodes(namespaceDeclaration.getModifiers(), ctx::receiveTree)); - namespaceDeclaration = namespaceDeclaration.withNamespace(ctx.receiveNonNullNode(namespaceDeclaration.getNamespace(), JavaScriptReceiver::receiveSpace)); + namespaceDeclaration = namespaceDeclaration.withKind(ctx.receiveNonNullNode(namespaceDeclaration.getKind(), ctx::receiveTree)); namespaceDeclaration = namespaceDeclaration.getPadding().withName(ctx.receiveNonNullNode(namespaceDeclaration.getPadding().getName(), JavaScriptReceiver::receiveRightPaddedTree)); namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNonNullNode(namespaceDeclaration.getBody(), ctx::receiveTree)); return namespaceDeclaration; } + @Override + public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, ReceiverContext ctx) { + kind = kind.withId(ctx.receiveNonNullValue(kind.getId(), UUID.class)); + kind = kind.withPrefix(ctx.receiveNonNullNode(kind.getPrefix(), JavaScriptReceiver::receiveSpace)); + kind = kind.withMarkers(ctx.receiveNonNullNode(kind.getMarkers(), ctx::receiveMarkers)); + kind = kind.withType(ctx.receiveNonNullValue(kind.getType(), JS.NamespaceDeclaration.Kind.Type.class)); + return kind; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class)); @@ -1477,12 +1486,21 @@ public T create(Class type, ReceiverContext ctx) { ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), ctx.receiveNonNullNodes(null, ctx::receiveTree), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree), ctx.receiveNonNullNode(null, ctx::receiveTree) ); } + if (type == JS.NamespaceDeclaration.Kind.class) { + return (T) new JS.NamespaceDeclaration.Kind( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullValue(null, JS.NamespaceDeclaration.Kind.Type.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 f9e4fb95..ab4ee627 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 @@ -405,12 +405,21 @@ public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getMarkers, ctx::sendMarkers); ctx.sendNodes(namespaceDeclaration, JS.NamespaceDeclaration::getModifiers, ctx::sendTree, Tree::getId); - ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getNamespace, JavaScriptSender::sendSpace); + ctx.sendNode(namespaceDeclaration, e -> e.getPadding().getKind(), ctx::sendTree); ctx.sendNode(namespaceDeclaration, e -> e.getPadding().getName(), JavaScriptSender::sendRightPadded); ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getBody, ctx::sendTree); return namespaceDeclaration; } + @Override + public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, SenderContext ctx) { + ctx.sendValue(kind, JS.NamespaceDeclaration.Kind::getId); + ctx.sendNode(kind, JS.NamespaceDeclaration.Kind::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(kind, JS.NamespaceDeclaration.Kind::getMarkers, ctx::sendMarkers); + ctx.sendValue(kind, JS.NamespaceDeclaration.Kind::getType); + return kind; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.sendValue(annotatedType, J.AnnotatedType::getId); 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 fcc8a89a..81adca99 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -22,6 +22,7 @@ import org.openrewrite.java.tree.*; import org.openrewrite.javascript.tree.*; import org.openrewrite.marker.Markers; +import org.openrewrite.remote.SenderContext; import java.util.List; import java.util.Objects; @@ -670,9 +671,22 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, } else { ns = (JS.NamespaceDeclaration) temp; } - ns = ns.withNamespace(visitSpace(ns.getNamespace(), JsSpace.Location.JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, p)); + ns = ns = ns.getPadding().withName(visitRightPadded(ns.getPadding().getName(), JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p)); ns = ns.withBody(visitAndCast(ns.getBody(), p)); return ns; } + + public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, SenderContext ctx) { + kind.getPadding().withKind( + namespaceDeclaration.getPadding().getKind().withMarkers( + visitMarkers(namespaceDeclaration.getPadding().getKind().getMarkers(), p) + ) + ); + ns = ns.getPadding().withKind( + ns.getPadding().getKind().withPrefix( + visitSpace(ns.getPadding().getKind().getPrefix(), JsSpace.Location.JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, p) + ) + ); + } } 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 c6f95301..bff33b09 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 @@ -254,8 +254,15 @@ public J visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, Print public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, PrintOutputCapture

p) { beforeSyntax(namespaceDeclaration, JsSpace.Location.JSNAMESPACE_DECLARATION_PREFIX, p); namespaceDeclaration.getModifiers().forEach(it -> delegate.visitModifier(it, p)); - visitSpace(namespaceDeclaration.getNamespace(), JsSpace.Location.JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, p); - p.append("namespace"); + visitSpace(namespaceDeclaration.getPadding().getKind().getPrefix(), JsSpace.Location.JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, p); + switch (namespaceDeclaration.getKind()) { + case Namespace: + p.append("namespace"); + break; + case Module: + p.append("module"); + break; + } visit(namespaceDeclaration.getName(), p); visit(namespaceDeclaration.getBody(), p); afterSyntax(namespaceDeclaration, 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 baccf784..99f52b28 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 @@ -2587,9 +2587,20 @@ final class NamespaceDeclaration implements JS, Statement { @Getter List modifiers; - @With - @Getter - Space namespace; + Kind kind; + + public Kind.Type getKind() { + return kind.getType(); + } + + public NamespaceDeclaration withKind(Kind.Type type) { + Kind k = getPadding().getKind(); + if (k.type == type) { + return this; + } else { + return getPadding().withKind(k.withType(type)); + } + } JRightPadded name; @@ -2616,6 +2627,30 @@ public CoordinateBuilder.Statement getCoordinates() { return new CoordinateBuilder.Statement(this); } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @Data + public static final class Kind implements JS { + + @With + @EqualsAndHashCode.Include + UUID id; + + @With + Space prefix; + + @With + Markers markers; + + @With + Type type; + + public enum Type { + Namespace, + Module, + } + } + public NamespaceDeclaration.Padding getPadding() { NamespaceDeclaration.Padding p; if (this.padding == null) { @@ -2636,12 +2671,20 @@ public static class Padding { private final NamespaceDeclaration t; public NamespaceDeclaration withName(JRightPadded name) { - return t.name == name ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.namespace, name, t.body); + return t.name == name ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.kind, name, t.body); } public JRightPadded getName() { return t.name; } + + public Kind getKind() { + return t.kind; + } + + public NamespaceDeclaration withKind(Kind kind) { + return t.kind == kind ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, kind, t.name, t.body); + } } } } From de95bdfc9320563c16b4e4ef1ba95450944e666c Mon Sep 17 00:00:00 2001 From: Oleh Dokuka Date: Tue, 12 Nov 2024 15:01:29 +0200 Subject: [PATCH 2/2] add more fixes --- openrewrite/src/javascript/parser.ts | 38 ++--- openrewrite/src/javascript/remote/receiver.ts | 27 ++-- openrewrite/src/javascript/remote/sender.ts | 13 +- .../src/javascript/tree/support_types.ts | 3 +- openrewrite/src/javascript/tree/tree.ts | 136 ++++++++---------- openrewrite/src/javascript/visitor.ts | 10 +- .../parser/variableDeclarations.test.ts | 6 + .../javascript/remote/JavaScriptReceiver.java | 28 ++-- .../javascript/remote/JavaScriptSender.java | 14 +- .../javascript/JavaScriptVisitor.java | 21 +-- .../internal/JavaScriptPrinter.java | 8 +- .../org/openrewrite/javascript/tree/JS.java | 68 +++------ .../javascript/tree/JsRightPadded.java | 2 +- .../openrewrite/javascript/tree/JsSpace.java | 7 +- 14 files changed, 139 insertions(+), 242 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index ccbc929a..ba75bcbd 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -250,26 +250,7 @@ export class JavaScriptParserVisitor { private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration | ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration) { - if (ts.isVariableStatement(node)) { - return [new J.Modifier( - randomId(), - Space.EMPTY, - Markers.EMPTY, - node.declarationList.getFirstToken()?.getText()!, - J.Modifier.Type.LanguageExtension, - [] - )]; - } else if (ts.isModuleDeclaration(node)) { - return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; - } else if (ts.isClassDeclaration(node)) { - return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; - } else if (ts.isEnumDeclaration(node) || ts.isInterfaceDeclaration(node)) { - return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; - } else if (ts.isPropertyDeclaration(node)) { - return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; - } else if (ts.isPropertySignature(node)) { - return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; - } else if (ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node)) { + if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node) || ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node)) { return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; } else if (ts.isVariableDeclarationList(node)) { let modifier: string | undefined; @@ -310,7 +291,7 @@ export class JavaScriptParserVisitor { throw new Error(`Cannot get modifiers from ${node}`); } - private mapModifier = (node: ts.Modifier) => { + private mapModifier = (node: ts.Modifier | ts.ModifierLike) => { let kind: J.Modifier.Type; switch (node.kind) { case ts.SyntaxKind.PublicKeyword: @@ -1648,7 +1629,7 @@ export class JavaScriptParserVisitor { } visitVariableStatement(node: ts.VariableStatement) { - return this.visitVariableDeclarationList(node.declarationList); + return this.visitVariableDeclarationList(node.declarationList).withModifiers(this.mapModifiers(node)).withPrefix(this.prefix(node)); } visitExpressionStatement(node: ts.ExpressionStatement): J.Statement { @@ -1833,8 +1814,10 @@ export class JavaScriptParserVisitor { const kind = node.getFirstToken(this.sourceFile); return new JS.ScopedVariableDeclarations( randomId(), - this.prefix(node), + Space.EMPTY, Markers.EMPTY, + [], + this.prefix(node), kind?.kind === ts.SyntaxKind.LetKeyword ? JS.ScopedVariableDeclarations.Scope.Let : kind?.kind === ts.SyntaxKind.ConstKeyword ? JS.ScopedVariableDeclarations.Scope.Const : JS.ScopedVariableDeclarations.Scope.Var, node.declarations.map(declaration => { @@ -1995,16 +1978,16 @@ export class JavaScriptParserVisitor { const body = this.visit(node.body as ts.Node); let namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword); - const kindType = namespaceKeyword ? JS.NamespaceDeclaration.Kind.Type.Namespace : JS.NamespaceDeclaration.Kind.Type.Module + const keywordType = namespaceKeyword ? JS.NamespaceDeclaration.KeywordType.Namespace : JS.NamespaceDeclaration.KeywordType.Module namespaceKeyword ??= this.findChildNode(node, ts.SyntaxKind.ModuleKeyword); - const kind = new JS.NamespaceDeclaration.Kind(randomId(), this.prefix(namespaceKeyword!), Markers.EMPTY, kindType) if (body instanceof JS.NamespaceDeclaration) { return new JS.NamespaceDeclaration( randomId(), Space.EMPTY, Markers.EMPTY, this.mapModifiers(node), - kind, + namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY, + keywordType, this.rightPadded( new J.FieldAccess( randomId(), @@ -2028,7 +2011,8 @@ export class JavaScriptParserVisitor { node.parent.kind === ts.SyntaxKind.ModuleBlock ? this.prefix(node) : Space.EMPTY, Markers.EMPTY, this.mapModifiers(node), - kind, + namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY, + keywordType, this.rightPadded(this.convert(node.name), this.prefix(node)), // J.FieldAccess body // J.Block ); diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index 00827c5c..38368567 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -183,6 +183,8 @@ class Visitor extends JavaScriptVisitor { scopedVariableDeclarations = scopedVariableDeclarations.withId(ctx.receiveValue(scopedVariableDeclarations.id, ValueType.UUID)!); scopedVariableDeclarations = scopedVariableDeclarations.withPrefix(ctx.receiveNode(scopedVariableDeclarations.prefix, receiveSpace)!); scopedVariableDeclarations = scopedVariableDeclarations.withMarkers(ctx.receiveNode(scopedVariableDeclarations.markers, ctx.receiveMarkers)!); + scopedVariableDeclarations = scopedVariableDeclarations.withModifiers(ctx.receiveNodes(scopedVariableDeclarations.modifiers, ctx.receiveTree)!); + scopedVariableDeclarations = scopedVariableDeclarations.withScopePrefix(ctx.receiveNode(scopedVariableDeclarations.scopePrefix, receiveSpace)!); scopedVariableDeclarations = scopedVariableDeclarations.withScope(ctx.receiveValue(scopedVariableDeclarations.scope, ValueType.Enum)); scopedVariableDeclarations = scopedVariableDeclarations.padding.withVariables(ctx.receiveNodes(scopedVariableDeclarations.padding.variables, receiveRightPaddedTree)!); return scopedVariableDeclarations; @@ -345,20 +347,13 @@ class Visitor extends JavaScriptVisitor { namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNode(namespaceDeclaration.prefix, receiveSpace)!); namespaceDeclaration = namespaceDeclaration.withMarkers(ctx.receiveNode(namespaceDeclaration.markers, ctx.receiveMarkers)!); namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNodes(namespaceDeclaration.modifiers, ctx.receiveTree)!); - namespaceDeclaration = namespaceDeclaration.withKind(ctx.receiveNode(namespaceDeclaration.kind, ctx.receiveTree)!); + namespaceDeclaration = namespaceDeclaration.withKeywordPrefix(ctx.receiveNode(namespaceDeclaration.keywordPrefix, receiveSpace)!); + namespaceDeclaration = namespaceDeclaration.withKeywordType(ctx.receiveValue(namespaceDeclaration.keywordType, ValueType.Enum)!); namespaceDeclaration = namespaceDeclaration.padding.withName(ctx.receiveNode(namespaceDeclaration.padding.name, receiveRightPaddedTree)!); namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)!); return namespaceDeclaration; } - public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, ctx: ReceiverContext): J { - kind = kind.withId(ctx.receiveValue(kind.id, ValueType.UUID)!); - kind = kind.withPrefix(ctx.receiveNode(kind.prefix, receiveSpace)!); - kind = kind.withMarkers(ctx.receiveNode(kind.markers, ctx.receiveMarkers)!); - kind = kind.withType(ctx.receiveValue(kind.type, ValueType.Enum)!); - return kind; - } - 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)!); @@ -1200,6 +1195,8 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.UUID)!, ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNodes(null, ctx.receiveTree)!, + ctx.receiveNode(null, receiveSpace)!, ctx.receiveValue(null, ValueType.Enum), ctx.receiveNodes(null, receiveRightPaddedTree)! ); @@ -1378,21 +1375,13 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveNodes(null, ctx.receiveTree)!, - ctx.receiveNode(null, ctx.receiveTree)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveValue(null, ValueType.Enum)!, ctx.receiveNode>(null, receiveRightPaddedTree)!, ctx.receiveNode(null, ctx.receiveTree)! ); } - if (type === "org.openrewrite.javascript.tree.JS$NamespaceDeclaration$Kind") { - return new NamespaceDeclaration.Kind( - ctx.receiveValue(null, ValueType.UUID)!, - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveValue(null, ValueType.Enum)! - ); - } - 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 de0177ee..8a698601 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -178,6 +178,8 @@ class Visitor extends JavaScriptVisitor { ctx.sendValue(scopedVariableDeclarations, v => v.id, ValueType.UUID); ctx.sendNode(scopedVariableDeclarations, v => v.prefix, Visitor.sendSpace); ctx.sendNode(scopedVariableDeclarations, v => v.markers, ctx.sendMarkers); + ctx.sendNodes(scopedVariableDeclarations, v => v.modifiers, ctx.sendTree, t => t.id); + ctx.sendNode(scopedVariableDeclarations, v => v.scopePrefix, Visitor.sendSpace); ctx.sendValue(scopedVariableDeclarations, v => v.scope, ValueType.Enum); ctx.sendNodes(scopedVariableDeclarations, v => v.padding.variables, Visitor.sendRightPadded(ValueType.Tree), t => t.element.id); return scopedVariableDeclarations; @@ -340,20 +342,13 @@ class Visitor extends JavaScriptVisitor { ctx.sendNode(namespaceDeclaration, v => v.prefix, Visitor.sendSpace); ctx.sendNode(namespaceDeclaration, v => v.markers, ctx.sendMarkers); ctx.sendNodes(namespaceDeclaration, v => v.modifiers, ctx.sendTree, t => t.id); - ctx.sendNode(namespaceDeclaration, v => v.kind, ctx.sendTree); + ctx.sendNode(namespaceDeclaration, v => v.keywordPrefix, Visitor.sendSpace); + ctx.sendValue(namespaceDeclaration, v => v.keywordType, ValueType.Enum); ctx.sendNode(namespaceDeclaration, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree)); ctx.sendNode(namespaceDeclaration, v => v.body, ctx.sendTree); return namespaceDeclaration; } - public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, ctx: SenderContext): J { - ctx.sendValue(kind, v => v.id, ValueType.UUID); - ctx.sendNode(kind, v => v.prefix, Visitor.sendSpace); - ctx.sendNode(kind, v => v.markers, ctx.sendMarkers); - ctx.sendValue(kind, v => v.type, ValueType.Enum); - return kind; - } - 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/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index 961121b9..2bcc004b 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -201,6 +201,7 @@ export namespace JsSpace { OBJECT_BINDING_DECLARATIONS_PREFIX, PROPERTY_ASSIGNMENT_PREFIX, SCOPED_VARIABLE_DECLARATIONS_PREFIX, + SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX, TEMPLATE_EXPRESSION_PREFIX, TEMPLATE_EXPRESSION_VALUE_AFTER, TEMPLATE_EXPRESSION_VALUE_PREFIX, @@ -217,7 +218,7 @@ export namespace JsSpace { JSVARIABLE_DECLARATIONS_VARARGS, JSVARIABLE_DECLARATIONS_JSNAMED_VARIABLE_PREFIX, NAMESPACE_DECLARATION_PREFIX, - NAMESPACE_DECLARATION_NAMESPACE, + NAMESPACE_DECLARATION_KEYWORD_PREFIX, JSMETHOD_DECLARATION_PREFIX } } diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index fbbc9151..395a58d2 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -1393,11 +1393,13 @@ export class PropertyAssignment extends JSMixin(Object) implements Statement, Ty @LstType("org.openrewrite.javascript.tree.JS$ScopedVariableDeclarations") export class ScopedVariableDeclarations extends JSMixin(Object) implements Statement { - public constructor(id: UUID, prefix: Space, markers: Markers, scope: ScopedVariableDeclarations.Scope | null, variables: JRightPadded[]) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], scopePrefix: Space, scope: ScopedVariableDeclarations.Scope | null, variables: JRightPadded[]) { super(); this._id = id; this._prefix = prefix; this._markers = markers; + this._modifiers = modifiers; + this._scopePrefix = scopePrefix; this._scope = scope; this._variables = variables; } @@ -1409,7 +1411,7 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State } public withId(id: UUID): ScopedVariableDeclarations { - return id === this._id ? this : new ScopedVariableDeclarations(id, this._prefix, this._markers, this._scope, this._variables); + return id === this._id ? this : new ScopedVariableDeclarations(id, this._prefix, this._markers, this._modifiers, this._scopePrefix, this._scope, this._variables); } private readonly _prefix: Space; @@ -1419,7 +1421,7 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State } public withPrefix(prefix: Space): ScopedVariableDeclarations { - return prefix === this._prefix ? this : new ScopedVariableDeclarations(this._id, prefix, this._markers, this._scope, this._variables); + return prefix === this._prefix ? this : new ScopedVariableDeclarations(this._id, prefix, this._markers, this._modifiers, this._scopePrefix, this._scope, this._variables); } private readonly _markers: Markers; @@ -1429,7 +1431,27 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State } public withMarkers(markers: Markers): ScopedVariableDeclarations { - return markers === this._markers ? this : new ScopedVariableDeclarations(this._id, this._prefix, markers, this._scope, this._variables); + return markers === this._markers ? this : new ScopedVariableDeclarations(this._id, this._prefix, markers, this._modifiers, this._scopePrefix, this._scope, this._variables); + } + + private readonly _modifiers: Java.Modifier[]; + + public get modifiers(): Java.Modifier[] { + return this._modifiers; + } + + public withModifiers(modifiers: Java.Modifier[]): ScopedVariableDeclarations { + return modifiers === this._modifiers ? this : new ScopedVariableDeclarations(this._id, this._prefix, this._markers, modifiers, this._scopePrefix, this._scope, this._variables); + } + + private readonly _scopePrefix: Space; + + public get scopePrefix(): Space { + return this._scopePrefix; + } + + public withScopePrefix(scopePrefix: Space): ScopedVariableDeclarations { + return scopePrefix === this._scopePrefix ? this : new ScopedVariableDeclarations(this._id, this._prefix, this._markers, this._modifiers, scopePrefix, this._scope, this._variables); } private readonly _scope: ScopedVariableDeclarations.Scope | null; @@ -1439,7 +1461,7 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State } public withScope(scope: ScopedVariableDeclarations.Scope | null): ScopedVariableDeclarations { - return scope === this._scope ? this : new ScopedVariableDeclarations(this._id, this._prefix, this._markers, scope, this._variables); + return scope === this._scope ? this : new ScopedVariableDeclarations(this._id, this._prefix, this._markers, this._modifiers, this._scopePrefix, scope, this._variables); } private readonly _variables: JRightPadded[]; @@ -1463,7 +1485,7 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State return t._variables; } public withVariables(variables: JRightPadded[]): ScopedVariableDeclarations { - return t._variables === variables ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._scope, variables); + return t._variables === variables ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, t._scopePrefix, t._scope, variables); } } } @@ -2838,13 +2860,14 @@ export class JSMethodDeclaration extends JSMixin(Object) implements Statement, T @LstType("org.openrewrite.javascript.tree.JS$NamespaceDeclaration") export class NamespaceDeclaration extends JSMixin(Object) implements Statement { - public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], kind: NamespaceDeclaration.Kind, name: JRightPadded, body: Java.Block) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], keywordPrefix: Space, keywordType: NamespaceDeclaration.KeywordType, name: JRightPadded, body: Java.Block) { super(); this._id = id; this._prefix = prefix; this._markers = markers; this._modifiers = modifiers; - this._kind = kind; + this._keywordPrefix = keywordPrefix; + this._keywordType = keywordType; this._name = name; this._body = body; } @@ -2856,7 +2879,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withId(id: UUID): NamespaceDeclaration { - return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._kind, this._name, this._body); + return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._keywordPrefix, this._keywordType, this._name, this._body); } private readonly _prefix: Space; @@ -2866,7 +2889,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withPrefix(prefix: Space): NamespaceDeclaration { - return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._kind, this._name, this._body); + return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._keywordPrefix, this._keywordType, this._name, this._body); } private readonly _markers: Markers; @@ -2876,7 +2899,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withMarkers(markers: Markers): NamespaceDeclaration { - return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._kind, this._name, this._body); + return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._keywordPrefix, this._keywordType, this._name, this._body); } private readonly _modifiers: Java.Modifier[]; @@ -2886,13 +2909,27 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withModifiers(modifiers: Java.Modifier[]): NamespaceDeclaration { - return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, this._kind, this._name, this._body); + return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, this._keywordPrefix, this._keywordType, this._name, this._body); + } + + private readonly _keywordPrefix: Space; + + public get keywordPrefix(): Space { + return this._keywordPrefix; + } + + public withKeywordPrefix(keywordPrefix: Space): NamespaceDeclaration { + return keywordPrefix === this._keywordPrefix ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, keywordPrefix, this._keywordType, this._name, this._body); } - private readonly _kind: NamespaceDeclaration.Kind; + private readonly _keywordType: NamespaceDeclaration.KeywordType; + + public get keywordType(): NamespaceDeclaration.KeywordType { + return this._keywordType; + } - public withKind(kind: NamespaceDeclaration.Kind): NamespaceDeclaration { - return kind === this._kind ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, kind, this._name, this._body); + public withKeywordType(keywordType: NamespaceDeclaration.KeywordType): NamespaceDeclaration { + return keywordType === this._keywordType ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._keywordPrefix, keywordType, this._name, this._body); } private readonly _name: JRightPadded; @@ -2912,7 +2949,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } public withBody(body: Java.Block): NamespaceDeclaration { - return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._kind, this._name, body); + return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._keywordPrefix, this._keywordType, this._name, body); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { @@ -2926,7 +2963,7 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { return t._name; } public withName(name: JRightPadded): NamespaceDeclaration { - return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._kind, name, t._body); + return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._keywordPrefix, t._keywordType, name, t._body); } } } @@ -2934,68 +2971,9 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { } export namespace NamespaceDeclaration { - @LstType("org.openrewrite.javascript.tree.JS$NamespaceDeclaration$Kind") - export class Kind extends JSMixin(Object) { - public constructor(id: UUID, prefix: Space, markers: Markers, _type: NamespaceDeclaration.Kind.Type) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._type = _type; - } - - private readonly _id: UUID; - - public get id(): UUID { - return this._id; - } - - public withId(id: UUID): NamespaceDeclaration.Kind { - return id === this._id ? this : new NamespaceDeclaration.Kind(id, this._prefix, this._markers, this._type); - } - - private readonly _prefix: Space; - - public get prefix(): Space { - return this._prefix; - } - - public withPrefix(prefix: Space): NamespaceDeclaration.Kind { - return prefix === this._prefix ? this : new NamespaceDeclaration.Kind(this._id, prefix, this._markers, this._type); - } - - private readonly _markers: Markers; - - public get markers(): Markers { - return this._markers; - } - - public withMarkers(markers: Markers): NamespaceDeclaration.Kind { - return markers === this._markers ? this : new NamespaceDeclaration.Kind(this._id, this._prefix, markers, this._type); - } - - private readonly _type: NamespaceDeclaration.Kind.Type; - - public get type(): NamespaceDeclaration.Kind.Type { - return this._type; - } - - public withType(_type: NamespaceDeclaration.Kind.Type): NamespaceDeclaration.Kind { - return _type === this._type ? this : new NamespaceDeclaration.Kind(this._id, this._prefix, this._markers, _type); - } - - public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { - return v.visitNamespaceDeclarationKind(this, p); - } - - } - - export namespace Kind { - export enum Type { - Namespace = 0, - Module = 1, - - } + export enum KeywordType { + Namespace = 0, + Module = 1, } diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index f12acfbd..41a36c6c 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -224,6 +224,8 @@ export class JavaScriptVisitor

extends JavaVisitor

{ } scopedVariableDeclarations = tempStatement as ScopedVariableDeclarations; scopedVariableDeclarations = scopedVariableDeclarations.withMarkers(this.visitMarkers(scopedVariableDeclarations.markers, p)); + scopedVariableDeclarations = scopedVariableDeclarations.withModifiers(ListUtils.map(scopedVariableDeclarations.modifiers, el => this.visitAndCast(el, p))); + scopedVariableDeclarations = scopedVariableDeclarations.withScopePrefix(this.visitJsSpace(scopedVariableDeclarations.scopePrefix, JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX, p)!); scopedVariableDeclarations = scopedVariableDeclarations.padding.withVariables(ListUtils.map(scopedVariableDeclarations.padding.variables, el => this.visitJsRightPadded(el, JsRightPadded.Location.SCOPED_VARIABLE_DECLARATIONS_VARIABLES, p))); return scopedVariableDeclarations; } @@ -452,18 +454,12 @@ export class JavaScriptVisitor

extends JavaVisitor

{ namespaceDeclaration = tempStatement as NamespaceDeclaration; namespaceDeclaration = namespaceDeclaration.withMarkers(this.visitMarkers(namespaceDeclaration.markers, p)); namespaceDeclaration = namespaceDeclaration.withModifiers(ListUtils.map(namespaceDeclaration.modifiers, el => this.visitAndCast(el, p))); - namespaceDeclaration = namespaceDeclaration.withKind(this.visitAndCast(namespaceDeclaration.kind, p)!); + namespaceDeclaration = namespaceDeclaration.withKeywordPrefix(this.visitJsSpace(namespaceDeclaration.keywordPrefix, JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p)!); namespaceDeclaration = namespaceDeclaration.padding.withName(this.visitJsRightPadded(namespaceDeclaration.padding.name, JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p)!); namespaceDeclaration = namespaceDeclaration.withBody(this.visitAndCast(namespaceDeclaration.body, p)!); return namespaceDeclaration; } - public visitNamespaceDeclarationKind(kind: NamespaceDeclaration.Kind, p: P): J | null { - kind = kind.withPrefix(this.visitJsSpace(kind.prefix, JsSpace.Location.NAMESPACE_DECLARATION_KIND_PREFIX, p)!); - kind = kind.withMarkers(this.visitMarkers(kind.markers, p)); - return kind; - } - public visitJsLeftPadded(left: JLeftPadded | null, loc: JsLeftPadded.Location, p: P): JLeftPadded { return extensions.visitJsLeftPadded(this, left, loc, p); } diff --git a/openrewrite/test/javascript/parser/variableDeclarations.test.ts b/openrewrite/test/javascript/parser/variableDeclarations.test.ts index c3a39879..6372f2f1 100644 --- a/openrewrite/test/javascript/parser/variableDeclarations.test.ts +++ b/openrewrite/test/javascript/parser/variableDeclarations.test.ts @@ -78,4 +78,10 @@ describe('variable declaration mapping', () => { typeScript(' /*0.1*/ let /*0.2*/ a /*1*/ : /*2*/ number =2 /*3*/ , /*4*/ b /*5*/:/*6*/ /*7*/string /*8*/ =/*9*/ "2" /*10*/ ; //11') ); }); + test('exported variables', () => { + rewriteRun( + //language=typescript + typeScript(' export /*0.1*/ let /*0.2*/ a /*1*/ : /*2*/ number =2 /*3*/ , /*4*/ b /*5*/:/*6*/ /*7*/string /*8*/ =/*9*/ "2" /*10*/ ; //11') + ); + }); }); 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 3443e9e6..ec29d57a 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 @@ -244,6 +244,8 @@ public JS.ScopedVariableDeclarations visitScopedVariableDeclarations(JS.ScopedVa scopedVariableDeclarations = scopedVariableDeclarations.withId(ctx.receiveNonNullValue(scopedVariableDeclarations.getId(), UUID.class)); scopedVariableDeclarations = scopedVariableDeclarations.withPrefix(ctx.receiveNonNullNode(scopedVariableDeclarations.getPrefix(), JavaScriptReceiver::receiveSpace)); scopedVariableDeclarations = scopedVariableDeclarations.withMarkers(ctx.receiveNonNullNode(scopedVariableDeclarations.getMarkers(), ctx::receiveMarkers)); + scopedVariableDeclarations = scopedVariableDeclarations.withModifiers(ctx.receiveNonNullNodes(scopedVariableDeclarations.getModifiers(), ctx::receiveTree)); + scopedVariableDeclarations = scopedVariableDeclarations.withScopePrefix(ctx.receiveNonNullNode(scopedVariableDeclarations.getScopePrefix(), JavaScriptReceiver::receiveSpace)); scopedVariableDeclarations = scopedVariableDeclarations.withScope(ctx.receiveValue(scopedVariableDeclarations.getScope(), JS.ScopedVariableDeclarations.Scope.class)); scopedVariableDeclarations = scopedVariableDeclarations.getPadding().withVariables(ctx.receiveNonNullNodes(scopedVariableDeclarations.getPadding().getVariables(), JavaScriptReceiver::receiveRightPaddedTree)); return scopedVariableDeclarations; @@ -422,21 +424,13 @@ public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration = namespaceDeclaration.withPrefix(ctx.receiveNonNullNode(namespaceDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace)); namespaceDeclaration = namespaceDeclaration.withMarkers(ctx.receiveNonNullNode(namespaceDeclaration.getMarkers(), ctx::receiveMarkers)); namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNonNullNodes(namespaceDeclaration.getModifiers(), ctx::receiveTree)); - namespaceDeclaration = namespaceDeclaration.withKind(ctx.receiveNonNullNode(namespaceDeclaration.getKind(), ctx::receiveTree)); + namespaceDeclaration = namespaceDeclaration.withKeywordPrefix(ctx.receiveNonNullNode(namespaceDeclaration.getKeywordPrefix(), JavaScriptReceiver::receiveSpace)); + namespaceDeclaration = namespaceDeclaration.withKeywordType(ctx.receiveNonNullValue(namespaceDeclaration.getKeywordType(), JS.NamespaceDeclaration.KeywordType.class)); namespaceDeclaration = namespaceDeclaration.getPadding().withName(ctx.receiveNonNullNode(namespaceDeclaration.getPadding().getName(), JavaScriptReceiver::receiveRightPaddedTree)); namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNonNullNode(namespaceDeclaration.getBody(), ctx::receiveTree)); return namespaceDeclaration; } - @Override - public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, ReceiverContext ctx) { - kind = kind.withId(ctx.receiveNonNullValue(kind.getId(), UUID.class)); - kind = kind.withPrefix(ctx.receiveNonNullNode(kind.getPrefix(), JavaScriptReceiver::receiveSpace)); - kind = kind.withMarkers(ctx.receiveNonNullNode(kind.getMarkers(), ctx::receiveMarkers)); - kind = kind.withType(ctx.receiveNonNullValue(kind.getType(), JS.NamespaceDeclaration.Kind.Type.class)); - return kind; - } - @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class)); @@ -1308,6 +1302,8 @@ public T create(Class type, ReceiverContext ctx) { ctx.receiveNonNullValue(null, UUID.class), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNodes(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveValue(null, JS.ScopedVariableDeclarations.Scope.class), ctx.receiveNonNullNodes(null, JavaScriptReceiver::receiveRightPaddedTree) ); @@ -1486,21 +1482,13 @@ public T create(Class type, ReceiverContext ctx) { ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), ctx.receiveNonNullNodes(null, ctx::receiveTree), - ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullValue(null, JS.NamespaceDeclaration.KeywordType.class), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree), ctx.receiveNonNullNode(null, ctx::receiveTree) ); } - if (type == JS.NamespaceDeclaration.Kind.class) { - return (T) new JS.NamespaceDeclaration.Kind( - ctx.receiveNonNullValue(null, UUID.class), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNonNullValue(null, JS.NamespaceDeclaration.Kind.Type.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 ab4ee627..5a0ecb10 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 @@ -227,6 +227,8 @@ public JS.ScopedVariableDeclarations visitScopedVariableDeclarations(JS.ScopedVa ctx.sendValue(scopedVariableDeclarations, JS.ScopedVariableDeclarations::getId); ctx.sendNode(scopedVariableDeclarations, JS.ScopedVariableDeclarations::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(scopedVariableDeclarations, JS.ScopedVariableDeclarations::getMarkers, ctx::sendMarkers); + ctx.sendNodes(scopedVariableDeclarations, JS.ScopedVariableDeclarations::getModifiers, ctx::sendTree, Tree::getId); + ctx.sendNode(scopedVariableDeclarations, JS.ScopedVariableDeclarations::getScopePrefix, JavaScriptSender::sendSpace); ctx.sendValue(scopedVariableDeclarations, JS.ScopedVariableDeclarations::getScope); ctx.sendNodes(scopedVariableDeclarations, e -> e.getPadding().getVariables(), JavaScriptSender::sendRightPadded, e -> e.getElement().getId()); return scopedVariableDeclarations; @@ -405,21 +407,13 @@ public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getMarkers, ctx::sendMarkers); ctx.sendNodes(namespaceDeclaration, JS.NamespaceDeclaration::getModifiers, ctx::sendTree, Tree::getId); - ctx.sendNode(namespaceDeclaration, e -> e.getPadding().getKind(), ctx::sendTree); + ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getKeywordPrefix, JavaScriptSender::sendSpace); + ctx.sendValue(namespaceDeclaration, JS.NamespaceDeclaration::getKeywordType); ctx.sendNode(namespaceDeclaration, e -> e.getPadding().getName(), JavaScriptSender::sendRightPadded); ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getBody, ctx::sendTree); return namespaceDeclaration; } - @Override - public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, SenderContext ctx) { - ctx.sendValue(kind, JS.NamespaceDeclaration.Kind::getId); - ctx.sendNode(kind, JS.NamespaceDeclaration.Kind::getPrefix, JavaScriptSender::sendSpace); - ctx.sendNode(kind, JS.NamespaceDeclaration.Kind::getMarkers, ctx::sendMarkers); - ctx.sendValue(kind, JS.NamespaceDeclaration.Kind::getType); - return kind; - } - @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.sendValue(annotatedType, J.AnnotatedType::getId); 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 81adca99..73e218e3 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -22,7 +22,6 @@ import org.openrewrite.java.tree.*; import org.openrewrite.javascript.tree.*; import org.openrewrite.marker.Markers; -import org.openrewrite.remote.SenderContext; import java.util.List; import java.util.Objects; @@ -321,6 +320,9 @@ public J visitScopedVariableDeclarations(JS.ScopedVariableDeclarations scopedVar JS.ScopedVariableDeclarations vd = scopedVariableDeclarations; vd = vd.withPrefix(visitSpace(vd.getPrefix(), JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_PREFIX, p)); vd = vd.withMarkers(visitMarkers(vd.getMarkers(), p)); + vd = vd.withModifiers(ListUtils.map(vd.getModifiers(), + mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); + vd = vd.withScopePrefix(visitSpace(vd.getScopePrefix(), JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX, p)); Statement temp = (Statement) visitStatement(vd, p); if (!(temp instanceof JS.ScopedVariableDeclarations)) { return temp; @@ -660,7 +662,7 @@ public J visitJSMethodDeclaration(JS.JSMethodDeclaration method, P p) { public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, P p) { JS.NamespaceDeclaration ns = namespaceDeclaration; - ns = ns.withPrefix(visitSpace(ns.getPrefix(), JsSpace.Location.JSNAMESPACE_DECLARATION_PREFIX, p)); + ns = ns.withPrefix(visitSpace(ns.getPrefix(), JsSpace.Location.NAMESPACE_DECLARATION_PREFIX, p)); ns = ns.withMarkers(visitMarkers(ns.getMarkers(), p)); ns = ns.withModifiers(ListUtils.map(ns.getModifiers(), mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); @@ -671,22 +673,9 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, } else { ns = (JS.NamespaceDeclaration) temp; } - ns = + ns = ns.withKeywordPrefix(visitSpace(ns.getKeywordPrefix(), JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p)); ns = ns.getPadding().withName(visitRightPadded(ns.getPadding().getName(), JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p)); ns = ns.withBody(visitAndCast(ns.getBody(), p)); return ns; } - - public JS.NamespaceDeclaration.Kind visitNamespaceDeclarationKind(JS.NamespaceDeclaration.Kind kind, SenderContext ctx) { - kind.getPadding().withKind( - namespaceDeclaration.getPadding().getKind().withMarkers( - visitMarkers(namespaceDeclaration.getPadding().getKind().getMarkers(), p) - ) - ); - ns = ns.getPadding().withKind( - ns.getPadding().getKind().withPrefix( - visitSpace(ns.getPadding().getKind().getPrefix(), JsSpace.Location.JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, p) - ) - ); - } } 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 bff33b09..a9c36c64 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 @@ -252,10 +252,10 @@ public J visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, Print @Override public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, PrintOutputCapture

p) { - beforeSyntax(namespaceDeclaration, JsSpace.Location.JSNAMESPACE_DECLARATION_PREFIX, p); + beforeSyntax(namespaceDeclaration, JsSpace.Location.NAMESPACE_DECLARATION_PREFIX, p); namespaceDeclaration.getModifiers().forEach(it -> delegate.visitModifier(it, p)); - visitSpace(namespaceDeclaration.getPadding().getKind().getPrefix(), JsSpace.Location.JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, p); - switch (namespaceDeclaration.getKind()) { + visitSpace(namespaceDeclaration.getKeywordPrefix(), JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p); + switch (namespaceDeclaration.getKeywordType()) { case Namespace: p.append("namespace"); break; @@ -272,7 +272,9 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, @Override public J visitScopedVariableDeclarations(JS.ScopedVariableDeclarations variableDeclarations, PrintOutputCapture

p) { beforeSyntax(variableDeclarations, Space.Location.VARIABLE_DECLARATIONS_PREFIX, p); + variableDeclarations.getModifiers().forEach(m -> delegate.visitModifier(m, p)); + visitSpace(variableDeclarations.getScopePrefix(), JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX, p); if (variableDeclarations.getScope() != null) { switch (variableDeclarations.getScope()) { case Let: 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 99f52b28..9249c9c6 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 @@ -1350,6 +1350,14 @@ class ScopedVariableDeclarations implements JS, Statement { @With Markers markers; + @With + @Getter + List modifiers; + + @Getter + @With + Space scopePrefix; + @Getter @With @Nullable @@ -1405,7 +1413,7 @@ public List> getVariables() { } public ScopedVariableDeclarations withVariables(List> variables) { - return t.variables == variables ? t : new ScopedVariableDeclarations(t.id, t.prefix, t.markers, t.scope, variables); + return t.variables == variables ? t : new ScopedVariableDeclarations(t.id, t.prefix, t.markers, t.modifiers, t.scopePrefix, t.scope, variables); } } } @@ -2587,20 +2595,13 @@ final class NamespaceDeclaration implements JS, Statement { @Getter List modifiers; - Kind kind; - - public Kind.Type getKind() { - return kind.getType(); - } + @With + @Getter + Space keywordPrefix; - public NamespaceDeclaration withKind(Kind.Type type) { - Kind k = getPadding().getKind(); - if (k.type == type) { - return this; - } else { - return getPadding().withKind(k.withType(type)); - } - } + @With + @Getter + KeywordType keywordType; JRightPadded name; @@ -2627,30 +2628,6 @@ public CoordinateBuilder.Statement getCoordinates() { return new CoordinateBuilder.Statement(this); } - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) - @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) - @Data - public static final class Kind implements JS { - - @With - @EqualsAndHashCode.Include - UUID id; - - @With - Space prefix; - - @With - Markers markers; - - @With - Type type; - - public enum Type { - Namespace, - Module, - } - } - public NamespaceDeclaration.Padding getPadding() { NamespaceDeclaration.Padding p; if (this.padding == null) { @@ -2666,25 +2643,22 @@ public NamespaceDeclaration.Padding getPadding() { return p; } + public enum KeywordType { + Namespace, + Module, + } + @RequiredArgsConstructor public static class Padding { private final NamespaceDeclaration t; public NamespaceDeclaration withName(JRightPadded name) { - return t.name == name ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.kind, name, t.body); + return t.name == name ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.keywordPrefix, t.keywordType, name, t.body); } public JRightPadded getName() { return t.name; } - - public Kind getKind() { - return t.kind; - } - - public NamespaceDeclaration withKind(Kind kind) { - return t.kind == kind ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, kind, t.name, t.body); - } } } } 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 5e76f9d0..164a9638 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 @@ -34,7 +34,7 @@ public enum Location { TUPLE_ELEMENT_SUFFIX(JsSpace.Location.TUPLE_ELEMENT_SUFFIX), UNION_TYPE(JsSpace.Location.UNION_TYPE_SUFFIX), JSNAMED_VARIABLE(JsSpace.Location.JSNAMED_VARIABLE_SUFFIX), - NAMESPACE_DECLARATION_NAME(JsSpace.Location.JSNAMESPACE_DECLARATION_PREFIX), + NAMESPACE_DECLARATION_NAME(JsSpace.Location.NAMESPACE_DECLARATION_PREFIX), JSMETHOD_DECLARATION_PARAMETER(JsSpace.Location.JSMETHOD_DECLARATION_PARAMETERS); 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 3c0bdfb0..2cd28ffa 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 @@ -52,6 +52,7 @@ public enum Location { PROPERTY_ASSIGNMENT_NAME_SUFFIX, PROPERTY_ASSIGNMENT_PREFIX, SCOPED_VARIABLE_DECLARATIONS_PREFIX, + SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX, SCOPED_VARIABLE_DECLARATIONS_VARIABLE_SUFFIX, TAG_SUFFIX, TEMPLATE_EXPRESSION_PREFIX, @@ -77,9 +78,9 @@ public enum Location { JSVARIABLE_PREFIX, JSVARIABLE_INITIALIZER, JSNAMED_VARIABLE_SUFFIX, - JSNAMESPACE_DECLARATION_PREFIX, JSMETHOD_DECLARATION_PARAMETERS, - JSNAMESPACE_KEYWORD_DECLARATION_PREFIX, - JSMETHOD_DECLARATION_PREFIX + JSMETHOD_DECLARATION_PREFIX, + NAMESPACE_DECLARATION_PREFIX, + NAMESPACE_DECLARATION_KEYWORD_PREFIX, } }