diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 48432979..3a0079ee 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1841,21 +1841,29 @@ export class JavaScriptParserVisitor { 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 => { - return this.rightPadded(new J.VariableDeclarations( - randomId(), - this.prefix(declaration), - Markers.EMPTY, - [], // FIXME decorators? - [], // FIXME modifiers? - this.mapTypeInfo(declaration), - null, // FIXME varargs - [], - [this.rightPadded(this.visit(declaration), Space.EMPTY)] - ), this.suffix(declaration)); + this.leftPadded( + 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) => { + return this.rightPadded( + new J.VariableDeclarations( + randomId(), + this.prefix(declaration), + Markers.EMPTY, + [], // FIXME decorators? + [], // FIXME modifiers? + this.mapTypeInfo(declaration), + null, // FIXME varargs + [], + [this.rightPadded(this.visit(declaration), Space.EMPTY)] + ), + this.suffix(declaration) + ); }) ); } @@ -2010,8 +2018,10 @@ export class JavaScriptParserVisitor { Space.EMPTY, Markers.EMPTY, this.mapModifiers(node), - namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY, - keywordType, + this.leftPadded( + namespaceKeyword ? this.prefix(namespaceKeyword) : Space.EMPTY, + keywordType + ), this.rightPadded( new J.FieldAccess( randomId(), @@ -2035,8 +2045,10 @@ 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, - keywordType, + this.leftPadded( + 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 38368567..2c22dce2 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -184,8 +184,7 @@ class Visitor extends JavaScriptVisitor { 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.withScope(ctx.receiveNode(scopedVariableDeclarations.padding.scope, leftPaddedValueReceiver(ValueType.Enum))); scopedVariableDeclarations = scopedVariableDeclarations.padding.withVariables(ctx.receiveNodes(scopedVariableDeclarations.padding.variables, receiveRightPaddedTree)!); return scopedVariableDeclarations; } @@ -347,8 +346,7 @@ 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.withKeywordPrefix(ctx.receiveNode(namespaceDeclaration.keywordPrefix, receiveSpace)!); - namespaceDeclaration = namespaceDeclaration.withKeywordType(ctx.receiveValue(namespaceDeclaration.keywordType, ValueType.Enum)!); + namespaceDeclaration = namespaceDeclaration.padding.withKeywordType(ctx.receiveNode(namespaceDeclaration.padding.keywordType, leftPaddedValueReceiver(ValueType.Enum))!); namespaceDeclaration = namespaceDeclaration.padding.withName(ctx.receiveNode(namespaceDeclaration.padding.name, receiveRightPaddedTree)!); namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)!); return namespaceDeclaration; @@ -1196,8 +1194,7 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveNodes(null, ctx.receiveTree)!, - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveValue(null, ValueType.Enum), + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Enum)), ctx.receiveNodes(null, receiveRightPaddedTree)! ); } @@ -1375,8 +1372,7 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveNodes(null, ctx.receiveTree)!, - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveValue(null, ValueType.Enum)!, + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Enum))!, ctx.receiveNode>(null, receiveRightPaddedTree)!, ctx.receiveNode(null, ctx.receiveTree)! ); diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index 8a698601..e5afaa85 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -179,8 +179,7 @@ class Visitor extends JavaScriptVisitor { 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.sendNode(scopedVariableDeclarations, v => v.padding.scope, Visitor.sendLeftPadded(ValueType.Enum)); ctx.sendNodes(scopedVariableDeclarations, v => v.padding.variables, Visitor.sendRightPadded(ValueType.Tree), t => t.element.id); return scopedVariableDeclarations; } @@ -342,8 +341,7 @@ 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.keywordPrefix, Visitor.sendSpace); - ctx.sendValue(namespaceDeclaration, v => v.keywordType, ValueType.Enum); + ctx.sendNode(namespaceDeclaration, v => v.padding.keywordType, Visitor.sendLeftPadded(ValueType.Enum)); ctx.sendNode(namespaceDeclaration, v => v.padding.name, Visitor.sendRightPadded(ValueType.Tree)); ctx.sendNode(namespaceDeclaration, v => v.body, ctx.sendTree); return namespaceDeclaration; diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index 2bcc004b..58f6d45b 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -236,7 +236,9 @@ export namespace JsLeftPadded { UNARY_OPERATOR, JSVARIABLE_DECLARATIONS_JSNAMED_VARIABLE_INITIALIZER, JSVARIABLE_DECLARATIONS_JSNAMED_VARIABLE_DIMENSIONS_AFTER_NAME, - JSMETHOD_DECLARATION_DEFAULT_VALUE + JSMETHOD_DECLARATION_DEFAULT_VALUE, + NAMESPACE_DECLARATION_KEYWORD_TYPE, + SCOPED_VARIABLE_DECLARATIONS_SCOPE } } export namespace JsRightPadded { diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 395a58d2..46f70bc6 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -1393,13 +1393,12 @@ 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, modifiers: Java.Modifier[], scopePrefix: Space, scope: ScopedVariableDeclarations.Scope | null, variables: JRightPadded[]) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], scope: JLeftPadded | 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; } @@ -1411,7 +1410,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._modifiers, this._scopePrefix, this._scope, this._variables); + return id === this._id ? this : new ScopedVariableDeclarations(id, this._prefix, this._markers, this._modifiers, this._scope, this._variables); } private readonly _prefix: Space; @@ -1421,7 +1420,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._modifiers, this._scopePrefix, this._scope, this._variables); + return prefix === this._prefix ? this : new ScopedVariableDeclarations(this._id, prefix, this._markers, this._modifiers, this._scope, this._variables); } private readonly _markers: Markers; @@ -1431,7 +1430,7 @@ 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._modifiers, this._scopePrefix, this._scope, this._variables); + return markers === this._markers ? this : new ScopedVariableDeclarations(this._id, this._prefix, markers, this._modifiers, this._scope, this._variables); } private readonly _modifiers: Java.Modifier[]; @@ -1441,27 +1440,17 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State } 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); + return modifiers === this._modifiers ? this : new ScopedVariableDeclarations(this._id, this._prefix, this._markers, modifiers, 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; + private readonly _scope: JLeftPadded | null; public get scope(): ScopedVariableDeclarations.Scope | null { - return this._scope; + return this._scope === null ? null : this._scope.element; } public withScope(scope: ScopedVariableDeclarations.Scope | null): ScopedVariableDeclarations { - return scope === this._scope ? this : new ScopedVariableDeclarations(this._id, this._prefix, this._markers, this._modifiers, this._scopePrefix, scope, this._variables); + return this.padding.withScope(JLeftPadded.withElement(this._scope, scope)); } private readonly _variables: JRightPadded[]; @@ -1481,11 +1470,17 @@ export class ScopedVariableDeclarations extends JSMixin(Object) implements State get padding() { const t = this; return new class { + public get scope(): JLeftPadded | null { + return t._scope; + } + public withScope(scope: JLeftPadded | null): ScopedVariableDeclarations { + return t._scope === scope ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, scope, t._variables); + } public get variables(): JRightPadded[] { return t._variables; } public withVariables(variables: JRightPadded[]): ScopedVariableDeclarations { - return t._variables === variables ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, t._scopePrefix, t._scope, variables); + return t._variables === variables ? t : new ScopedVariableDeclarations(t._id, t._prefix, t._markers, t._modifiers, t._scope, variables); } } } @@ -2860,13 +2855,12 @@ 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[], keywordPrefix: Space, keywordType: NamespaceDeclaration.KeywordType, name: JRightPadded, body: Java.Block) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], keywordType: JLeftPadded, name: JRightPadded, body: Java.Block) { super(); this._id = id; this._prefix = prefix; this._markers = markers; this._modifiers = modifiers; - this._keywordPrefix = keywordPrefix; this._keywordType = keywordType; this._name = name; this._body = body; @@ -2879,7 +2873,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._keywordPrefix, this._keywordType, this._name, this._body); + return id === this._id ? this : new NamespaceDeclaration(id, this._prefix, this._markers, this._modifiers, this._keywordType, this._name, this._body); } private readonly _prefix: Space; @@ -2889,7 +2883,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._keywordPrefix, this._keywordType, this._name, this._body); + return prefix === this._prefix ? this : new NamespaceDeclaration(this._id, prefix, this._markers, this._modifiers, this._keywordType, this._name, this._body); } private readonly _markers: Markers; @@ -2899,7 +2893,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._keywordPrefix, this._keywordType, this._name, this._body); + return markers === this._markers ? this : new NamespaceDeclaration(this._id, this._prefix, markers, this._modifiers, this._keywordType, this._name, this._body); } private readonly _modifiers: Java.Modifier[]; @@ -2909,27 +2903,17 @@ 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._keywordPrefix, this._keywordType, this._name, this._body); + return modifiers === this._modifiers ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, modifiers, 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 _keywordType: NamespaceDeclaration.KeywordType; + private readonly _keywordType: JLeftPadded; public get keywordType(): NamespaceDeclaration.KeywordType { - return this._keywordType; + return this._keywordType.element; } 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); + return this.padding.withKeywordType(this._keywordType.withElement(keywordType)); } private readonly _name: JRightPadded; @@ -2949,7 +2933,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._keywordPrefix, this._keywordType, this._name, body); + return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._keywordType, this._name, body); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { @@ -2959,11 +2943,17 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { get padding() { const t = this; return new class { + public get keywordType(): JLeftPadded { + return t._keywordType; + } + public withKeywordType(keywordType: JLeftPadded): NamespaceDeclaration { + return t._keywordType === keywordType ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, keywordType, t._name, t._body); + } public get name(): JRightPadded { return t._name; } public withName(name: JRightPadded): NamespaceDeclaration { - return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._keywordPrefix, t._keywordType, name, t._body); + return t._name === name ? t : new NamespaceDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._keywordType, name, t._body); } } } diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index 41a36c6c..b4867767 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -225,7 +225,7 @@ 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.withScope(this.visitJsLeftPadded(scopedVariableDeclarations.padding.scope, JsLeftPadded.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE, p)); scopedVariableDeclarations = scopedVariableDeclarations.padding.withVariables(ListUtils.map(scopedVariableDeclarations.padding.variables, el => this.visitJsRightPadded(el, JsRightPadded.Location.SCOPED_VARIABLE_DECLARATIONS_VARIABLES, p))); return scopedVariableDeclarations; } @@ -454,7 +454,7 @@ 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.withKeywordPrefix(this.visitJsSpace(namespaceDeclaration.keywordPrefix, JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p)!); + namespaceDeclaration = namespaceDeclaration.padding.withKeywordType(this.visitJsLeftPadded(namespaceDeclaration.padding.keywordType, JsLeftPadded.Location.NAMESPACE_DECLARATION_KEYWORD_TYPE, 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; 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 ec29d57a..7048ca29 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 @@ -245,8 +245,7 @@ public JS.ScopedVariableDeclarations visitScopedVariableDeclarations(JS.ScopedVa 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().withScope(ctx.receiveNode(scopedVariableDeclarations.getPadding().getScope(), leftPaddedValueReceiver(org.openrewrite.javascript.tree.JS.ScopedVariableDeclarations.Scope.class))); scopedVariableDeclarations = scopedVariableDeclarations.getPadding().withVariables(ctx.receiveNonNullNodes(scopedVariableDeclarations.getPadding().getVariables(), JavaScriptReceiver::receiveRightPaddedTree)); return scopedVariableDeclarations; } @@ -424,8 +423,7 @@ 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.withKeywordPrefix(ctx.receiveNonNullNode(namespaceDeclaration.getKeywordPrefix(), JavaScriptReceiver::receiveSpace)); - namespaceDeclaration = namespaceDeclaration.withKeywordType(ctx.receiveNonNullValue(namespaceDeclaration.getKeywordType(), JS.NamespaceDeclaration.KeywordType.class)); + namespaceDeclaration = namespaceDeclaration.getPadding().withKeywordType(ctx.receiveNonNullNode(namespaceDeclaration.getPadding().getKeywordType(), leftPaddedValueReceiver(org.openrewrite.javascript.tree.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; @@ -1303,8 +1301,7 @@ 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.receiveValue(null, JS.ScopedVariableDeclarations.Scope.class), + ctx.receiveNode(null, leftPaddedValueReceiver(org.openrewrite.javascript.tree.JS.ScopedVariableDeclarations.Scope.class)), ctx.receiveNonNullNodes(null, JavaScriptReceiver::receiveRightPaddedTree) ); } @@ -1482,8 +1479,7 @@ 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.receiveNonNullValue(null, JS.NamespaceDeclaration.KeywordType.class), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(org.openrewrite.javascript.tree.JS.NamespaceDeclaration.KeywordType.class)), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree), ctx.receiveNonNullNode(null, ctx::receiveTree) ); 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 5a0ecb10..b2edcb80 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 @@ -228,8 +228,7 @@ public JS.ScopedVariableDeclarations visitScopedVariableDeclarations(JS.ScopedVa 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.sendNode(scopedVariableDeclarations, e -> e.getPadding().getScope(), JavaScriptSender::sendLeftPadded); ctx.sendNodes(scopedVariableDeclarations, e -> e.getPadding().getVariables(), JavaScriptSender::sendRightPadded, e -> e.getElement().getId()); return scopedVariableDeclarations; } @@ -407,8 +406,7 @@ 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::getKeywordPrefix, JavaScriptSender::sendSpace); - ctx.sendValue(namespaceDeclaration, JS.NamespaceDeclaration::getKeywordType); + ctx.sendNode(namespaceDeclaration, e -> e.getPadding().getKeywordType(), JavaScriptSender::sendLeftPadded); ctx.sendNode(namespaceDeclaration, e -> e.getPadding().getName(), JavaScriptSender::sendRightPadded); ctx.sendNode(namespaceDeclaration, JS.NamespaceDeclaration::getBody, ctx::sendTree); return namespaceDeclaration; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptParser.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptParser.java index ebb8732b..86b146fa 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptParser.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptParser.java @@ -191,7 +191,7 @@ private static Builder verifyRemotingInstallation(Path dir) throws IOException, exportResource("META-INF/package.json", dir.toFile()); - List command = new ArrayList(Arrays.asList("npm", "install", "--force")); + List command = new ArrayList(Arrays.asList("npm", "update", "--force")); ProcessBuilder processBuilder = new ProcessBuilder(command); Process process = processBuilder.directory(dir.toFile()).start(); int exitCode = process.waitFor(); 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 73e218e3..7705c5e4 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -322,7 +322,7 @@ public J visitScopedVariableDeclarations(JS.ScopedVariableDeclarations scopedVar 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)); + vd = vd.getPadding().withScope(visitLeftPadded(vd.getPadding().getScope(), JsLeftPadded.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE, p)); Statement temp = (Statement) visitStatement(vd, p); if (!(temp instanceof JS.ScopedVariableDeclarations)) { return temp; @@ -664,16 +664,16 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, JS.NamespaceDeclaration ns = namespaceDeclaration; 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)))); - ns = ns.withModifiers(ListUtils.map(ns.getModifiers(), m -> visitAndCast(m, p))); Statement temp = (Statement) visitStatement(ns, p); if (!(temp instanceof JS.NamespaceDeclaration)) { return temp; } else { ns = (JS.NamespaceDeclaration) temp; } - ns = ns.withKeywordPrefix(visitSpace(ns.getKeywordPrefix(), JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p)); + ns = ns.withModifiers(ListUtils.map(ns.getModifiers(), + mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); + ns = ns.withModifiers(ListUtils.map(ns.getModifiers(), m -> visitAndCast(m, p))); + ns = ns.getPadding().withKeywordType(visitLeftPadded(ns.getPadding().getKeywordType(), JsLeftPadded.Location.NAMESPACE_DECLARATION_KEYWORD_TYPE, p)); ns = ns.getPadding().withName(visitRightPadded(ns.getPadding().getName(), JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p)); ns = ns.withBody(visitAndCast(ns.getBody(), p)); return ns; 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 a9c36c64..cd9e2579 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,7 +254,7 @@ public J visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, Print public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, PrintOutputCapture

p) { beforeSyntax(namespaceDeclaration, JsSpace.Location.NAMESPACE_DECLARATION_PREFIX, p); namespaceDeclaration.getModifiers().forEach(it -> delegate.visitModifier(it, p)); - visitSpace(namespaceDeclaration.getKeywordPrefix(), JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p); + visitSpace(namespaceDeclaration.getPadding().getKeywordType().getBefore(), JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX, p); switch (namespaceDeclaration.getKeywordType()) { case Namespace: p.append("namespace"); @@ -274,9 +274,10 @@ public J visitScopedVariableDeclarations(JS.ScopedVariableDeclarations variableD 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()) { + JLeftPadded scope = variableDeclarations.getPadding().getScope(); + if (scope != null) { + visitSpace(scope.getBefore(), JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX, p); + switch (scope.getElement()) { case Let: p.append("let"); break; 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 9249c9c6..f1f3f48f 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 @@ -1354,14 +1354,17 @@ class ScopedVariableDeclarations implements JS, Statement { @Getter List modifiers; - @Getter - @With - Space scopePrefix; + @Nullable + JLeftPadded scope; - @Getter - @With @Nullable - Scope scope; + public Scope getScope() { + return scope != null ? scope.getElement() : null; + } + + public ScopedVariableDeclarations withScope(@Nullable Scope scope) { + return getPadding().withScope(JLeftPadded.withElement(this.scope, scope)); + } List> variables; @@ -1413,7 +1416,16 @@ public List> getVariables() { } public ScopedVariableDeclarations withVariables(List> variables) { - return t.variables == variables ? t : new ScopedVariableDeclarations(t.id, t.prefix, t.markers, t.modifiers, t.scopePrefix, t.scope, variables); + return t.variables == variables ? t : new ScopedVariableDeclarations(t.id, t.prefix, t.markers, t.modifiers, t.scope, variables); + } + + @Nullable + public JLeftPadded getScope() { + return t.scope; + } + + public ScopedVariableDeclarations withScope(@Nullable JLeftPadded scope) { + return t.scope == scope ? t : new ScopedVariableDeclarations(t.id, t.prefix, t.markers, t.modifiers, scope, t.variables); } } } @@ -2595,13 +2607,15 @@ final class NamespaceDeclaration implements JS, Statement { @Getter List modifiers; - @With - @Getter - Space keywordPrefix; + JLeftPadded keywordType; - @With - @Getter - KeywordType keywordType; + public KeywordType getKeywordType() { + return keywordType.getElement(); + } + + public NamespaceDeclaration withKeywordType(KeywordType kt) { + return getPadding().withKeywordType(JLeftPadded.withElement(this.keywordType, kt)); + } JRightPadded name; @@ -2653,12 +2667,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.keywordPrefix, t.keywordType, name, t.body); + return t.name == name ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.keywordType, name, t.body); } public JRightPadded getName() { return t.name; } + + public JLeftPadded getKeywordType() { + return t.keywordType; + } + + public NamespaceDeclaration withKeywordType(JLeftPadded keywordType) { + return t.keywordType == keywordType ? t : new NamespaceDeclaration(t.id, t.prefix, t.markers, t.modifiers, keywordType, t.name, t.body); + } } } } 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 3aabbe51..9fdedc76 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 @@ -27,6 +27,8 @@ public enum Location { TYPE_DECLARATION_INITIALIZER(JsSpace.Location.TYPE_DECLARATION_INITIALIZER_PREFIX), TYPE_OPERATOR(JsSpace.Location.TYPE_OPERATOR_PREFIX), JSVARIABLE_INITIALIZER(JsSpace.Location.JSVARIABLE_INITIALIZER), + SCOPED_VARIABLE_DECLARATIONS_SCOPE(JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE_PREFIX), + NAMESPACE_DECLARATION_KEYWORD_TYPE(JsSpace.Location.NAMESPACE_DECLARATION_KEYWORD_PREFIX), ; private final JsSpace.Location beforeLocation;