-
Notifications
You must be signed in to change notification settings - Fork 9
add method declaration support #125
Changes from all commits
ec9a588
0aa661b
a8a9e3c
a483f21
21bc19f
ca71be3
e651dea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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(), | ||
|
|
@@ -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; | ||
|
|
@@ -570,7 +570,19 @@ export class JavaScriptParserVisitor { | |
| } | ||
|
|
||
| visitTypeParameter(node: ts.TypeParameterDeclaration) { | ||
| return this.visitUnknown(node); | ||
| if (node.constraint || (node.modifiers && node.modifiers.length) || node.default) { | ||
| return this.visitUnknown(node); | ||
| } | ||
|
|
||
| return new J.TypeParameter( | ||
| randomId(), | ||
| this.prefix(node), | ||
| Markers.EMPTY, | ||
| [], | ||
| [], | ||
| this.visit(node.name), | ||
| null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Adding the constraints can be done separately. Unlike Java I think TS allows specifying anonymous types inline as in: type Animal = { name: string };
type Dog = { breed: string } & Animal;
function handleAnimal<T extends Animal>(animal: T) {
console.log(animal.name);
}
function handleAnimals<T extends { breed: string } & Animal>(animal: T) {
console.log(animal.name);
}There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup. and for now idk how to solve that properly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the parsing will not be an issue, ad the property is of type TypeTree. We just need to implement the mapping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, ok |
||
| ); | ||
| } | ||
|
|
||
| visitParameter(node: ts.ParameterDeclaration) { | ||
|
|
@@ -665,7 +677,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 => this.rightPadded(this.visit(tp), this.suffix(tp)))) | ||
| : null, | ||
| node.type ? new JS.TypeInfo(randomId(), this.prefix(node.getChildAt(node.getChildren().indexOf(node.type) - 1)), Markers.EMPTY, 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) { | ||
|
|
@@ -1487,7 +1518,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) { | ||
|
|
@@ -2011,7 +2042,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>) ?? []; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.