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

constructor support #136

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 22 additions & 5 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from "../core";
import {binarySearch, compareTextSpans, getNextSibling, TextSpan} from "./parserUtils";
import {JavaScriptTypeMapping} from "./typeMapping";
import {SignatureDeclarationBase} from "typescript";

export class JavaScriptParser extends Parser {

Expand Down Expand Up @@ -246,7 +247,7 @@ export class JavaScriptParserVisitor {
);
}

private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration | ts.PropertySignature ) {
private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration | ts.PropertySignature | ts.ConstructorDeclaration) {
if (ts.isVariableStatement(node)) {
return [new J.Modifier(
randomId(),
Expand All @@ -264,7 +265,7 @@ export class JavaScriptParserVisitor {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isPropertySignature(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(node)) {
} else if (ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isVariableDeclarationList(node)) {
let modifier: string | undefined;
Expand Down Expand Up @@ -796,7 +797,23 @@ export class JavaScriptParserVisitor {
}

visitConstructor(node: ts.ConstructorDeclaration) {
return this.visitUnknown(node);
return new J.MethodDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapDecorators(node),
this.mapModifiers(node),
null,
null,
new J.MethodDeclaration.IdentifierWithAnnotations(
this.mapIdentifier(node.getChildren().find(n => n.kind == ts.SyntaxKind.ConstructorKeyword)!, "constructor"), []
),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
null,
this.mapMethodType(node)
);
}

visitGetAccessor(node: ts.GetAccessorDeclaration) {
Expand Down Expand Up @@ -1692,7 +1709,7 @@ export class JavaScriptParserVisitor {
);
}

private getParameterListNodes(node: ts.FunctionDeclaration | ts.MethodDeclaration | ts.MethodSignature) {
private getParameterListNodes(node: ts.SignatureDeclarationBase) {
const children = node.getChildren(this.sourceFile);
for (let i = 0; i < children.length; i++) {
if (children[i].kind == ts.SyntaxKind.OpenParenToken) {
Expand Down Expand Up @@ -2329,7 +2346,7 @@ export class JavaScriptParserVisitor {
return args;
}

private mapDecorators(node: ts.ClassDeclaration | ts.FunctionDeclaration | ts.MethodDeclaration): J.Annotation[] {
private mapDecorators(node: ts.ClassDeclaration | ts.FunctionDeclaration | ts.MethodDeclaration | ts.ConstructorDeclaration): J.Annotation[] {
return node.modifiers?.filter(ts.isDecorator)?.map(this.convert<J.Annotation>) ?? [];
}

Expand Down
41 changes: 36 additions & 5 deletions openrewrite/test/javascript/parser/class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('class mapping', () => {
max(): number {
return 2;
}
} /*asdasdas*/
} /*asdasdas*/
//asdasf
`)
);
Expand All @@ -136,12 +136,12 @@ describe('class mapping', () => {
//language=typescript
typeScript(`
class X {

b: number = 6
c: string = "abc";
a /*asdasd*/ = /*abc*/ 5
} /*asdasdas*/

} /*asdasdas*/
//asdasf
`)
);
Expand Down Expand Up @@ -173,10 +173,41 @@ describe('class mapping', () => {
b: number /* abc */ = 6;
c = 10;
a /*asdasd*/ = /*abc*/ 5

}
//asdasf
`)
);
});


test('class with simple ctor', () => {
rewriteRun(
//language=typescript
typeScript(`class A {
constructor() {
}
}`)
);
});

test('class with private ctor', () => {
rewriteRun(
//language=typescript
typeScript(`class A {
/*0*/ private /*1*/ constructor /*2*/ ( /*3*/ ) /*4*/ {
}
}`)
);
});

test('class with parametrized ctor', () => {
rewriteRun(
//language=typescript
typeScript(`class A {
/*1*/ constructor /*2*/ ( a, /*3*/ b : string, /*4*/ c /*5*/ ) {
}
}`)
);
});
});