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

add method declaration support #125

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions openrewrite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/diff": "^5.2.2",
"@types/jest": "^29.5.13",
"@types/uuid": "^10.0.0",
"@openrewrite/rewrite-remote" : "~0.3.0",
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
"jest": "^29.7.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
Expand Down
4 changes: 2 additions & 2 deletions openrewrite/src/java/remote/receiver.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as extensions from "./remote_extensions";
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
import {JavaVisitor} from '../visitor';
import {JavaVisitor} from '..';
import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from '../tree';
import * as Java from "../tree";
import * as Java from "../../java/tree";

export class JavaReceiver implements Receiver<J> {
public fork(ctx: ReceiverContext): ReceiverContext {
Expand Down
4 changes: 2 additions & 2 deletions openrewrite/src/java/remote/sender.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as extensions from "./remote_extensions";
import {Cursor, ListUtils, Tree} from '../../core';
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
import {JavaVisitor} from '../visitor';
import {JavaVisitor} from '..';
import {J, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree, AnnotatedType, Annotation, ArrayAccess, ArrayType, Assert, Assignment, AssignmentOperation, Binary, Block, Break, Case, ClassDeclaration, CompilationUnit, Continue, DoWhileLoop, Empty, EnumValue, EnumValueSet, FieldAccess, ForEachLoop, ForLoop, ParenthesizedTypeTree, Identifier, If, Import, InstanceOf, IntersectionType, Label, Lambda, Literal, MemberReference, MethodDeclaration, MethodInvocation, Modifier, MultiCatch, NewArray, ArrayDimension, NewClass, NullableType, Package, ParameterizedType, Parentheses, ControlParentheses, Primitive, Return, Switch, SwitchExpression, Synchronized, Ternary, Throw, Try, TypeCast, TypeParameter, TypeParameters, Unary, VariableDeclarations, WhileLoop, Wildcard, Yield, Unknown} from '../tree';
import * as Java from "../tree";
import * as Java from "../../java/tree";

export class JavaSender implements Sender<J> {
public send(after: J, before: J | null, ctx: SenderContext): void {
Expand Down
43 changes: 37 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 @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
}

Copy link
Member Author

Choose a reason for hiding this comment

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

yup. and for now idk how to solve that properly

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

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

right, ok

);
}

visitParameter(node: ts.ParameterDeclaration) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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>) ?? [];
}

Expand Down
19 changes: 18 additions & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, NameTree, Space, Statement, TypeTree, TypedTree} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -292,6 +292,14 @@ class Visitor extends JavaScriptVisitor<ReceiverContext> {
return _yield;
}

public visitTypeInfo(typeInfo: TypeInfo, ctx: ReceiverContext): J {
typeInfo = typeInfo.withId(ctx.receiveValue(typeInfo.id, ValueType.UUID)!);
typeInfo = typeInfo.withPrefix(ctx.receiveNode(typeInfo.prefix, receiveSpace)!);
typeInfo = typeInfo.withMarkers(ctx.receiveNode(typeInfo.markers, ctx.receiveMarkers)!);
typeInfo = typeInfo.withTypeIdentifier(ctx.receiveNode(typeInfo.typeIdentifier, ctx.receiveTree)!);
return typeInfo;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: ReceiverContext): J {
annotatedType = annotatedType.withId(ctx.receiveValue(annotatedType.id, ValueType.UUID)!);
annotatedType = annotatedType.withPrefix(ctx.receiveNode(annotatedType.prefix, receiveSpace)!);
Expand Down Expand Up @@ -1253,6 +1261,15 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.javascript.tree.JS$TypeInfo") {
return new TypeInfo(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree)!
);
}

if (type === "org.openrewrite.java.tree.J$AnnotatedType") {
return new Java.AnnotatedType(
ctx.receiveValue(null, ValueType.UUID)!,
Expand Down
10 changes: 9 additions & 1 deletion openrewrite/src/javascript/remote/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
import {Cursor, ListUtils, Tree} from '../../core';
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
import {JavaScriptVisitor} from '..';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield} from '../tree';
import {JS, JsLeftPadded, JsRightPadded, JsContainer, JsSpace, CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo} from '../tree';
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../../java";
import * as Java from "../../java/tree";

Expand Down Expand Up @@ -287,6 +287,14 @@ class Visitor extends JavaScriptVisitor<SenderContext> {
return _yield;
}

public visitTypeInfo(typeInfo: TypeInfo, ctx: SenderContext): J {
ctx.sendValue(typeInfo, v => v.id, ValueType.UUID);
ctx.sendNode(typeInfo, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(typeInfo, v => v.markers, ctx.sendMarkers);
ctx.sendNode(typeInfo, v => v.typeIdentifier, ctx.sendTree);
return typeInfo;
}

public visitAnnotatedType(annotatedType: Java.AnnotatedType, ctx: SenderContext): J {
ctx.sendValue(annotatedType, v => v.id, ValueType.UUID);
ctx.sendNode(annotatedType, v => v.prefix, Visitor.sendSpace);
Expand Down
1 change: 1 addition & 0 deletions openrewrite/src/javascript/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export namespace JsSpace {
UNION_PREFIX,
VOID_PREFIX,
YIELD_PREFIX,
TYPE_INFO_PREFIX,
}
}
export namespace JsLeftPadded {
Expand Down
64 changes: 64 additions & 0 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2345,3 +2345,67 @@ export class Yield extends JSMixin(Object) implements Expression {
}

}

@LstType("org.openrewrite.javascript.tree.JS$TypeInfo")
export class TypeInfo extends JSMixin(Object) implements Expression, TypeTree {
public constructor(id: UUID, prefix: Space, markers: Markers, typeIdentifier: TypeTree) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._typeIdentifier = typeIdentifier;
}

private readonly _id: UUID;

public get id(): UUID {
return this._id;
}

public withId(id: UUID): TypeInfo {
return id === this._id ? this : new TypeInfo(id, this._prefix, this._markers, this._typeIdentifier);
}

private readonly _prefix: Space;

public get prefix(): Space {
return this._prefix;
}

public withPrefix(prefix: Space): TypeInfo {
return prefix === this._prefix ? this : new TypeInfo(this._id, prefix, this._markers, this._typeIdentifier);
}

private readonly _markers: Markers;

public get markers(): Markers {
return this._markers;
}

public withMarkers(markers: Markers): TypeInfo {
return markers === this._markers ? this : new TypeInfo(this._id, this._prefix, markers, this._typeIdentifier);
}

private readonly _typeIdentifier: TypeTree;

public get typeIdentifier(): TypeTree {
return this._typeIdentifier;
}

public withTypeIdentifier(typeIdentifier: TypeTree): TypeInfo {
return typeIdentifier === this._typeIdentifier ? this : new TypeInfo(this._id, this._prefix, this._markers, typeIdentifier);
}

public acceptJavaScript<P>(v: JavaScriptVisitor<P>, p: P): J | null {
return v.visitTypeInfo(this, p);
}

public get type(): JavaType | null {
return extensions.getJavaType(this);
}

public withType(type: JavaType): TypeInfo {
return extensions.withJavaType(this, type);
}

}
15 changes: 14 additions & 1 deletion openrewrite/src/javascript/visitor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as extensions from "./extensions";
import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core";
import {JS, isJavaScript, JsLeftPadded, JsRightPadded, JsContainer, JsSpace} from "./tree";
import {CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield} from "./tree";
import {CompilationUnit, Alias, ArrowFunction, Await, DefaultType, Delete, Export, ExpressionStatement, FunctionType, JsImport, JsBinary, ObjectBindingDeclarations, PropertyAssignment, ScopedVariableDeclarations, StatementExpression, TemplateExpression, Tuple, TypeDeclaration, TypeOf, TypeOperator, Unary, Union, Void, Yield, TypeInfo} from "./tree";
import {Expression, J, JContainer, JLeftPadded, JRightPadded, Space, Statement} from "../java/tree";
import {JavaVisitor} from "../java";
import * as Java from "../java/tree";
Expand Down Expand Up @@ -382,6 +382,19 @@ export class JavaScriptVisitor<P> extends JavaVisitor<P> {
return _yield;
}

public visitTypeInfo(typeInfo: TypeInfo, p: P): J | null {
typeInfo = typeInfo.withPrefix(this.visitJsSpace(typeInfo.prefix, JsSpace.Location.TYPE_INFO_PREFIX, p)!);
let tempExpression = this.visitExpression(typeInfo, p) as Expression;
if (!(tempExpression instanceof TypeInfo))
{
return tempExpression;
}
typeInfo = tempExpression as TypeInfo;
typeInfo = typeInfo.withMarkers(this.visitMarkers(typeInfo.markers, p));
typeInfo = typeInfo.withTypeIdentifier(this.visitAndCast(typeInfo.typeIdentifier, p)!);
return typeInfo;
}

public visitJsLeftPadded<T>(left: JLeftPadded<T> | null, loc: JsLeftPadded.Location, p: P): JLeftPadded<T> {
return extensions.visitJsLeftPadded(this, left, loc, p);
}
Expand Down
Loading
Loading