Skip to content

Commit

Permalink
add method declaration support
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegDokuka committed Oct 8, 2024
1 parent 29511db commit de94ed5
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 7 deletions.
1 change: 1 addition & 0 deletions openrewrite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/diff": "^5.2.2",
"@types/jest": "^29.5.13",
"@types/uuid": "^10.0.0",
"@openrewrite/rewrite-remote" : "~0.3.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
Expand Down
39 changes: 33 additions & 6 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class JavaScriptParserVisitor {
);
}

private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration) {
private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration) {
if (ts.isVariableStatement(node)) {
return [new J.Modifier(
randomId(),
Expand All @@ -260,7 +260,7 @@ export class JavaScriptParserVisitor {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
} else if (ts.isPropertyDeclaration(node)) {
return []; // FIXME
} else if (ts.isFunctionDeclaration(node) || ts.isParameter(node)) {
} else if (ts.isFunctionDeclaration(node) || ts.isParameter(node) || ts.isMethodDeclaration(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 @@ -549,7 +549,15 @@ export class JavaScriptParserVisitor {
}

visitTypeParameter(node: ts.TypeParameterDeclaration) {
return this.visitUnknown(node);
return new J.TypeParameter(
randomId(),
this.prefix(node),
Markers.EMPTY,
[],
[],
this.visit(node.name),
null // fix me. this should support complex generics
);
}

visitParameter(node: ts.ParameterDeclaration) {
Expand Down Expand Up @@ -644,7 +652,26 @@ export class JavaScriptParserVisitor {
}

visitMethodDeclaration(node: ts.MethodDeclaration) {
return this.visitUnknown(node);
return new J.MethodDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
this.mapDecorators(node),
this.mapModifiers(node),
node.typeParameters
? new J.TypeParameters(randomId(), this.suffix(node.name), Markers.EMPTY, [], node.typeParameters.map(tp => new JRightPadded<J.TypeParameter>(this.visit(tp), this.suffix(tp), Markers.EMPTY)))
: null,
node.type ? this.visit(node.type) : null,
new J.MethodDeclaration.IdentifierWithAnnotations(
node.name ? this.visit(node.name) : this.mapIdentifier(node, ""),
[]
),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
null,
this.mapMethodType(node)
);
}

visitClassStaticBlockDeclaration(node: ts.ClassStaticBlockDeclaration) {
Expand Down Expand Up @@ -1462,7 +1489,7 @@ export class JavaScriptParserVisitor {
);
}

private getParameterListNodes(node: ts.FunctionDeclaration) {
private getParameterListNodes(node: ts.FunctionDeclaration | ts.MethodDeclaration) {
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 @@ -1968,7 +1995,7 @@ export class JavaScriptParserVisitor {
return args;
}

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

Expand Down
124 changes: 124 additions & 0 deletions openrewrite/test/javascript/parser/method.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import {connect, disconnect, rewriteRun, rewriteRunWithOptions, typeScript} from '../testHarness';

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

test('simple', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test() {
// hello world comment
}
}
`)
);
});


test('single parameter', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test(input) {
// hello world comment
}
}
`)
);
});

test('single typed parameter', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test(input: string) {
// hello world comment
}
}
`)
);
});

test('single typed parameter with initializer', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test(input /*asda*/: string = /*8asdas */ "hello world" ) {
// hello world comment
}
}
`)
);
});

test('single parameter with initializer', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test(input = 1) {
// hello world comment
}
}
`)
);
});

test('multi parameters', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test(input: string, a = 1, test: number) {
// hello world comment
}
}
`)
);
});

test('parameter with trailing comma', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test(input: string , ) {
// hello world comment
}
}
`)
);
});

test('type parameters', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test<T>(input: T , ) {
// hello world comment
}
}
`)
);
});

test('multiple type parameters', () => {
rewriteRun(
//language=typescript
typeScript(`
class Handler {
test<T>(input: T , ) {
// hello world comment
}
}
`)
);
});
});
2 changes: 1 addition & 1 deletion openrewrite/test/javascript/testHarness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function connect(): Promise<RemotingContext> {
client.once('connect', () => {
remoting = new RemotingContext();
remoting.connect(client);
PrinterFactory.current = new RemotePrinterFactory(remoting.client!);
PrinterFactory.current = new RemotePrinterFactory(remoting.client!) as any;
resolve(remoting);
});
client.setTimeout(10000, () => {
Expand Down

0 comments on commit de94ed5

Please sign in to comment.