From 74433db31b393d2a6d568bf1c787b6d170c4cc3d Mon Sep 17 00:00:00 2001 From: Oleh Dokuka Date: Fri, 29 Nov 2024 15:11:14 +0200 Subject: [PATCH] export decl and bugfixes --- openrewrite/src/javascript/parser.ts | 99 ++-- openrewrite/src/javascript/remote/receiver.ts | 94 +++- openrewrite/src/javascript/remote/sender.ts | 45 +- .../src/javascript/tree/support_types.ts | 12 + openrewrite/src/javascript/tree/tree.ts | 427 +++++++++++++++++- openrewrite/src/javascript/visitor.ts | 63 ++- .../test/javascript/parser/arrow.test.ts | 12 + .../test/javascript/parser/decorator.test.ts | 32 ++ .../test/javascript/parser/export.test.ts | 144 ++++++ rewrite-javascript-remote/build.gradle.kts | 3 +- .../javascript/remote/JavaScriptReceiver.java | 94 +++- .../javascript/remote/JavaScriptSender.java | 47 +- .../remote/JavaScriptValidator.java | 29 +- rewrite-javascript/build.gradle.kts | 5 +- .../javascript/JavaScriptParser.java | 2 +- .../javascript/JavaScriptVisitor.java | 196 +++++--- .../internal/JavaScriptPrinter.java | 59 ++- .../org/openrewrite/javascript/tree/JS.java | 383 +++++++++++++++- .../javascript/tree/JsContainer.java | 4 +- .../javascript/tree/JsLeftPadded.java | 6 + .../javascript/tree/JsRightPadded.java | 4 +- .../openrewrite/javascript/tree/JsSpace.java | 13 +- 22 files changed, 1626 insertions(+), 147 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index d8100626..3f52e039 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -261,22 +261,19 @@ 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.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment) { 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.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node) + || ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node) || ts.isFunctionDeclaration(node)) { return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : []; - } else if (ts.isFunctionDeclaration(node)) { - return [...node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [], - // empty modifier is used to capture spaces before FunctionKeyword - new J.Modifier( - randomId(), - this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!), - Markers.EMPTY, - null, - J.Modifier.Type.LanguageExtension, - [] - )] + } + else if (ts.isExportAssignment(node)) { + const defaultModifier = this.findChildNode(node, ts.SyntaxKind.DefaultKeyword); + return [ + ...node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [], + ...defaultModifier && ts.isModifier(defaultModifier) ? [this.mapModifier(defaultModifier)] : [] + ] } else if (ts.isVariableDeclarationList(node)) { let modifier: string | undefined; @@ -657,7 +654,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -681,7 +678,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -714,7 +711,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -738,7 +735,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -845,7 +842,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -871,7 +868,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -895,7 +892,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - [], + this.mapDecorators(node), this.mapModifiers(node), this.mapTypeInfo(node), null, @@ -1571,7 +1568,7 @@ export class JavaScriptParserVisitor { const typeArguments = node.typeArguments ? this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments) : null; let select: JRightPadded | null; - let name: J.Identifier = new J.Identifier( randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null); + let name: J.Identifier = new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null); if (ts.isIdentifier(node.expression) && !node.questionDotToken) { select = null; @@ -1682,7 +1679,8 @@ export class JavaScriptParserVisitor { this.prefix(node), Markers.EMPTY, [], - node.name ? this.visit(node.name) : null, + this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!), !!node.asteriskToken), + this.leftPadded(node.asteriskToken ? this.prefix(node.asteriskToken) : Space.EMPTY, node.name ? this.visit(node.name) : new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null)), this.mapTypeParametersAsObject(node), this.mapCommaSeparatedList(this.getParameterListNodes(node)), this.mapTypeInfo(node), @@ -2476,7 +2474,8 @@ export class JavaScriptParserVisitor { this.prefix(node), Markers.EMPTY, this.mapModifiers(node), - node.name ? this.visit(node.name) : null, + this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FunctionKeyword)!), !!node.asteriskToken), + this.leftPadded(node.asteriskToken ? this.prefix(node.asteriskToken) : Space.EMPTY, node.name ? this.visit(node.name) : new J.Identifier(randomId(), Space.EMPTY, Markers.EMPTY, [], "", null, null)), this.mapTypeParametersAsObject(node), this.mapCommaSeparatedList(this.getParameterListNodes(node)), this.mapTypeInfo(node), @@ -2771,23 +2770,65 @@ export class JavaScriptParserVisitor { } visitExportAssignment(node: ts.ExportAssignment) { - return this.visitUnknown(node); + return new JS.ExportAssignment( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.mapModifiers(node), + this.leftPadded(node.isExportEquals ? this.prefix(this.findChildNode(node, ts.SyntaxKind.EqualsToken)!) : Space.EMPTY, (!!node.isExportEquals)), + this.visit(node.expression) + ); } visitExportDeclaration(node: ts.ExportDeclaration) { - return this.visitUnknown(node); + return new JS.ExportDeclaration( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.mapModifiers(node), + this.leftPadded(node.isTypeOnly ? this.prefix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!) : Space.EMPTY, node.isTypeOnly), + node.exportClause ? this.visit(node.exportClause) : this.mapIdentifier(this.findChildNode(node, ts.SyntaxKind.AsteriskToken)!, "*"), + node.moduleSpecifier ? this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.FromKeyword)!), this.visit(node.moduleSpecifier)) : null + ); } visitNamedExports(node: ts.NamedExports) { - return this.visitUnknown(node); + return new JS.NamedExports( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.mapCommaSeparatedList(node.getChildren()), + this.mapType(node) + ); } visitNamespaceExport(node: ts.NamespaceExport) { - return this.visitUnknown(node); + return new JS.Alias( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.rightPadded(this.mapIdentifier(this.findChildNode(node, ts.SyntaxKind.AsteriskToken)!, "*"), this.prefix(this.findChildNode(node, ts.SyntaxKind.AsKeyword)!)), + this.visit(node.name) + ) } visitExportSpecifier(node: ts.ExportSpecifier) { - return this.visitUnknown(node); + return new JS.ExportSpecifier( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.leftPadded(node.isTypeOnly ? this.prefix(this.findChildNode(node, ts.SyntaxKind.TypeKeyword)!) : Space.EMPTY, node.isTypeOnly), + node.propertyName + ? new JS.Alias( + randomId(), + this.prefix(node.propertyName), + Markers.EMPTY, + this.rightPadded(this.convert(node.propertyName), this.suffix(node.propertyName)), + this.convert(node.name) + ) + : this.convert(node.name), + this.mapType(node) + ); } visitMissingDeclaration(node: ts.MissingDeclaration) { @@ -3314,7 +3355,7 @@ export class JavaScriptParserVisitor { return args; } - private mapDecorators(node: ts.ClassDeclaration | ts.FunctionDeclaration | ts.MethodDeclaration | ts.ConstructorDeclaration): J.Annotation[] { + private mapDecorators(node: ts.ClassDeclaration | ts.FunctionDeclaration | ts.MethodDeclaration | ts.ConstructorDeclaration | ts.ParameterDeclaration | ts.PropertyDeclaration): J.Annotation[] { return node.modifiers?.filter(ts.isDecorator)?.map(this.convert) ?? []; } diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index 5df47b72..5cbc972c 100644 --- a/openrewrite/src/javascript/remote/receiver.ts +++ b/openrewrite/src/javascript/remote/receiver.ts @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions"; import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core'; import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote'; import {JavaScriptVisitor} from '..'; -import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java"; import * as Java from "../../java/tree"; @@ -431,7 +431,8 @@ class Visitor extends JavaScriptVisitor { functionDeclaration = functionDeclaration.withPrefix(ctx.receiveNode(functionDeclaration.prefix, receiveSpace)!); functionDeclaration = functionDeclaration.withMarkers(ctx.receiveNode(functionDeclaration.markers, ctx.receiveMarkers)!); functionDeclaration = functionDeclaration.withModifiers(ctx.receiveNodes(functionDeclaration.modifiers, ctx.receiveTree)!); - functionDeclaration = functionDeclaration.withName(ctx.receiveNode(functionDeclaration.name, ctx.receiveTree)); + functionDeclaration = functionDeclaration.padding.withAsteriskToken(ctx.receiveNode(functionDeclaration.padding.asteriskToken, leftPaddedValueReceiver(ValueType.Primitive))!); + functionDeclaration = functionDeclaration.padding.withName(ctx.receiveNode(functionDeclaration.padding.name, receiveLeftPaddedTree)!); functionDeclaration = functionDeclaration.withTypeParameters(ctx.receiveNode(functionDeclaration.typeParameters, ctx.receiveTree)); functionDeclaration = functionDeclaration.padding.withParameters(ctx.receiveNode(functionDeclaration.padding.parameters, receiveContainer)!); functionDeclaration = functionDeclaration.withReturnTypeExpression(ctx.receiveNode(functionDeclaration.returnTypeExpression, ctx.receiveTree)); @@ -480,6 +481,46 @@ class Visitor extends JavaScriptVisitor { return bindingElement; } + public visitExportDeclaration(exportDeclaration: ExportDeclaration, ctx: ReceiverContext): J { + exportDeclaration = exportDeclaration.withId(ctx.receiveValue(exportDeclaration.id, ValueType.UUID)!); + exportDeclaration = exportDeclaration.withPrefix(ctx.receiveNode(exportDeclaration.prefix, receiveSpace)!); + exportDeclaration = exportDeclaration.withMarkers(ctx.receiveNode(exportDeclaration.markers, ctx.receiveMarkers)!); + exportDeclaration = exportDeclaration.withModifiers(ctx.receiveNodes(exportDeclaration.modifiers, ctx.receiveTree)!); + exportDeclaration = exportDeclaration.padding.withTypeOnly(ctx.receiveNode(exportDeclaration.padding.typeOnly, leftPaddedValueReceiver(ValueType.Primitive))!); + exportDeclaration = exportDeclaration.withExportClause(ctx.receiveNode(exportDeclaration.exportClause, ctx.receiveTree)); + exportDeclaration = exportDeclaration.padding.withModuleSpecifier(ctx.receiveNode(exportDeclaration.padding.moduleSpecifier, receiveLeftPaddedTree)); + return exportDeclaration; + } + + public visitExportAssignment(exportAssignment: ExportAssignment, ctx: ReceiverContext): J { + exportAssignment = exportAssignment.withId(ctx.receiveValue(exportAssignment.id, ValueType.UUID)!); + exportAssignment = exportAssignment.withPrefix(ctx.receiveNode(exportAssignment.prefix, receiveSpace)!); + exportAssignment = exportAssignment.withMarkers(ctx.receiveNode(exportAssignment.markers, ctx.receiveMarkers)!); + exportAssignment = exportAssignment.withModifiers(ctx.receiveNodes(exportAssignment.modifiers, ctx.receiveTree)!); + exportAssignment = exportAssignment.padding.withExportEquals(ctx.receiveNode(exportAssignment.padding.exportEquals, leftPaddedValueReceiver(ValueType.Primitive))!); + exportAssignment = exportAssignment.withExpression(ctx.receiveNode(exportAssignment.expression, ctx.receiveTree)); + return exportAssignment; + } + + public visitNamedExports(namedExports: NamedExports, ctx: ReceiverContext): J { + namedExports = namedExports.withId(ctx.receiveValue(namedExports.id, ValueType.UUID)!); + namedExports = namedExports.withPrefix(ctx.receiveNode(namedExports.prefix, receiveSpace)!); + namedExports = namedExports.withMarkers(ctx.receiveNode(namedExports.markers, ctx.receiveMarkers)!); + namedExports = namedExports.padding.withElements(ctx.receiveNode(namedExports.padding.elements, receiveContainer)!); + namedExports = namedExports.withType(ctx.receiveValue(namedExports.type, ValueType.Object)); + return namedExports; + } + + public visitExportSpecifier(exportSpecifier: ExportSpecifier, ctx: ReceiverContext): J { + exportSpecifier = exportSpecifier.withId(ctx.receiveValue(exportSpecifier.id, ValueType.UUID)!); + exportSpecifier = exportSpecifier.withPrefix(ctx.receiveNode(exportSpecifier.prefix, receiveSpace)!); + exportSpecifier = exportSpecifier.withMarkers(ctx.receiveNode(exportSpecifier.markers, ctx.receiveMarkers)!); + exportSpecifier = exportSpecifier.padding.withTypeOnly(ctx.receiveNode(exportSpecifier.padding.typeOnly, leftPaddedValueReceiver(ValueType.Primitive))!); + exportSpecifier = exportSpecifier.withSpecifier(ctx.receiveNode(exportSpecifier.specifier, ctx.receiveTree)!); + exportSpecifier = exportSpecifier.withType(ctx.receiveValue(exportSpecifier.type, ValueType.Object)); + return exportSpecifier; + } + public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: ReceiverContext): J { annotatedType = annotatedType.withId(ctx.receiveValue(annotatedType.id, ValueType.UUID)!); annotatedType = annotatedType.withPrefix(ctx.receiveNode(annotatedType.prefix, receiveSpace)!); @@ -1172,7 +1213,7 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveNode>(null, receiveRightPaddedTree)!, - ctx.receiveNode(null, ctx.receiveTree)! + ctx.receiveNode(null, ctx.receiveTree)! ); } @@ -1594,7 +1635,8 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, receiveSpace)!, ctx.receiveNode(null, ctx.receiveMarkers)!, ctx.receiveNodes(null, ctx.receiveTree)!, - ctx.receiveNode(null, ctx.receiveTree), + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, + ctx.receiveNode>(null, receiveLeftPaddedTree)!, ctx.receiveNode(null, ctx.receiveTree), ctx.receiveNode>(null, receiveContainer)!, ctx.receiveNode(null, ctx.receiveTree), @@ -1647,6 +1689,50 @@ class Factory implements ReceiverFactory { ); } + if (type === "org.openrewrite.javascript.tree.JS$ExportDeclaration") { + return new ExportDeclaration( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNodes(null, ctx.receiveTree)!, + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, + ctx.receiveNode(null, ctx.receiveTree), + ctx.receiveNode>(null, receiveLeftPaddedTree) + ); + } + + if (type === "org.openrewrite.javascript.tree.JS$ExportAssignment") { + return new ExportAssignment( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNodes(null, ctx.receiveTree)!, + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, + ctx.receiveNode(null, ctx.receiveTree) + ); + } + + if (type === "org.openrewrite.javascript.tree.JS$NamedExports") { + return new NamedExports( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNode>(null, receiveContainer)!, + ctx.receiveValue(null, ValueType.Object) + ); + } + + if (type === "org.openrewrite.javascript.tree.JS$ExportSpecifier") { + return new ExportSpecifier( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNode>(null, leftPaddedValueReceiver(ValueType.Primitive))!, + ctx.receiveNode(null, ctx.receiveTree)!, + ctx.receiveValue(null, ValueType.Object) + ); + } + if (type === "org.openrewrite.java.tree.J$AnnotatedType") { return new Java.AnnotatedType( ctx.receiveValue(null, ValueType.UUID)!, diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index 056b86bb..d5bfd7af 100644 --- a/openrewrite/src/javascript/remote/sender.ts +++ b/openrewrite/src/javascript/remote/sender.ts @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions"; import {Cursor, ListUtils, Tree} from '../../core'; import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote'; import {JavaScriptVisitor} from '..'; -import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java"; import * as Java from "../../java/tree"; @@ -426,7 +426,8 @@ class Visitor extends JavaScriptVisitor { ctx.sendNode(functionDeclaration, v => v.prefix, Visitor.sendSpace); ctx.sendNode(functionDeclaration, v => v.markers, ctx.sendMarkers); ctx.sendNodes(functionDeclaration, v => v.modifiers, ctx.sendTree, t => t.id); - ctx.sendNode(functionDeclaration, v => v.name, ctx.sendTree); + ctx.sendNode(functionDeclaration, v => v.padding.asteriskToken, Visitor.sendLeftPadded(ValueType.Primitive)); + ctx.sendNode(functionDeclaration, v => v.padding.name, Visitor.sendLeftPadded(ValueType.Tree)); ctx.sendNode(functionDeclaration, v => v.typeParameters, ctx.sendTree); ctx.sendNode(functionDeclaration, v => v.padding.parameters, Visitor.sendContainer(ValueType.Tree)); ctx.sendNode(functionDeclaration, v => v.returnTypeExpression, ctx.sendTree); @@ -475,6 +476,46 @@ class Visitor extends JavaScriptVisitor { return bindingElement; } + public visitExportDeclaration(exportDeclaration: ExportDeclaration, ctx: SenderContext): J { + ctx.sendValue(exportDeclaration, v => v.id, ValueType.UUID); + ctx.sendNode(exportDeclaration, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(exportDeclaration, v => v.markers, ctx.sendMarkers); + ctx.sendNodes(exportDeclaration, v => v.modifiers, ctx.sendTree, t => t.id); + ctx.sendNode(exportDeclaration, v => v.padding.typeOnly, Visitor.sendLeftPadded(ValueType.Primitive)); + ctx.sendNode(exportDeclaration, v => v.exportClause, ctx.sendTree); + ctx.sendNode(exportDeclaration, v => v.padding.moduleSpecifier, Visitor.sendLeftPadded(ValueType.Tree)); + return exportDeclaration; + } + + public visitExportAssignment(exportAssignment: ExportAssignment, ctx: SenderContext): J { + ctx.sendValue(exportAssignment, v => v.id, ValueType.UUID); + ctx.sendNode(exportAssignment, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(exportAssignment, v => v.markers, ctx.sendMarkers); + ctx.sendNodes(exportAssignment, v => v.modifiers, ctx.sendTree, t => t.id); + ctx.sendNode(exportAssignment, v => v.padding.exportEquals, Visitor.sendLeftPadded(ValueType.Primitive)); + ctx.sendNode(exportAssignment, v => v.expression, ctx.sendTree); + return exportAssignment; + } + + public visitNamedExports(namedExports: NamedExports, ctx: SenderContext): J { + ctx.sendValue(namedExports, v => v.id, ValueType.UUID); + ctx.sendNode(namedExports, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(namedExports, v => v.markers, ctx.sendMarkers); + ctx.sendNode(namedExports, v => v.padding.elements, Visitor.sendContainer(ValueType.Tree)); + ctx.sendTypedValue(namedExports, v => v.type, ValueType.Object); + return namedExports; + } + + public visitExportSpecifier(exportSpecifier: ExportSpecifier, ctx: SenderContext): J { + ctx.sendValue(exportSpecifier, v => v.id, ValueType.UUID); + ctx.sendNode(exportSpecifier, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(exportSpecifier, v => v.markers, ctx.sendMarkers); + ctx.sendNode(exportSpecifier, v => v.padding.typeOnly, Visitor.sendLeftPadded(ValueType.Primitive)); + ctx.sendNode(exportSpecifier, v => v.specifier, ctx.sendTree); + ctx.sendTypedValue(exportSpecifier, v => v.type, ValueType.Object); + return exportSpecifier; + } + public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: SenderContext): J { ctx.sendValue(annotatedType, v => v.id, ValueType.UUID); ctx.sendNode(annotatedType, v => v.prefix, Visitor.sendSpace); diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index 5bc2ab19..d42b174d 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -241,6 +241,11 @@ export namespace JsSpace { TEMPLATE_EXPRESSION_TEMPLATE_SPAN_PREFIX, TAGGED_TEMPLATE_EXPRESSION_PREFIX, CONDITIONAL_TYPE_PREFIX, + EXPORT_DECLARATION_PREFIX, + EXPORT_DECLARATION_TYPE_ONLY_PREFIX, + EXPORT_SPECIFIER_PREFIX, + NAMED_EXPORTS_PREFIX, + EXPORT_ASSIGNMENT_PREFIX, } } export namespace JsLeftPadded { @@ -266,6 +271,12 @@ export namespace JsLeftPadded { INDEX_SIGNATURE_DECLARATION_TYPE_EXPRESSION, JSFOR_OF_LOOP_AWAIT, BINDING_ELEMENT_INITIALIZER, + EXPORT_DECLARATION_TYPE_ONLY, + EXPORT_SPECIFIER_TYPE_ONLY, + EXPORT_ASSIGNMENT_EXPORT_EQUALS, + EXPORT_DECLARATION_MODULE_SPECIFIER, + FUNCTION_DECLARATION_ASTERISK_TOKEN, + FUNCTION_DECLARATION_NAME, } } export namespace JsRightPadded { @@ -307,5 +318,6 @@ export namespace JsContainer { EXPRESSION_WITH_TYPE_ARGUMENTS_TYPE_ARGUMENTS, TAGGED_TEMPLATE_EXPRESSION_TYPE_ARGUMENTS, CONDITIONAL_TYPE_CONDITION, + NAMED_EXPORTS_ELEMENTS, } } diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 55021b8b..e2517bbc 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -164,7 +164,7 @@ export class CompilationUnit extends SourceFileMixin(JSMixin(Object)) implements @LstType("org.openrewrite.javascript.tree.JS$Alias") export class Alias extends JSMixin(Object) implements Expression { - public constructor(id: UUID, prefix: Space, markers: Markers, propertyName: JRightPadded, alias: Java.Identifier) { + public constructor(id: UUID, prefix: Space, markers: Markers, propertyName: JRightPadded, alias: Expression) { super(); this._id = id; this._prefix = prefix; @@ -213,13 +213,13 @@ export class Alias extends JSMixin(Object) implements Expression { return this.padding.withPropertyName(this._propertyName.withElement(propertyName)); } - private readonly _alias: Java.Identifier; + private readonly _alias: Expression; - public get alias(): Java.Identifier { + public get alias(): Expression { return this._alias; } - public withAlias(alias: Java.Identifier): Alias { + public withAlias(alias: Expression): Alias { return alias === this._alias ? this : new Alias(this._id, this._prefix, this._markers, this._propertyName, alias); } @@ -3636,12 +3636,13 @@ export namespace NamespaceDeclaration { @LstType("org.openrewrite.javascript.tree.JS$FunctionDeclaration") export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree { - public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], name: Java.Identifier | null, typeParameters: Java.TypeParameters | null, parameters: JContainer, returnTypeExpression: TypeTree | null, body: J, _type: JavaType | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], asteriskToken: JLeftPadded, name: JLeftPadded, typeParameters: Java.TypeParameters | null, parameters: JContainer, returnTypeExpression: TypeTree | null, body: J, _type: JavaType | null) { super(); this._id = id; this._prefix = prefix; this._markers = markers; this._modifiers = modifiers; + this._asteriskToken = asteriskToken; this._name = name; this._typeParameters = typeParameters; this._parameters = parameters; @@ -3657,7 +3658,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withId(id: UUID): FunctionDeclaration { - return id === this._id ? this : new FunctionDeclaration(id, this._prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); + return id === this._id ? this : new FunctionDeclaration(id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _prefix: Space; @@ -3667,7 +3668,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withPrefix(prefix: Space): FunctionDeclaration { - return prefix === this._prefix ? this : new FunctionDeclaration(this._id, prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); + return prefix === this._prefix ? this : new FunctionDeclaration(this._id, prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _markers: Markers; @@ -3677,7 +3678,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withMarkers(markers: Markers): FunctionDeclaration { - return markers === this._markers ? this : new FunctionDeclaration(this._id, this._prefix, markers, this._modifiers, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); + return markers === this._markers ? this : new FunctionDeclaration(this._id, this._prefix, markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _modifiers: Java.Modifier[]; @@ -3687,17 +3688,27 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withModifiers(modifiers: Java.Modifier[]): FunctionDeclaration { - return modifiers === this._modifiers ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, modifiers, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); + return modifiers === this._modifiers ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } - private readonly _name: Java.Identifier | null; + private readonly _asteriskToken: JLeftPadded; - public get name(): Java.Identifier | null { - return this._name; + public get asteriskToken(): boolean { + return this._asteriskToken.element; + } + + public withAsteriskToken(asteriskToken: boolean): FunctionDeclaration { + return this.padding.withAsteriskToken(this._asteriskToken.withElement(asteriskToken)); + } + + private readonly _name: JLeftPadded; + + public get name(): Java.Identifier { + return this._name.element; } - public withName(name: Java.Identifier | null): FunctionDeclaration { - return name === this._name ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); + public withName(name: Java.Identifier): FunctionDeclaration { + return this.padding.withName(this._name.withElement(name)); } private readonly _typeParameters: Java.TypeParameters | null; @@ -3707,7 +3718,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withTypeParameters(typeParameters: Java.TypeParameters | null): FunctionDeclaration { - return typeParameters === this._typeParameters ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._name, typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); + return typeParameters === this._typeParameters ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _parameters: JContainer; @@ -3727,7 +3738,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withReturnTypeExpression(returnTypeExpression: TypeTree | null): FunctionDeclaration { - return returnTypeExpression === this._returnTypeExpression ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._parameters, returnTypeExpression, this._body, this._type); + return returnTypeExpression === this._returnTypeExpression ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, returnTypeExpression, this._body, this._type); } private readonly _body: J; @@ -3737,7 +3748,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withBody(body: J): FunctionDeclaration { - return body === this._body ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, body, this._type); + return body === this._body ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, body, this._type); } private readonly _type: JavaType | null; @@ -3747,7 +3758,7 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E } public withType(_type: JavaType | null): FunctionDeclaration { - return _type === this._type ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, _type); + return _type === this._type ? this : new FunctionDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._asteriskToken, this._name, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, _type); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { @@ -3757,11 +3768,23 @@ export class FunctionDeclaration extends JSMixin(Object) implements Statement, E get padding() { const t = this; return new class { + public get asteriskToken(): JLeftPadded { + return t._asteriskToken; + } + public withAsteriskToken(asteriskToken: JLeftPadded): FunctionDeclaration { + return t._asteriskToken === asteriskToken ? t : new FunctionDeclaration(t._id, t._prefix, t._markers, t._modifiers, asteriskToken, t._name, t._typeParameters, t._parameters, t._returnTypeExpression, t._body, t._type); + } + public get name(): JLeftPadded { + return t._name; + } + public withName(name: JLeftPadded): FunctionDeclaration { + return t._name === name ? t : new FunctionDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._asteriskToken, name, t._typeParameters, t._parameters, t._returnTypeExpression, t._body, t._type); + } public get parameters(): JContainer { return t._parameters; } public withParameters(parameters: JContainer): FunctionDeclaration { - return t._parameters === parameters ? t : new FunctionDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._name, t._typeParameters, parameters, t._returnTypeExpression, t._body, t._type); + return t._parameters === parameters ? t : new FunctionDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._asteriskToken, t._name, t._typeParameters, parameters, t._returnTypeExpression, t._body, t._type); } } } @@ -4135,3 +4158,369 @@ export class BindingElement extends JSMixin(Object) implements Statement, Expres } } + +@LstType("org.openrewrite.javascript.tree.JS$ExportDeclaration") +export class ExportDeclaration extends JSMixin(Object) implements Statement { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], typeOnly: JLeftPadded, exportClause: Expression | null, moduleSpecifier: JLeftPadded | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._modifiers = modifiers; + this._typeOnly = typeOnly; + this._exportClause = exportClause; + this._moduleSpecifier = moduleSpecifier; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): ExportDeclaration { + return id === this._id ? this : new ExportDeclaration(id, this._prefix, this._markers, this._modifiers, this._typeOnly, this._exportClause, this._moduleSpecifier); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): ExportDeclaration { + return prefix === this._prefix ? this : new ExportDeclaration(this._id, prefix, this._markers, this._modifiers, this._typeOnly, this._exportClause, this._moduleSpecifier); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): ExportDeclaration { + return markers === this._markers ? this : new ExportDeclaration(this._id, this._prefix, markers, this._modifiers, this._typeOnly, this._exportClause, this._moduleSpecifier); + } + + private readonly _modifiers: Java.Modifier[]; + + public get modifiers(): Java.Modifier[] { + return this._modifiers; + } + + public withModifiers(modifiers: Java.Modifier[]): ExportDeclaration { + return modifiers === this._modifiers ? this : new ExportDeclaration(this._id, this._prefix, this._markers, modifiers, this._typeOnly, this._exportClause, this._moduleSpecifier); + } + + private readonly _typeOnly: JLeftPadded; + + public get typeOnly(): boolean { + return this._typeOnly.element; + } + + public withTypeOnly(typeOnly: boolean): ExportDeclaration { + return this.padding.withTypeOnly(this._typeOnly.withElement(typeOnly)); + } + + private readonly _exportClause: Expression | null; + + public get exportClause(): Expression | null { + return this._exportClause; + } + + public withExportClause(exportClause: Expression | null): ExportDeclaration { + return exportClause === this._exportClause ? this : new ExportDeclaration(this._id, this._prefix, this._markers, this._modifiers, this._typeOnly, exportClause, this._moduleSpecifier); + } + + private readonly _moduleSpecifier: JLeftPadded | null; + + public get moduleSpecifier(): Expression | null { + return this._moduleSpecifier === null ? null : this._moduleSpecifier.element; + } + + public withModuleSpecifier(moduleSpecifier: Expression | null): ExportDeclaration { + return this.padding.withModuleSpecifier(JLeftPadded.withElement(this._moduleSpecifier, moduleSpecifier)); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitExportDeclaration(this, p); + } + + get padding() { + const t = this; + return new class { + public get typeOnly(): JLeftPadded { + return t._typeOnly; + } + public withTypeOnly(typeOnly: JLeftPadded): ExportDeclaration { + return t._typeOnly === typeOnly ? t : new ExportDeclaration(t._id, t._prefix, t._markers, t._modifiers, typeOnly, t._exportClause, t._moduleSpecifier); + } + public get moduleSpecifier(): JLeftPadded | null { + return t._moduleSpecifier; + } + public withModuleSpecifier(moduleSpecifier: JLeftPadded | null): ExportDeclaration { + return t._moduleSpecifier === moduleSpecifier ? t : new ExportDeclaration(t._id, t._prefix, t._markers, t._modifiers, t._typeOnly, t._exportClause, moduleSpecifier); + } + } + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$ExportAssignment") +export class ExportAssignment extends JSMixin(Object) implements Statement { + public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], exportEquals: JLeftPadded, expression: Expression | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._modifiers = modifiers; + this._exportEquals = exportEquals; + this._expression = expression; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): ExportAssignment { + return id === this._id ? this : new ExportAssignment(id, this._prefix, this._markers, this._modifiers, this._exportEquals, this._expression); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): ExportAssignment { + return prefix === this._prefix ? this : new ExportAssignment(this._id, prefix, this._markers, this._modifiers, this._exportEquals, this._expression); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): ExportAssignment { + return markers === this._markers ? this : new ExportAssignment(this._id, this._prefix, markers, this._modifiers, this._exportEquals, this._expression); + } + + private readonly _modifiers: Java.Modifier[]; + + public get modifiers(): Java.Modifier[] { + return this._modifiers; + } + + public withModifiers(modifiers: Java.Modifier[]): ExportAssignment { + return modifiers === this._modifiers ? this : new ExportAssignment(this._id, this._prefix, this._markers, modifiers, this._exportEquals, this._expression); + } + + private readonly _exportEquals: JLeftPadded; + + public get exportEquals(): boolean { + return this._exportEquals.element; + } + + public withExportEquals(exportEquals: boolean): ExportAssignment { + return this.padding.withExportEquals(this._exportEquals.withElement(exportEquals)); + } + + private readonly _expression: Expression | null; + + public get expression(): Expression | null { + return this._expression; + } + + public withExpression(expression: Expression | null): ExportAssignment { + return expression === this._expression ? this : new ExportAssignment(this._id, this._prefix, this._markers, this._modifiers, this._exportEquals, expression); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitExportAssignment(this, p); + } + + get padding() { + const t = this; + return new class { + public get exportEquals(): JLeftPadded { + return t._exportEquals; + } + public withExportEquals(exportEquals: JLeftPadded): ExportAssignment { + return t._exportEquals === exportEquals ? t : new ExportAssignment(t._id, t._prefix, t._markers, t._modifiers, exportEquals, t._expression); + } + } + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$NamedExports") +export class NamedExports extends JSMixin(Object) implements Expression { + public constructor(id: UUID, prefix: Space, markers: Markers, elements: JContainer, _type: JavaType | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._elements = elements; + this._type = _type; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): NamedExports { + return id === this._id ? this : new NamedExports(id, this._prefix, this._markers, this._elements, this._type); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): NamedExports { + return prefix === this._prefix ? this : new NamedExports(this._id, prefix, this._markers, this._elements, this._type); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): NamedExports { + return markers === this._markers ? this : new NamedExports(this._id, this._prefix, markers, this._elements, this._type); + } + + private readonly _elements: JContainer; + + public get elements(): ExportSpecifier[] { + return this._elements.elements; + } + + public withElements(elements: ExportSpecifier[]): NamedExports { + return this.padding.withElements(JContainer.withElements(this._elements, elements)); + } + + private readonly _type: JavaType | null; + + public get type(): JavaType | null { + return this._type; + } + + public withType(_type: JavaType | null): NamedExports { + return _type === this._type ? this : new NamedExports(this._id, this._prefix, this._markers, this._elements, _type); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitNamedExports(this, p); + } + + get padding() { + const t = this; + return new class { + public get elements(): JContainer { + return t._elements; + } + public withElements(elements: JContainer): NamedExports { + return t._elements === elements ? t : new NamedExports(t._id, t._prefix, t._markers, elements, t._type); + } + } + } + +} + +@LstType("org.openrewrite.javascript.tree.JS$ExportSpecifier") +export class ExportSpecifier extends JSMixin(Object) implements Expression, TypedTree { + public constructor(id: UUID, prefix: Space, markers: Markers, typeOnly: JLeftPadded, specifier: Expression, _type: JavaType | null) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._typeOnly = typeOnly; + this._specifier = specifier; + this._type = _type; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): ExportSpecifier { + return id === this._id ? this : new ExportSpecifier(id, this._prefix, this._markers, this._typeOnly, this._specifier, this._type); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): ExportSpecifier { + return prefix === this._prefix ? this : new ExportSpecifier(this._id, prefix, this._markers, this._typeOnly, this._specifier, this._type); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): ExportSpecifier { + return markers === this._markers ? this : new ExportSpecifier(this._id, this._prefix, markers, this._typeOnly, this._specifier, this._type); + } + + private readonly _typeOnly: JLeftPadded; + + public get typeOnly(): boolean { + return this._typeOnly.element; + } + + public withTypeOnly(typeOnly: boolean): ExportSpecifier { + return this.padding.withTypeOnly(this._typeOnly.withElement(typeOnly)); + } + + private readonly _specifier: Expression; + + public get specifier(): Expression { + return this._specifier; + } + + public withSpecifier(specifier: Expression): ExportSpecifier { + return specifier === this._specifier ? this : new ExportSpecifier(this._id, this._prefix, this._markers, this._typeOnly, specifier, this._type); + } + + private readonly _type: JavaType | null; + + public get type(): JavaType | null { + return this._type; + } + + public withType(_type: JavaType | null): ExportSpecifier { + return _type === this._type ? this : new ExportSpecifier(this._id, this._prefix, this._markers, this._typeOnly, this._specifier, _type); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitExportSpecifier(this, p); + } + + get padding() { + const t = this; + return new class { + public get typeOnly(): JLeftPadded { + return t._typeOnly; + } + public withTypeOnly(typeOnly: JLeftPadded): ExportSpecifier { + return t._typeOnly === typeOnly ? t : new ExportSpecifier(t._id, t._prefix, t._markers, typeOnly, t._specifier, t._type); + } + } + } + +} diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index fad3e942..a2f09075 100644 --- a/openrewrite/src/javascript/visitor.ts +++ b/openrewrite/src/javascript/visitor.ts @@ -1,7 +1,7 @@ import * as extensions from "./extensions"; import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core"; import {JS, isJavaScript, JsLeftPadded, JsRightPadded, JsContainer, JsSpace} from "./tree"; -import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement} from "./tree"; +import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, JsImport, JsImportSpecifier, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier} from "./tree"; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree"; import {JavaVisitor} from "../java"; import * as Java from "../java/tree"; @@ -593,7 +593,8 @@ export class JavaScriptVisitor

extends JavaVisitor

{ functionDeclaration = tempExpression as FunctionDeclaration; functionDeclaration = functionDeclaration.withMarkers(this.visitMarkers(functionDeclaration.markers, p)); functionDeclaration = functionDeclaration.withModifiers(ListUtils.map(functionDeclaration.modifiers, el => this.visitAndCast(el, p))); - functionDeclaration = functionDeclaration.withName(this.visitAndCast(functionDeclaration.name, p)); + functionDeclaration = functionDeclaration.padding.withAsteriskToken(this.visitJsLeftPadded(functionDeclaration.padding.asteriskToken, JsLeftPadded.Location.FUNCTION_DECLARATION_ASTERISK_TOKEN, p)!); + functionDeclaration = functionDeclaration.padding.withName(this.visitJsLeftPadded(functionDeclaration.padding.name, JsLeftPadded.Location.FUNCTION_DECLARATION_NAME, p)!); functionDeclaration = functionDeclaration.withTypeParameters(this.visitAndCast(functionDeclaration.typeParameters, p)); functionDeclaration = functionDeclaration.padding.withParameters(this.visitJsContainer(functionDeclaration.padding.parameters, JsContainer.Location.FUNCTION_DECLARATION_PARAMETERS, p)!); functionDeclaration = functionDeclaration.withReturnTypeExpression(this.visitAndCast(functionDeclaration.returnTypeExpression, p)); @@ -663,6 +664,64 @@ export class JavaScriptVisitor

extends JavaVisitor

{ return bindingElement; } + public visitExportDeclaration(exportDeclaration: ExportDeclaration, p: P): J | null { + exportDeclaration = exportDeclaration.withPrefix(this.visitJsSpace(exportDeclaration.prefix, JsSpace.Location.EXPORT_DECLARATION_PREFIX, p)!); + let tempStatement = this.visitStatement(exportDeclaration, p) as Statement; + if (!(tempStatement instanceof ExportDeclaration)) + { + return tempStatement; + } + exportDeclaration = tempStatement as ExportDeclaration; + exportDeclaration = exportDeclaration.withMarkers(this.visitMarkers(exportDeclaration.markers, p)); + exportDeclaration = exportDeclaration.withModifiers(ListUtils.map(exportDeclaration.modifiers, el => this.visitAndCast(el, p))); + exportDeclaration = exportDeclaration.padding.withTypeOnly(this.visitJsLeftPadded(exportDeclaration.padding.typeOnly, JsLeftPadded.Location.EXPORT_DECLARATION_TYPE_ONLY, p)!); + exportDeclaration = exportDeclaration.withExportClause(this.visitAndCast(exportDeclaration.exportClause, p)); + exportDeclaration = exportDeclaration.padding.withModuleSpecifier(this.visitJsLeftPadded(exportDeclaration.padding.moduleSpecifier, JsLeftPadded.Location.EXPORT_DECLARATION_MODULE_SPECIFIER, p)); + return exportDeclaration; + } + + public visitExportAssignment(exportAssignment: ExportAssignment, p: P): J | null { + exportAssignment = exportAssignment.withPrefix(this.visitJsSpace(exportAssignment.prefix, JsSpace.Location.EXPORT_ASSIGNMENT_PREFIX, p)!); + let tempStatement = this.visitStatement(exportAssignment, p) as Statement; + if (!(tempStatement instanceof ExportAssignment)) + { + return tempStatement; + } + exportAssignment = tempStatement as ExportAssignment; + exportAssignment = exportAssignment.withMarkers(this.visitMarkers(exportAssignment.markers, p)); + exportAssignment = exportAssignment.withModifiers(ListUtils.map(exportAssignment.modifiers, el => this.visitAndCast(el, p))); + exportAssignment = exportAssignment.padding.withExportEquals(this.visitJsLeftPadded(exportAssignment.padding.exportEquals, JsLeftPadded.Location.EXPORT_ASSIGNMENT_EXPORT_EQUALS, p)!); + exportAssignment = exportAssignment.withExpression(this.visitAndCast(exportAssignment.expression, p)); + return exportAssignment; + } + + public visitNamedExports(namedExports: NamedExports, p: P): J | null { + namedExports = namedExports.withPrefix(this.visitJsSpace(namedExports.prefix, JsSpace.Location.NAMED_EXPORTS_PREFIX, p)!); + let tempExpression = this.visitExpression(namedExports, p) as Expression; + if (!(tempExpression instanceof NamedExports)) + { + return tempExpression; + } + namedExports = tempExpression as NamedExports; + namedExports = namedExports.withMarkers(this.visitMarkers(namedExports.markers, p)); + namedExports = namedExports.padding.withElements(this.visitJsContainer(namedExports.padding.elements, JsContainer.Location.NAMED_EXPORTS_ELEMENTS, p)!); + return namedExports; + } + + public visitExportSpecifier(exportSpecifier: ExportSpecifier, p: P): J | null { + exportSpecifier = exportSpecifier.withPrefix(this.visitJsSpace(exportSpecifier.prefix, JsSpace.Location.EXPORT_SPECIFIER_PREFIX, p)!); + let tempExpression = this.visitExpression(exportSpecifier, p) as Expression; + if (!(tempExpression instanceof ExportSpecifier)) + { + return tempExpression; + } + exportSpecifier = tempExpression as ExportSpecifier; + exportSpecifier = exportSpecifier.withMarkers(this.visitMarkers(exportSpecifier.markers, p)); + exportSpecifier = exportSpecifier.padding.withTypeOnly(this.visitJsLeftPadded(exportSpecifier.padding.typeOnly, JsLeftPadded.Location.EXPORT_SPECIFIER_TYPE_ONLY, p)!); + exportSpecifier = exportSpecifier.withSpecifier(this.visitAndCast(exportSpecifier.specifier, p)!); + return exportSpecifier; + } + public visitJsLeftPadded(left: JLeftPadded | null, loc: JsLeftPadded.Location, p: P): JLeftPadded { return extensions.visitJsLeftPadded(this, left, loc, p); } diff --git a/openrewrite/test/javascript/parser/arrow.test.ts b/openrewrite/test/javascript/parser/arrow.test.ts index 5aaff4e2..c2e6a89d 100644 --- a/openrewrite/test/javascript/parser/arrow.test.ts +++ b/openrewrite/test/javascript/parser/arrow.test.ts @@ -170,4 +170,16 @@ describe('arrow mapping', () => { ); }); + test('typed with dimond cast', () => { + rewriteRun( + //language=typescript + typeScript(` + export const addTodo3 = (text: string) => ({ + type: "ADD_TODO", + text + }) + `) + ); + }); + }); diff --git a/openrewrite/test/javascript/parser/decorator.test.ts b/openrewrite/test/javascript/parser/decorator.test.ts index a1f724a9..5b6afd69 100644 --- a/openrewrite/test/javascript/parser/decorator.test.ts +++ b/openrewrite/test/javascript/parser/decorator.test.ts @@ -28,6 +28,38 @@ describe('class decorator mapping', () => { typeScript('@foo . bar ( ) class A {}') ); }); + test('class / method / params / properties decorators', () => { + rewriteRun( + //language=typescript + typeScript(` + @UseGuards(WorkspaceAuthGuard) + @Resolver() + export class RelationMetadataResolver { + constructor( + @Args('input') + private readonly relationMetadataService: RelationMetadataService, + ) {} + + @Args('input') input: DeleteOneRelationInput; + + @Mutation(() => RelationMetadataDTO) + async deleteOneRelation( + @Args('input') input: DeleteOneRelationInput, + @AuthWorkspace() { id: workspaceId }: Workspace, + ) { + try { + return await this.relationMetadataService.deleteOneRelation( + input.id, + workspaceId, + ); + } catch (error) { + relationMetadataGraphqlApiExceptionHandler(error); + } + } + } + `) + ); + }); }); // according to TypeScript documentation decorators are not allowed with diff --git a/openrewrite/test/javascript/parser/export.test.ts b/openrewrite/test/javascript/parser/export.test.ts index b1917493..4f25ea1b 100644 --- a/openrewrite/test/javascript/parser/export.test.ts +++ b/openrewrite/test/javascript/parser/export.test.ts @@ -14,6 +14,150 @@ describe('export keyword tests', () => { `) ); }); + + test('type export', () => { + rewriteRun( + //language=typescript + typeScript(` + export type ObjectMetadataItemWithFieldMaps = ObjectMetadataInterface & { + fieldsById: FieldMetadataMap; + fieldsByName: FieldMetadataMap; + }; + `), + //language=typescript + typeScript(` + import type { SomeThing } from "./some-module.js"; + export type { SomeThing }; + `) + ); + }); + + test('class export', () => { + rewriteRun( + //language=typescript + typeScript(` + + export class RelationMetadataResolver { + + } + + export default class RelationMetadataResolver { + + } + `) + ); + }); + + test('namespace export', () => { + rewriteRun( + //language=typescript + typeScript(` + export namespace MyNamespace { + export const x = 10; + export function greet() { + return 'Hello'; + } + } + `) + ); + }); + + test('enum export', () => { + rewriteRun( + //language=typescript + typeScript(` + export enum RemoteServerType { + POSTGRES_FDW = 'postgres_fdw', + STRIPE_FDW = 'stripe_fdw', + } + `) + ); + }); + + test('object export', () => { + rewriteRun( + //language=typescript + typeScript(` + function foo() {}; + function bar() {}; + export {foo, bar}; + `), + //language=typescript + typeScript(` + function foo() {} + function bar() {}; + export default {foo, bar}; + `), + //language=typescript + typeScript(` + // Default export of a variable + export default 42; + `) + ); + }); + + test('re-export', () => { + rewriteRun( + //language=typescript + typeScript(` + // Re-exporting everything from another module + export * from './accessibility'; + export * as name1 from "module-name" ; + // Re-exporting specific members from another module + export { foo, bar } from './anotherModule'; + export { foo as myFoo, bar as myBar } from './anotherModule'; + `) + ); + }); + + test('single statement export', () => { + rewriteRun( + //language=typescript + typeScript(` + // Exporting a single item as default using \`export =\` + export = MyClass; + `) + ); + }); + + test('e2e', () => { + rewriteRun( + //language=typescript + typeScript(` + // Exporting declarations + export let name1, name2/*, … */; // also var + export const name1 = 1, name2 = 2/*, … */; // also var, let + export function functionName() { /* … */ } + export class ClassName { /* … */ } + export function* generatorFunctionName() { /* … */ } + export const { name1, name2: bar } = {} ; + export const [ name1, name2 ] = array; + + // Export list + export { name1, /* …, */ nameN } ; + export { variable1 as name1, variable2 as name2, /* …, */ nameN }; + export { variable1 as "string name" }; + export { name1 as default /*, … */ }; + + // Default exports + export default expression; + export default function functionName() { /* … */ } + export default class ClassName { /* … */ } + export default function* generatorFunctionName() { /* … */ } + export default function () { /* … */ } + export default class { /* … */ } + export default function* () { /* … */ } + + // Aggregating modules + export * from "module-name"; + export * as name1 from "module-name" ; + export { name1, /* …, */ nameN } from "module-name"; + export { import1 as name1, import2 as name2, /* …, */ nameN } from "module-name" ; + export { default, /* …, */ } from "module-name"; + export { default as name1 } from "module-name"; + `) + ); + }); }); diff --git a/rewrite-javascript-remote/build.gradle.kts b/rewrite-javascript-remote/build.gradle.kts index fde4f5db..75804bc0 100644 --- a/rewrite-javascript-remote/build.gradle.kts +++ b/rewrite-javascript-remote/build.gradle.kts @@ -3,7 +3,8 @@ plugins { } -val latest = if (System.getenv("RELEASE_PUBLICATION") != null) "latest.release" else "latest.integration" +//val latest = if (System.getenv("RELEASE_PUBLICATION") != null) "latest.release" else "latest.integration" +val latest = "latest.release"; dependencies { compileOnly("com.google.auto.service:auto-service-annotations:1.1.1") 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 d44ce6bd..e2340dc4 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 @@ -517,7 +517,8 @@ public JS.FunctionDeclaration visitFunctionDeclaration(JS.FunctionDeclaration fu functionDeclaration = functionDeclaration.withPrefix(ctx.receiveNonNullNode(functionDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace)); functionDeclaration = functionDeclaration.withMarkers(ctx.receiveNonNullNode(functionDeclaration.getMarkers(), ctx::receiveMarkers)); functionDeclaration = functionDeclaration.withModifiers(ctx.receiveNonNullNodes(functionDeclaration.getModifiers(), ctx::receiveTree)); - functionDeclaration = functionDeclaration.withName(ctx.receiveNode(functionDeclaration.getName(), ctx::receiveTree)); + functionDeclaration = functionDeclaration.getPadding().withAsteriskToken(ctx.receiveNonNullNode(functionDeclaration.getPadding().getAsteriskToken(), leftPaddedValueReceiver(java.lang.Boolean.class))); + functionDeclaration = functionDeclaration.getPadding().withName(ctx.receiveNonNullNode(functionDeclaration.getPadding().getName(), JavaScriptReceiver::receiveLeftPaddedTree)); functionDeclaration = functionDeclaration.withTypeParameters(ctx.receiveNode(functionDeclaration.getTypeParameters(), ctx::receiveTree)); functionDeclaration = functionDeclaration.getPadding().withParameters(ctx.receiveNonNullNode(functionDeclaration.getPadding().getParameters(), JavaScriptReceiver::receiveContainer)); functionDeclaration = functionDeclaration.withReturnTypeExpression(ctx.receiveNode(functionDeclaration.getReturnTypeExpression(), ctx::receiveTree)); @@ -570,6 +571,50 @@ public JS.BindingElement visitBindingElement(JS.BindingElement bindingElement, R return bindingElement; } + @Override + public JS.ExportDeclaration visitExportDeclaration(JS.ExportDeclaration exportDeclaration, ReceiverContext ctx) { + exportDeclaration = exportDeclaration.withId(ctx.receiveNonNullValue(exportDeclaration.getId(), UUID.class)); + exportDeclaration = exportDeclaration.withPrefix(ctx.receiveNonNullNode(exportDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace)); + exportDeclaration = exportDeclaration.withMarkers(ctx.receiveNonNullNode(exportDeclaration.getMarkers(), ctx::receiveMarkers)); + exportDeclaration = exportDeclaration.withModifiers(ctx.receiveNonNullNodes(exportDeclaration.getModifiers(), ctx::receiveTree)); + exportDeclaration = exportDeclaration.getPadding().withTypeOnly(ctx.receiveNonNullNode(exportDeclaration.getPadding().getTypeOnly(), leftPaddedValueReceiver(java.lang.Boolean.class))); + exportDeclaration = exportDeclaration.withExportClause(ctx.receiveNode(exportDeclaration.getExportClause(), ctx::receiveTree)); + exportDeclaration = exportDeclaration.getPadding().withModuleSpecifier(ctx.receiveNode(exportDeclaration.getPadding().getModuleSpecifier(), JavaScriptReceiver::receiveLeftPaddedTree)); + return exportDeclaration; + } + + @Override + public JS.ExportAssignment visitExportAssignment(JS.ExportAssignment exportAssignment, ReceiverContext ctx) { + exportAssignment = exportAssignment.withId(ctx.receiveNonNullValue(exportAssignment.getId(), UUID.class)); + exportAssignment = exportAssignment.withPrefix(ctx.receiveNonNullNode(exportAssignment.getPrefix(), JavaScriptReceiver::receiveSpace)); + exportAssignment = exportAssignment.withMarkers(ctx.receiveNonNullNode(exportAssignment.getMarkers(), ctx::receiveMarkers)); + exportAssignment = exportAssignment.withModifiers(ctx.receiveNonNullNodes(exportAssignment.getModifiers(), ctx::receiveTree)); + exportAssignment = exportAssignment.getPadding().withExportEquals(ctx.receiveNonNullNode(exportAssignment.getPadding().getExportEquals(), leftPaddedValueReceiver(java.lang.Boolean.class))); + exportAssignment = exportAssignment.withExpression(ctx.receiveNode(exportAssignment.getExpression(), ctx::receiveTree)); + return exportAssignment; + } + + @Override + public JS.NamedExports visitNamedExports(JS.NamedExports namedExports, ReceiverContext ctx) { + namedExports = namedExports.withId(ctx.receiveNonNullValue(namedExports.getId(), UUID.class)); + namedExports = namedExports.withPrefix(ctx.receiveNonNullNode(namedExports.getPrefix(), JavaScriptReceiver::receiveSpace)); + namedExports = namedExports.withMarkers(ctx.receiveNonNullNode(namedExports.getMarkers(), ctx::receiveMarkers)); + namedExports = namedExports.getPadding().withElements(ctx.receiveNonNullNode(namedExports.getPadding().getElements(), JavaScriptReceiver::receiveContainer)); + namedExports = namedExports.withType(ctx.receiveValue(namedExports.getType(), JavaType.class)); + return namedExports; + } + + @Override + public JS.ExportSpecifier visitExportSpecifier(JS.ExportSpecifier exportSpecifier, ReceiverContext ctx) { + exportSpecifier = exportSpecifier.withId(ctx.receiveNonNullValue(exportSpecifier.getId(), UUID.class)); + exportSpecifier = exportSpecifier.withPrefix(ctx.receiveNonNullNode(exportSpecifier.getPrefix(), JavaScriptReceiver::receiveSpace)); + exportSpecifier = exportSpecifier.withMarkers(ctx.receiveNonNullNode(exportSpecifier.getMarkers(), ctx::receiveMarkers)); + exportSpecifier = exportSpecifier.getPadding().withTypeOnly(ctx.receiveNonNullNode(exportSpecifier.getPadding().getTypeOnly(), leftPaddedValueReceiver(java.lang.Boolean.class))); + exportSpecifier = exportSpecifier.withSpecifier(ctx.receiveNonNullNode(exportSpecifier.getSpecifier(), ctx::receiveTree)); + exportSpecifier = exportSpecifier.withType(ctx.receiveValue(exportSpecifier.getType(), JavaType.class)); + return exportSpecifier; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, ReceiverContext ctx) { annotatedType = annotatedType.withId(ctx.receiveNonNullValue(annotatedType.getId(), UUID.class)); @@ -1714,7 +1759,8 @@ public T create(Class type, ReceiverContext ctx) { ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), ctx.receiveNonNullNode(null, ctx::receiveMarkers), ctx.receiveNonNullNodes(null, ctx::receiveTree), - ctx.receiveNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree), ctx.receiveNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer), ctx.receiveNode(null, ctx::receiveTree), @@ -1767,6 +1813,50 @@ public T create(Class type, ReceiverContext ctx) { ); } + if (type == JS.ExportDeclaration.class) { + return (T) new JS.ExportDeclaration( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNodes(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), + ctx.receiveNode(null, ctx::receiveTree), + ctx.receiveNode(null, JavaScriptReceiver::receiveLeftPaddedTree) + ); + } + + if (type == JS.ExportAssignment.class) { + return (T) new JS.ExportAssignment( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNodes(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), + ctx.receiveNode(null, ctx::receiveTree) + ); + } + + if (type == JS.NamedExports.class) { + return (T) new JS.NamedExports( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveContainer), + ctx.receiveValue(null, JavaType.class) + ); + } + + if (type == JS.ExportSpecifier.class) { + return (T) new JS.ExportSpecifier( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, leftPaddedValueReceiver(java.lang.Boolean.class)), + ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveValue(null, JavaType.class) + ); + } + if (type == J.AnnotatedType.class) { return (T) new J.AnnotatedType( ctx.receiveNonNullValue(null, UUID.class), diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptSender.java index a61d78b1..057b760d 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 @@ -500,7 +500,8 @@ public JS.FunctionDeclaration visitFunctionDeclaration(JS.FunctionDeclaration fu ctx.sendNode(functionDeclaration, JS.FunctionDeclaration::getPrefix, JavaScriptSender::sendSpace); ctx.sendNode(functionDeclaration, JS.FunctionDeclaration::getMarkers, ctx::sendMarkers); ctx.sendNodes(functionDeclaration, JS.FunctionDeclaration::getModifiers, ctx::sendTree, Tree::getId); - ctx.sendNode(functionDeclaration, JS.FunctionDeclaration::getName, ctx::sendTree); + ctx.sendNode(functionDeclaration, e -> e.getPadding().getAsteriskToken(), JavaScriptSender::sendLeftPadded); + ctx.sendNode(functionDeclaration, e -> e.getPadding().getName(), JavaScriptSender::sendLeftPadded); ctx.sendNode(functionDeclaration, JS.FunctionDeclaration::getTypeParameters, ctx::sendTree); ctx.sendNode(functionDeclaration, e -> e.getPadding().getParameters(), JavaScriptSender::sendContainer); ctx.sendNode(functionDeclaration, JS.FunctionDeclaration::getReturnTypeExpression, ctx::sendTree); @@ -553,6 +554,50 @@ public JS.BindingElement visitBindingElement(JS.BindingElement bindingElement, S return bindingElement; } + @Override + public JS.ExportDeclaration visitExportDeclaration(JS.ExportDeclaration exportDeclaration, SenderContext ctx) { + ctx.sendValue(exportDeclaration, JS.ExportDeclaration::getId); + ctx.sendNode(exportDeclaration, JS.ExportDeclaration::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(exportDeclaration, JS.ExportDeclaration::getMarkers, ctx::sendMarkers); + ctx.sendNodes(exportDeclaration, JS.ExportDeclaration::getModifiers, ctx::sendTree, Tree::getId); + ctx.sendNode(exportDeclaration, e -> e.getPadding().getTypeOnly(), JavaScriptSender::sendLeftPadded); + ctx.sendNode(exportDeclaration, JS.ExportDeclaration::getExportClause, ctx::sendTree); + ctx.sendNode(exportDeclaration, e -> e.getPadding().getModuleSpecifier(), JavaScriptSender::sendLeftPadded); + return exportDeclaration; + } + + @Override + public JS.ExportAssignment visitExportAssignment(JS.ExportAssignment exportAssignment, SenderContext ctx) { + ctx.sendValue(exportAssignment, JS.ExportAssignment::getId); + ctx.sendNode(exportAssignment, JS.ExportAssignment::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(exportAssignment, JS.ExportAssignment::getMarkers, ctx::sendMarkers); + ctx.sendNodes(exportAssignment, JS.ExportAssignment::getModifiers, ctx::sendTree, Tree::getId); + ctx.sendNode(exportAssignment, e -> e.getPadding().getExportEquals(), JavaScriptSender::sendLeftPadded); + ctx.sendNode(exportAssignment, JS.ExportAssignment::getExpression, ctx::sendTree); + return exportAssignment; + } + + @Override + public JS.NamedExports visitNamedExports(JS.NamedExports namedExports, SenderContext ctx) { + ctx.sendValue(namedExports, JS.NamedExports::getId); + ctx.sendNode(namedExports, JS.NamedExports::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(namedExports, JS.NamedExports::getMarkers, ctx::sendMarkers); + ctx.sendNode(namedExports, e -> e.getPadding().getElements(), JavaScriptSender::sendContainer); + ctx.sendTypedValue(namedExports, JS.NamedExports::getType); + return namedExports; + } + + @Override + public JS.ExportSpecifier visitExportSpecifier(JS.ExportSpecifier exportSpecifier, SenderContext ctx) { + ctx.sendValue(exportSpecifier, JS.ExportSpecifier::getId); + ctx.sendNode(exportSpecifier, JS.ExportSpecifier::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(exportSpecifier, JS.ExportSpecifier::getMarkers, ctx::sendMarkers); + ctx.sendNode(exportSpecifier, e -> e.getPadding().getTypeOnly(), JavaScriptSender::sendLeftPadded); + ctx.sendNode(exportSpecifier, JS.ExportSpecifier::getSpecifier, ctx::sendTree); + ctx.sendTypedValue(exportSpecifier, JS.ExportSpecifier::getType); + return exportSpecifier; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, SenderContext ctx) { ctx.sendValue(annotatedType, J.AnnotatedType::getId); diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptValidator.java index cb5d3540..f51f0d2d 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 @@ -58,7 +58,7 @@ public JS.CompilationUnit visitCompilationUnit(JS.CompilationUnit compilationUni @Override public JS.Alias visitAlias(JS.Alias alias, P p) { visitAndValidate(alias.getPropertyName(), J.Identifier.class, p); - visitAndValidate(alias.getAlias(), J.Identifier.class, p); + visitAndValidate(alias.getAlias(), Expression.class, p); return alias; } @@ -368,6 +368,33 @@ public JS.BindingElement visitBindingElement(JS.BindingElement bindingElement, P return bindingElement; } + @Override + public JS.ExportDeclaration visitExportDeclaration(JS.ExportDeclaration exportDeclaration, P p) { + ListUtils.map(exportDeclaration.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p)); + visitAndValidate(exportDeclaration.getExportClause(), Expression.class, p); + visitAndValidate(exportDeclaration.getModuleSpecifier(), Expression.class, p); + return exportDeclaration; + } + + @Override + public JS.ExportAssignment visitExportAssignment(JS.ExportAssignment exportAssignment, P p) { + ListUtils.map(exportAssignment.getModifiers(), el -> visitAndValidate(el, J.Modifier.class, p)); + visitAndValidate(exportAssignment.getExpression(), Expression.class, p); + return exportAssignment; + } + + @Override + public JS.NamedExports visitNamedExports(JS.NamedExports namedExports, P p) { + visitAndValidate(namedExports.getElements(), JS.ExportSpecifier.class, p); + return namedExports; + } + + @Override + public JS.ExportSpecifier visitExportSpecifier(JS.ExportSpecifier exportSpecifier, P p) { + visitAndValidate(exportSpecifier.getSpecifier(), Expression.class, p); + return exportSpecifier; + } + @Override public J.AnnotatedType visitAnnotatedType(J.AnnotatedType annotatedType, P p) { ListUtils.map(annotatedType.getAnnotations(), el -> visitAndValidate(el, J.Annotation.class, p)); diff --git a/rewrite-javascript/build.gradle.kts b/rewrite-javascript/build.gradle.kts index 955aa1ec..2f8c4fb7 100644 --- a/rewrite-javascript/build.gradle.kts +++ b/rewrite-javascript/build.gradle.kts @@ -5,7 +5,8 @@ plugins { } -val latest = if (System.getenv("RELEASE_PUBLICATION") != null) "latest.release" else "latest.integration" +//val latest = if (System.getenv("RELEASE_PUBLICATION") != null) "latest.release" else "latest.integration" +val latest = "latest.release"; dependencies { compileOnly("org.openrewrite:rewrite-test") @@ -42,4 +43,4 @@ tasks.withType { this as CoreJavadocOptions addStringOption("Xdoclint:none", "-quiet") } -} \ No newline at end of file +} 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 020d1249..76f3a983 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptParser.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptParser.java @@ -96,7 +96,7 @@ public Stream parseInputs(Iterable inputs, @Nullable Path rel assert client != null; assert remotingContext != null; try (EncodingDetectingInputStream is = input.getSource(ctx)) { - SourceFile parsed = client.runUsingSocket((socket, messenger) -> requireNonNull(messenger.sendRequest(generator -> { + SourceFile parsed = client.withNewSocket((socket, messenger) -> requireNonNull(messenger.sendRequest(generator -> { if (input.isSynthetic() || !Files.isRegularFile(input.getPath())) { generator.writeString("parse-source"); generator.writeString(is.readFully()); 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 a1400a99..03e26300 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -66,8 +66,8 @@ public J visitAlias(JS.Alias alias, P p) { a = (JS.Alias) temp; } - a = a.getPadding().withPropertyName(visitRightPadded(a.getPadding().getPropertyName(), JsRightPadded.Location.ALIAS_PROPERTY_NAME, p)); - a = a.withAlias(visitAndCast(a.getAlias(), p)); + a = a.getPadding().withPropertyName(Objects.requireNonNull(visitRightPadded(a.getPadding().getPropertyName(), JsRightPadded.Location.ALIAS_PROPERTY_NAME, p))); + a = a.withAlias(Objects.requireNonNull(visitAndCast(a.getAlias(), p))); return a; } @@ -84,8 +84,6 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, P p) { } a = a.withLeadingAnnotations(ListUtils.map(a.getLeadingAnnotations(), ann -> visitAndCast(ann, p))); - a = a.withModifiers(ListUtils.map(a.getModifiers(), - mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); a = a.withModifiers(ListUtils.map(a.getModifiers(), m -> visitAndCast(m, p))); a = a.withTypeParameters(visitAndCast(a.getTypeParameters(), p)); @@ -101,10 +99,10 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, P p) { ) ) ); - a = a.withParameters(visitAndCast(a.getParameters(), p)); + a = a.withParameters(Objects.requireNonNull(visitAndCast(a.getParameters(), p))); a = a.withReturnTypeExpression(visitAndCast(a.getReturnTypeExpression(), p)); a = a.withArrow(visitSpace(a.getArrow(), Space.Location.LAMBDA_ARROW_PREFIX, p)); - a = a.withBody(visitAndCast(a.getBody(), p)); + a = a.withBody(Objects.requireNonNull(visitAndCast(a.getBody(), p))); a = a.withType(visitType(a.getType(), p)); return a; } @@ -120,7 +118,7 @@ public J visitAwait(JS.Await await, P p) { } else { a = (JS.Await) temp; } - a = a.withExpression(visitAndCast(a.getExpression(), p)); + a = a.withExpression(Objects.requireNonNull(visitAndCast(a.getExpression(), p))); return a; } @@ -142,7 +140,7 @@ public J visitConditionalType(JS.ConditionalType conditionalType, P p) { JS.ConditionalType t = conditionalType; t = t.withPrefix(visitSpace(t.getPrefix(), JsSpace.Location.CONDITIONAL_TYPE_PREFIX, p)); t = t.withMarkers(visitMarkers(t.getMarkers(), p)); - t = t.withCheckType(visitAndCast(t.getCheckType(), p)); + t = t.withCheckType(Objects.requireNonNull(visitAndCast(t.getCheckType(), p))); if (t.getCheckType() instanceof NameTree) { t = t.withCheckType((Expression) visitTypeName((NameTree) t.getCheckType(), p)); } @@ -161,9 +159,9 @@ public J visitDefaultType(JS.DefaultType defaultType, P p) { } else { d = (JS.DefaultType) temp; } - d = d.withLeft(visitAndCast(d.getLeft(), p)); + d = d.withLeft(Objects.requireNonNull(visitAndCast(d.getLeft(), p))); d = d.withBeforeEquals(visitSpace(d.getBeforeEquals(), Space.Location.ASSIGNMENT_OPERATION_PREFIX, p)); - d = d.withRight(visitAndCast(d.getRight(), p)); + d = d.withRight(Objects.requireNonNull(visitAndCast(d.getRight(), p))); d = d.withType(visitType(d.getType(), p)); return d; } @@ -233,7 +231,7 @@ public J visitExpressionWithTypeArguments(JS.ExpressionWithTypeArguments express } else { ta = (JS.ExpressionWithTypeArguments) temp; } - ta = ta.withClazz(visitAndCast(ta.getClazz(), p)); + ta = ta.withClazz(Objects.requireNonNull(visitAndCast(ta.getClazz(), p))); if (ta.getPadding().getTypeArguments() != null) { ta = ta.getPadding().withTypeArguments(visitContainer(ta.getPadding().getTypeArguments(), JsContainer.Location.EXPR_WITH_TYPE_ARG_PARAMETERS, p)); } @@ -252,11 +250,11 @@ public J visitFunctionType(JS.FunctionType functionType, P p) { } else { f = (JS.FunctionType) temp; } - f = f.getPadding().withConstructorType(visitRightPadded(f.getPadding().getConstructorType(), JsRightPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, p)); - f = f.getPadding().withParameters(visitContainer(f.getPadding().getParameters(), JContainer.Location.LANGUAGE_EXTENSION, p)); + f = f.getPadding().withConstructorType(Objects.requireNonNull(visitRightPadded(f.getPadding().getConstructorType(), JsRightPadded.Location.FUNCTION_TYPE_CONSTRUCTOR, 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))); f = f.withArrow(visitSpace(f.getArrow(), JsSpace.Location.FUNCTION_TYPE_ARROW_PREFIX, p)); - f = f.withReturnType(visitAndCast(f.getReturnType(), p)); + f = f.withReturnType(Objects.requireNonNull(visitAndCast(f.getReturnType(), p))); f = f.withType(visitType(f.getType(), p)); return f; } @@ -271,9 +269,9 @@ public J visitJsBinary(JS.JsBinary binary, P p) { } else { b = (JS.JsBinary) temp; } - b = b.withLeft(visitAndCast(b.getLeft(), p)); - b = b.getPadding().withOperator(visitLeftPadded(b.getPadding().getOperator(), JsLeftPadded.Location.BINARY_OPERATOR, p)); - b = b.withRight(visitAndCast(b.getRight(), p)); + b = b.withLeft(Objects.requireNonNull(visitAndCast(b.getLeft(), p))); + b = b.getPadding().withOperator(Objects.requireNonNull(visitLeftPadded(b.getPadding().getOperator(), JsLeftPadded.Location.BINARY_OPERATOR, p))); + b = b.withRight(Objects.requireNonNull(visitAndCast(b.getRight(), p))); b = b.withType(visitType(b.getType(), p)); return b; } @@ -288,7 +286,7 @@ public J visitJsImport(JS.JsImport jsImport, P p) { } else { i = (JS.JsImport) temp; } - i = i.getPadding().withImportType(visitLeftPadded(i.getPadding().getImportType(), JsLeftPadded.Location.JS_IMPORT_IMPORT_TYPE, p)); + i = i.getPadding().withImportType(Objects.requireNonNull(visitLeftPadded(i.getPadding().getImportType(), JsLeftPadded.Location.JS_IMPORT_IMPORT_TYPE, p))); i = i.withName(visitAndCast(i.getName(), p)); if (i.getPadding().getImports() != null) { i = i.getPadding().withImports(visitContainer(i.getPadding().getImports(), JsContainer.Location.IMPORT_ELEMENT, p)); @@ -314,7 +312,7 @@ public J visitJsImportSpecifier(JS.JsImportSpecifier jis, P p) { } else { i = (JS.JsImportSpecifier) temp; } - i = i.getPadding().withImportType(visitLeftPadded(i.getPadding().getImportType(), JsLeftPadded.Location.JS_IMPORT_SPECIFIER_IMPORT_TYPE, p)); + i = i.getPadding().withImportType(Objects.requireNonNull(visitLeftPadded(i.getPadding().getImportType(), JsLeftPadded.Location.JS_IMPORT_SPECIFIER_IMPORT_TYPE, p))); i = i.withSpecifier(Objects.requireNonNull(visitAndCast(i.getSpecifier(), p))); i = i.withType(visitType(i.getType(), p)); return i; @@ -331,14 +329,12 @@ public J visitObjectBindingDeclarations(JS.ObjectBindingDeclarations objectBindi o = (JS.ObjectBindingDeclarations) temp; } o = o.withLeadingAnnotations(ListUtils.map(o.getLeadingAnnotations(), a -> visitAndCast(a, p))); - o = o.withModifiers(ListUtils.map(o.getModifiers(), - mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); o = o.withModifiers(Objects.requireNonNull(ListUtils.map(o.getModifiers(), e -> visitAndCast(e, p)))); o = o.withTypeExpression(visitAndCast(o.getTypeExpression(), p)); o = o.withTypeExpression(o.getTypeExpression() == null ? null : visitTypeName(o.getTypeExpression(), p)); - o = o.getPadding().withBindings(visitContainer(o.getPadding().getBindings(), JsContainer.Location.BINDING_ELEMENT, p)); + o = o.getPadding().withBindings(Objects.requireNonNull(visitContainer(o.getPadding().getBindings(), JsContainer.Location.BINDING_ELEMENT, p))); if (o.getPadding().getInitializer() != null) { o = o.getPadding().withInitializer(visitLeftPadded(o.getPadding().getInitializer(), JsLeftPadded.Location.BINDING_ELEMENT_INITIALIZER, p)); @@ -356,7 +352,7 @@ public J visitPropertyAssignment(JS.PropertyAssignment propertyAssignment, P p) } else { pa = (JS.PropertyAssignment) temp; } - pa = pa.getPadding().withName(visitRightPadded(pa.getPadding().getName(), JsRightPadded.Location.PROPERTY_ASSIGNMENT_NAME, p)); + pa = pa.getPadding().withName(Objects.requireNonNull(visitRightPadded(pa.getPadding().getName(), JsRightPadded.Location.PROPERTY_ASSIGNMENT_NAME, p))); pa = pa.withInitializer(visitAndCast(pa.getInitializer(), p)); return pa; } @@ -365,8 +361,6 @@ public J visitScopedVariableDeclarations(JS.ScopedVariableDeclarations scopedVar JS.ScopedVariableDeclarations vd = scopedVariableDeclarations; vd = vd.withPrefix(visitSpace(vd.getPrefix(), JsSpace.Location.SCOPED_VARIABLE_DECLARATIONS_PREFIX, p)); vd = vd.withMarkers(visitMarkers(vd.getMarkers(), p)); - vd = vd.withModifiers(ListUtils.map(vd.getModifiers(), - mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); vd = vd.getPadding().withScope(visitLeftPadded(vd.getPadding().getScope(), JsLeftPadded.Location.SCOPED_VARIABLE_DECLARATIONS_SCOPE, p)); Statement temp = (Statement) visitStatement(vd, p); if (!(temp instanceof JS.ScopedVariableDeclarations)) { @@ -408,7 +402,7 @@ public J visitTaggedTemplateExpression (JS.TaggedTemplateExpression taggedTempla if (ta.getPadding().getTypeArguments() != null) { ta = ta.getPadding().withTypeArguments(visitContainer(ta.getPadding().getTypeArguments(), JsContainer.Location.TEMPLATE_EXPRESSION_TYPE_ARG_PARAMETERS, p)); } - ta = ta.withTemplateExpression(visitAndCast(ta.getTemplateExpression(), p)); + ta = ta.withTemplateExpression(Objects.requireNonNull(visitAndCast(ta.getTemplateExpression(), p))); return ta; } @@ -422,7 +416,7 @@ public J visitTemplateExpression(JS.TemplateExpression templateExpression, P p) } else { te = (JS.TemplateExpression) temp; } - te = te.withHead(visitAndCast(te.getHead(), p)); + te = te.withHead(Objects.requireNonNull(visitAndCast(te.getHead(), p))); te = te.getPadding().withTemplateSpans(ListUtils.map(te.getPadding().getTemplateSpans(), (t) -> this.visitRightPadded(t, JsRightPadded.Location.TEMPLATE_EXPRESSION_TEMPLATE_SPAN, p))); te = te.withType(visitType(te.getType(), p)); return te; @@ -432,8 +426,8 @@ public J visitTemplateExpressionTemplateSpan(JS.TemplateExpression.TemplateSpan JS.TemplateExpression.TemplateSpan s = span; s = s.withPrefix(visitSpace(s.getPrefix(), JsSpace.Location.TEMPLATE_EXPRESSION_SPAN_PREFIX, p)); s = s.withMarkers(visitMarkers(s.getMarkers(), p)); - s = s.withExpression(visitAndCast(s.getExpression(), p)); - s = s.withTail(visitAndCast(s.getTail(), p)); + s = s.withExpression(Objects.requireNonNull(visitAndCast(s.getExpression(), p))); + s = s.withTail(Objects.requireNonNull(visitAndCast(s.getTail(), p))); s = s.withTail(s.getTail()); return s; } @@ -448,7 +442,7 @@ public J visitTuple(JS.Tuple tuple, P p) { } else { t = (JS.Tuple) temp; } - t = t.getPadding().withElements(visitContainer(t.getPadding().getElements(), JsContainer.Location.TUPLE_ELEMENT, p)); + t = t.getPadding().withElements(Objects.requireNonNull(visitContainer(t.getPadding().getElements(), JsContainer.Location.TUPLE_ELEMENT, p))); t = t.withType(visitType(t.getType(), p)); return t; } @@ -463,13 +457,11 @@ public J visitTypeDeclaration(JS.TypeDeclaration typeDeclaration, P p) { } else { t = (JS.TypeDeclaration) temp; } - t = t.withModifiers(ListUtils.map(t.getModifiers(), - mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); t = t.withModifiers(Objects.requireNonNull(ListUtils.map(t.getModifiers(), e -> visitAndCast(e, p)))); - t = t.getPadding().withName(visitLeftPadded(t.getPadding().getName(), JsLeftPadded.Location.TYPE_DECLARATION_NAME, p)); + t = t.getPadding().withName(Objects.requireNonNull(visitLeftPadded(t.getPadding().getName(), JsLeftPadded.Location.TYPE_DECLARATION_NAME, p))); t = t.withTypeParameters(visitAndCast(t.getTypeParameters(), p)); - t = t.getPadding().withInitializer(visitLeftPadded(t.getPadding().getInitializer(), - JsLeftPadded.Location.TYPE_DECLARATION_INITIALIZER, p)); + t = t.getPadding().withInitializer(Objects.requireNonNull(visitLeftPadded(t.getPadding().getInitializer(), + JsLeftPadded.Location.TYPE_DECLARATION_INITIALIZER, p))); t = t.withType(visitType(t.getType(), p)); return t; } @@ -484,7 +476,7 @@ public J visitTypeOperator(JS.TypeOperator typeOperator, P p) { } else { t = (JS.TypeOperator) temp; } - t = t.getPadding().withExpression(visitLeftPadded(t.getPadding().getExpression(), JsLeftPadded.Location.TYPE_OPERATOR, p)); + t = t.getPadding().withExpression(Objects.requireNonNull(visitLeftPadded(t.getPadding().getExpression(), JsLeftPadded.Location.TYPE_OPERATOR, p))); return t; } @@ -504,8 +496,8 @@ public J visitUnary(JS.Unary unary, P p) { } else { u = (JS.Unary) temp2; } - u = u.getPadding().withOperator(visitLeftPadded(u.getPadding().getOperator(), JLeftPadded.Location.UNARY_OPERATOR, p)); - u = u.withExpression(visitAndCast(u.getExpression(), p)); + u = u.getPadding().withOperator(Objects.requireNonNull(visitLeftPadded(u.getPadding().getOperator(), JLeftPadded.Location.UNARY_OPERATOR, p))); + u = u.withExpression(Objects.requireNonNull(visitAndCast(u.getExpression(), p))); u = u.withType(visitType(u.getType(), p)); return u; } @@ -540,6 +532,72 @@ public J visitIntersection(JS.Intersection intersection, P p) { return u; } + public J visitExportDeclaration(JS.ExportDeclaration exportDeclaration, P p) { + JS.ExportDeclaration ed = exportDeclaration; + ed = ed.withPrefix(visitSpace(ed.getPrefix(), JsSpace.Location.EXPORT_DECLARATION_PREFIX, p)); + ed = ed.withMarkers(visitMarkers(ed.getMarkers(), p)); + Statement temp = (Statement) visitStatement(ed, p); + if (!(temp instanceof JS.ExportDeclaration)) { + return temp; + } else { + ed = (JS.ExportDeclaration) temp; + } + + ed = ed.withModifiers(Objects.requireNonNull(ListUtils.map(ed.getModifiers(), e -> visitAndCast(e, p)))); + ed = ed.getPadding().withTypeOnly(Objects.requireNonNull(visitLeftPadded(ed.getPadding().getTypeOnly(), JsLeftPadded.Location.EXPORT_DECLARATION_TYPE_ONLY, p))); + ed = ed.withExportClause(visitAndCast(ed.getExportClause(), p)); + ed = ed.getPadding().withModuleSpecifier(visitLeftPadded(ed.getPadding().getModuleSpecifier(), JsLeftPadded.Location.EXPORT_DECLARATION_MODULE_SPECIFIER, p)); + return ed; + } + + public J visitExportAssignment(JS.ExportAssignment exportAssignment, P p) { + JS.ExportAssignment es = exportAssignment; + es = es.withPrefix(visitSpace(es.getPrefix(), JsSpace.Location.EXPORT_ASSIGNMENT_PREFIX, p)); + es = es.withMarkers(visitMarkers(es.getMarkers(), p)); + Statement temp = (Statement) visitStatement(es, p); + if (!(temp instanceof JS.ExportAssignment)) { + return temp; + } else { + es = (JS.ExportAssignment) temp; + } + + es = es.withModifiers(Objects.requireNonNull(ListUtils.map(es.getModifiers(), e -> visitAndCast(e, p)))); + es = es.getPadding().withExportEquals(Objects.requireNonNull(visitLeftPadded(es.getPadding().getExportEquals(), JsLeftPadded.Location.EXPORT_ASSIGNMENT_EXPORT_EQUALS, p))); + es = es.withExpression(visitAndCast(es.getExpression(), p)); + return es; + } + + public J visitNamedExports(JS.NamedExports namedExports, P p) { + JS.NamedExports ne = namedExports; + ne = ne.withPrefix(visitSpace(ne.getPrefix(), JsSpace.Location.NAMED_EXPORTS_PREFIX, p)); + ne = ne.withMarkers(visitMarkers(ne.getMarkers(), p)); + Expression temp = (Expression) visitExpression(ne, p); + if (!(temp instanceof JS.NamedExports)) { + return temp; + } else { + ne = (JS.NamedExports) temp; + } + ne = ne.getPadding().withElements(Objects.requireNonNull(visitContainer(ne.getPadding().getElements(), JsContainer.Location.NAMED_EXPORTS_ELEMENTS, p))); + ne = ne.withType(visitType(ne.getType(), p)); + return ne; + } + + public J visitExportSpecifier(JS.ExportSpecifier exportSpecifier, P p) { + JS.ExportSpecifier es = exportSpecifier; + es = es.withPrefix(visitSpace(es.getPrefix(), JsSpace.Location.EXPORT_SPECIFIER_PREFIX, p)); + es = es.withMarkers(visitMarkers(es.getMarkers(), p)); + Expression temp = (Expression) visitExpression(es, p); + if (!(temp instanceof JS.ExportSpecifier)) { + return temp; + } else { + es = (JS.ExportSpecifier) temp; + } + es = es.getPadding().withTypeOnly(Objects.requireNonNull(visitLeftPadded(es.getPadding().getTypeOnly(), JsLeftPadded.Location.EXPORT_SPECIFIER_TYPE_ONLY, p))); + es = es.withSpecifier(Objects.requireNonNull(visitAndCast(es.getSpecifier(), p))); + es = es.withType(visitType(es.getType(), p)); + return es; + } + // TODO: remove me. Requires changes from rewrite-java. @Override public J visitAnnotatedType(J.AnnotatedType annotatedType, P p) { @@ -606,15 +664,15 @@ public Space visitSpace(Space space, JsSpace.Location loc, P p) { return visitSpace(space, Space.Location.LANGUAGE_EXTENSION, p); } - public JRightPadded visitRightPadded(@Nullable JRightPadded right, JsRightPadded.Location loc, P p) { + public @Nullable JRightPadded visitRightPadded(@Nullable JRightPadded right, JsRightPadded.Location loc, P p) { return super.visitRightPadded(right, JRightPadded.Location.LANGUAGE_EXTENSION, p); } - public JLeftPadded visitLeftPadded(JLeftPadded left, JsLeftPadded.Location loc, P p) { + public @Nullable JLeftPadded visitLeftPadded(@Nullable JLeftPadded left, JsLeftPadded.Location loc, P p) { return super.visitLeftPadded(left, JLeftPadded.Location.LANGUAGE_EXTENSION, p); } - public JContainer visitContainer(JContainer container, JsContainer.Location loc, P p) { + public @Nullable JContainer visitContainer(@Nullable JContainer container, JsContainer.Location loc, P p) { return super.visitContainer(container, JContainer.Location.LANGUAGE_EXTENSION, p); } @@ -628,7 +686,7 @@ public J visitTypeOf(JS.TypeOf typeOf, P p) { } else { t = (JS.TypeOf) temp; } - t = t.withExpression(visitAndCast(t.getExpression(), p)); + t = t.withExpression(Objects.requireNonNull(visitAndCast(t.getExpression(), p))); t = t.withType(visitType(t.getType(), p)); return t; } @@ -643,7 +701,7 @@ public J visitTypeQuery(JS.TypeQuery typeQuery, P p) { } else { t = (JS.TypeQuery) temp; } - t = t.withTypeExpression(visitAndCast(t.getTypeExpression(), p)); + t = t.withTypeExpression(Objects.requireNonNull(visitAndCast(t.getTypeExpression(), p))); t = t.withType(visitType(t.getType(), p)); return t; } @@ -658,7 +716,7 @@ public J visitVoid(JS.Void aVoid, P p) { } else { v = (JS.Void) temp; } - v = v.withExpression(visitAndCast(v.getExpression(), p)); + v = v.withExpression(Objects.requireNonNull(visitAndCast(v.getExpression(), p))); return v; } @@ -686,7 +744,7 @@ public J visitTypeInfo(JS.TypeInfo typeInfo, P p) { } else { ti = (JS.TypeInfo) temp; } - ti = ti.withTypeIdentifier(visitAndCast(ti.getTypeIdentifier(), p)); + ti = ti.withTypeIdentifier(Objects.requireNonNull(visitAndCast(ti.getTypeIdentifier(), p))); return ti; } @@ -694,7 +752,7 @@ public J visitJSVariableDeclarations(JS.JSVariableDeclarations multiVariable, P JS.JSVariableDeclarations m = multiVariable.withPrefix(this.visitSpace(multiVariable.getPrefix(), JsSpace.Location.JSVARIABLE_DECLARATIONS_PREFIX, p)); m = m.withMarkers(this.visitMarkers(m.getMarkers(), p)); Statement temp = (Statement)this.visitStatement(m, p); - if (!(temp instanceof J.VariableDeclarations)) { + if (!(temp instanceof JS.JSVariableDeclarations)) { return temp; } else { m = (JS.JSVariableDeclarations)temp; @@ -711,7 +769,7 @@ public J visitJSVariableDeclarations(JS.JSVariableDeclarations multiVariable, P public J visitJSVariableDeclarationsJSNamedVariable(JS.JSVariableDeclarations.JSNamedVariable variable, P p) { JS.JSVariableDeclarations.JSNamedVariable v = variable.withPrefix(this.visitSpace(variable.getPrefix(), JsSpace.Location.JSVARIABLE_PREFIX, p)); v = v.withMarkers(this.visitMarkers(v.getMarkers(), p)); - v = v.withName(this.visitAndCast(v.getName(), p)); + v = v.withName(Objects.requireNonNull(this.visitAndCast(v.getName(), p))); v = v.withDimensionsAfterName(ListUtils.map(v.getDimensionsAfterName(), (dim) -> dim.withBefore(this.visitSpace(dim.getBefore(), Space.Location.DIMENSION_PREFIX, p)).withElement(this.visitSpace((Space)dim.getElement(), Space.Location.DIMENSION, p)))); if (v.getPadding().getInitializer() != null) { v = v.getPadding().withInitializer(this.visitLeftPadded(v.getPadding().getInitializer(), JsLeftPadded.Location.JSVARIABLE_INITIALIZER, p)); @@ -739,8 +797,8 @@ public J visitJSMethodDeclaration(JS.JSMethodDeclaration method, P p) { m.getReturnTypeExpression() == null ? null : visitTypeName(m.getReturnTypeExpression(), p)); - m = m.withName(this.visitAndCast(m.getName(), p)); - m = m.getPadding().withParameters(visitContainer(m.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, p)); + m = m.withName(Objects.requireNonNull(this.visitAndCast(m.getName(), p))); + m = m.getPadding().withParameters(Objects.requireNonNull(visitContainer(m.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, p))); if (m.getPadding().getThrowz() != null) { m = m.getPadding().withThrowz(visitContainer(m.getPadding().getThrowz(), JContainer.Location.THROWS, p)); } @@ -762,12 +820,10 @@ public J visitNamespaceDeclaration(JS.NamespaceDeclaration namespaceDeclaration, } else { ns = (JS.NamespaceDeclaration) temp; } - 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)); + 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))); return ns; } @@ -783,11 +839,12 @@ public J visitFunctionDeclaration(JS.FunctionDeclaration functionDeclaration, P } f = f.withModifiers(ListUtils.map(f.getModifiers(), e -> visitAndCast(e, p))); - f = f.withName(this.visitAndCast(f.getName(), p)); + f = f.getPadding().withAsteriskToken(Objects.requireNonNull(visitLeftPadded(f.getPadding().getAsteriskToken(), JsLeftPadded.Location.FUNCTION_DECLARATION_ASTERISK_TOKEN, p))); + f = f.getPadding().withName(Objects.requireNonNull(visitLeftPadded(f.getPadding().getName(), JsLeftPadded.Location.FUNCTION_DECLARATION_NAME, p))); f = f.withTypeParameters(visitAndCast(f.getTypeParameters(), p)); - f = f.getPadding().withParameters(visitContainer(f.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, p)); + f = f.getPadding().withParameters(Objects.requireNonNull(visitContainer(f.getPadding().getParameters(), JContainer.Location.METHOD_DECLARATION_PARAMETERS, p))); f = f.withReturnTypeExpression(visitAndCast(f.getReturnTypeExpression(), p)); - f = f.withBody(visitAndCast(f.getBody(), p)); + f = f.withBody(Objects.requireNonNull(visitAndCast(f.getBody(), p))); f = f.withType(visitType(f.getType(), p)); return f; } @@ -821,10 +878,9 @@ public J visitIndexSignatureDeclaration(JS.IndexSignatureDeclaration indexSignat isd = (JS.IndexSignatureDeclaration) temp; } - isd = isd.withModifiers(ListUtils.map(isd.getModifiers(), - mod -> mod.withPrefix(visitSpace(mod.getPrefix(), Space.Location.MODIFIER_PREFIX, p)))); - isd = isd.getPadding().withParameters(visitContainer(isd.getPadding().getParameters(), JsContainer.Location.INDEXED_SIGNATURE_DECLARATION_PARAMETERS, p)); - isd = isd.getPadding().withTypeExpression(visitLeftPadded(isd.getPadding().getTypeExpression(), JsLeftPadded.Location.INDEXED_SIGNATURE_DECLARATION_TYPE_EXPRESSION, p)); + isd = isd.withModifiers(Objects.requireNonNull(ListUtils.map(isd.getModifiers(), e -> visitAndCast(e, p)))); + isd = isd.getPadding().withParameters(Objects.requireNonNull(visitContainer(isd.getPadding().getParameters(), JsContainer.Location.INDEXED_SIGNATURE_DECLARATION_PARAMETERS, p))); + isd = isd.getPadding().withTypeExpression(Objects.requireNonNull(visitLeftPadded(isd.getPadding().getTypeExpression(), JsLeftPadded.Location.INDEXED_SIGNATURE_DECLARATION_TYPE_EXPRESSION, p))); isd = isd.withType(visitType(isd.getType(), p)); return isd; } @@ -839,9 +895,9 @@ public J visitJSForOfLoop(JS.JSForOfLoop jsForOfLoop, P p) { } else { f = (JS.JSForOfLoop) temp; } - f = f.getPadding().withAwait(visitLeftPadded(f.getPadding().getAwait(), JsLeftPadded.Location.FOR_OF_AWAIT, p)); - f = f.withControl(visitAndCast(f.getControl(), p)); - f = f.getPadding().withBody(visitRightPadded(f.getPadding().getBody(), JsRightPadded.Location.FOR_BODY, p)); + f = f.getPadding().withAwait(Objects.requireNonNull(visitLeftPadded(f.getPadding().getAwait(), JsLeftPadded.Location.FOR_OF_AWAIT, p))); + f = f.withControl(Objects.requireNonNull(visitAndCast(f.getControl(), p))); + f = f.getPadding().withBody(Objects.requireNonNull(visitRightPadded(f.getPadding().getBody(), JsRightPadded.Location.FOR_BODY, p))); return f; } @@ -850,13 +906,13 @@ public J visitJSForInLoop(JS.JSForInLoop jsForInLoop, P p) { f = f.withPrefix(visitSpace(f.getPrefix(), JsSpace.Location.FOR_IN_LOOP_PREFIX, p)); f = f.withMarkers(visitMarkers(f.getMarkers(), p)); Statement temp = (Statement) visitStatement(f, p); - if (!(temp instanceof JS.JSForOfLoop)) { + if (!(temp instanceof JS.JSForInLoop)) { return temp; } else { f = (JS.JSForInLoop) temp; } - f = f.withControl(visitAndCast(f.getControl(), p)); - f = f.getPadding().withBody(visitRightPadded(f.getPadding().getBody(), JsRightPadded.Location.FOR_BODY, p)); + f = f.withControl(Objects.requireNonNull(visitAndCast(f.getControl(), p))); + f = f.getPadding().withBody(Objects.requireNonNull(visitRightPadded(f.getPadding().getBody(), JsRightPadded.Location.FOR_BODY, p))); return f; } @@ -864,8 +920,8 @@ public J visitJSForInOfLoopControl(JS.JSForInOfLoopControl jsForInOfLoopControl, JS.JSForInOfLoopControl c = jsForInOfLoopControl; c = c.withPrefix(visitSpace(c.getPrefix(), JsSpace.Location.FOR_LOOP_CONTROL_PREFIX, p)); c = c.withMarkers(visitMarkers(c.getMarkers(), p)); - c = c.getPadding().withVariable(visitRightPadded(c.getPadding().getVariable(), JsRightPadded.Location.FOR_CONTROL_VAR, p)); - c = c.getPadding().withIterable(visitRightPadded(c.getPadding().getIterable(), JsRightPadded.Location.FOR_CONTROL_ITER, p)); + c = c.getPadding().withVariable(Objects.requireNonNull(visitRightPadded(c.getPadding().getVariable(), JsRightPadded.Location.FOR_CONTROL_VAR, p))); + c = c.getPadding().withIterable(Objects.requireNonNull(visitRightPadded(c.getPadding().getIterable(), JsRightPadded.Location.FOR_CONTROL_ITER, p))); return c; } @@ -879,7 +935,7 @@ public J visitArrayBindingPattern(JS.ArrayBindingPattern arrayBindingPattern, P } else { c = (JS.ArrayBindingPattern) temp; } - c = c.getPadding().withElements(visitContainer(c.getPadding().getElements(), JsContainer.Location.ARRAY_BINDING_PATTERN_ELEMENTS, p)); + c = c.getPadding().withElements(Objects.requireNonNull(visitContainer(c.getPadding().getElements(), JsContainer.Location.ARRAY_BINDING_PATTERN_ELEMENTS, p))); return c; } } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 27d421a8..2a4eaec2 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 @@ -594,7 +594,7 @@ public J visitJSMethodDeclaration(JS.JSMethodDeclaration method, PrintOutputCapt beforeSyntax(method, JsSpace.Location.JSMETHOD_DECLARATION_PREFIX, p); visitSpace(Space.EMPTY, Space.Location.ANNOTATIONS, p); visit(method.getLeadingAnnotations(), p); - method.getModifiers().forEach(it -> visitModifier(it, p)); + method.getModifiers().forEach(it -> delegate.visitModifier(it, p)); Asterisk asterisk = method.getMarkers().findFirst(Asterisk.class).orElse(null); if (asterisk != null) { @@ -627,9 +627,10 @@ public J visitJSMethodDeclaration(JS.JSMethodDeclaration method, PrintOutputCapt public J visitFunctionDeclaration(JS.FunctionDeclaration functionDeclaration, PrintOutputCapture

p) { beforeSyntax(functionDeclaration, JsSpace.Location.FUNCTION_DECLARATION_PREFIX, p); functionDeclaration.getModifiers().forEach(m -> delegate.visitModifier(m, p)); - p.append("function"); - visit(functionDeclaration.getName(), p); + visitLeftPaddedBoolean("function", functionDeclaration.getPadding().getAsteriskToken(), JsLeftPadded.Location.FUNCTION_DECLARATION_ASTERISK_TOKEN, p); + + visitLeftPadded(functionDeclaration.hasAsteriskToken() ? "*" : "", functionDeclaration.getPadding().getName(), JsLeftPadded.Location.FUNCTION_DECLARATION_NAME, p); J.TypeParameters typeParameters = functionDeclaration.getTypeParameters(); if (typeParameters != null) { @@ -722,6 +723,54 @@ public J visitArrayBindingPattern(JS.ArrayBindingPattern abp, PrintOutputCapture return abp; } + @Override + public J visitExportDeclaration(JS.ExportDeclaration ed, PrintOutputCapture

p) { + beforeSyntax(ed, JsSpace.Location.EXPORT_DECLARATION_PREFIX, p); + p.append("export"); + ed.getModifiers().forEach(it -> delegate.visitModifier(it, p)); + if (ed.isTypeOnly()) { + visitLeftPaddedBoolean("type", ed.getPadding().getTypeOnly(), JsLeftPadded.Location.EXPORT_DECLARATION_TYPE_ONLY, p); + } + visit(ed.getExportClause(), p); + visitLeftPadded("from", ed.getPadding().getModuleSpecifier(), JsLeftPadded.Location.EXPORT_DECLARATION_MODULE_SPECIFIER, p); + afterSyntax(ed, p); + return ed; + } + + @Override + public J visitExportAssignment(JS.ExportAssignment es, PrintOutputCapture

p) { + beforeSyntax(es, JsSpace.Location.EXPORT_ASSIGNMENT_PREFIX, p); + p.append("export"); + es.getModifiers().forEach(it -> delegate.visitModifier(it, p)); + if (es.isExportEquals()) { + visitLeftPaddedBoolean("=", es.getPadding().getExportEquals(), JsLeftPadded.Location.EXPORT_ASSIGNMENT_EXPORT_EQUALS, p); + } + visit(es.getExpression(), p); + afterSyntax(es, p); + return es; + } + + @Override + public J visitNamedExports(JS.NamedExports ne, PrintOutputCapture

p) { + beforeSyntax(ne, JsSpace.Location.NAMED_EXPORTS_PREFIX, p); + visitContainer("{", ne.getPadding().getElements(), JsContainer.Location.NAMED_EXPORTS_ELEMENTS, ",", "}", p); + afterSyntax(ne, p); + return ne; + } + + @Override + public J visitExportSpecifier(JS.ExportSpecifier es, PrintOutputCapture

p) { + beforeSyntax(es, JsSpace.Location.EXPORT_SPECIFIER_PREFIX, p); + if (es.isTypeOnly()) { + visitLeftPaddedBoolean("type", es.getPadding().getTypeOnly(), JsLeftPadded.Location.EXPORT_SPECIFIER_TYPE_ONLY, p); + } + + visit(es.getSpecifier(), p); + + afterSyntax(es, p); + return es; + } + private class JavaScriptJavaPrinter extends JavaPrinter

{ @Override @@ -887,7 +936,7 @@ public J visitMethodDeclaration(J.MethodDeclaration method, PrintOutputCapture

visitModifier(it, p)); + method.getModifiers().forEach(it -> delegate.visitModifier(it, p)); Asterisk asterisk = method.getMarkers().findFirst(Asterisk.class).orElse(null); if (asterisk != null) { @@ -1083,7 +1132,7 @@ public J visitTypeParameter(J.TypeParameter typeParameter, PrintOutputCapture

public J visitVariableDeclarations(J.VariableDeclarations multiVariable, PrintOutputCapture

p) { beforeSyntax(multiVariable, Space.Location.VARIABLE_DECLARATIONS_PREFIX, p); visit(multiVariable.getLeadingAnnotations(), p); - multiVariable.getModifiers().forEach(it -> visitModifier(it, p)); + multiVariable.getModifiers().forEach(it -> delegate.visitModifier(it, p)); List> variables = multiVariable.getPadding().getVariables(); for (int i = 0; i < variables.size(); i++) { 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 f14f65a3..f4ac1526 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 @@ -328,7 +328,7 @@ final class Alias implements JS, Expression { JRightPadded propertyName; @With - J.Identifier alias; + Expression alias; public J.Identifier getPropertyName() { return propertyName.getElement(); @@ -3369,9 +3369,25 @@ final class FunctionDeclaration implements JS, Statement, Expression, TypedTree @With List modifiers; - @Getter - @With - J.@Nullable Identifier name; + JLeftPadded asteriskToken; + + public boolean hasAsteriskToken() { + return asteriskToken.getElement(); + } + + public FunctionDeclaration withAsteriskToken(boolean hasAsteriskToken) { + return getPadding().withAsteriskToken(JLeftPadded.withElement(this.asteriskToken, hasAsteriskToken)); + } + + JLeftPadded name; + + public J.Identifier getName() { + return name.getElement(); + } + + public FunctionDeclaration withName(J.Identifier name) { + return getPadding().withName(JLeftPadded.withElement(this.name, name)); + } @Getter @With @@ -3436,7 +3452,23 @@ public JContainer getParameters() { } public FunctionDeclaration withParameters(JContainer parameters) { - return t.parameters == parameters ? t : new FunctionDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.name, t.typeParameters, parameters, t.returnTypeExpression, t.body, t.type); + return t.parameters == parameters ? t : new FunctionDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.asteriskToken, t.name, t.typeParameters, parameters, t.returnTypeExpression, t.body, t.type); + } + + public JLeftPadded getAsteriskToken() { + return t.asteriskToken; + } + + public FunctionDeclaration withAsteriskToken(JLeftPadded asteriskToken) { + return t.asteriskToken == asteriskToken ? t : new FunctionDeclaration(t.id, t.prefix, t.markers, t.modifiers, asteriskToken, t.name, t.typeParameters, t.parameters, t.returnTypeExpression, t.body, t.type); + } + + public JLeftPadded getName() { + return t.name; + } + + public FunctionDeclaration withName(JLeftPadded name) { + return t.name == name ? t : new FunctionDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.asteriskToken, name, t.typeParameters, t.parameters, t.returnTypeExpression, t.body, t.type); } } } @@ -3767,4 +3799,345 @@ public BindingElement withInitializer(@Nullable JLeftPadded initiali } } } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ExportDeclaration implements JS, Statement { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + List modifiers; + + JLeftPadded typeOnly; + + public boolean isTypeOnly() { + return typeOnly.getElement(); + } + + public ExportDeclaration withTypeOnly(boolean importType) { + return getPadding().withTypeOnly(JLeftPadded.withElement(this.typeOnly, importType)); + } + + @With + @Getter + @Nullable + Expression exportClause; + + + @Nullable + JLeftPadded moduleSpecifier; + + @Nullable + public Expression getModuleSpecifier() { + return moduleSpecifier != null ? moduleSpecifier.getElement() : null; + } + + public ExportDeclaration withModuleSpecifier(@Nullable Expression moduleSpecifier) { + return getPadding().withModuleSpecifier(JLeftPadded.withElement(this.moduleSpecifier, moduleSpecifier)); + } + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitExportDeclaration(this, p); + } + + @Override + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ExportDeclaration t; + + public JLeftPadded getTypeOnly() { + return t.typeOnly; + } + + public ExportDeclaration withTypeOnly(JLeftPadded typeOnly) { + return t.typeOnly == typeOnly ? t : new ExportDeclaration(t.id, t.prefix, t.markers, t.modifiers, typeOnly, t.exportClause, t.moduleSpecifier); + } + + @Nullable + public JLeftPadded getModuleSpecifier() { + return t.moduleSpecifier; + } + + public ExportDeclaration withModuleSpecifier(@Nullable JLeftPadded moduleSpecifier) { + return t.moduleSpecifier == moduleSpecifier ? t : new ExportDeclaration(t.id, t.prefix, t.markers, t.modifiers, t.typeOnly, t.exportClause, moduleSpecifier); + } + } + + } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ExportAssignment implements JS, Statement { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + @With + @Getter + List modifiers; + + JLeftPadded exportEquals; + + public boolean isExportEquals() { + return exportEquals.getElement(); + } + + public ExportAssignment withExportEquals(boolean isExportEquals) { + return getPadding().withExportEquals(JLeftPadded.withElement(this.exportEquals, isExportEquals)); + } + + @With + @Getter + @Nullable + Expression expression; + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitExportAssignment(this, p); + } + + @Override + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); + } + + public Padding getPadding() { + Padding p; + if (this.padding == null) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ExportAssignment t; + + public JLeftPadded getExportEquals() { + return t.exportEquals; + } + + public ExportAssignment withExportEquals(JLeftPadded exportEquals) { + return t.exportEquals == exportEquals ? t : new ExportAssignment(t.id, t.prefix, t.markers, t.modifiers, exportEquals, t.expression); + } + } + + } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class NamedExports implements JS, Expression { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JContainer elements; + + public List getElements() { + return elements.getElements(); + } + + public NamedExports withElements(List elements) { + return getPadding().withElements(JContainer.withElements(this.elements, elements)); + } + + @Nullable + @With + @Getter + JavaType type; + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitNamedExports(this, p); + } + + @Override + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public NamedExports.Padding getPadding() { + NamedExports.Padding p; + if (this.padding == null) { + p = new NamedExports.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new NamedExports.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final NamedExports t; + + public JContainer getElements() { + return t.elements; + } + + public NamedExports withElements(JContainer elements) { + return t.elements == elements ? t : new NamedExports(t.id, t.prefix, t.markers, elements, t.type); + } + } + } + + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + final class ExportSpecifier implements JS, Expression, TypedTree { + @Nullable + @NonFinal + transient WeakReference padding; + + @With + @EqualsAndHashCode.Include + @Getter + UUID id; + + @With + @Getter + Space prefix; + + @With + @Getter + Markers markers; + + JLeftPadded typeOnly; + + public boolean isTypeOnly() { + return typeOnly.getElement(); + } + + public ExportSpecifier withTypeOnly(boolean isTypeOnly) { + return getPadding().withTypeOnly(JLeftPadded.withElement(this.typeOnly, isTypeOnly)); + } + + @With + @Getter + Expression specifier; + + @With + @Nullable + @Getter + JavaType type; + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitExportSpecifier(this, p); + } + + @Override + public CoordinateBuilder.Expression getCoordinates() { + return new CoordinateBuilder.Expression(this); + } + + public ExportSpecifier.Padding getPadding() { + ExportSpecifier.Padding p; + if (this.padding == null) { + p = new ExportSpecifier.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new ExportSpecifier.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ExportSpecifier t; + + public JLeftPadded getTypeOnly() { + return t.typeOnly; + } + + public ExportSpecifier withTypeOnly(JLeftPadded typeOnly) { + return t.typeOnly == typeOnly ? t : new ExportSpecifier(t.id, t.prefix, t.markers, t.typeOnly, t.specifier, t.type); + } + } + + } } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java index d21d3da2..a92d3812 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsContainer.java @@ -32,7 +32,9 @@ public enum Location { ARRAY_BINDING_PATTERN_ELEMENTS(JsSpace.Location.ARRAY_BINDING_PATTERN_ELEMENTS_PREFIX, JsRightPadded.Location.ARRAY_BINDING_PATTERN_ELEMENTS), EXPR_WITH_TYPE_ARG_PARAMETERS(JsSpace.Location.EXPR_WITH_TYPE_ARG_PARAMETERS, JsRightPadded.Location.EXPR_WITH_TYPE_ARG_PARAMETERS_SUFFIX), TEMPLATE_EXPRESSION_TYPE_ARG_PARAMETERS(JsSpace.Location.TEMPLATE_EXPRESSION_TYPE_ARG_PARAMETERS, JsRightPadded.Location.TEMPLATE_EXPRESSION_TYPE_ARG_PARAMETERS_SUFFIX), - CONDITIONAL_TYPE_CONDITION(JsSpace.Location.CONDITIONAL_TYPE_CONDITION, JsRightPadded.Location.CONDITIONAL_TYPE_CONDITION); + CONDITIONAL_TYPE_CONDITION(JsSpace.Location.CONDITIONAL_TYPE_CONDITION, JsRightPadded.Location.CONDITIONAL_TYPE_CONDITION), + NAMED_EXPORTS_ELEMENTS(JsSpace.Location.NAMED_EXPORTS_ELEMENTS_PREFIX, JsRightPadded.Location.NAMED_EXPORTS_ELEMENTS), + ; private final JsSpace.Location beforeLocation; private final JsRightPadded.Location elementLocation; 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 666bd6b9..c07dd37e 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 @@ -34,6 +34,12 @@ public enum Location { JS_IMPORT_SPECIFIER_IMPORT_TYPE(JsSpace.Location.JS_IMPORT_SPECIFIER_IMPORT_TYPE_PREFIX), INDEXED_SIGNATURE_DECLARATION_TYPE_EXPRESSION(JsSpace.Location.INDEXED_SIGNATURE_DECLARATION_TYPE_EXPRESSION_PREFIX), FOR_OF_AWAIT(JsSpace.Location.FOR_OF_AWAIT_PREFIX), + EXPORT_DECLARATION_TYPE_ONLY(JsSpace.Location.EXPORT_DECLARATION_TYPE_ONLY_PREFIX), + EXPORT_DECLARATION_MODULE_SPECIFIER(JsSpace.Location.EXPORT_DECLARATION_MODULE_SPECIFIER_PREFIX), + EXPORT_SPECIFIER_TYPE_ONLY(JsSpace.Location.EXPORT_SPECIFIER_TYPE_ONLY_PREFIX), + EXPORT_ASSIGNMENT_EXPORT_EQUALS(JsSpace.Location.EXPORT_ASSIGNMENT_EXPORT_EQUALS_PREFIX), + FUNCTION_DECLARATION_NAME(JsSpace.Location.FUNCTION_DECLARATION_NAME_PREFIX), + FUNCTION_DECLARATION_ASTERISK_TOKEN(JsSpace.Location.FUNCTION_DECLARATION_ASTERISK_TOKEN_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 d8c1a117..e0dea622 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 @@ -48,7 +48,9 @@ public enum Location { TEMPLATE_EXPRESSION_TAG(JsSpace.Location.TEMPLATE_EXPRESSION_TAG_SUFFIX), TEMPLATE_EXPRESSION_TYPE_ARG_PARAMETERS_SUFFIX(JsSpace.Location.TEMPLATE_EXPRESSION_TYPE_ARG_PARAMETERS_SUFFIX), TEMPLATE_EXPRESSION_TEMPLATE_SPAN(JsSpace.Location.TEMPLATE_EXPRESSION_TEMPLATE_SPAN_SUFFIX), - CONDITIONAL_TYPE_CONDITION(JsSpace.Location.CONDITIONAL_TYPE_CONDITION_SUFFIX); + CONDITIONAL_TYPE_CONDITION(JsSpace.Location.CONDITIONAL_TYPE_CONDITION_SUFFIX), + NAMED_EXPORTS_ELEMENTS(JsSpace.Location.NAMED_EXPORTS_ELEMENTS_SUFFIX), + ; private final JsSpace.Location afterLocation; diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java index 858d10fb..e472d935 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 @@ -119,6 +119,17 @@ public enum Location { CONDITIONAL_TYPE_PREFIX, CONDITIONAL_TYPE_CONDITION, CONDITIONAL_TYPE_CONDITION_SUFFIX, - + EXPORT_DECLARATION_PREFIX, + EXPORT_DECLARATION_TYPE_ONLY_PREFIX, + EXPORT_DECLARATION_MODULE_SPECIFIER_PREFIX, + EXPORT_SPECIFIER_PREFIX, + EXPORT_SPECIFIER_TYPE_ONLY_PREFIX, + NAMED_EXPORTS_PREFIX, + NAMED_EXPORTS_ELEMENTS_PREFIX, + NAMED_EXPORTS_ELEMENTS_SUFFIX, + EXPORT_ASSIGNMENT_PREFIX, + EXPORT_ASSIGNMENT_EXPORT_EQUALS_PREFIX, + FUNCTION_DECLARATION_NAME_PREFIX, + FUNCTION_DECLARATION_ASTERISK_TOKEN_PREFIX, } }