Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 6f0af81

Browse files
arodionovAndrii Rodionov
andauthored
Updates for NamespaceDeclaration and FunctionType (#176)
Implemented visitNamespaceExportDeclaration Fixes for: - visitGet/SetAccessor - visitNewExpression Co-authored-by: Andrii Rodionov <andriih@moderne.io>
1 parent ef269ed commit 6f0af81

File tree

23 files changed

+419
-65
lines changed

23 files changed

+419
-65
lines changed

openrewrite/src/java/remote/receiver.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
22
import {Checksum, Cursor, FileAttributes, ListUtils, Tree} from '../../core';
33
import {DetailsReceiver, Receiver, ReceiverContext, ReceiverFactory, ValueType} from '@openrewrite/rewrite-remote';
44
import {JavaVisitor} from '..';
5-
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';
5+
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';
66
import * as Java from "../../java/tree";
77

88
export class JavaReceiver implements Receiver<J> {
@@ -708,6 +708,14 @@ class Visitor extends JavaVisitor<ReceiverContext> {
708708
return source;
709709
}
710710

711+
public visitErroneous(erroneous: Erroneous, ctx: ReceiverContext): J {
712+
erroneous = erroneous.withId(ctx.receiveValue(erroneous.id, ValueType.UUID)!);
713+
erroneous = erroneous.withPrefix(ctx.receiveNode(erroneous.prefix, receiveSpace)!);
714+
erroneous = erroneous.withMarkers(ctx.receiveNode(erroneous.markers, ctx.receiveMarkers)!);
715+
erroneous = erroneous.withText(ctx.receiveValue(erroneous.text, ValueType.Primitive)!);
716+
return erroneous;
717+
}
718+
711719
}
712720

713721
class Factory implements ReceiverFactory {
@@ -1463,6 +1471,15 @@ class Factory implements ReceiverFactory {
14631471
);
14641472
}
14651473

1474+
if (type === "org.openrewrite.java.tree.J$Erroneous") {
1475+
return new Erroneous(
1476+
ctx.receiveValue(null, ValueType.UUID)!,
1477+
ctx.receiveNode(null, receiveSpace)!,
1478+
ctx.receiveNode(null, ctx.receiveMarkers)!,
1479+
ctx.receiveValue(null, ValueType.Primitive)!
1480+
);
1481+
}
1482+
14661483
throw new Error("No factory method for type: " + type);
14671484
}
14681485
}

openrewrite/src/java/remote/sender.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as extensions from "./remote_extensions";
22
import {Cursor, ListUtils, Tree} from '../../core';
33
import {Sender, SenderContext, ValueType} from '@openrewrite/rewrite-remote';
44
import {JavaVisitor} from '..';
5-
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';
5+
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';
66
import * as Java from "../../java/tree";
77

88
export class JavaSender implements Sender<J> {
@@ -708,6 +708,14 @@ class Visitor extends JavaVisitor<SenderContext> {
708708
return source;
709709
}
710710

711+
public visitErroneous(erroneous: Erroneous, ctx: SenderContext): J {
712+
ctx.sendValue(erroneous, v => v.id, ValueType.UUID);
713+
ctx.sendNode(erroneous, v => v.prefix, Visitor.sendSpace);
714+
ctx.sendNode(erroneous, v => v.markers, ctx.sendMarkers);
715+
ctx.sendValue(erroneous, v => v.text, ValueType.Primitive);
716+
return erroneous;
717+
}
718+
711719
private static sendContainer<T>(type: ValueType): (container: JContainer<T>, ctx: SenderContext) => void {
712720
return extensions.sendContainer(type);
713721
}

openrewrite/src/java/tree/support_types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ export namespace Space {
691691
WILDCARD_BOUND,
692692
WILDCARD_PREFIX,
693693
YIELD_PREFIX,
694+
ERRONEOUS_PREFIX,
694695
}
695696
}
696697

openrewrite/src/java/tree/tree.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6320,3 +6320,67 @@ export namespace Unknown {
63206320
}
63216321

63226322
}
6323+
6324+
@LstType("org.openrewrite.java.tree.J$Erroneous")
6325+
export class Erroneous extends JMixin(Object) implements Statement, Expression {
6326+
public constructor(id: UUID, prefix: Space, markers: Markers, text: string) {
6327+
super();
6328+
this._id = id;
6329+
this._prefix = prefix;
6330+
this._markers = markers;
6331+
this._text = text;
6332+
}
6333+
6334+
private readonly _id: UUID;
6335+
6336+
public get id(): UUID {
6337+
return this._id;
6338+
}
6339+
6340+
public withId(id: UUID): Erroneous {
6341+
return id === this._id ? this : new Erroneous(id, this._prefix, this._markers, this._text);
6342+
}
6343+
6344+
private readonly _prefix: Space;
6345+
6346+
public get prefix(): Space {
6347+
return this._prefix;
6348+
}
6349+
6350+
public withPrefix(prefix: Space): Erroneous {
6351+
return prefix === this._prefix ? this : new Erroneous(this._id, prefix, this._markers, this._text);
6352+
}
6353+
6354+
private readonly _markers: Markers;
6355+
6356+
public get markers(): Markers {
6357+
return this._markers;
6358+
}
6359+
6360+
public withMarkers(markers: Markers): Erroneous {
6361+
return markers === this._markers ? this : new Erroneous(this._id, this._prefix, markers, this._text);
6362+
}
6363+
6364+
private readonly _text: string;
6365+
6366+
public get text(): string {
6367+
return this._text;
6368+
}
6369+
6370+
public withText(text: string): Erroneous {
6371+
return text === this._text ? this : new Erroneous(this._id, this._prefix, this._markers, text);
6372+
}
6373+
6374+
public acceptJava<P>(v: JavaVisitor<P>, p: P): J | null {
6375+
return v.visitErroneous(this, p);
6376+
}
6377+
6378+
public get type(): JavaType | null {
6379+
return extensions.getJavaType(this);
6380+
}
6381+
6382+
public withType(type: JavaType): Erroneous {
6383+
return extensions.withJavaType(this, type);
6384+
}
6385+
6386+
}

openrewrite/src/java/visitor.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as extensions from "./extensions";
22
import {ListUtils, SourceFile, Tree, TreeVisitor} from "../core";
33
import {J, isJava, Comment, Expression, JavaSourceFile, JavaType, JContainer, JLeftPadded, JRightPadded, Loop, MethodCall, NameTree, Space, Statement, TextComment, TypedTree, TypeTree} from "./tree";
4-
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";
4+
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";
55

66
export class JavaVisitor<P> extends TreeVisitor<J, P> {
77
isAcceptable(sourceFile: SourceFile, p: P): boolean {
@@ -965,6 +965,24 @@ export class JavaVisitor<P> extends TreeVisitor<J, P> {
965965
return source;
966966
}
967967

968+
public visitErroneous(erroneous: Erroneous, p: P): J | null {
969+
erroneous = erroneous.withPrefix(this.visitSpace(erroneous.prefix, Space.Location.ERRONEOUS_PREFIX, p)!);
970+
let tempStatement = this.visitStatement(erroneous, p) as Statement;
971+
if (!(tempStatement instanceof Erroneous))
972+
{
973+
return tempStatement;
974+
}
975+
erroneous = tempStatement as Erroneous;
976+
let tempExpression = this.visitExpression(erroneous, p) as Expression;
977+
if (!(tempExpression instanceof Erroneous))
978+
{
979+
return tempExpression;
980+
}
981+
erroneous = tempExpression as Erroneous;
982+
erroneous = erroneous.withMarkers(this.visitMarkers(erroneous.markers, p));
983+
return erroneous;
984+
}
985+
968986
public visitContainer<T>(container: JContainer<T> | null, loc: JContainer.Location, p: P): JContainer<T> | null {
969987
return extensions.visitContainer(this, container, loc, p);
970988
}

openrewrite/src/javascript/parser.ts

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,13 @@ export class JavaScriptParserVisitor {
262262
private mapModifiers(node: ts.VariableDeclarationList | ts.VariableStatement | ts.ClassDeclaration | ts.PropertyDeclaration
263263
| ts.FunctionDeclaration | ts.ParameterDeclaration | ts.MethodDeclaration | ts.EnumDeclaration | ts.InterfaceDeclaration
264264
| ts.PropertySignature | ts.ConstructorDeclaration | ts.ModuleDeclaration | ts.GetAccessorDeclaration | ts.SetAccessorDeclaration
265-
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression) {
265+
| ts.ArrowFunction | ts.IndexSignatureDeclaration | ts.TypeAliasDeclaration | ts.ExportDeclaration | ts.ExportAssignment | ts.FunctionExpression
266+
| ts.ConstructorTypeNode) {
266267
if (ts.isVariableStatement(node) || ts.isModuleDeclaration(node) || ts.isClassDeclaration(node) || ts.isEnumDeclaration(node)
267268
|| ts.isInterfaceDeclaration(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node) || ts.isParameter(node)
268269
|| ts.isMethodDeclaration(node) || ts.isConstructorDeclaration(node) || ts.isArrowFunction(node)
269270
|| ts.isIndexSignatureDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isExportDeclaration(node)
270-
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node)) {
271+
|| ts.isFunctionDeclaration(node) || ts.isFunctionExpression(node) || ts.isConstructorTypeNode(node)) {
271272
return node.modifiers ? node.modifiers?.filter(ts.isModifier).map(this.mapModifier) : [];
272273
}
273274
else if (ts.isExportAssignment(node)) {
@@ -1148,6 +1149,25 @@ export class JavaScriptParserVisitor {
11481149
}
11491150

11501151
visitGetAccessor(node: ts.GetAccessorDeclaration) {
1152+
const name = this.visit(node.name);
1153+
if (!(name instanceof J.Identifier)) {
1154+
return new JS.JSMethodDeclaration(
1155+
randomId(),
1156+
this.prefix(node),
1157+
Markers.EMPTY,
1158+
[],
1159+
this.mapModifiers(node),
1160+
null,
1161+
this.mapTypeInfo(node),
1162+
name,
1163+
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
1164+
null,
1165+
node.body ? this.convert<J.Block>(node.body) : null,
1166+
null,
1167+
this.mapMethodType(node)
1168+
);
1169+
}
1170+
11511171
return new J.MethodDeclaration(
11521172
randomId(),
11531173
this.prefix(node),
@@ -1157,7 +1177,7 @@ export class JavaScriptParserVisitor {
11571177
null,
11581178
this.mapTypeInfo(node),
11591179
new J.MethodDeclaration.IdentifierWithAnnotations(
1160-
this.visit(node.name),
1180+
name,
11611181
[]
11621182
),
11631183
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
@@ -1169,6 +1189,25 @@ export class JavaScriptParserVisitor {
11691189
}
11701190

11711191
visitSetAccessor(node: ts.SetAccessorDeclaration) {
1192+
const name = this.visit(node.name);
1193+
if (!(name instanceof J.Identifier)) {
1194+
return new JS.JSMethodDeclaration(
1195+
randomId(),
1196+
this.prefix(node),
1197+
Markers.EMPTY,
1198+
[],
1199+
this.mapModifiers(node),
1200+
null,
1201+
null,
1202+
name,
1203+
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
1204+
null,
1205+
node.body ? this.convert<J.Block>(node.body) : null,
1206+
null,
1207+
this.mapMethodType(node)
1208+
);
1209+
}
1210+
11721211
return new J.MethodDeclaration(
11731212
randomId(),
11741213
this.prefix(node),
@@ -1178,7 +1217,7 @@ export class JavaScriptParserVisitor {
11781217
null,
11791218
null,
11801219
new J.MethodDeclaration.IdentifierWithAnnotations(
1181-
this.visit(node.name),
1220+
name,
11821221
[]
11831222
),
11841223
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
@@ -1289,7 +1328,8 @@ export class JavaScriptParserVisitor {
12891328
randomId(),
12901329
this.prefix(node),
12911330
Markers.EMPTY,
1292-
this.rightPadded(false, Space.EMPTY),
1331+
[],
1332+
this.leftPadded(Space.EMPTY, false),
12931333
this.mapTypeParametersAsObject(node),
12941334
new JContainer(
12951335
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
@@ -1305,7 +1345,8 @@ export class JavaScriptParserVisitor {
13051345
randomId(),
13061346
this.prefix(node),
13071347
Markers.EMPTY,
1308-
this.rightPadded(true, this.suffix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!)),
1348+
this.mapModifiers(node),
1349+
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.NewKeyword)!), true),
13091350
this.mapTypeParametersAsObject(node),
13101351
new JContainer(
13111352
this.prefix(node.getChildAt(node.getChildren().findIndex(n => n.pos === node.parameters.pos) - 1)),
@@ -1920,7 +1961,7 @@ export class JavaScriptParserVisitor {
19201961
this.visit(node.expression),
19211962
this.mapTypeArguments(this.prefix(this.findChildNode(node, ts.SyntaxKind.LessThanToken)!), node.typeArguments),
19221963
null
1923-
): this.visit(node.expression),
1964+
): new TypeTreeExpression(randomId(), Space.EMPTY, Markers.EMPTY, this.visit(node.expression)),
19241965
this.mapCommaSeparatedList(this.getParameterListNodes(node)),
19251966
null,
19261967
this.mapMethodType(node)
@@ -2926,7 +2967,7 @@ export class JavaScriptParserVisitor {
29262967
}
29272968

29282969
visitModuleDeclaration(node: ts.ModuleDeclaration) {
2929-
const body = this.visit(node.body as ts.Node);
2970+
const body = node.body ? this.visit(node.body as ts.Node) : null;
29302971

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

30373078
visitNamespaceExportDeclaration(node: ts.NamespaceExportDeclaration) {
3038-
return this.visitUnknown(node);
3079+
return new JS.NamespaceDeclaration(
3080+
randomId(),
3081+
this.prefix(node),
3082+
Markers.EMPTY,
3083+
[
3084+
new J.Modifier(
3085+
randomId(),
3086+
Space.EMPTY,
3087+
Markers.EMPTY,
3088+
'export',
3089+
J.Modifier.Type.LanguageExtension,
3090+
[]
3091+
),
3092+
new J.Modifier(
3093+
randomId(),
3094+
this.prefix(this.findChildNode(node, ts.SyntaxKind.AsKeyword)!),
3095+
Markers.EMPTY,
3096+
'as',
3097+
J.Modifier.Type.LanguageExtension,
3098+
[]
3099+
)
3100+
],
3101+
this.leftPadded(this.prefix(this.findChildNode(node, ts.SyntaxKind.NamespaceKeyword)!), JS.NamespaceDeclaration.KeywordType.Namespace),
3102+
this.rightPadded(this.convert(node.name), this.suffix(node.name)),
3103+
null
3104+
);
30393105
}
30403106

30413107
visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration) {

0 commit comments

Comments
 (0)