Skip to content

Commit

Permalink
Updates for NamespaceDeclaration and FunctionType (#176)
Browse files Browse the repository at this point in the history
Implemented visitNamespaceExportDeclaration

Fixes for:
 - visitGet/SetAccessor
 - visitNewExpression

Co-authored-by: Andrii Rodionov <[email protected]>
  • Loading branch information
arodionov and Andrii Rodionov authored Dec 19, 2024
1 parent ef269ed commit 6f0af81
Show file tree
Hide file tree
Showing 23 changed files with 419 additions and 65 deletions.
19 changes: 18 additions & 1 deletion openrewrite/src/java/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 {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 {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, Erroneous} from '../tree';
import * as Java from "../../java/tree";

export class JavaReceiver implements Receiver<J> {
Expand Down Expand Up @@ -708,6 +708,14 @@ class Visitor extends JavaVisitor<ReceiverContext> {
return source;
}

public visitErroneous(erroneous: Erroneous, ctx: ReceiverContext): J {
erroneous = erroneous.withId(ctx.receiveValue(erroneous.id, ValueType.UUID)!);
erroneous = erroneous.withPrefix(ctx.receiveNode(erroneous.prefix, receiveSpace)!);
erroneous = erroneous.withMarkers(ctx.receiveNode(erroneous.markers, ctx.receiveMarkers)!);
erroneous = erroneous.withText(ctx.receiveValue(erroneous.text, ValueType.Primitive)!);
return erroneous;
}

}

class Factory implements ReceiverFactory {
Expand Down Expand Up @@ -1463,6 +1471,15 @@ class Factory implements ReceiverFactory {
);
}

if (type === "org.openrewrite.java.tree.J$Erroneous") {
return new Erroneous(
ctx.receiveValue(null, ValueType.UUID)!,
ctx.receiveNode(null, receiveSpace)!,
ctx.receiveNode(null, ctx.receiveMarkers)!,
ctx.receiveValue(null, ValueType.Primitive)!
);
}

throw new Error("No factory method for type: " + type);
}
}
Expand Down
10 changes: 9 additions & 1 deletion openrewrite/src/java/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 {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 {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, Erroneous} from '../tree';
import * as Java from "../../java/tree";

export class JavaSender implements Sender<J> {
Expand Down Expand Up @@ -708,6 +708,14 @@ class Visitor extends JavaVisitor<SenderContext> {
return source;
}

public visitErroneous(erroneous: Erroneous, ctx: SenderContext): J {
ctx.sendValue(erroneous, v => v.id, ValueType.UUID);
ctx.sendNode(erroneous, v => v.prefix, Visitor.sendSpace);
ctx.sendNode(erroneous, v => v.markers, ctx.sendMarkers);
ctx.sendValue(erroneous, v => v.text, ValueType.Primitive);
return erroneous;
}

private static sendContainer<T>(type: ValueType): (container: JContainer<T>, ctx: SenderContext) => void {
return extensions.sendContainer(type);
}
Expand Down
1 change: 1 addition & 0 deletions openrewrite/src/java/tree/support_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ export namespace Space {
WILDCARD_BOUND,
WILDCARD_PREFIX,
YIELD_PREFIX,
ERRONEOUS_PREFIX,
}
}

Expand Down
64 changes: 64 additions & 0 deletions openrewrite/src/java/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6320,3 +6320,67 @@ export namespace Unknown {
}

}

@LstType("org.openrewrite.java.tree.J$Erroneous")
export class Erroneous extends JMixin(Object) implements Statement, Expression {
public constructor(id: UUID, prefix: Space, markers: Markers, text: string) {
super();
this._id = id;
this._prefix = prefix;
this._markers = markers;
this._text = text;
}

private readonly _id: UUID;

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

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

private readonly _prefix: Space;

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

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

private readonly _markers: Markers;

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

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

private readonly _text: string;

public get text(): string {
return this._text;
}

public withText(text: string): Erroneous {
return text === this._text ? this : new Erroneous(this._id, this._prefix, this._markers, text);
}

public acceptJava<P>(v: JavaVisitor<P>, p: P): J | null {
return v.visitErroneous(this, p);
}

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

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

}
20 changes: 19 additions & 1 deletion openrewrite/src/java/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 {J, isJava, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree} from "./tree";
import {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 {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, Erroneous} from "./tree";

export class JavaVisitor<P> extends TreeVisitor<J, P> {
isAcceptable(sourceFile: SourceFile, p: P): boolean {
Expand Down Expand Up @@ -965,6 +965,24 @@ export class JavaVisitor<P> extends TreeVisitor<J, P> {
return source;
}

public visitErroneous(erroneous: Erroneous, p: P): J | null {
erroneous = erroneous.withPrefix(this.visitSpace(erroneous.prefix, Space.Location.ERRONEOUS_PREFIX, p)!);
let tempStatement = this.visitStatement(erroneous, p) as Statement;
if (!(tempStatement instanceof Erroneous))
{
return tempStatement;
}
erroneous = tempStatement as Erroneous;
let tempExpression = this.visitExpression(erroneous, p) as Expression;
if (!(tempExpression instanceof Erroneous))
{
return tempExpression;
}
erroneous = tempExpression as Erroneous;
erroneous = erroneous.withMarkers(this.visitMarkers(erroneous.markers, p));
return erroneous;
}

public visitContainer<T>(container: JContainer<T> | null, loc: JContainer.Location, p: P): JContainer<T> | null {
return extensions.visitContainer(this, container, loc, p);
}
Expand Down
84 changes: 75 additions & 9 deletions openrewrite/src/javascript/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,13 @@ 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 | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression) {
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression
| ts.ConstructorTypeNode) {
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)
|| ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node)
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node)
|| ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node)
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node)) {
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
}
else if (ts.isExportAssignment(node)) {
Expand Down Expand Up @@ -1148,6 +1149,25 @@ export class JavaScriptParserVisitor {
}

visitGetAccessor(node: ts.GetAccessorDeclaration) {
const name = this.visit(node.name);
if (!(name instanceof J.Identifier)) {
return new JS.JSMethodDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
[],
this.mapModifiers(node),
null,
this.mapTypeInfo(node),
name,
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
null,
this.mapMethodType(node)
);
}

return new J.MethodDeclaration(
randomId(),
this.prefix(node),
Expand All @@ -1157,7 +1177,7 @@ export class JavaScriptParserVisitor {
null,
this.mapTypeInfo(node),
new J.MethodDeclaration.IdentifierWithAnnotations(
this.visit(node.name),
name,
[]
),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
Expand All @@ -1169,6 +1189,25 @@ export class JavaScriptParserVisitor {
}

visitSetAccessor(node: ts.SetAccessorDeclaration) {
const name = this.visit(node.name);
if (!(name instanceof J.Identifier)) {
return new JS.JSMethodDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
[],
this.mapModifiers(node),
null,
null,
name,
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
node.body ? this.convert<J.Block>(node.body) : null,
null,
this.mapMethodType(node)
);
}

return new J.MethodDeclaration(
randomId(),
this.prefix(node),
Expand All @@ -1178,7 +1217,7 @@ export class JavaScriptParserVisitor {
null,
null,
new J.MethodDeclaration.IdentifierWithAnnotations(
this.visit(node.name),
name,
[]
),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
Expand Down Expand Up @@ -1289,7 +1328,8 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(false, Space.EMPTY),
[],
this.leftPadded(Space.EMPTY, false),
this.mapTypeParametersAsObject(node),
new JContainer(
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
Expand All @@ -1305,7 +1345,8 @@ export class JavaScriptParserVisitor {
randomId(),
this.prefix(node),
Markers.EMPTY,
this.rightPadded(true, this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!)),
this.mapModifiers(node),
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!), true),
this.mapTypeParametersAsObject(node),
new JContainer(
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
Expand Down Expand Up @@ -1920,7 +1961,7 @@ export class JavaScriptParserVisitor {
this.visit(node.expression),
this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments),
null
): this.visit(node.expression),
): new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, this.visit(node.expression)),
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
null,
this.mapMethodType(node)
Expand Down Expand Up @@ -2926,7 +2967,7 @@ export class JavaScriptParserVisitor {
}

visitModuleDeclaration(node: ts.ModuleDeclaration) {
const body = this.visit(node.body as ts.Node);
const body = node.body ? this.visit(node.body as ts.Node) : null;

let namespaceKeyword = this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword);
const keywordType = namespaceKeyword ? JS.NamespaceDeclaration.KeywordType.Namespace : JS.NamespaceDeclaration.KeywordType.Module
Expand Down Expand Up @@ -3035,7 +3076,32 @@ export class JavaScriptParserVisitor {
}

visitNamespaceExportDeclaration(node: ts.NamespaceExportDeclaration) {
return this.visitUnknown(node);
return new JS.NamespaceDeclaration(
randomId(),
this.prefix(node),
Markers.EMPTY,
[
new J.Modifier(
randomId(),
Space.EMPTY,
Markers.EMPTY,
'export',
J.Modifier.Type.LanguageExtension,
[]
),
new J.Modifier(
randomId(),
this.prefix(this.findChildNode(node, ts.SyntaxKind.AsKeyword)!),
Markers.EMPTY,
'as',
J.Modifier.Type.LanguageExtension,
[]
)
],
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword)!), JS.NamespaceDeclaration.KeywordType.Namespace),
this.rightPadded(this.convert(node.name), this.suffix(node.name)),
null
);
}

visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {
Expand Down
Loading

0 comments on commit 6f0af81

Please sign in to comment.