From 7f1f4669af287629a804e2eaa4d6adbfb93e9bf5 Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Mon, 11 Nov 2024 13:23:02 +0100 Subject: [PATCH 1/2] Implemented support for ConstructSignature --- openrewrite/src/javascript/parser.ts | 41 +++++++++++++++++-- .../test/javascript/parser/interface.test.ts | 23 ++++++++--- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index c365efd3..3c7b0cee 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -839,7 +839,6 @@ export class JavaScriptParserVisitor { Markers.EMPTY, [], // no decorators allowed [], // no modifiers allowed - this.mapTypeParametersAsObject(node), this.mapTypeInfo(node), this.getOptionalUnary(node), @@ -913,7 +912,8 @@ export class JavaScriptParserVisitor { } private mapTypeInfo(node: ts.MethodDeclaration | ts.PropertyDeclaration | ts.VariableDeclaration | ts.ParameterDeclaration - | ts.PropertySignature | ts.MethodSignature | ts.ArrowFunction | ts.CallSignatureDeclaration | ts.GetAccessorDeclaration | ts.FunctionDeclaration) { + | ts.PropertySignature | ts.MethodSignature | ts.ArrowFunction | ts.CallSignatureDeclaration | ts.GetAccessorDeclaration + | ts.FunctionDeclaration | ts.ConstructSignatureDeclaration) { return node.type ? new JS.TypeInfo(randomId(), this.prefix(node.getChildAt(node.getChildren().indexOf(node.type) - 1)), Markers.EMPTY, this.visit(node.type)) : null; } @@ -922,7 +922,7 @@ export class JavaScriptParserVisitor { randomId(), this.prefix(node), Markers.EMPTY, - new JRightPadded(true,this.prefix(node.body.getChildren().find(v => v.kind === ts.SyntaxKind.OpenBraceToken)!), Markers.EMPTY), + new JRightPadded(true, this.prefix(this.findChildNode(node.body, ts.SyntaxKind.OpenBraceToken)!), Markers.EMPTY), node.body.statements.map(ce => new JRightPadded( this.convert(ce), ce.getLastToken()?.kind === ts.SyntaxKind.SemicolonToken ? this.prefix(ce.getLastToken()!) : Space.EMPTY, @@ -1023,7 +1023,40 @@ export class JavaScriptParserVisitor { } visitConstructSignature(node: ts.ConstructSignatureDeclaration) { - return this.visitUnknown(node); + return new J.MethodDeclaration( + randomId(), + this.prefix(node), + Markers.EMPTY, + [], // no decorators allowed + [], // no modifiers allowed + node.typeParameters + ? new J.TypeParameters( + randomId(), + this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!), + Markers.EMPTY, + [], + node.typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp))) + ) + : null, + this.mapTypeInfo(node), + new J.MethodDeclaration.IdentifierWithAnnotations( + new J.Identifier( + randomId(), + Space.EMPTY, + Markers.EMPTY, + [], + 'new', + null, + null + ), + [] + ), + this.mapCommaSeparatedList(this.getParameterListNodes(node)), + null, + null, + null, + this.mapMethodType(node) + ); } visitIndexSignature(node: ts.IndexSignatureDeclaration) { diff --git a/openrewrite/test/javascript/parser/interface.test.ts b/openrewrite/test/javascript/parser/interface.test.ts index 38dc624a..0a475a57 100644 --- a/openrewrite/test/javascript/parser/interface.test.ts +++ b/openrewrite/test/javascript/parser/interface.test.ts @@ -1,6 +1,6 @@ import {connect, disconnect, rewriteRun, typeScript} from '../testHarness'; -describe('as mapping', () => { +describe('interface mapping', () => { beforeAll(() => connect()); afterAll(() => disconnect()); @@ -186,7 +186,7 @@ describe('as mapping', () => { ); }); - test.skip('interface with constructor signature', () => { + test('interface with constructor signature', () => { rewriteRun( //language=typescript typeScript(` @@ -197,12 +197,23 @@ describe('as mapping', () => { ); }); - test.skip('interface with optional constructor signature', () => { + test('interface with constructor signature with type parameters', () => { rewriteRun( //language=typescript typeScript(` - interface Constructible { - new? (name: string, age: number): Person; // Interface that defines a constructor signature + interface GenericConstructor { + new (value: R, value: T): GenericClass; + } + `) + ); + }); + + test('interface with constructor signature with type parameters and comments', () => { + rewriteRun( + //language=typescript + typeScript(` + interface GenericConstructor { + /*a*/new /*b*//*g*/ (value1: R, value2: T)/*e*/: GenericClass/*j*/; } `) ); @@ -372,7 +383,7 @@ describe('as mapping', () => { //language=typescript typeScript(` interface X { - find ? (v1: R, v2: T): string; + find ? (v1: R, v2: T): string; } `) ); From 81bbe7022ddee77920dffddbe1ebeb7515722a7b Mon Sep 17 00:00:00 2001 From: Andrii Rodionov Date: Tue, 12 Nov 2024 13:18:09 +0100 Subject: [PATCH 2/2] adjusted mapTypeParametersAsObject method for ConstructSignatureDeclaration --- openrewrite/src/javascript/parser.ts | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/openrewrite/src/javascript/parser.ts b/openrewrite/src/javascript/parser.ts index 3c7b0cee..a5239f31 100644 --- a/openrewrite/src/javascript/parser.ts +++ b/openrewrite/src/javascript/parser.ts @@ -14,7 +14,6 @@ import { } from "../core"; import {binarySearch, compareTextSpans, getNextSibling, TextSpan} from "./parserUtils"; import {JavaScriptTypeMapping} from "./typeMapping"; -import {SignatureDeclarationBase} from "typescript"; export class JavaScriptParser extends Parser { @@ -1029,15 +1028,7 @@ export class JavaScriptParserVisitor { Markers.EMPTY, [], // no decorators allowed [], // no modifiers allowed - node.typeParameters - ? new J.TypeParameters( - randomId(), - this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!), - Markers.EMPTY, - [], - node.typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp))) - ) - : null, + this.mapTypeParametersAsObject(node), this.mapTypeInfo(node), new J.MethodDeclaration.IdentifierWithAnnotations( new J.Identifier( @@ -2605,10 +2596,16 @@ export class JavaScriptParserVisitor { : null; } - private mapTypeParametersAsObject(node: ts.MethodDeclaration | ts.MethodSignature | ts.FunctionDeclaration | ts.CallSignatureDeclaration) : J.TypeParameters | null { - return node.typeParameters - ? new J.TypeParameters(randomId(), node.questionToken ? this.suffix(node.questionToken) : node.name ? this.suffix(node.name) : Space.EMPTY, Markers.EMPTY, [], node.typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp)))) - : null; + private mapTypeParametersAsObject(node: ts.MethodDeclaration | ts.MethodSignature | ts.FunctionDeclaration | ts.CallSignatureDeclaration | ts.ConstructSignatureDeclaration) : J.TypeParameters | null { + if (!node.typeParameters) return null; + + let ts_prefix; + if (ts.isConstructSignatureDeclaration(node)) { + ts_prefix = this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!); + } else { + ts_prefix = node.questionToken ? this.suffix(node.questionToken) : node.name ? this.suffix(node.name) : Space.EMPTY; + } + return new J.TypeParameters(randomId(), ts_prefix, Markers.EMPTY, [], node.typeParameters.map(tp => this.rightPadded(this.visit(tp), this.suffix(tp)))); } private mapTypeParametersList(typeParamsNodeArray: ts.NodeArray) : JRightPadded[] {