Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented support for ConstructSignature #141

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)!),
arodionov marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not we keep that test as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it doesn't construct signature, it is the same as test

interface X {
                    find ? <R, T> (v1: R, v2: T): string;
                }

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