From 6afb022d58ce858b2befdb14bf7522caebfed918 Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Wed, 11 Dec 2024 15:08:46 +0100 Subject: [PATCH 1/3] Refactored arrow function and fix errors --- openrewrite/src/javascript/parser.ts | 27 +++- openrewrite/src/javascript/remote/receiver.ts | 25 +++- openrewrite/src/javascript/remote/sender.ts | 13 +- .../src/javascript/tree/support_types.ts | 3 + openrewrite/src/javascript/tree/tree.ts | 117 +++++++++++++---- openrewrite/src/javascript/visitor.ts | 18 ++- .../parser/expressionStatement.test.ts | 12 ++ .../test/javascript/parser/function.test.ts | 12 ++ openrewrite/test/javascript/parser/if.test.ts | 12 ++ .../javascript/remote/JavaScriptReceiver.java | 25 +++- .../javascript/remote/JavaScriptSender.java | 12 +- .../remote/JavaScriptValidator.java | 8 +- .../javascript/JavaScriptVisitor.java | 16 ++- .../internal/JavaScriptPrinter.java | 40 ++++-- .../org/openrewrite/javascript/tree/JS.java | 119 +++++++++++++++++- .../javascript/tree/JsLeftPadded.java | 3 +- .../javascript/tree/JsRightPadded.java | 3 +- .../openrewrite/javascript/tree/JsSpace.java | 3 + 18 files changed, 405 insertions(+), 63 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 44447c30..3fcd3586 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1913,8 +1913,7 @@ export class JavaScriptParserVisitor { isParenthesized ? [this.rightPadded(this.newJEmpty(), this.prefix(this.findChildNode(node, ts.SyntaxKind.CloseParenToken)!))] : [] // to handle the case: (/*no*/) => ... ), this.mapTypeInfo(node), - this.prefix(node.equalsGreaterThanToken), - this.convert(node.body), + this.leftPadded(this.prefix(node.equalsGreaterThanToken), this.convert(node.body)), this.mapType(node) ); } @@ -2368,6 +2367,7 @@ export class JavaScriptParserVisitor { } visitSyntheticExpression(node: ts.SyntheticExpression) { + // SyntheticExpression is a special type of node used internally by the TypeScript compiler return this.visitUnknown(node); } @@ -2431,6 +2431,7 @@ export class JavaScriptParserVisitor { visitIfStatement(node: ts.IfStatement) { const semicolonAfterThen = node.thenStatement.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken; + const semicolonAfterElse = node.elseStatement?.getLastToken()?.kind == ts.SyntaxKind.SemicolonToken; return new J.If( randomId(), this.prefix(node), @@ -2452,8 +2453,8 @@ export class JavaScriptParserVisitor { Markers.EMPTY, this.rightPadded( this.convert(node.elseStatement), - semicolonAfterThen ? this.prefix(node.elseStatement.getLastToken()!) : Space.EMPTY, - semicolonAfterThen ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY + semicolonAfterElse ? this.prefix(node.elseStatement.getLastToken()!) : Space.EMPTY, + semicolonAfterElse ? Markers.build([new Semicolon(randomId())]) : Markers.EMPTY ) ) : null ); @@ -2641,7 +2642,23 @@ export class JavaScriptParserVisitor { } visitDebuggerStatement(node: ts.DebuggerStatement) { - return this.visitUnknown(node); + return new JS.DebuggerStatement( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.rightPadded( + new J.Literal( + randomId(), + this.prefix(node), + Markers.EMPTY, + null, + "debugger", + null, + this.mapPrimitiveType(node) + ), + this.suffix(this.findChildNode(node, ts.SyntaxKind.DebuggerKeyword)!), + ) + ); } visitVariableDeclaration(node: ts.VariableDeclaration) { diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index febc686e..17b9d685 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, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DebuggerStatement, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java"; import * as Java from "../../java/tree"; @@ -60,8 +60,7 @@ class Visitor extends JavaScriptVisitor { arrowFunction = arrowFunction.withTypeParameters(ctx.receiveNode(arrowFunction.typeParameters, ctx.receiveTree)); arrowFunction = arrowFunction.withParameters(ctx.receiveNode(arrowFunction.parameters, ctx.receiveTree)!); arrowFunction = arrowFunction.withReturnTypeExpression(ctx.receiveNode(arrowFunction.returnTypeExpression, ctx.receiveTree)); - arrowFunction = arrowFunction.withArrow(ctx.receiveNode(arrowFunction.arrow, receiveSpace)!); - arrowFunction = arrowFunction.withBody(ctx.receiveNode(arrowFunction.body, ctx.receiveTree)!); + arrowFunction = arrowFunction.padding.withBody(ctx.receiveNode(arrowFunction.padding.body, receiveLeftPaddedTree)!); arrowFunction = arrowFunction.withType(ctx.receiveValue(arrowFunction.type, ValueType.Object)); return arrowFunction; } @@ -85,6 +84,14 @@ class Visitor extends JavaScriptVisitor { return conditionalType; } + public visitDebuggerStatement(debuggerStatement: DebuggerStatement, ctx: ReceiverContext): J { + debuggerStatement = debuggerStatement.withId(ctx.receiveValue(debuggerStatement.id, ValueType.UUID)!); + debuggerStatement = debuggerStatement.withPrefix(ctx.receiveNode(debuggerStatement.prefix, receiveSpace)!); + debuggerStatement = debuggerStatement.withMarkers(ctx.receiveNode(debuggerStatement.markers, ctx.receiveMarkers)!); + debuggerStatement = debuggerStatement.padding.withDebugger(ctx.receiveNode(debuggerStatement.padding.debugger, receiveRightPaddedTree)!); + return debuggerStatement; + } + public visitDefaultType(defaultType: DefaultType, ctx: ReceiverContext): J { defaultType = defaultType.withId(ctx.receiveValue(defaultType.id, ValueType.UUID)!); defaultType = defaultType.withPrefix(ctx.receiveNode(defaultType.prefix, receiveSpace)!); @@ -1349,8 +1356,7 @@ class Factory implements ReceiverFactory { ctx.receiveNode(null, ctx.receiveTree), ctx.receiveNode(null, ctx.receiveTree)!, ctx.receiveNode(null, ctx.receiveTree), - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveNode(null, ctx.receiveTree)!, + ctx.receiveNode>(null, receiveLeftPaddedTree)!, ctx.receiveValue(null, ValueType.Object) ); } @@ -1376,6 +1382,15 @@ class Factory implements ReceiverFactory { ); } + if (type === "org.openrewrite.javascript.tree.JS$DebuggerStatement") { + return new DebuggerStatement( + ctx.receiveValue(null, ValueType.UUID)!, + ctx.receiveNode(null, receiveSpace)!, + ctx.receiveNode(null, ctx.receiveMarkers)!, + ctx.receiveNode>(null, receiveRightPaddedTree)! + ); + } + if (type === "org.openrewrite.javascript.tree.JS$DefaultType") { return new DefaultType( ctx.receiveValue(null, ValueType.UUID)!, diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index c01149ba..5ef88934 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, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DebuggerStatement, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java"; import * as Java from "../../java/tree"; @@ -55,8 +55,7 @@ class Visitor extends JavaScriptVisitor { ctx.sendNode(arrowFunction, v => v.typeParameters, ctx.sendTree); ctx.sendNode(arrowFunction, v => v.parameters, ctx.sendTree); ctx.sendNode(arrowFunction, v => v.returnTypeExpression, ctx.sendTree); - ctx.sendNode(arrowFunction, v => v.arrow, Visitor.sendSpace); - ctx.sendNode(arrowFunction, v => v.body, ctx.sendTree); + ctx.sendNode(arrowFunction, v => v.padding.body, Visitor.sendLeftPadded(ValueType.Tree)); ctx.sendTypedValue(arrowFunction, v => v.type, ValueType.Object); return arrowFunction; } @@ -80,6 +79,14 @@ class Visitor extends JavaScriptVisitor { return conditionalType; } + public visitDebuggerStatement(debuggerStatement: DebuggerStatement, ctx: SenderContext): J { + ctx.sendValue(debuggerStatement, v => v.id, ValueType.UUID); + ctx.sendNode(debuggerStatement, v => v.prefix, Visitor.sendSpace); + ctx.sendNode(debuggerStatement, v => v.markers, ctx.sendMarkers); + ctx.sendNode(debuggerStatement, v => v.padding.debugger, Visitor.sendRightPadded(ValueType.Tree)); + return debuggerStatement; + } + public visitDefaultType(defaultType: DefaultType, ctx: SenderContext): J { ctx.sendValue(defaultType, v => v.id, ValueType.UUID); ctx.sendNode(defaultType, v => v.prefix, Visitor.sendSpace); diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index ab52c316..d9c4e512 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -260,6 +260,7 @@ export namespace JsSpace { MAPPED_TYPE_KEYS_REMAPPING_PREFIX, MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX, TYPE_TREE_EXPRESSION_PREFIX, + DEBUGGER_STATEMENT_PREFIX, } } export namespace JsLeftPadded { @@ -302,6 +303,7 @@ export namespace JsLeftPadded { MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE_TYPE, MAPPED_TYPE_HAS_READONLY, MAPPED_TYPE_HAS_QUESTION_TOKEN, + ARROW_FUNCTION_BODY, } } export namespace JsRightPadded { @@ -330,6 +332,7 @@ export namespace JsRightPadded { INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT, MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER, MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE, + DEBUGGER_STATEMENT_DEBUGGER, } } export namespace JsContainer { diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 4d919042..965c5dc5 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -251,7 +251,7 @@ export class Alias extends JSMixin(Object) implements Expression { @LstType("org.openrewrite.javascript.tree.JS$ArrowFunction") export class ArrowFunction extends JSMixin(Object) implements Statement, Expression, TypedTree { - public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeParameters: Java.TypeParameters | null, parameters: Java.Lambda.Parameters, returnTypeExpression: TypeTree | null, arrow: Space, body: J, _type: JavaType | null) { + public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeParameters: Java.TypeParameters | null, parameters: Java.Lambda.Parameters, returnTypeExpression: TypeTree | null, body: JLeftPadded, _type: JavaType | null) { super(); this._id = id; this._prefix = prefix; @@ -261,7 +261,6 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express this._typeParameters = typeParameters; this._parameters = parameters; this._returnTypeExpression = returnTypeExpression; - this._arrow = arrow; this._body = body; this._type = _type; } @@ -273,7 +272,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withId(id: UUID): ArrowFunction { - return id === this._id ? this : new ArrowFunction(id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return id === this._id ? this : new ArrowFunction(id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _prefix: Space; @@ -283,7 +282,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withPrefix(prefix: Space): ArrowFunction { - return prefix === this._prefix ? this : new ArrowFunction(this._id, prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return prefix === this._prefix ? this : new ArrowFunction(this._id, prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _markers: Markers; @@ -293,7 +292,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withMarkers(markers: Markers): ArrowFunction { - return markers === this._markers ? this : new ArrowFunction(this._id, this._prefix, markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return markers === this._markers ? this : new ArrowFunction(this._id, this._prefix, markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _leadingAnnotations: Java.Annotation[]; @@ -303,7 +302,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withLeadingAnnotations(leadingAnnotations: Java.Annotation[]): ArrowFunction { - return leadingAnnotations === this._leadingAnnotations ? this : new ArrowFunction(this._id, this._prefix, this._markers, leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return leadingAnnotations === this._leadingAnnotations ? this : new ArrowFunction(this._id, this._prefix, this._markers, leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _modifiers: Java.Modifier[]; @@ -313,7 +312,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withModifiers(modifiers: Java.Modifier[]): ArrowFunction { - return modifiers === this._modifiers ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return modifiers === this._modifiers ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _typeParameters: Java.TypeParameters | null; @@ -323,7 +322,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withTypeParameters(typeParameters: Java.TypeParameters | null): ArrowFunction { - return typeParameters === this._typeParameters ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return typeParameters === this._typeParameters ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, typeParameters, this._parameters, this._returnTypeExpression, this._body, this._type); } private readonly _parameters: Java.Lambda.Parameters; @@ -333,7 +332,7 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withParameters(parameters: Java.Lambda.Parameters): ArrowFunction { - return parameters === this._parameters ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, parameters, this._returnTypeExpression, this._arrow, this._body, this._type); + return parameters === this._parameters ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, parameters, this._returnTypeExpression, this._body, this._type); } private readonly _returnTypeExpression: TypeTree | null; @@ -343,27 +342,17 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withReturnTypeExpression(returnTypeExpression: TypeTree | null): ArrowFunction { - return returnTypeExpression === this._returnTypeExpression ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, returnTypeExpression, this._arrow, this._body, this._type); - } - - private readonly _arrow: Space; - - public get arrow(): Space { - return this._arrow; - } - - public withArrow(arrow: Space): ArrowFunction { - return arrow === this._arrow ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, arrow, this._body, this._type); + return returnTypeExpression === this._returnTypeExpression ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, returnTypeExpression, this._body, this._type); } - private readonly _body: J; + private readonly _body: JLeftPadded; public get body(): J { - return this._body; + return this._body.element; } public withBody(body: J): ArrowFunction { - return body === this._body ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, body, this._type); + return this.padding.withBody(this._body.withElement(body)); } private readonly _type: JavaType | null; @@ -373,13 +362,25 @@ export class ArrowFunction extends JSMixin(Object) implements Statement, Express } public withType(_type: JavaType | null): ArrowFunction { - return _type === this._type ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._arrow, this._body, _type); + return _type === this._type ? this : new ArrowFunction(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, this._typeParameters, this._parameters, this._returnTypeExpression, this._body, _type); } public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { return v.visitArrowFunction(this, p); } + get padding() { + const t = this; + return new class { + public get body(): JLeftPadded { + return t._body; + } + public withBody(body: JLeftPadded): ArrowFunction { + return t._body === body ? t : new ArrowFunction(t._id, t._prefix, t._markers, t._leadingAnnotations, t._modifiers, t._typeParameters, t._parameters, t._returnTypeExpression, body, t._type); + } + } + } + } @LstType("org.openrewrite.javascript.tree.JS$Await") @@ -539,6 +540,74 @@ export class ConditionalType extends JSMixin(Object) implements TypeTree, Expres } +@LstType("org.openrewrite.javascript.tree.JS$DebuggerStatement") +export class DebuggerStatement extends JSMixin(Object) implements Statement { + public constructor(id: UUID, prefix: Space, markers: Markers, _debugger: JRightPadded) { + super(); + this._id = id; + this._prefix = prefix; + this._markers = markers; + this._debugger = _debugger; + } + + private readonly _id: UUID; + + public get id(): UUID { + return this._id; + } + + public withId(id: UUID): DebuggerStatement { + return id === this._id ? this : new DebuggerStatement(id, this._prefix, this._markers, this._debugger); + } + + private readonly _prefix: Space; + + public get prefix(): Space { + return this._prefix; + } + + public withPrefix(prefix: Space): DebuggerStatement { + return prefix === this._prefix ? this : new DebuggerStatement(this._id, prefix, this._markers, this._debugger); + } + + private readonly _markers: Markers; + + public get markers(): Markers { + return this._markers; + } + + public withMarkers(markers: Markers): DebuggerStatement { + return markers === this._markers ? this : new DebuggerStatement(this._id, this._prefix, markers, this._debugger); + } + + private readonly _debugger: JRightPadded; + + public get debugger(): Java.Literal { + return this._debugger.element; + } + + public withDebugger(_debugger: Java.Literal): DebuggerStatement { + return this.padding.withDebugger(this._debugger.withElement(_debugger)); + } + + public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { + return v.visitDebuggerStatement(this, p); + } + + get padding() { + const t = this; + return new class { + public get debugger(): JRightPadded { + return t._debugger; + } + public withDebugger(_debugger: JRightPadded): DebuggerStatement { + return t._debugger === _debugger ? t : new DebuggerStatement(t._id, t._prefix, t._markers, _debugger); + } + } + } + +} + @LstType("org.openrewrite.javascript.tree.JS$DefaultType") export class DefaultType extends JSMixin(Object) implements Expression, TypedTree, NameTree { public constructor(id: UUID, prefix: Space, markers: Markers, left: Expression, beforeEquals: Space, right: Expression, _type: JavaType | null) { diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index b967e369..723c1b03 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, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree"; +import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DebuggerStatement, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree"; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree"; import {JavaVisitor} from "../java"; import * as Java from "../java/tree"; @@ -54,8 +54,7 @@ export class JavaScriptVisitor

extends JavaVisitor

{ arrowFunction = arrowFunction.withTypeParameters(this.visitAndCast(arrowFunction.typeParameters, p)); arrowFunction = arrowFunction.withParameters(this.visitAndCast(arrowFunction.parameters, p)!); arrowFunction = arrowFunction.withReturnTypeExpression(this.visitAndCast(arrowFunction.returnTypeExpression, p)); - arrowFunction = arrowFunction.withArrow(this.visitJsSpace(arrowFunction.arrow, JsSpace.Location.ARROW_FUNCTION_ARROW, p)!); - arrowFunction = arrowFunction.withBody(this.visitAndCast(arrowFunction.body, p)!); + arrowFunction = arrowFunction.padding.withBody(this.visitJsLeftPadded(arrowFunction.padding.body, JsLeftPadded.Location.ARROW_FUNCTION_BODY, p)!); return arrowFunction; } @@ -86,6 +85,19 @@ export class JavaScriptVisitor

extends JavaVisitor

{ return conditionalType; } + public visitDebuggerStatement(debuggerStatement: DebuggerStatement, p: P): J | null { + debuggerStatement = debuggerStatement.withPrefix(this.visitJsSpace(debuggerStatement.prefix, JsSpace.Location.DEBUGGER_STATEMENT_PREFIX, p)!); + let tempStatement = this.visitStatement(debuggerStatement, p) as Statement; + if (!(tempStatement instanceof DebuggerStatement)) + { + return tempStatement; + } + debuggerStatement = tempStatement as DebuggerStatement; + debuggerStatement = debuggerStatement.withMarkers(this.visitMarkers(debuggerStatement.markers, p)); + debuggerStatement = debuggerStatement.padding.withDebugger(this.visitJsRightPadded(debuggerStatement.padding.debugger, JsRightPadded.Location.DEBUGGER_STATEMENT_DEBUGGER, p)!); + return debuggerStatement; + } + public visitDefaultType(defaultType: DefaultType, p: P): J | null { defaultType = defaultType.withPrefix(this.visitJsSpace(defaultType.prefix, JsSpace.Location.DEFAULT_TYPE_PREFIX, p)!); let tempExpression = this.visitExpression(defaultType, p) as Expression; diff --git a/openrewrite/test/javascript/parser/expressionStatement.test.ts b/openrewrite/test/javascript/parser/expressionStatement.test.ts index 0ebfa2a7..95dc8b24 100644 --- a/openrewrite/test/javascript/parser/expressionStatement.test.ts +++ b/openrewrite/test/javascript/parser/expressionStatement.test.ts @@ -190,4 +190,16 @@ describe('expression statement mapping', () => { ); }); + test('debugging statement', () => { + rewriteRun( + //language=typescript + typeScript(` + function calculate(value: number) { + /*a*/debugger/*b*/;/*c*/ // Pauses execution when debugging + return value * 2; + } + `) + ); + }); + }); diff --git a/openrewrite/test/javascript/parser/function.test.ts b/openrewrite/test/javascript/parser/function.test.ts index 6d9c8bb0..f8dde7b8 100644 --- a/openrewrite/test/javascript/parser/function.test.ts +++ b/openrewrite/test/javascript/parser/function.test.ts @@ -421,4 +421,16 @@ describe('function mapping', () => { `) ); }); + + test('no additional comma test', () => { + rewriteRun( + //language=typescript + typeScript(` + new Promise(function() { + let x; + let y; + }) + `) + ); + }); }); diff --git a/openrewrite/test/javascript/parser/if.test.ts b/openrewrite/test/javascript/parser/if.test.ts index db2e6fad..3608aa1a 100644 --- a/openrewrite/test/javascript/parser/if.test.ts +++ b/openrewrite/test/javascript/parser/if.test.ts @@ -22,4 +22,16 @@ describe('if mapping', () => { typeScript('if (true) console.log("foo"); else console.log("bar");') ); }); + + test('if-else with semicolon', () => { + rewriteRun( + //language=typescript + typeScript(` + if (true) { + console.log("foo"); + } else + console.log("bar"); + `) + ); + }); }); 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 a678f859..86735c4d 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 @@ -110,8 +110,7 @@ public JS.ArrowFunction visitArrowFunction(JS.ArrowFunction arrowFunction, Recei arrowFunction = arrowFunction.withTypeParameters(ctx.receiveNode(arrowFunction.getTypeParameters(), ctx::receiveTree)); arrowFunction = arrowFunction.withParameters(ctx.receiveNonNullNode(arrowFunction.getParameters(), ctx::receiveTree)); arrowFunction = arrowFunction.withReturnTypeExpression(ctx.receiveNode(arrowFunction.getReturnTypeExpression(), ctx::receiveTree)); - arrowFunction = arrowFunction.withArrow(ctx.receiveNonNullNode(arrowFunction.getArrow(), JavaScriptReceiver::receiveSpace)); - arrowFunction = arrowFunction.withBody(ctx.receiveNonNullNode(arrowFunction.getBody(), ctx::receiveTree)); + arrowFunction = arrowFunction.getPadding().withBody(ctx.receiveNonNullNode(arrowFunction.getPadding().getBody(), JavaScriptReceiver::receiveLeftPaddedTree)); arrowFunction = arrowFunction.withType(ctx.receiveValue(arrowFunction.getType(), JavaType.class)); return arrowFunction; } @@ -137,6 +136,15 @@ public JS.ConditionalType visitConditionalType(JS.ConditionalType conditionalTyp return conditionalType; } + @Override + public JS.DebuggerStatement visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, ReceiverContext ctx) { + debuggerStatement = debuggerStatement.withId(ctx.receiveNonNullValue(debuggerStatement.getId(), UUID.class)); + debuggerStatement = debuggerStatement.withPrefix(ctx.receiveNonNullNode(debuggerStatement.getPrefix(), JavaScriptReceiver::receiveSpace)); + debuggerStatement = debuggerStatement.withMarkers(ctx.receiveNonNullNode(debuggerStatement.getMarkers(), ctx::receiveMarkers)); + debuggerStatement = debuggerStatement.getPadding().withDebugger(ctx.receiveNonNullNode(debuggerStatement.getPadding().getDebugger(), JavaScriptReceiver::receiveRightPaddedTree)); + return debuggerStatement; + } + @Override public JS.DefaultType visitDefaultType(JS.DefaultType defaultType, ReceiverContext ctx) { defaultType = defaultType.withId(ctx.receiveNonNullValue(defaultType.getId(), UUID.class)); @@ -1455,6 +1463,7 @@ protected Function computeValue(Class type) { if (type == JS.ArrowFunction.class) return Factory::createJSArrowFunction; if (type == JS.Await.class) return Factory::createJSAwait; if (type == JS.ConditionalType.class) return Factory::createJSConditionalType; + if (type == JS.DebuggerStatement.class) return Factory::createJSDebuggerStatement; if (type == JS.DefaultType.class) return Factory::createJSDefaultType; if (type == JS.Delete.class) return Factory::createJSDelete; if (type == JS.Export.class) return Factory::createJSExport; @@ -1624,8 +1633,7 @@ private static JS.ArrowFunction createJSArrowFunction(ReceiverContext ctx) { ctx.receiveNode(null, ctx::receiveTree), ctx.receiveNonNullNode(null, ctx::receiveTree), ctx.receiveNode(null, ctx::receiveTree), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNonNullNode(null, ctx::receiveTree), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveLeftPaddedTree), ctx.receiveValue(null, JavaType.class) ); } @@ -1651,6 +1659,15 @@ private static JS.ConditionalType createJSConditionalType(ReceiverContext ctx) { ); } + private static JS.DebuggerStatement createJSDebuggerStatement(ReceiverContext ctx) { + return new JS.DebuggerStatement( + ctx.receiveNonNullValue(null, UUID.class), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), + ctx.receiveNonNullNode(null, ctx::receiveMarkers), + ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree) + ); + } + private static JS.DefaultType createJSDefaultType(ReceiverContext ctx) { return new JS.DefaultType( 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 bd69cc8d..39f3e34e 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 @@ -92,8 +92,7 @@ public JS.ArrowFunction visitArrowFunction(JS.ArrowFunction arrowFunction, Sende ctx.sendNode(arrowFunction, JS.ArrowFunction::getTypeParameters, ctx::sendTree); ctx.sendNode(arrowFunction, JS.ArrowFunction::getParameters, ctx::sendTree); ctx.sendNode(arrowFunction, JS.ArrowFunction::getReturnTypeExpression, ctx::sendTree); - ctx.sendNode(arrowFunction, JS.ArrowFunction::getArrow, JavaScriptSender::sendSpace); - ctx.sendNode(arrowFunction, JS.ArrowFunction::getBody, ctx::sendTree); + ctx.sendNode(arrowFunction, e -> e.getPadding().getBody(), JavaScriptSender::sendLeftPadded); ctx.sendTypedValue(arrowFunction, JS.ArrowFunction::getType); return arrowFunction; } @@ -119,6 +118,15 @@ public JS.ConditionalType visitConditionalType(JS.ConditionalType conditionalTyp return conditionalType; } + @Override + public JS.DebuggerStatement visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, SenderContext ctx) { + ctx.sendValue(debuggerStatement, JS.DebuggerStatement::getId); + ctx.sendNode(debuggerStatement, JS.DebuggerStatement::getPrefix, JavaScriptSender::sendSpace); + ctx.sendNode(debuggerStatement, JS.DebuggerStatement::getMarkers, ctx::sendMarkers); + ctx.sendNode(debuggerStatement, e -> e.getPadding().getDebugger(), JavaScriptSender::sendRightPadded); + return debuggerStatement; + } + @Override public JS.DefaultType visitDefaultType(JS.DefaultType defaultType, SenderContext ctx) { ctx.sendValue(defaultType, JS.DefaultType::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 4bab4581..05df81c1 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 @@ -77,7 +77,7 @@ public JS.ArrowFunction visitArrowFunction(JS.ArrowFunction arrowFunction, P p) ListUtils.map(arrowFunction.getLeadingAnnotations(), el -> visitAndValidateNonNull(el, J.Annotation.class, p)); ListUtils.map(arrowFunction.getModifiers(), el -> visitAndValidateNonNull(el, J.Modifier.class, p)); visitAndValidate(arrowFunction.getTypeParameters(), J.TypeParameters.class, p); - visitAndValidateNonNull(arrowFunction.getParameters(), J.Lambda.Parameters.class, p); + visitAndValidate(arrowFunction.getParameters().getParameters(), J.class, p); visitAndValidate(arrowFunction.getReturnTypeExpression(), TypeTree.class, p); visitAndValidateNonNull(arrowFunction.getBody(), J.class, p); return arrowFunction; @@ -96,6 +96,12 @@ public JS.ConditionalType visitConditionalType(JS.ConditionalType conditionalTyp return conditionalType; } + @Override + public JS.DebuggerStatement visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, P p) { + visitAndValidateNonNull(debuggerStatement.getDebugger(), J.Literal.class, p); + return debuggerStatement; + } + @Override public JS.DefaultType visitDefaultType(JS.DefaultType defaultType, P p) { visitAndValidateNonNull(defaultType.getLeft(), Expression.class, p); 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 54950e7d..14732fa4 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -101,7 +101,7 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, P 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.getPadding().withBody(visitLeftPadded(a.getPadding().getBody(), JsLeftPadded.Location.LAMBDA_ARROW, p)); a = a.withBody(Objects.requireNonNull(visitAndCast(a.getBody(), p))); a = a.withType(visitType(a.getType(), p)); return a; @@ -166,6 +166,20 @@ public J visitDefaultType(JS.DefaultType defaultType, P p) { return d; } + public J visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, P p) { + JS.DebuggerStatement d = debuggerStatement; + d = d.withPrefix(visitSpace(d.getPrefix(), JsSpace.Location.DEBUGGER_STATEMENT_PREFIX, p)); + d = d.withMarkers(visitMarkers(d.getMarkers(), p)); + Statement temp = (Statement) visitStatement(d, p); + if (!(temp instanceof JS.DebuggerStatement)) { + return temp; + } else { + d = (JS.DebuggerStatement) temp; + } + d = d.getPadding().withDebugger(Objects.requireNonNull(visitRightPadded(d.getPadding().getDebugger(), JsRightPadded.Location.DEBUGGER, p))); + return d; + } + public J visitDelete(JS.Delete delete, P p) { JS.Delete d = delete; d = d.withPrefix(visitSpace(d.getPrefix(), JsSpace.Location.DELETE_PREFIX, p)); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 7873bd21..8e107458 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 @@ -50,6 +50,16 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { } } + @Override + public void setCursor(@Nullable Cursor cursor) { + super.setCursor(cursor); + delegate.internalSetCursor(cursor); + } + + private void internalSetCursor(@Nullable Cursor cursor) { + super.setCursor(cursor); + } + @Override public J visitCompilationUnit(JS.CompilationUnit cu, PrintOutputCapture

p) { beforeSyntax(cu, Space.Location.COMPILATION_UNIT_PREFIX, p); @@ -100,11 +110,8 @@ public J visitArrowFunction(JS.ArrowFunction arrowFunction, PrintOutputCapture

"); - visit(arrowFunction.getBody(), p); - } + visitLeftPadded("=>", arrowFunction.getPadding().getBody(), JsLeftPadded.Location.LAMBDA_ARROW, p); + afterSyntax(arrowFunction, p); return arrowFunction; } @@ -151,6 +158,14 @@ public J visitDefaultType(JS.DefaultType defaultType, PrintOutputCapture

p) { return defaultType; } + @Override + public J visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, PrintOutputCapture

p) { + beforeSyntax(debuggerStatement, JsSpace.Location.DEBUGGER_STATEMENT_PREFIX, p); + visitRightPadded(debuggerStatement.getPadding().getDebugger(), JsRightPadded.Location.DEBUGGER, p); + afterSyntax(debuggerStatement, p); + return debuggerStatement; + } + @Override public J visitDelete(JS.Delete delete, PrintOutputCapture

p) { beforeSyntax(delete, JsSpace.Location.DELETE_PREFIX, p); @@ -962,6 +977,16 @@ public J visit(@Nullable Tree tree, PrintOutputCapture

p) { } } + @Override + public void setCursor(@Nullable Cursor cursor) { + super.setCursor(cursor); + JavaScriptPrinter.this.internalSetCursor(cursor); + } + + public void internalSetCursor(@Nullable Cursor cursor) { + super.setCursor(cursor); + } + @Override public J visitEnumValue(J.EnumValue enum_, PrintOutputCapture

p) { beforeSyntax(enum_, Space.Location.ENUM_VALUE_PREFIX, p); @@ -1292,10 +1317,7 @@ public J visitTypeParameter(J.TypeParameter typeParameter, PrintOutputCapture

visitSpace(bounds.getBefore(), JContainer.Location.TYPE_BOUNDS.getBeforeLocation(), p); JRightPadded constraintType = bounds.getPadding().getElements().get(0); - if (getCursor().getParentTreeCursor().getValue() instanceof JS.MappedType) { - p.append("in"); - this.visitRightPadded(constraintType, JContainer.Location.TYPE_BOUNDS.getElementLocation(), p); - } else if (!(constraintType.getElement() instanceof J.Empty)) { + if (!(constraintType.getElement() instanceof J.Empty)) { p.append("extends"); this.visitRightPadded(constraintType, JContainer.Location.TYPE_BOUNDS.getElementLocation(), p); } diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java index 63dddf78..35cf931d 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 @@ -394,9 +394,15 @@ public JS.Alias withPropertyName(JRightPadded propertyName) { */ @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) @Data final class ArrowFunction implements JS, Statement, Expression, TypedTree { + @Nullable + @NonFinal + transient WeakReference padding; + @With @EqualsAndHashCode.Include UUID id; @@ -425,11 +431,15 @@ final class ArrowFunction implements JS, Statement, Expression, TypedTree { @Nullable TypeTree returnTypeExpression; - @With - Space arrow; + JLeftPadded body; - @With - J body; + public J getBody() { + return body.getElement(); + } + + public ArrowFunction withBody(J body) { + return getPadding().withBody(JLeftPadded.withElement(this.body, body)); + } @With @Nullable @@ -445,6 +455,35 @@ public

J acceptJavaScript(JavaScriptVisitor

v, P p) { public CoordinateBuilder.Statement getCoordinates() { return new CoordinateBuilder.Statement(this); } + + public ArrowFunction.Padding getPadding() { + ArrowFunction.Padding p; + if (this.padding == null) { + p = new ArrowFunction.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.f != this) { + p = new ArrowFunction.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final ArrowFunction f; + + public JLeftPadded getBody() { + return f.body; + } + + public ArrowFunction withBody(JLeftPadded body) { + return f.body == body ? f: new ArrowFunction(f.id, f.prefix, f.markers, f.leadingAnnotations, f.modifiers, f.typeParameters, f.parameters, f.returnTypeExpression, body, f.type); + } + } + } @Getter @@ -555,6 +594,78 @@ public ConditionalType withCondition(@Nullable JContainer condition) } } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) + @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) + @RequiredArgsConstructor + @AllArgsConstructor(access = AccessLevel.PRIVATE) + class DebuggerStatement implements JS, Statement { + + @Nullable + @NonFinal + transient WeakReference padding; + + @Getter + @With + @EqualsAndHashCode.Include + UUID id; + + @Getter + @With + Space prefix; + + @Getter + @With + Markers markers; + + JRightPadded debugger; + + public J.Literal getDebugger() { + return debugger.getElement(); + } + + public DebuggerStatement withDebugger(J.Literal debugger) { + return getPadding().withDebugger(JRightPadded.withElement(this.debugger, debugger)); + } + + @Override + public

J acceptJavaScript(JavaScriptVisitor

v, P p) { + return v.visitDebuggerStatement(this, p); + } + + @Override + public CoordinateBuilder.Statement getCoordinates() { + return new CoordinateBuilder.Statement(this); + } + + public DebuggerStatement.Padding getPadding() { + DebuggerStatement.Padding p; + if (this.padding == null) { + p = new DebuggerStatement.Padding(this); + this.padding = new WeakReference<>(p); + } else { + p = this.padding.get(); + if (p == null || p.t != this) { + p = new DebuggerStatement.Padding(this); + this.padding = new WeakReference<>(p); + } + } + return p; + } + + @RequiredArgsConstructor + public static class Padding { + private final DebuggerStatement t; + + public JRightPadded getDebugger() { + return t.debugger; + } + + public DebuggerStatement withDebugger(JRightPadded debugger) { + return t.debugger == debugger ? t : new DebuggerStatement(t.id, t.prefix, t.markers, debugger); + } + } + } + @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @Data 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 e83cf194..9782ab56 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 @@ -50,7 +50,8 @@ public enum Location { MAPPED_TYPE_SUFFIX_TOKEN(JsSpace.Location.MAPPED_TYPE_SUFFIX_TOKEN_PREFIX), MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE(JsSpace.Location.MAPPED_TYPE_MAPPED_TYPE_PARAMETER_ITERATE_PREFIX), MAPPED_TYPE_READONLY(JsSpace.Location.MAPPED_TYPE_READONLY_PREFIX), - MAPPED_TYPE_QUESTION_TOKEN(JsSpace.Location.MAPPED_TYPE_QUESTION_TOKEN_PREFIX); + MAPPED_TYPE_QUESTION_TOKEN(JsSpace.Location.MAPPED_TYPE_QUESTION_TOKEN_PREFIX), + LAMBDA_ARROW(JsSpace.Location.LAMBDA_ARROW_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 7e334636..d88835dd 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 @@ -56,7 +56,8 @@ public enum Location { INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT(JsSpace.Location.INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT_SUFFIX), MAPPED_TYPE_VALUE_TYPE(JsSpace.Location.MAPPED_TYPE_VALUE_TYPE_SUFFIX), MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER_SUFFIX), - MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE_SUFFIX); + MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE_SUFFIX), + DEBUGGER(JsSpace.Location.DEBUGGER_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 49e66313..8f61fea2 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java @@ -164,5 +164,8 @@ public enum Location { MAPPED_TYPE_READONLY_PREFIX, MAPPED_TYPE_QUESTION_TOKEN_PREFIX, TYPE_TREE_EXPRESSION_PREFIX, + DEBUGGER_STATEMENT_PREFIX, + DEBUGGER_SUFFIX, + LAMBDA_ARROW_PREFIX; } } From 132b159ff197f4fa36f7002a560cd4f7a2f38884 Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Wed, 11 Dec 2024 19:22:11 +0100 Subject: [PATCH 2/3] - fix power binary "**" - fix J.NewExpression with type arguments --- openrewrite/src/javascript/parser.ts | 16 +++++++++++++--- openrewrite/src/javascript/tree/tree.ts | 3 ++- .../test/javascript/parser/binary.test.ts | 7 +++++++ openrewrite/test/javascript/parser/class.test.ts | 10 ++++++++++ .../javascript/internal/JavaScriptPrinter.java | 3 +++ .../java/org/openrewrite/javascript/tree/JS.java | 3 ++- 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 3fcd3586..b402393c 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -1833,8 +1833,15 @@ export class JavaScriptParserVisitor { Markers.EMPTY, null, Space.EMPTY, - this.visit(node.expression), - this.mapCommaSeparatedList(node.arguments ? node.getChildren(this.sourceFile).slice(2) : []), + node.typeArguments ? new J.ParameterizedType( + randomId(), + Space.EMPTY, + Markers.EMPTY, + this.visit(node.expression), + this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments), + null + ): this.visit(node.expression), + this.mapCommaSeparatedList(this.getParameterListNodes(node)), null, this.mapMethodType(node) ); @@ -2088,6 +2095,9 @@ export class JavaScriptParserVisitor { case ts.SyntaxKind.BarBarEqualsToken: assignmentOperation = JS.JsAssignmentOperation.Type.Or; break; + case ts.SyntaxKind.AsteriskAsteriskToken: + assignmentOperation = JS.JsAssignmentOperation.Type.Power; + break; } if (assignmentOperation !== undefined) { @@ -2750,7 +2760,7 @@ export class JavaScriptParserVisitor { ); } - private getParameterListNodes(node: ts.SignatureDeclarationBase, openToken : ts.SyntaxKind = ts.SyntaxKind.OpenParenToken) { + private getParameterListNodes(node: ts.SignatureDeclarationBase | ts.NewExpression, openToken : ts.SyntaxKind = ts.SyntaxKind.OpenParenToken) { const children = node.getChildren(this.sourceFile); for (let i = 0; i < children.length; i++) { if (children[i].kind == openToken) { diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 965c5dc5..503206a0 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -4508,7 +4508,7 @@ export namespace NamespaceDeclaration { } @LstType("org.openrewrite.javascript.tree.JS$FunctionDeclaration") -export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree { +export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree, TypeTree { 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; @@ -5664,6 +5664,7 @@ export namespace JsAssignmentOperation { QuestionQuestion = 0, And = 1, Or = 2, + Power = 3, } diff --git a/openrewrite/test/javascript/parser/binary.test.ts b/openrewrite/test/javascript/parser/binary.test.ts index ce5e50ce..cace22d1 100644 --- a/openrewrite/test/javascript/parser/binary.test.ts +++ b/openrewrite/test/javascript/parser/binary.test.ts @@ -80,6 +80,13 @@ describe('arithmetic operator mapping', () => { typeScript('1 >>> 2') ); }); + + test('power operation ', () => { + rewriteRun( + //language=typescript + typeScript('2 ** 3') + ); + }); }); describe('bitwise operator mapping', () => { diff --git a/openrewrite/test/javascript/parser/class.test.ts b/openrewrite/test/javascript/parser/class.test.ts index 1655f2d5..556fd283 100644 --- a/openrewrite/test/javascript/parser/class.test.ts +++ b/openrewrite/test/javascript/parser/class.test.ts @@ -469,4 +469,14 @@ describe('class mapping', () => { ); }); + + test('new class with type arguments', () => { + rewriteRun( + //language=typescript + typeScript(` + const _onDidChangeEnvironment = /*a*/new/*b*/ EventEmitter/*c*//*f*/(/*g*/)/*h*/; + `) + ); + }); + }); 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 8e107458..4bbd24d0 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 @@ -344,6 +344,9 @@ public J visitJsAssignmentOperation(JS.JsAssignmentOperation assignOp, PrintOutp case Or: keyword = "||="; break; + case Power: + keyword = "**"; + break; } beforeSyntax(assignOp, JsSpace.Location.ASSIGNMENT_OPERATION_PREFIX, p); visit(assignOp.getVariable(), p); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JS.java index 35cf931d..2de22e30 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 @@ -4189,7 +4189,7 @@ public NamespaceDeclaration withKeywordType(JLeftPadded keywordType @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) - final class FunctionDeclaration implements JS, Statement, Expression, TypedTree { + final class FunctionDeclaration implements JS, Statement, Expression, TypedTree, TypeTree { @Nullable @NonFinal @@ -5172,6 +5172,7 @@ public enum Type { QuestionQuestion, And, Or, + Power } public JsAssignmentOperation.Padding getPadding() { From eda83b07d7b1ee4c21cb46e550d191d77ef95f22 Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Thu, 12 Dec 2024 11:18:09 +0100 Subject: [PATCH 3/3] - exclamation token for PropertyDeclaration --- openrewrite/src/javascript/parser.ts | 128 +++++++++++++----- openrewrite/src/javascript/remote/receiver.ts | 19 +-- openrewrite/src/javascript/remote/sender.ts | 10 +- .../src/javascript/tree/support_types.ts | 2 - openrewrite/src/javascript/tree/tree.ts | 73 +--------- openrewrite/src/javascript/visitor.ts | 15 +- .../test/javascript/parser/class.test.ts | 11 ++ .../javascript/parser/typeLiteral.test.ts | 11 ++ .../parser/variableDeclarations.test.ts | 26 ++++ .../javascript/remote/JavaScriptReceiver.java | 19 --- .../javascript/remote/JavaScriptSender.java | 9 -- .../remote/JavaScriptValidator.java | 6 - .../javascript/JavaScriptVisitor.java | 14 -- .../internal/JavaScriptPrinter.java | 10 +- .../org/openrewrite/javascript/tree/JS.java | 77 +---------- .../javascript/tree/JsRightPadded.java | 3 +- .../openrewrite/javascript/tree/JsSpace.java | 2 - 17 files changed, 151 insertions(+), 284 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index b402393c..139a0877 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -539,6 +539,14 @@ export class JavaScriptParserVisitor { return this.mapIdentifier(node, 'never'); } + visitSymbolKeyword(node: ts.Node) { + return this.mapIdentifier(node, 'symbol'); + } + + visitBigIntKeyword(node: ts.Node) { + return this.mapIdentifier(node, 'bigint'); + } + private mapLiteral(node: ts.LiteralExpression | ts.TrueLiteral | ts.FalseLiteral | ts.NullLiteral | ts.Identifier | ts.TemplateHead | ts.TemplateMiddle | ts.TemplateTail, value: any): J.Literal { return new J.Literal( @@ -815,28 +823,54 @@ export class JavaScriptParserVisitor { ); } - return new J.VariableDeclarations( - randomId(), - this.prefix(node), - Markers.EMPTY, - [], // no decorators allowed - this.mapModifiers(node), - this.mapTypeInfo(node), - null, - [], - [this.rightPadded( - new J.VariableDeclarations.NamedVariable( - randomId(), - this.prefix(node.name), - Markers.EMPTY, - this.visit(node.name), - [], - null, - this.mapVariableType(node) - ), - Space.EMPTY - )] - ); + const nameExpression = this.visit(node.name) + + if (nameExpression instanceof J.Identifier) { + return new J.VariableDeclarations( + randomId(), + this.prefix(node), + Markers.EMPTY, + [], // no decorators allowed + this.mapModifiers(node), + this.mapTypeInfo(node), + null, + [], + [this.rightPadded( + new J.VariableDeclarations.NamedVariable( + randomId(), + this.prefix(node.name), + Markers.EMPTY, + nameExpression, + [], + null, + this.mapVariableType(node) + ), + Space.EMPTY + )] + ); + } else { + return new JS.JSVariableDeclarations( + randomId(), + this.prefix(node), + Markers.EMPTY, + [], // no decorators allowed + this.mapModifiers(node), + this.mapTypeInfo(node), + null, + [this.rightPadded( + new JS.JSVariableDeclarations.JSNamedVariable( + randomId(), + this.prefix(node.name), + Markers.EMPTY, + nameExpression, + [], + null, + this.mapVariableType(node) + ), + Space.EMPTY + )] + ); + } } visitPropertyDeclaration(node: ts.PropertyDeclaration) { @@ -864,6 +898,37 @@ export class JavaScriptParserVisitor { ); } + if (node.exclamationToken) { + return new JS.JSVariableDeclarations( + randomId(), + this.prefix(node), + Markers.EMPTY, + this.mapDecorators(node), + this.mapModifiers(node), + this.mapTypeInfo(node), + null, + [this.rightPadded( + new JS.JSVariableDeclarations.JSNamedVariable( + randomId(), + this.prefix(node.name), + Markers.EMPTY, + new JS.Unary( + randomId(), + Space.EMPTY, + Markers.EMPTY, + this.leftPadded(this.suffix(node.name), JS.Unary.Type.Exclamation), + this.visit(node.name), + this.mapType(node) + ), + [], + node.initializer ? this.leftPadded(this.prefix(node.getChildAt(node.getChildren().indexOf(node.initializer) - 1)), this.visit(node.initializer)) : null, + this.mapVariableType(node) + ), + Space.EMPTY + )] + ); + } + const nameExpression = this.visit(node.name) if (nameExpression instanceof J.Identifier) { @@ -1408,6 +1473,8 @@ export class JavaScriptParserVisitor { return JS.TypeOperator.Type.KeyOf; case ts.SyntaxKind.ReadonlyKeyword: return JS.TypeOperator.Type.ReadOnly; + case ts.SyntaxKind.UniqueKeyword: + return JS.TypeOperator.Type.Unique; } } @@ -2652,22 +2719,9 @@ export class JavaScriptParserVisitor { } visitDebuggerStatement(node: ts.DebuggerStatement) { - return new JS.DebuggerStatement( + return new ExpressionStatement( randomId(), - this.prefix(node), - Markers.EMPTY, - this.rightPadded( - new J.Literal( - randomId(), - this.prefix(node), - Markers.EMPTY, - null, - "debugger", - null, - this.mapPrimitiveType(node) - ), - this.suffix(this.findChildNode(node, ts.SyntaxKind.DebuggerKeyword)!), - ) + this.mapIdentifier(node, 'debugger') ); } diff --git a/openrewrite/src/javascript/remote/receiver.ts b/openrewrite/src/javascript/remote/receiver.ts index 17b9d685..b8756e92 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, DebuggerStatement, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java"; import * as Java from "../../java/tree"; @@ -84,14 +84,6 @@ class Visitor extends JavaScriptVisitor { return conditionalType; } - public visitDebuggerStatement(debuggerStatement: DebuggerStatement, ctx: ReceiverContext): J { - debuggerStatement = debuggerStatement.withId(ctx.receiveValue(debuggerStatement.id, ValueType.UUID)!); - debuggerStatement = debuggerStatement.withPrefix(ctx.receiveNode(debuggerStatement.prefix, receiveSpace)!); - debuggerStatement = debuggerStatement.withMarkers(ctx.receiveNode(debuggerStatement.markers, ctx.receiveMarkers)!); - debuggerStatement = debuggerStatement.padding.withDebugger(ctx.receiveNode(debuggerStatement.padding.debugger, receiveRightPaddedTree)!); - return debuggerStatement; - } - public visitDefaultType(defaultType: DefaultType, ctx: ReceiverContext): J { defaultType = defaultType.withId(ctx.receiveValue(defaultType.id, ValueType.UUID)!); defaultType = defaultType.withPrefix(ctx.receiveNode(defaultType.prefix, receiveSpace)!); @@ -1382,15 +1374,6 @@ class Factory implements ReceiverFactory { ); } - if (type === "org.openrewrite.javascript.tree.JS$DebuggerStatement") { - return new DebuggerStatement( - ctx.receiveValue(null, ValueType.UUID)!, - ctx.receiveNode(null, receiveSpace)!, - ctx.receiveNode(null, ctx.receiveMarkers)!, - ctx.receiveNode>(null, receiveRightPaddedTree)! - ); - } - if (type === "org.openrewrite.javascript.tree.JS$DefaultType") { return new DefaultType( ctx.receiveValue(null, ValueType.UUID)!, diff --git a/openrewrite/src/javascript/remote/sender.ts b/openrewrite/src/javascript/remote/sender.ts index 5ef88934..b50de0ed 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, DebuggerStatement, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; +import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from '../tree'; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java"; import * as Java from "../../java/tree"; @@ -79,14 +79,6 @@ class Visitor extends JavaScriptVisitor { return conditionalType; } - public visitDebuggerStatement(debuggerStatement: DebuggerStatement, ctx: SenderContext): J { - ctx.sendValue(debuggerStatement, v => v.id, ValueType.UUID); - ctx.sendNode(debuggerStatement, v => v.prefix, Visitor.sendSpace); - ctx.sendNode(debuggerStatement, v => v.markers, ctx.sendMarkers); - ctx.sendNode(debuggerStatement, v => v.padding.debugger, Visitor.sendRightPadded(ValueType.Tree)); - return debuggerStatement; - } - public visitDefaultType(defaultType: DefaultType, ctx: SenderContext): J { ctx.sendValue(defaultType, v => v.id, ValueType.UUID); ctx.sendNode(defaultType, v => v.prefix, Visitor.sendSpace); diff --git a/openrewrite/src/javascript/tree/support_types.ts b/openrewrite/src/javascript/tree/support_types.ts index d9c4e512..eb6b417f 100644 --- a/openrewrite/src/javascript/tree/support_types.ts +++ b/openrewrite/src/javascript/tree/support_types.ts @@ -260,7 +260,6 @@ export namespace JsSpace { MAPPED_TYPE_KEYS_REMAPPING_PREFIX, MAPPED_TYPE_MAPPED_TYPE_PARAMETER_PREFIX, TYPE_TREE_EXPRESSION_PREFIX, - DEBUGGER_STATEMENT_PREFIX, } } export namespace JsLeftPadded { @@ -332,7 +331,6 @@ export namespace JsRightPadded { INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT, MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER, MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE, - DEBUGGER_STATEMENT_DEBUGGER, } } export namespace JsContainer { diff --git a/openrewrite/src/javascript/tree/tree.ts b/openrewrite/src/javascript/tree/tree.ts index 503206a0..734e8e87 100644 --- a/openrewrite/src/javascript/tree/tree.ts +++ b/openrewrite/src/javascript/tree/tree.ts @@ -540,74 +540,6 @@ export class ConditionalType extends JSMixin(Object) implements TypeTree, Expres } -@LstType("org.openrewrite.javascript.tree.JS$DebuggerStatement") -export class DebuggerStatement extends JSMixin(Object) implements Statement { - public constructor(id: UUID, prefix: Space, markers: Markers, _debugger: JRightPadded) { - super(); - this._id = id; - this._prefix = prefix; - this._markers = markers; - this._debugger = _debugger; - } - - private readonly _id: UUID; - - public get id(): UUID { - return this._id; - } - - public withId(id: UUID): DebuggerStatement { - return id === this._id ? this : new DebuggerStatement(id, this._prefix, this._markers, this._debugger); - } - - private readonly _prefix: Space; - - public get prefix(): Space { - return this._prefix; - } - - public withPrefix(prefix: Space): DebuggerStatement { - return prefix === this._prefix ? this : new DebuggerStatement(this._id, prefix, this._markers, this._debugger); - } - - private readonly _markers: Markers; - - public get markers(): Markers { - return this._markers; - } - - public withMarkers(markers: Markers): DebuggerStatement { - return markers === this._markers ? this : new DebuggerStatement(this._id, this._prefix, markers, this._debugger); - } - - private readonly _debugger: JRightPadded; - - public get debugger(): Java.Literal { - return this._debugger.element; - } - - public withDebugger(_debugger: Java.Literal): DebuggerStatement { - return this.padding.withDebugger(this._debugger.withElement(_debugger)); - } - - public acceptJavaScript

(v: JavaScriptVisitor

, p: P): J | null { - return v.visitDebuggerStatement(this, p); - } - - get padding() { - const t = this; - return new class { - public get debugger(): JRightPadded { - return t._debugger; - } - public withDebugger(_debugger: JRightPadded): DebuggerStatement { - return t._debugger === _debugger ? t : new DebuggerStatement(t._id, t._prefix, t._markers, _debugger); - } - } - } - -} - @LstType("org.openrewrite.javascript.tree.JS$DefaultType") export class DefaultType extends JSMixin(Object) implements Expression, TypedTree, NameTree { public constructor(id: UUID, prefix: Space, markers: Markers, left: Expression, beforeEquals: Space, right: Expression, _type: JavaType | null) { @@ -2547,7 +2479,7 @@ export class TaggedTemplateExpression extends JSMixin(Object) implements Stateme } @LstType("org.openrewrite.javascript.tree.JS$TemplateExpression") -export class TemplateExpression extends JSMixin(Object) implements Statement, Expression { +export class TemplateExpression extends JSMixin(Object) implements Statement, Expression, TypeTree { public constructor(id: UUID, prefix: Space, markers: Markers, head: Java.Literal, templateSpans: JRightPadded[], _type: JavaType | null) { super(); this._id = id; @@ -3128,6 +3060,7 @@ export namespace TypeOperator { export enum Type { ReadOnly = 0, KeyOf = 1, + Unique = 2, } @@ -4508,7 +4441,7 @@ export namespace NamespaceDeclaration { } @LstType("org.openrewrite.javascript.tree.JS$FunctionDeclaration") -export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree, TypeTree { +export class FunctionDeclaration extends JSMixin(Object) implements Statement, Expression, TypedTree { public constructor(id: UUID, prefix: Space, markers: Markers, modifiers: Java.Modifier[], asteriskToken: JLeftPadded, name: JLeftPadded, typeParameters: Java.TypeParameters | null, parameters: JContainer, returnTypeExpression: TypeTree | null, body: J, _type: JavaType | null) { super(); this._id = id; diff --git a/openrewrite/src/javascript/visitor.ts b/openrewrite/src/javascript/visitor.ts index 723c1b03..dbccf3fa 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, DebuggerStatement, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree"; +import {CompilationUnit, Alias, ArrowFunction, Await, ConditionalType, DefaultType, Delete, Export, ExpressionStatement, ExpressionWithTypeArguments, FunctionType, InferType, ImportType, JsImport, JsImportSpecifier, JsBinary, LiteralType, MappedType, ObjectBindingDeclarations, PropertyAssignment, SatisfiesExpression, ScopedVariableDeclarations, StatementExpression, TaggedTemplateExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeQuery, TypeOperator, TypePredicate, Unary, Union, Intersection, Void, Yield, TypeInfo, JSVariableDeclarations, JSMethodDeclaration, JSForOfLoop, JSForInLoop, JSForInOfLoopControl, NamespaceDeclaration, FunctionDeclaration, TypeLiteral, IndexSignatureDeclaration, ArrayBindingPattern, BindingElement, ExportDeclaration, ExportAssignment, NamedExports, ExportSpecifier, IndexedAccessType, JsAssignmentOperation, TypeTreeExpression} from "./tree"; import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree"; import {JavaVisitor} from "../java"; import * as Java from "../java/tree"; @@ -85,19 +85,6 @@ export class JavaScriptVisitor

extends JavaVisitor

{ return conditionalType; } - public visitDebuggerStatement(debuggerStatement: DebuggerStatement, p: P): J | null { - debuggerStatement = debuggerStatement.withPrefix(this.visitJsSpace(debuggerStatement.prefix, JsSpace.Location.DEBUGGER_STATEMENT_PREFIX, p)!); - let tempStatement = this.visitStatement(debuggerStatement, p) as Statement; - if (!(tempStatement instanceof DebuggerStatement)) - { - return tempStatement; - } - debuggerStatement = tempStatement as DebuggerStatement; - debuggerStatement = debuggerStatement.withMarkers(this.visitMarkers(debuggerStatement.markers, p)); - debuggerStatement = debuggerStatement.padding.withDebugger(this.visitJsRightPadded(debuggerStatement.padding.debugger, JsRightPadded.Location.DEBUGGER_STATEMENT_DEBUGGER, p)!); - return debuggerStatement; - } - public visitDefaultType(defaultType: DefaultType, p: P): J | null { defaultType = defaultType.withPrefix(this.visitJsSpace(defaultType.prefix, JsSpace.Location.DEFAULT_TYPE_PREFIX, p)!); let tempExpression = this.visitExpression(defaultType, p) as Expression; diff --git a/openrewrite/test/javascript/parser/class.test.ts b/openrewrite/test/javascript/parser/class.test.ts index 556fd283..62cb2e34 100644 --- a/openrewrite/test/javascript/parser/class.test.ts +++ b/openrewrite/test/javascript/parser/class.test.ts @@ -479,4 +479,15 @@ describe('class mapping', () => { ); }); + test('property declaration', () => { + rewriteRun( + //language=typescript + typeScript(` + export class CodeLoopbackClient { + private server!: http.Server | https.Server; + } + `) + ); + }); + }); diff --git a/openrewrite/test/javascript/parser/typeLiteral.test.ts b/openrewrite/test/javascript/parser/typeLiteral.test.ts index 78831eaa..7401bde9 100644 --- a/openrewrite/test/javascript/parser/typeLiteral.test.ts +++ b/openrewrite/test/javascript/parser/typeLiteral.test.ts @@ -45,4 +45,15 @@ describe('type literal mapping', () => { `) ); }); + + test('type literal in params', () => { + rewriteRun( + //language=typescript + typeScript(` + type ascii = { + " ": 32; "!": 33; + } + `) + ); + }); }); diff --git a/openrewrite/test/javascript/parser/variableDeclarations.test.ts b/openrewrite/test/javascript/parser/variableDeclarations.test.ts index a58c9013..f2ab59fb 100644 --- a/openrewrite/test/javascript/parser/variableDeclarations.test.ts +++ b/openrewrite/test/javascript/parser/variableDeclarations.test.ts @@ -88,6 +88,7 @@ describe('variable declaration mapping', () => { `) ); }); + test('exported variables', () => { rewriteRun( //language=typescript @@ -95,6 +96,31 @@ describe('variable declaration mapping', () => { ); }); + test('unique symbol', () => { + rewriteRun( + //language=typescript + typeScript('declare const unset: unique symbol;') + ); + }); + + test('bigint', () => { + rewriteRun( + //language=typescript + typeScript('type res3 = Call;\n') + ); + }); + + test('property signature as an array', () => { + rewriteRun( + //language=typescript + typeScript(` + export type Apply = (fn & { + [rawArgs]: args; + })["return"]; + `) + ); + }); + test('declaration with destruction', () => { rewriteRun( //language=typescript diff --git a/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java b/rewrite-javascript-remote/src/main/java/org/openrewrite/javascript/remote/JavaScriptReceiver.java index 86735c4d..b4117a31 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 @@ -136,15 +136,6 @@ public JS.ConditionalType visitConditionalType(JS.ConditionalType conditionalTyp return conditionalType; } - @Override - public JS.DebuggerStatement visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, ReceiverContext ctx) { - debuggerStatement = debuggerStatement.withId(ctx.receiveNonNullValue(debuggerStatement.getId(), UUID.class)); - debuggerStatement = debuggerStatement.withPrefix(ctx.receiveNonNullNode(debuggerStatement.getPrefix(), JavaScriptReceiver::receiveSpace)); - debuggerStatement = debuggerStatement.withMarkers(ctx.receiveNonNullNode(debuggerStatement.getMarkers(), ctx::receiveMarkers)); - debuggerStatement = debuggerStatement.getPadding().withDebugger(ctx.receiveNonNullNode(debuggerStatement.getPadding().getDebugger(), JavaScriptReceiver::receiveRightPaddedTree)); - return debuggerStatement; - } - @Override public JS.DefaultType visitDefaultType(JS.DefaultType defaultType, ReceiverContext ctx) { defaultType = defaultType.withId(ctx.receiveNonNullValue(defaultType.getId(), UUID.class)); @@ -1463,7 +1454,6 @@ protected Function computeValue(Class type) { if (type == JS.ArrowFunction.class) return Factory::createJSArrowFunction; if (type == JS.Await.class) return Factory::createJSAwait; if (type == JS.ConditionalType.class) return Factory::createJSConditionalType; - if (type == JS.DebuggerStatement.class) return Factory::createJSDebuggerStatement; if (type == JS.DefaultType.class) return Factory::createJSDefaultType; if (type == JS.Delete.class) return Factory::createJSDelete; if (type == JS.Export.class) return Factory::createJSExport; @@ -1659,15 +1649,6 @@ private static JS.ConditionalType createJSConditionalType(ReceiverContext ctx) { ); } - private static JS.DebuggerStatement createJSDebuggerStatement(ReceiverContext ctx) { - return new JS.DebuggerStatement( - ctx.receiveNonNullValue(null, UUID.class), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace), - ctx.receiveNonNullNode(null, ctx::receiveMarkers), - ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveRightPaddedTree) - ); - } - private static JS.DefaultType createJSDefaultType(ReceiverContext ctx) { return new JS.DefaultType( 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 39f3e34e..dd643dfb 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 @@ -118,15 +118,6 @@ public JS.ConditionalType visitConditionalType(JS.ConditionalType conditionalTyp return conditionalType; } - @Override - public JS.DebuggerStatement visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, SenderContext ctx) { - ctx.sendValue(debuggerStatement, JS.DebuggerStatement::getId); - ctx.sendNode(debuggerStatement, JS.DebuggerStatement::getPrefix, JavaScriptSender::sendSpace); - ctx.sendNode(debuggerStatement, JS.DebuggerStatement::getMarkers, ctx::sendMarkers); - ctx.sendNode(debuggerStatement, e -> e.getPadding().getDebugger(), JavaScriptSender::sendRightPadded); - return debuggerStatement; - } - @Override public JS.DefaultType visitDefaultType(JS.DefaultType defaultType, SenderContext ctx) { ctx.sendValue(defaultType, JS.DefaultType::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 05df81c1..85ecef55 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 @@ -96,12 +96,6 @@ public JS.ConditionalType visitConditionalType(JS.ConditionalType conditionalTyp return conditionalType; } - @Override - public JS.DebuggerStatement visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, P p) { - visitAndValidateNonNull(debuggerStatement.getDebugger(), J.Literal.class, p); - return debuggerStatement; - } - @Override public JS.DefaultType visitDefaultType(JS.DefaultType defaultType, P p) { visitAndValidateNonNull(defaultType.getLeft(), Expression.class, p); 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 14732fa4..4bdcfb23 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/JavaScriptVisitor.java @@ -166,20 +166,6 @@ public J visitDefaultType(JS.DefaultType defaultType, P p) { return d; } - public J visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, P p) { - JS.DebuggerStatement d = debuggerStatement; - d = d.withPrefix(visitSpace(d.getPrefix(), JsSpace.Location.DEBUGGER_STATEMENT_PREFIX, p)); - d = d.withMarkers(visitMarkers(d.getMarkers(), p)); - Statement temp = (Statement) visitStatement(d, p); - if (!(temp instanceof JS.DebuggerStatement)) { - return temp; - } else { - d = (JS.DebuggerStatement) temp; - } - d = d.getPadding().withDebugger(Objects.requireNonNull(visitRightPadded(d.getPadding().getDebugger(), JsRightPadded.Location.DEBUGGER, p))); - return d; - } - public J visitDelete(JS.Delete delete, P p) { JS.Delete d = delete; d = d.withPrefix(visitSpace(d.getPrefix(), JsSpace.Location.DELETE_PREFIX, p)); diff --git a/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java b/rewrite-javascript/src/main/java/org/openrewrite/javascript/internal/JavaScriptPrinter.java index 4bbd24d0..bf96a0e1 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 @@ -158,14 +158,6 @@ public J visitDefaultType(JS.DefaultType defaultType, PrintOutputCapture

p) { return defaultType; } - @Override - public J visitDebuggerStatement(JS.DebuggerStatement debuggerStatement, PrintOutputCapture

p) { - beforeSyntax(debuggerStatement, JsSpace.Location.DEBUGGER_STATEMENT_PREFIX, p); - visitRightPadded(debuggerStatement.getPadding().getDebugger(), JsRightPadded.Location.DEBUGGER, p); - afterSyntax(debuggerStatement, p); - return debuggerStatement; - } - @Override public J visitDelete(JS.Delete delete, PrintOutputCapture

p) { beforeSyntax(delete, JsSpace.Location.DELETE_PREFIX, p); @@ -595,6 +587,8 @@ public J visitTypeOperator(JS.TypeOperator typeOperator, PrintOutputCapture

p keyword = "readonly"; } else if (typeOperator.getOperator() == JS.TypeOperator.Type.KeyOf) { keyword = "keyof"; + } else if (typeOperator.getOperator() == JS.TypeOperator.Type.Unique) { + keyword = "unique"; } p.append(keyword); 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 2de22e30..848fc5b4 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 @@ -594,78 +594,6 @@ public ConditionalType withCondition(@Nullable JContainer condition) } } - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) - @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) - @RequiredArgsConstructor - @AllArgsConstructor(access = AccessLevel.PRIVATE) - class DebuggerStatement implements JS, Statement { - - @Nullable - @NonFinal - transient WeakReference padding; - - @Getter - @With - @EqualsAndHashCode.Include - UUID id; - - @Getter - @With - Space prefix; - - @Getter - @With - Markers markers; - - JRightPadded debugger; - - public J.Literal getDebugger() { - return debugger.getElement(); - } - - public DebuggerStatement withDebugger(J.Literal debugger) { - return getPadding().withDebugger(JRightPadded.withElement(this.debugger, debugger)); - } - - @Override - public

J acceptJavaScript(JavaScriptVisitor

v, P p) { - return v.visitDebuggerStatement(this, p); - } - - @Override - public CoordinateBuilder.Statement getCoordinates() { - return new CoordinateBuilder.Statement(this); - } - - public DebuggerStatement.Padding getPadding() { - DebuggerStatement.Padding p; - if (this.padding == null) { - p = new DebuggerStatement.Padding(this); - this.padding = new WeakReference<>(p); - } else { - p = this.padding.get(); - if (p == null || p.t != this) { - p = new DebuggerStatement.Padding(this); - this.padding = new WeakReference<>(p); - } - } - return p; - } - - @RequiredArgsConstructor - public static class Padding { - private final DebuggerStatement t; - - public JRightPadded getDebugger() { - return t.debugger; - } - - public DebuggerStatement withDebugger(JRightPadded debugger) { - return t.debugger == debugger ? t : new DebuggerStatement(t.id, t.prefix, t.markers, debugger); - } - } - } - @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @Data @@ -2498,7 +2426,7 @@ public TaggedTemplateExpression withTypeArguments(@Nullable JContainer keywordType @EqualsAndHashCode(callSuper = false, onlyExplicitlyIncluded = true) @RequiredArgsConstructor @AllArgsConstructor(access = AccessLevel.PRIVATE) - final class FunctionDeclaration implements JS, Statement, Expression, TypedTree, TypeTree { + final class FunctionDeclaration implements JS, Statement, Expression, TypedTree { @Nullable @NonFinal 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 d88835dd..7e334636 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 @@ -56,8 +56,7 @@ public enum Location { INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT(JsSpace.Location.INDEXED_ACCESS_TYPE_INDEX_TYPE_ELEMENT_SUFFIX), MAPPED_TYPE_VALUE_TYPE(JsSpace.Location.MAPPED_TYPE_VALUE_TYPE_SUFFIX), MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_TYPE_PARAMETER_SUFFIX), - MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE_SUFFIX), - DEBUGGER(JsSpace.Location.DEBUGGER_SUFFIX); + MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE(JsSpace.Location.MAPPED_TYPE_KEYS_REMAPPING_NAME_TYPE_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 8f61fea2..a271763b 100644 --- a/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java +++ b/rewrite-javascript/src/main/java/org/openrewrite/javascript/tree/JsSpace.java @@ -164,8 +164,6 @@ public enum Location { MAPPED_TYPE_READONLY_PREFIX, MAPPED_TYPE_QUESTION_TOKEN_PREFIX, TYPE_TREE_EXPRESSION_PREFIX, - DEBUGGER_STATEMENT_PREFIX, - DEBUGGER_SUFFIX, LAMBDA_ARROW_PREFIX; } }