Skip to content

Commit

Permalink
Implemented support for ConstructSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Rodionov committed Nov 11, 2024
1 parent 38fd0db commit 7f1f466
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
41 changes: 37 additions & 4 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,6 @@ export class JavaScriptParserVisitor {
Markers.EMPTY,
[], // no decorators allowed
[], // no modifiers allowed

this.mapTypeParametersAsObject(node),
this.mapTypeInfo(node),
this.getOptionalUnary(node),
Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 17 additions & 6 deletions openrewrite/test/javascript/parser/interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {connect, disconnect, rewriteRun, typeScript} from '../testHarness';

describe('as mapping', () => {
describe('interface mapping', () => {
beforeAll(() => connect());
afterAll(() => disconnect());

Expand Down Expand Up @@ -186,7 +186,7 @@ describe('as mapping', () => {
);
});

test.skip('interface with constructor signature', () => {
test('interface with constructor signature', () => {
rewriteRun(
//language=typescript
typeScript(`
Expand All @@ -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<R, T> (value: R, value: T): GenericClass;
}
`)
);
});

test('interface with constructor signature with type parameters and comments', () => {
rewriteRun(
//language=typescript
typeScript(`
interface GenericConstructor {
/*a*/new /*b*/</*c*/R/*d*/,/*e*/ T/*f*/>/*g*/ (value1: R, value2: T)/*e*/: GenericClass/*j*/;
}
`)
);
Expand Down Expand Up @@ -372,7 +383,7 @@ describe('as mapping', () => {
//language=typescript
typeScript(`
interface X {
find ? <R, T> (v1: R, v2: T): string;
find ? <R, T> (v1: R, v2: T): string;
}
`)
);
Expand Down

0 comments on commit 7f1f466

Please sign in to comment.