From 6f0af81170b6dec3394761774649bfd4958b525b Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Thu, 19 Dec 2024 17:46:59 +0100 Subject: [PATCH] Updates for NamespaceDeclaration and FunctionType (#176) Implemented visitNamespaceExportDeclaration Fixes for: - visitGet/SetAccessor - visitNewExpression Co-authored-by: Andrii Rodionov --- openrewrite/src/java/remote/receiver.ts | 19 ++++- openrewrite/src/java/remote/sender.ts | 10 ++- openrewrite/src/java/tree/support_types.ts | 1 + openrewrite/src/java/tree/tree.ts | 64 ++++++++++++++ openrewrite/src/java/visitor.ts | 20 ++++- openrewrite/src/javascript/parser.ts | 84 +++++++++++++++++-- openrewrite/src/javascript/remote/receiver.ts | 27 +++++- openrewrite/src/javascript/remote/sender.ts | 11 ++- .../src/javascript/tree/support_types.ts | 2 +- openrewrite/src/javascript/tree/tree.ts | 45 ++++++---- openrewrite/src/javascript/visitor.ts | 5 +- .../test/javascript/parser/class.test.ts | 41 +++++++++ .../test/javascript/parser/namespace.test.ts | 41 +++++++++ .../test/javascript/parser/typeAlias.test.ts | 11 ++- .../javascript/remote/JavaScriptReceiver.java | 34 ++++++-- .../javascript/remote/JavaScriptSender.java | 17 +++- .../remote/JavaScriptValidator.java | 13 ++- .../javascript/JavaScriptVisitor.java | 5 +- .../internal/JavaScriptPrinter.java | 11 ++- .../org/openrewrite/javascript/tree/JS.java | 15 ++-- .../javascript/tree/JsLeftPadded.java | 3 +- .../javascript/tree/JsRightPadded.java | 1 - .../openrewrite/javascript/tree/JsSpace.java | 4 +- 23 files changed, 419 insertions(+), 65 deletions(-) diff --git a/openrewrite/src/java/remote/receiver.ts b/openrewrite/src/java/remote/receiver.ts index 132b5019..5cd54809 100644 --- a/openrewrite/src/java/remote/receiver.ts +++ b/openrewrite/src/java/remote/receiver.ts @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions"; import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core'; import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote'; import {JavaVisitor} from '..'; -import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from '../tree'; +import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown, Erroneous} from '../tree'; import * as Java from "../../java/tree"; export class JavaReceiver implements Receiver { @@ -708,6 +708,14 @@ class Visitor extends JavaVisitor { return source; } + public visitErroneous(erroneous: Erroneous, ctx: ReceiverContext): J { + erroneous = erroneous.withId(ctx.receiveValue(erroneous.id, ValueType.UUID)!); + erroneous = erroneous.withPrefix(ctx.receiveNode(erroneous.prefix, receiveSpace)!); + erroneous = erroneous.withMarkers(ctx.receiveNode(erroneous.markers, ctx.receiveMarkers)!); + erroneous = erroneous.withText(ctx.receiveValue(erroneous.text, ValueType.Primitive)!); + return erroneous; + } + } class Factory implements ReceiverFactory { @@ -1463,6 +1471,15 @@ class Factory implements ReceiverFactory { ); } + if (type === "org.openrewrite.java.tree.J$Erroneous") { + return new Erroneous( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveValue(null, ValueType.Primitive)! + ); + } + throw new Error("No factory method for type: " + type); } } diff --git a/openrewrite/src/java/remote/sender.ts b/openrewrite/src/java/remote/sender.ts index a8880dbf..f84cfd2c 100644 --- a/openrewrite/src/java/remote/sender.ts +++ b/openrewrite/src/java/remote/sender.ts @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions"; import {Cursor, ListUtils, Tree} from '../../core'; import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote'; import {JavaVisitor} from '..'; -import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from '../tree'; +import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown, Erroneous} from '../tree'; import * as Java from "../../java/tree"; export class JavaSender implements Sender { @@ -708,6 +708,14 @@ class Visitor extends JavaVisitor { return source; } + public visitErroneous(erroneous: Erroneous, ctx: SenderContext): J { + ctx.sendValue(erroneous, v => v.id, ValueType.UUID); + ctx.sendNode(erroneous, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(erroneous, v => v.markers, ctx.sendMarkers); + ctx.sendValue(erroneous, v => v.text, ValueType.Primitive); + return erroneous; + } + private static sendContainer(type: ValueType): (container: JContainer, ctx: SenderContext) => void { return extensions.sendContainer(type); } diff --git a/openrewrite/src/java/tree/support_types.ts b/openrewrite/src/java/tree/support_types.ts index b54a78fd..6cfa9ed0 100644 --- a/openrewrite/src/java/tree/support_types.ts +++ b/openrewrite/src/java/tree/support_types.ts @@ -691,6 +691,7 @@ export namespace Space { WILDCARD_BOUND, WILDCARD_PREFIX, YIELD_PREFIX, + ERRONEOUS_PREFIX, } } diff --git a/openrewrite/src/java/tree/tree.ts b/openrewrite/src/java/tree/tree.ts index 37d910cf..32d998aa 100644 --- a/openrewrite/src/java/tree/tree.ts +++ b/openrewrite/src/java/tree/tree.ts @@ -6320,3 +6320,67 @@ export namespace Unknown { } } + +@LstType("org.openrewrite.java.tree.J$Erroneous") +export class Erroneous extends JMixin(Object) implements Statement, Expression { + public constructor(id: UUID, prefix: Space, markers: Markers, text: string) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._text = text; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): Erroneous { + return id === this._id ? this : new Erroneous(id, this._prefix, this._markers, this._text); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): Erroneous { + return prefix === this._prefix ? this : new Erroneous(this._id, prefix, this._markers, this._text); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): Erroneous { + return markers === this._markers ? this : new Erroneous(this._id, this._prefix, markers, this._text); + } + + private readonly _text: string; + + public get text(): string { + return this._text; + } + + public withText(text: string): Erroneous { + return text === this._text ? this : new Erroneous(this._id, this._prefix, this._markers, text); + } + + public acceptJava

(v: JavaVisitor

, p: P): J | null { + return v.visitErroneous(this, p); + } + + public get type(): JavaType | null { + return extensions.getJavaType(this); + } + + public withType(type: JavaType): Erroneous { + return extensions.withJavaType(this, type); + } + +} diff --git a/openrewrite/src/java/visitor.ts b/openrewrite/src/java/visitor.ts index 39c9e5a8..6aea08a2 100644 --- a/openrewrite/src/java/visitor.ts +++ b/openrewrite/src/java/visitor.ts @@ -1,7 +1,7 @@ import * as extensions from "./extensions"; import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core"; import {J, isJava, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree} from "./tree"; -import {AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from "./tree"; +import {AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown, Erroneous} from "./tree"; export class JavaVisitor

extends TreeVisitor { isAcceptable(sourceFile: SourceFile, p: P): boolean { @@ -965,6 +965,24 @@ export class JavaVisitor

extends TreeVisitor { return source; } + public visitErroneous(erroneous: Erroneous, p: P): J | null { + erroneous = erroneous.withPrefix(this.visitSpace(erroneous.prefix, Space.Location.ERRONEOUS_PREFIX, p)!); + let tempStatement = this.visitStatement(erroneous, p) as Statement; + if (!(tempStatement instanceof Erroneous)) + { + return tempStatement; + } + erroneous = tempStatement as Erroneous; + let tempExpression = this.visitExpression(erroneous, p) as Expression; + if (!(tempExpression instanceof Erroneous)) + { + return tempExpression; + } + erroneous = tempExpression as Erroneous; + erroneous = erroneous.withMarkers(this.visitMarkers(erroneous.markers, p)); + return erroneous; + } + public visitContainer(container: JContainer | null, loc: JContainer.Location, p: P): JContainer | null { return extensions.visitContainer(this, container, loc, p); } diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 6a67380d..81e78b01 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -262,12 +262,13 @@ 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 - | ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression) { + | ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression + | ts.ConstructorTypeNode) { if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node) || ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node) || ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node) - || ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) { + || ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node)) { return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; } else if (ts.isExportAssignment(node)) { @@ -1148,6 +1149,25 @@ export class JavaScriptParserVisitor { } visitGetAccessor(node: ts.GetAccessorDeclaration) { + const name = this.visit(node.name); + if (!(name instanceof J.Identifier)) { + return new JS.JSMethodDeclaration( + randomId(), + this.prefix(node), + Markers.EMPTY, + [], + this.mapModifiers(node), + null, + this.mapTypeInfo(node), + name, + this.mapCommaSeparatedList(this.getParameterListNodes(node)), + null, + node.body ? this.convert(node.body) : null, + null, + this.mapMethodType(node) + ); + } + return new J.MethodDeclaration( randomId(), this.prefix(node), @@ -1157,7 +1177,7 @@ export class JavaScriptParserVisitor { null, this.mapTypeInfo(node), new J.MethodDeclaration.IdentifierWithAnnotations( - this.visit(node.name), + name, [] ), this.mapCommaSeparatedList(this.getParameterListNodes(node)), @@ -1169,6 +1189,25 @@ export class JavaScriptParserVisitor { } visitSetAccessor(node: ts.SetAccessorDeclaration) { + const name = this.visit(node.name); + if (!(name instanceof J.Identifier)) { + return new JS.JSMethodDeclaration( + randomId(), + this.prefix(node), + Markers.EMPTY, + [], + this.mapModifiers(node), + null, + null, + name, + this.mapCommaSeparatedList(this.getParameterListNodes(node)), + null, + node.body ? this.convert(node.body) : null, + null, + this.mapMethodType(node) + ); + } + return new J.MethodDeclaration( randomId(), this.prefix(node), @@ -1178,7 +1217,7 @@ export class JavaScriptParserVisitor { null, null, new J.MethodDeclaration.IdentifierWithAnnotations( - this.visit(node.name), + name, [] ), this.mapCommaSeparatedList(this.getParameterListNodes(node)), @@ -1289,7 +1328,8 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - this.rightPadded(false, Space.EMPTY), + [], + this.leftPadded(Space.EMPTY, false), this.mapTypeParametersAsObject(node), new JContainer( this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)), @@ -1305,7 +1345,8 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - this.rightPadded(true, this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!)), + this.mapModifiers(node), + this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!), true), this.mapTypeParametersAsObject(node), new JContainer( this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)), @@ -1920,7 +1961,7 @@ export class JavaScriptParserVisitor { this.visit(node.expression), this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments), null - ): this.visit(node.expression), + ): new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, this.visit(node.expression)), this.mapCommaSeparatedList(this.getParameterListNodes(node)), null, this.mapMethodType(node) @@ -2926,7 +2967,7 @@ export class JavaScriptParserVisitor { } visitModuleDeclaration(node: ts.ModuleDeclaration) { - const body = this.visit(node.body as ts.Node); + const body = node.body ? this.visit(node.body as ts.Node) : null; let namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword); const keywordType = namespaceKeyword ? JS.NamespaceDeclaration.KeywordType.Namespace : JS.NamespaceDeclaration.KeywordType.Module @@ -3035,7 +3076,32 @@ export class JavaScriptParserVisitor { } visitNamespaceExportDeclaration(node: ts.NamespaceExportDeclaration) { - return this.visitUnknown(node); + return new JS.NamespaceDeclaration( + randomId(), + this.prefix(node), + Markers.EMPTY, + [ + new J.Modifier( + randomId(), + Space.EMPTY, + Markers.EMPTY, + 'export', + J.Modifier.Type.LanguageExtension, + [] + ), + new J.Modifier( + randomId(), + this.prefix(this.findChildNode(node, ts.SyntaxKind.AsKeyword)!), + Markers.EMPTY, + 'as', + J.Modifier.Type.LanguageExtension, + [] + ) + ], + this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword)!), JS.NamespaceDeclaration.KeywordType.Namespace), + this.rightPadded(this.convert(node.name), this.suffix(node.name)), + null + ); } visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) { diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index 9cb8183a..7eeadb24 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -135,7 +135,8 @@ class Visitor extends JavaScriptVisitor { functionType = functionType.withId(ctx.receiveValue(functionType.id, ValueType.UUID)!); functionType = functionType.withPrefix(ctx.receiveNode(functionType.prefix, receiveSpace)!); functionType = functionType.withMarkers(ctx.receiveNode(functionType.markers, ctx.receiveMarkers)!); - functionType = functionType.padding.withConstructorType(ctx.receiveNode(functionType.padding.constructorType, rightPaddedValueReceiver(ValueType.Primitive))!); + functionType = functionType.withModifiers(ctx.receiveNodes(functionType.modifiers, ctx.receiveTree)!); + functionType = functionType.padding.withConstructorType(ctx.receiveNode(functionType.padding.constructorType, leftPaddedValueReceiver(ValueType.Primitive))!); functionType = functionType.withTypeParameters(ctx.receiveNode(functionType.typeParameters, ctx.receiveTree)); functionType = functionType.padding.withParameters(ctx.receiveNode(functionType.padding.parameters, receiveContainer)!); functionType = functionType.withArrow(ctx.receiveNode(functionType.arrow, receiveSpace)!); @@ -505,7 +506,7 @@ class Visitor extends JavaScriptVisitor { namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNodes(namespaceDeclaration.modifiers, ctx.receiveTree)!); 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)!); + namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.body, ctx.receiveTree)); return namespaceDeclaration; } @@ -1308,6 +1309,14 @@ class Visitor extends JavaScriptVisitor { return source; } + public visitErroneous(erroneous: Java.Erroneous, ctx: ReceiverContext): J { + erroneous = erroneous.withId(ctx.receiveValue(erroneous.id, ValueType.UUID)!); + erroneous = erroneous.withPrefix(ctx.receiveNode(erroneous.prefix, receiveSpace)!); + erroneous = erroneous.withMarkers(ctx.receiveNode(erroneous.markers, ctx.receiveMarkers)!); + erroneous = erroneous.withText(ctx.receiveValue(erroneous.text, ValueType.Primitive)!); + return erroneous; + } + } class Factory implements ReceiverFactory { @@ -1431,7 +1440,8 @@ class Factory implements ReceiverFactory { ctx.receiveValue(null, ValueType.UUID)!, ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveNode>(null, rightPaddedValueReceiver(ValueType.Primitive))!, + ctx.receiveNodes(null, ctx.receiveTree)!, + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, ctx.receiveNode(null, ctx.receiveTree), ctx.receiveNode>(null, receiveContainer)!, ctx.receiveNode(null, receiveSpace)!, @@ -1837,7 +1847,7 @@ class Factory implements ReceiverFactory { ctx.receiveNodes(null, ctx.receiveTree)!, ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Enum))!, ctx.receiveNode>(null, receiveRightPaddedTree)!, - ctx.receiveNode(null, ctx.receiveTree)! + ctx.receiveNode(null, ctx.receiveTree) ); } @@ -2721,6 +2731,15 @@ class Factory implements ReceiverFactory { ); } + if (type === "org.openrewrite.java.tree.J$Erroneous") { + return new Java.Erroneous( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveValue(null, ValueType.Primitive)! + ); + } + throw new Error("No factory method for type: " + type); } } diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index 37208d52..fa35c8b7 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -130,7 +130,8 @@ class Visitor extends JavaScriptVisitor { ctx.sendValue(functionType, v => v.id, ValueType.UUID); ctx.sendNode(functionType, v => v.prefix, Visitor.sendSpace); ctx.sendNode(functionType, v => v.markers, ctx.sendMarkers); - ctx.sendNode(functionType, v => v.padding.constructorType, Visitor.sendRightPadded(ValueType.Primitive)); + ctx.sendNodes(functionType, v => v.modifiers, ctx.sendTree, t => t.id); + ctx.sendNode(functionType, v => v.padding.constructorType, Visitor.sendLeftPadded(ValueType.Primitive)); ctx.sendNode(functionType, v => v.typeParameters, ctx.sendTree); ctx.sendNode(functionType, v => v.padding.parameters, Visitor.sendContainer(ValueType.Tree)); ctx.sendNode(functionType, v => v.arrow, Visitor.sendSpace); @@ -1308,6 +1309,14 @@ class Visitor extends JavaScriptVisitor { return source; } + public visitErroneous(erroneous: Java.Erroneous, ctx: SenderContext): J { + ctx.sendValue(erroneous, v => v.id, ValueType.UUID); + ctx.sendNode(erroneous, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(erroneous, v => v.markers, ctx.sendMarkers); + ctx.sendValue(erroneous, v => v.text, ValueType.Primitive); + return erroneous; + } + private static sendContainer(type: ValueType): (container: JContainer, ctx: SenderContext) => void { return extensions.sendContainer(type); } diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index 605346dd..a857a89d 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -304,6 +304,7 @@ export namespace JsLeftPadded { MAPPED_TYPE_HAS_QUESTION_TOKEN, ARROW_FUNCTION_BODY, YIELD_DELEGATED, + FUNCTION_TYPE_CONSTRUCTOR_TYPE, } } export namespace JsRightPadded { @@ -324,7 +325,6 @@ export namespace JsRightPadded { JSFOR_IN_OF_LOOP_CONTROL_ITERABLE, JSFOR_OF_LOOP_BODY, JSFOR_IN_LOOP_BODY, - FUNCTION_TYPE_CONSTRUCTOR_TYPE, TEMPLATE_EXPRESSION_TEMPLATE_SPANS, TAGGED_TEMPLATE_EXPRESSION_TAG, IMPORT_TYPE_HAS_TYPEOF, diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 7322b8b8..3815e1f0 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -895,11 +895,12 @@ export class ExpressionWithTypeArguments extends JSMixin(Object) implements Type @LstType("org.openrewrite.javascript.tree.JS$FunctionType") export class FunctionType extends JSMixin(Object) implements Expression, TypeTree { - public constructor(id: UUID, prefix: Space, markers: Markers, constructorType: JRightPadded, typeParameters: Java.TypeParameters | null, parameters: JContainer, arrow: Space, returnType: Expression, _type: JavaType | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], constructorType: JLeftPadded, typeParameters: Java.TypeParameters | null, parameters: JContainer, arrow: Space, returnType: Expression, _type: JavaType | null) { super(); this._id = id; this._prefix = prefix; this._markers = markers; + this._modifiers = modifiers; this._constructorType = constructorType; this._typeParameters = typeParameters; this._parameters = parameters; @@ -915,7 +916,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withId(id: UUID): FunctionType { - return id === this._id ? this : new FunctionType(id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); + return id === this._id ? this : new FunctionType(id, this._prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); } private readonly _prefix: Space; @@ -925,7 +926,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withPrefix(prefix: Space): FunctionType { - return prefix === this._prefix ? this : new FunctionType(this._id, prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); + return prefix === this._prefix ? this : new FunctionType(this._id, prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); } private readonly _markers: Markers; @@ -935,10 +936,20 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withMarkers(markers: Markers): FunctionType { - return markers === this._markers ? this : new FunctionType(this._id, this._prefix, markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); + return markers === this._markers ? this : new FunctionType(this._id, this._prefix, markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); } - private readonly _constructorType: JRightPadded; + private readonly _modifiers: Java.Modifier[]; + + public get modifiers(): Java.Modifier[] { + return this._modifiers; + } + + public withModifiers(modifiers: Java.Modifier[]): FunctionType { + return modifiers === this._modifiers ? this : new FunctionType(this._id, this._prefix, this._markers, modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, this._type); + } + + private readonly _constructorType: JLeftPadded; public get constructorType(): boolean { return this._constructorType.element; @@ -955,7 +966,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withTypeParameters(typeParameters: Java.TypeParameters | null): FunctionType { - return typeParameters === this._typeParameters ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, typeParameters, this._parameters, this._arrow, this._returnType, this._type); + return typeParameters === this._typeParameters ? this : new FunctionType(this._id, this._prefix, this._markers, this._modifiers, this._constructorType, typeParameters, this._parameters, this._arrow, this._returnType, this._type); } private readonly _parameters: JContainer; @@ -975,7 +986,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withArrow(arrow: Space): FunctionType { - return arrow === this._arrow ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, arrow, this._returnType, this._type); + return arrow === this._arrow ? this : new FunctionType(this._id, this._prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, arrow, this._returnType, this._type); } private readonly _returnType: Expression; @@ -985,7 +996,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withReturnType(returnType: Expression): FunctionType { - return returnType === this._returnType ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, returnType, this._type); + return returnType === this._returnType ? this : new FunctionType(this._id, this._prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, returnType, this._type); } private readonly _type: JavaType | null; @@ -995,7 +1006,7 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre } public withType(_type: JavaType | null): FunctionType { - return _type === this._type ? this : new FunctionType(this._id, this._prefix, this._markers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, _type); + return _type === this._type ? this : new FunctionType(this._id, this._prefix, this._markers, this._modifiers, this._constructorType, this._typeParameters, this._parameters, this._arrow, this._returnType, _type); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { @@ -1005,17 +1016,17 @@ export class FunctionType extends JSMixin(Object) implements Expression, TypeTre get padding() { const t = this; return new class { - public get constructorType(): JRightPadded { + public get constructorType(): JLeftPadded { return t._constructorType; } - public withConstructorType(constructorType: JRightPadded): FunctionType { - return t._constructorType === constructorType ? t : new FunctionType(t._id, t._prefix, t._markers, constructorType, t._typeParameters, t._parameters, t._arrow, t._returnType, t._type); + public withConstructorType(constructorType: JLeftPadded): FunctionType { + return t._constructorType === constructorType ? t : new FunctionType(t._id, t._prefix, t._markers, t._modifiers, constructorType, t._typeParameters, t._parameters, t._arrow, t._returnType, t._type); } public get parameters(): JContainer { return t._parameters; } public withParameters(parameters: JContainer): FunctionType { - return t._parameters === parameters ? t : new FunctionType(t._id, t._prefix, t._markers, t._constructorType, t._typeParameters, parameters, t._arrow, t._returnType, t._type); + return t._parameters === parameters ? t : new FunctionType(t._id, t._prefix, t._markers, t._modifiers, t._constructorType, t._typeParameters, parameters, t._arrow, t._returnType, t._type); } } } @@ -4339,7 +4350,7 @@ export class JSForInOfLoopControl extends JSMixin(Object) { @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[], keywordType: JLeftPadded, name: JRightPadded, body: Java.Block) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], keywordType: JLeftPadded, name: JRightPadded, body: Java.Block | null) { super(); this._id = id; this._prefix = prefix; @@ -4410,13 +4421,13 @@ export class NamespaceDeclaration extends JSMixin(Object) implements Statement { return this.padding.withName(this._name.withElement(name)); } - private readonly _body: Java.Block; + private readonly _body: Java.Block | null; - public get body(): Java.Block { + public get body(): Java.Block | null { return this._body; } - public withBody(body: Java.Block): NamespaceDeclaration { + public withBody(body: Java.Block | null): NamespaceDeclaration { return body === this._body ? this : new NamespaceDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._keywordType, this._name, body); } diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index 2ae55e73..60aecc03 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -163,7 +163,8 @@ export class JavaScriptVisitor

extends JavaVisitor

{ } functionType = tempExpression as FunctionType; functionType = functionType.withMarkers(this.visitMarkers(functionType.markers, p)); - functionType = functionType.padding.withConstructorType(this.visitJsRightPadded(functionType.padding.constructorType, JsRightPadded.Location.FUNCTION_TYPE_CONSTRUCTOR_TYPE, p)!); + functionType = functionType.withModifiers(ListUtils.map(functionType.modifiers, el => this.visitAndCast(el, p))); + functionType = functionType.padding.withConstructorType(this.visitJsLeftPadded(functionType.padding.constructorType, JsLeftPadded.Location.FUNCTION_TYPE_CONSTRUCTOR_TYPE, p)!); functionType = functionType.withTypeParameters(this.visitAndCast(functionType.typeParameters, p)); functionType = functionType.padding.withParameters(this.visitJsContainer(functionType.padding.parameters, JsContainer.Location.FUNCTION_TYPE_PARAMETERS, p)!); functionType = functionType.withArrow(this.visitJsSpace(functionType.arrow, JsSpace.Location.FUNCTION_TYPE_ARROW, p)!); @@ -691,7 +692,7 @@ export class JavaScriptVisitor

extends JavaVisitor

{ namespaceDeclaration = namespaceDeclaration.withModifiers(ListUtils.map(namespaceDeclaration.modifiers, el => this.visitAndCast(el, 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)!); + namespaceDeclaration = namespaceDeclaration.withBody(this.visitAndCast(namespaceDeclaration.body, p)); return namespaceDeclaration; } diff --git a/openrewrite/test/javascript/parser/class.test.ts b/openrewrite/test/javascript/parser/class.test.ts index a526d07d..91096c40 100644 --- a/openrewrite/test/javascript/parser/class.test.ts +++ b/openrewrite/test/javascript/parser/class.test.ts @@ -501,4 +501,45 @@ describe('class mapping', () => { ); }); + test('new expression parentheses', () => { + rewriteRun( + //language=typescript + typeScript(` + class Base { + attrs: any; + + constructor(attrs: any) { + this.attrs = attrs; + } + + clone() { + return new (this.constructor)(this.attrs); + } + } + `) + ); + }); + + test('get/set accessor with a name as expression', () => { + rewriteRun( + //language=typescript + typeScript(` + export function mochaGlobalSetup() { + globalThis.Path2D ??= class Path2D { + constructor(path) { + this.path = path + } + + get [Symbol.toStringTag]() { + return 'Path2D'; + } + + set [Symbol.toStringTag](path) { + } + } + } + `) + ); + }); + }); diff --git a/openrewrite/test/javascript/parser/namespace.test.ts b/openrewrite/test/javascript/parser/namespace.test.ts index adddf211..08f71f02 100644 --- a/openrewrite/test/javascript/parser/namespace.test.ts +++ b/openrewrite/test/javascript/parser/namespace.test.ts @@ -230,4 +230,45 @@ describe('namespace mapping', () => { `) ); }); + + test('empty body', () => { + rewriteRun( + //language=typescript + typeScript(` + declare module 'vue-count-to' /*a*/;/*b*/ + `) + ); + }); + + test('namespace export as', () => { + rewriteRun( + //language=typescript + typeScript(` + declare namespace MyLibrary { + function sayHello(name: string): void; + } + + /*a*/ export /*b*/ as /*c*/ namespace /*d*/ MyLibrary/*e*/; + `) + ); + }); + + test('complex namespace export as', () => { + rewriteRun( + //language=typescript + typeScript(` + export {Fraction} + + export as namespace math /*a*/ + + /*b*/ type NoLiteralType = T extends number + ? number + : T extends string + ? string + : T extends boolean + ? boolean + : T + `) + ); + }); }); diff --git a/openrewrite/test/javascript/parser/typeAlias.test.ts b/openrewrite/test/javascript/parser/typeAlias.test.ts index 5461b9c2..802676f2 100644 --- a/openrewrite/test/javascript/parser/typeAlias.test.ts +++ b/openrewrite/test/javascript/parser/typeAlias.test.ts @@ -71,7 +71,7 @@ describe('type alias mapping', () => { rewriteRun( //language=typescript typeScript(` - type MyConstructor = new (arg: string) => string; + type MyConstructor = abstract new (arg: string) => string; `) ); }); @@ -85,6 +85,15 @@ describe('type alias mapping', () => { ); }); + test('construct function type alias with abstract and comments', () => { + rewriteRun( + //language=typescript + typeScript(` + type MyConstructor = /*0*/ abstract /*a*/new/*b*/ (/*c*/arg: string) => string; + `) + ); + }); + test('recursive array type', () => { rewriteRun( //language=typescript 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 880a705f..6443b7be 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 @@ -1,11 +1,11 @@ /* * Copyright 2024 the original author or authors. *

- * Licensed under the Moderne Source Available License (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * https://docs.moderne.io/licensing/moderne-source-available-license + * https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + /* * -------------------THIS FILE IS AUTO GENERATED-------------------------- * Changes to this file may cause incorrect behavior and will be lost if @@ -192,7 +193,8 @@ public JS.FunctionType visitFunctionType(JS.FunctionType functionType, ReceiverC functionType = functionType.withId(ctx.receiveNonNullValue(functionType.getId(), UUID.class)); functionType = functionType.withPrefix(ctx.receiveNonNullNode(functionType.getPrefix(), JavaScriptReceiver::receiveSpace)); functionType = functionType.withMarkers(ctx.receiveNonNullNode(functionType.getMarkers(), ctx::receiveMarkers)); - functionType = functionType.getPadding().withConstructorType(ctx.receiveNonNullNode(functionType.getPadding().getConstructorType(), rightPaddedValueReceiver(java.lang.Boolean.class))); + functionType = functionType.withModifiers(ctx.receiveNonNullNodes(functionType.getModifiers(), ctx::receiveTree)); + functionType = functionType.getPadding().withConstructorType(ctx.receiveNonNullNode(functionType.getPadding().getConstructorType(), leftPaddedValueReceiver(java.lang.Boolean.class))); functionType = functionType.withTypeParameters(ctx.receiveNode(functionType.getTypeParameters(), ctx::receiveTree)); functionType = functionType.getPadding().withParameters(ctx.receiveNonNullNode(functionType.getPadding().getParameters(), JavaScriptReceiver::receiveContainer)); functionType = functionType.withArrow(ctx.receiveNonNullNode(functionType.getArrow(), JavaScriptReceiver::receiveSpace)); @@ -598,7 +600,7 @@ public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration = namespaceDeclaration.withModifiers(ctx.receiveNonNullNodes(namespaceDeclaration.getModifiers(), ctx::receiveTree)); 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)); + namespaceDeclaration = namespaceDeclaration.withBody(ctx.receiveNode(namespaceDeclaration.getBody(), ctx::receiveTree)); return namespaceDeclaration; } @@ -1452,6 +1454,15 @@ public J.Unknown.Source visitUnknownSource(J.Unknown.Source source, ReceiverCont return source; } + @Override + public J.Erroneous visitErroneous(J.Erroneous erroneous, ReceiverContext ctx) { + erroneous = erroneous.withId(ctx.receiveNonNullValue(erroneous.getId(), UUID.class)); + erroneous = erroneous.withPrefix(ctx.receiveNonNullNode(erroneous.getPrefix(), JavaScriptReceiver::receiveSpace)); + erroneous = erroneous.withMarkers(ctx.receiveNonNullNode(erroneous.getMarkers(), ctx::receiveMarkers)); + erroneous = erroneous.withText(ctx.receiveNonNullValue(erroneous.getText(), String.class)); + return erroneous; + } + } private static class Factory implements ReceiverFactory { @@ -1587,6 +1598,7 @@ protected Function computeValue(Class type) { if (type == J.Yield.class) return Factory::createJYield; if (type == J.Unknown.class) return Factory::createJUnknown; if (type == J.Unknown.Source.class) return Factory::createJUnknownSource; + if (type == J.Erroneous.class) return Factory::createJErroneous; throw new IllegalArgumentException("Unknown type: " + type); } }; @@ -1716,7 +1728,8 @@ private static JS.FunctionType createJSFunctionType(ReceiverContext ctx) { ctx.receiveNonNullValue(null, UUID.class), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNonNullNode(null, rightPaddedValueReceiver(java.lang.Boolean.class)), + ctx.receiveNonNullNodes(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), ctx.receiveNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), @@ -2122,7 +2135,7 @@ private static JS.NamespaceDeclaration createJSNamespaceDeclaration(ReceiverCont ctx.receiveNonNullNodes(null, ctx::receiveTree), ctx.receiveNonNullNode(null, leftPaddedValueReceiver(org.openrewrite.javascript.tree.JS.NamespaceDeclaration.KeywordType.class)), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree), - ctx.receiveNonNullNode(null, ctx::receiveTree) + ctx.receiveNode(null, ctx::receiveTree) ); } @@ -3006,6 +3019,15 @@ private static J.Unknown.Source createJUnknownSource(ReceiverContext ctx) { ); } + private static J.Erroneous createJErroneous(ReceiverContext ctx) { + return new J.Erroneous( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullValue(null, String.class) + ); + } + } private static J.ClassDeclaration.Kind receiveClassDeclarationKind(J.ClassDeclaration.@Nullable Kind kind, @Nullable Class type, ReceiverContext ctx) { 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 7d205085..3d76d3fc 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 @@ -1,11 +1,11 @@ /* * Copyright 2024 the original author or authors. *

- * Licensed under the Moderne Source Available License (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * https://docs.moderne.io/licensing/moderne-source-available-license + * https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + /* * -------------------THIS FILE IS AUTO GENERATED-------------------------- * Changes to this file may cause incorrect behavior and will be lost if @@ -174,7 +175,8 @@ public JS.FunctionType visitFunctionType(JS.FunctionType functionType, SenderCon ctx.sendValue(functionType, JS.FunctionType::getId); ctx.sendNode(functionType, JS.FunctionType::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(functionType, JS.FunctionType::getMarkers, ctx::sendMarkers); - ctx.sendNode(functionType, e -> e.getPadding().getConstructorType(), JavaScriptSender::sendRightPadded); + ctx.sendNodes(functionType, JS.FunctionType::getModifiers, ctx::sendTree, Tree::getId); + ctx.sendNode(functionType, e -> e.getPadding().getConstructorType(), JavaScriptSender::sendLeftPadded); ctx.sendNode(functionType, JS.FunctionType::getTypeParameters, ctx::sendTree); ctx.sendNode(functionType, e -> e.getPadding().getParameters(), JavaScriptSender::sendContainer); ctx.sendNode(functionType, JS.FunctionType::getArrow, JavaScriptSender::sendSpace); @@ -1463,6 +1465,15 @@ public J.Unknown.Source visitUnknownSource(J.Unknown.Source source, SenderContex return source; } + @Override + public J.Erroneous visitErroneous(J.Erroneous erroneous, SenderContext ctx) { + ctx.sendValue(erroneous, J.Erroneous::getId); + ctx.sendNode(erroneous, J.Erroneous::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(erroneous, J.Erroneous::getMarkers, ctx::sendMarkers); + ctx.sendValue(erroneous, J.Erroneous::getText); + return erroneous; + } + } private static void sendContainer(JContainer container, SenderContext ctx) { diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java index 22494d64..c782f314 100644 --- a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java +++ b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java @@ -1,11 +1,11 @@ /* * Copyright 2024 the original author or authors. *

- * Licensed under the Moderne Source Available License (the "License"); + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

- * https://docs.moderne.io/licensing/moderne-source-available-license + * https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + /* * -------------------THIS FILE IS AUTO GENERATED-------------------------- * Changes to this file may cause incorrect behavior and will be lost if @@ -131,6 +132,7 @@ public JS.ExpressionWithTypeArguments visitExpressionWithTypeArguments(JS.Expres @Override public JS.FunctionType visitFunctionType(JS.FunctionType functionType, P p) { + ListUtils.map(functionType.getModifiers(), el -> visitAndValidateNonNull(el, J.Modifier.class, p)); visitAndValidate(functionType.getTypeParameters(), J.TypeParameters.class, p); visitAndValidate(functionType.getParameters(), Statement.class, p); visitAndValidateNonNull(functionType.getReturnType(), Expression.class, p); @@ -392,7 +394,7 @@ public JS.JSForInOfLoopControl visitJSForInOfLoopControl(JS.JSForInOfLoopControl public JS.NamespaceDeclaration visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, P p) { ListUtils.map(namespaceDeclaration.getModifiers(), el -> visitAndValidateNonNull(el, J.Modifier.class, p)); visitAndValidateNonNull(namespaceDeclaration.getName(), Expression.class, p); - visitAndValidateNonNull(namespaceDeclaration.getBody(), J.Block.class, p); + visitAndValidate(namespaceDeclaration.getBody(), J.Block.class, p); return namespaceDeclaration; } @@ -948,4 +950,9 @@ public J.Unknown.Source visitUnknownSource(J.Unknown.Source source, P p) { return source; } + @Override + public J.Erroneous visitErroneous(J.Erroneous erroneous, P p) { + return erroneous; + } + } 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 5cb59f22..e74b4989 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -249,7 +249,8 @@ public J visitFunctionType(JS.FunctionType functionType, P p) { } else { f = (JS.FunctionType) temp; } - f = f.getPadding().withConstructorType(Objects.requireNonNull(visitRightPadded(f.getPadding().getConstructorType(), JsRightPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, p))); + f = f.withModifiers(Objects.requireNonNull(ListUtils.map(f.getModifiers(), e -> visitAndCast(e, p)))); + f = f.getPadding().withConstructorType(Objects.requireNonNull(visitLeftPadded(f.getPadding().getConstructorType(), JsLeftPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, p))); f = f.withTypeParameters(Objects.requireNonNull(visitAndCast(f.getTypeParameters(), p))); f = f.getPadding().withParameters(Objects.requireNonNull(visitContainer(f.getPadding().getParameters(), JContainer.Location.LANGUAGE_EXTENSION, p))); f = f.withParameters(ListUtils.map(f.getParameters(), e -> visitAndCast(e, p))); @@ -961,7 +962,7 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, ns = ns.withModifiers(ListUtils.map(ns.getModifiers(), m -> visitAndCast(m, p))); ns = ns.getPadding().withKeywordType(Objects.requireNonNull(visitLeftPadded(ns.getPadding().getKeywordType(), JsLeftPadded.Location.NAMESPACE_DECLARATION_KEYWORD_TYPE, p))); ns = ns.getPadding().withName(Objects.requireNonNull(visitRightPadded(ns.getPadding().getName(), JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p))); - ns = ns.withBody(Objects.requireNonNull(visitAndCast(ns.getBody(), 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 6fbf9ac1..53969677 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 @@ -198,9 +198,10 @@ public J visitExport(JS.Export export, PrintOutputCapture

p) { @Override public J visitFunctionType(JS.FunctionType functionType, PrintOutputCapture

p) { beforeSyntax(functionType, JsSpace.Location.FUNCTION_TYPE_PREFIX, p); + functionType.getModifiers().forEach(m -> delegate.visitModifier(m, p)); + if (functionType.isConstructorType()) { - p.append("new"); - visitRightPadded(functionType.getPadding().getConstructorType(), JsRightPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, p); + visitLeftPaddedBoolean("new", functionType.getPadding().getConstructorType(), JsLeftPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, p); } J.TypeParameters typeParameters = functionType.getTypeParameters(); if (typeParameters != null) { @@ -386,8 +387,10 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, p.append("module"); break; } - visit(namespaceDeclaration.getName(), p); - visit(namespaceDeclaration.getBody(), p); + this.visitRightPadded(namespaceDeclaration.getPadding().getName(), JsRightPadded.Location.NAMESPACE_DECLARATION_NAME, p); + if (namespaceDeclaration.getBody() != null) { + visit(namespaceDeclaration.getBody(), p); + } afterSyntax(namespaceDeclaration, p); return namespaceDeclaration; } 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 da53cdcb..1ce1beb5 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 @@ -918,7 +918,11 @@ class FunctionType implements JS, Expression, TypeTree { @With Markers markers; - JRightPadded constructorType; + @Getter + @With + List modifiers; + + JLeftPadded constructorType; public boolean isConstructorType() { return constructorType.getElement(); @@ -984,13 +988,13 @@ public FunctionType.Padding getPadding() { public static class Padding { private final FunctionType t; - public JRightPadded getConstructorType() { + public JLeftPadded getConstructorType() { return t.constructorType; } - public FunctionType withConstructorType(JRightPadded constructor) { + public FunctionType withConstructorType(JLeftPadded constructor) { return t.constructorType == constructor ? t : - new FunctionType(t.id, t.prefix, t.markers, constructor, t.typeParameters, t.parameters, t.arrow, t.returnType, t.type); + new FunctionType(t.id, t.prefix, t.markers, t.modifiers, constructor, t.typeParameters, t.parameters, t.arrow, t.returnType, t.type); } public JContainer getParameters() { @@ -998,7 +1002,7 @@ public JContainer getParameters() { } public FunctionType withParameters(JContainer parameters) { - return t.parameters == parameters ? t : new FunctionType(t.id, t.prefix, t.markers, t.constructorType, t.typeParameters, parameters, t.arrow, t.returnType, t.type); + return t.parameters == parameters ? t : new FunctionType(t.id, t.prefix, t.markers, t.modifiers, t.constructorType, t.typeParameters, parameters, t.arrow, t.returnType, t.type); } } } @@ -4111,6 +4115,7 @@ public NamespaceDeclaration withName(Expression expression) { @With @Getter + @Nullable Block body; @Override 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 0fde1404..bd43404f 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 @@ -52,7 +52,8 @@ public enum Location { MAPPED_TYPE_READONLY(JsSpace.Location.MAPPED_TYPE_READONLY_PREFIX), MAPPED_TYPE_QUESTION_TOKEN(JsSpace.Location.MAPPED_TYPE_QUESTION_TOKEN_PREFIX), LAMBDA_ARROW(JsSpace.Location.LAMBDA_ARROW_PREFIX), - JS_YIELD_DELEGATED(JsSpace.Location.JS_YIELD_DELEGATED_PREFIX); + JS_YIELD_DELEGATED(JsSpace.Location.JS_YIELD_DELEGATED_PREFIX), + FUNCTION_TYPE_CONSTRUCTOR(JsSpace.Location.FUNCTION_TYPE_CONSTRUCTOR_PREFIX); private final JsSpace.Location beforeLocation; 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 685c9b04..be7dfd33 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 @@ -42,7 +42,6 @@ public enum Location { FOR_CONTROL_VAR(JsSpace.Location.FOR_INIT_SUFFIX), FOR_CONTROL_ITER(JsSpace.Location.FOR_ITER_SUFFIX), FOR_BODY(JsSpace.Location.FOR_BODY_SUFFIX), - FUNCTION_TYPE_CONSTRUCTOR(JsSpace.Location.FUNCTION_TYPE_CONSTRUCTOR_SUFFIX), ARRAY_BINDING_PATTERN_ELEMENTS(JsSpace.Location.FOR_BODY_SUFFIX), EXPR_WITH_TYPE_ARG_PARAMETERS_SUFFIX(JsSpace.Location.EXPR_WITH_TYPE_ARG_PARAMETERS_SUFFIX), TEMPLATE_EXPRESSION_TAG(JsSpace.Location.TEMPLATE_EXPRESSION_TAG_SUFFIX), 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 cfbd314a..6314cac0 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 @@ -104,7 +104,6 @@ public enum Location { FOR_ITER_SUFFIX, FOR_IN_LOOP_PREFIX, TYPE_QUERY_PREFIX, - FUNCTION_TYPE_CONSTRUCTOR_SUFFIX, ARRAY_BINDING_PATTERN_PREFIX, ARRAY_BINDING_PATTERN_ELEMENTS_PREFIX, ARRAY_BINDING_PATTERN_ELEMENTS_SUFFIX, @@ -165,6 +164,7 @@ public enum Location { MAPPED_TYPE_QUESTION_TOKEN_PREFIX, TYPE_TREE_EXPRESSION_PREFIX, LAMBDA_ARROW_PREFIX, - JS_YIELD_DELEGATED_PREFIX; + JS_YIELD_DELEGATED_PREFIX, + FUNCTION_TYPE_CONSTRUCTOR_PREFIX; } }