Skip to content

Commit

Permalink
Change type to prevent class cast exception (#171)
Browse files Browse the repository at this point in the history
* Change type to prevent class cast exception

* updated after rebase

---------

Co-authored-by: Andrii Rodionov <[email protected]>
  • Loading branch information
arodionov and Andrii Rodionov authored Dec 16, 2024
1 parent 01ed2c9 commit 1fca27c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 48 deletions.
2 changes: 1 addition & 1 deletion openrewrite/src/javascript/remote/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,7 @@ class Factory implements ReceiverFactory {
ctx.receiveNodes<Java.Annotation>(null, ctx.receiveTree)!,
ctx.receiveNodes<Java.Modifier>(null, ctx.receiveTree)!,
ctx.receiveNode<TypeTree>(null, ctx.receiveTree),
ctx.receiveNode<JContainer<BindingElement>>(null, receiveContainer)!,
ctx.receiveNode<JContainer<J>>(null, receiveContainer)!,
ctx.receiveNode<JLeftPadded<Expression>>(null, receiveLeftPaddedTree)
);
}
Expand Down
12 changes: 6 additions & 6 deletions openrewrite/src/javascript/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@ export namespace MappedType {

@LstType("org.openrewrite.javascript.tree.JS$ObjectBindingDeclarations")
export class ObjectBindingDeclarations extends JSMixin(Object) implements Expression, TypedTree {
public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeExpression: TypeTree | null, bindings: JContainer<BindingElement>, initializer: JLeftPadded<Expression> | null) {
public constructor(id: UUID, prefix: Space, markers: Markers, leadingAnnotations: Java.Annotation[], modifiers: Java.Modifier[], typeExpression: TypeTree | null, bindings: JContainer<J>, initializer: JLeftPadded<Expression> | null) {
super();
this._id = id;
this._prefix = prefix;
Expand Down Expand Up @@ -2036,13 +2036,13 @@ export class ObjectBindingDeclarations extends JSMixin(Object) implements Expres
return typeExpression === this._typeExpression ? this : new ObjectBindingDeclarations(this._id, this._prefix, this._markers, this._leadingAnnotations, this._modifiers, typeExpression, this._bindings, this._initializer);
}

private readonly _bindings: JContainer<BindingElement>;
private readonly _bindings: JContainer<J>;

public get bindings(): BindingElement[] {
public get bindings(): J[] {
return this._bindings.elements;
}

public withBindings(bindings: BindingElement[]): ObjectBindingDeclarations {
public withBindings(bindings: J[]): ObjectBindingDeclarations {
return this.padding.withBindings(JContainer.withElements(this._bindings, bindings));
}

Expand Down Expand Up @@ -2071,10 +2071,10 @@ export class ObjectBindingDeclarations extends JSMixin(Object) implements Expres
get padding() {
const t = this;
return new class {
public get bindings(): JContainer<BindingElement> {
public get bindings(): JContainer<J> {
return t._bindings;
}
public withBindings(bindings: JContainer<BindingElement>): ObjectBindingDeclarations {
public withBindings(bindings: JContainer<J>): ObjectBindingDeclarations {
return t._bindings === bindings ? t : new ObjectBindingDeclarations(t._id, t._prefix, t._markers, t._leadingAnnotations, t._modifiers, t._typeExpression, bindings, t._initializer);
}
public get initializer(): JLeftPadded<Expression> | null {
Expand Down
9 changes: 9 additions & 0 deletions openrewrite/test/javascript/parser/arrow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,13 @@ describe('arrow mapping', () => {
);
});

test('arrow function with empty object binding', () => {
rewriteRun(
//language=typescript
typeScript(`
({/*a*/}) => ({/*b*/})
`)
);
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat
classDeclaration = classDeclaration.withPrefix(ctx.receiveNonNullNode(classDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace));
classDeclaration = classDeclaration.withMarkers(ctx.receiveNonNullNode(classDeclaration.getMarkers(), ctx::receiveMarkers));
classDeclaration = classDeclaration.withLeadingAnnotations(ctx.receiveNonNullNodes(classDeclaration.getLeadingAnnotations(), ctx::receiveTree));
classDeclaration = classDeclaration.withModifiers(ctx.receiveNonNullNodes(classDeclaration.getModifiers(), JavaScriptReceiver::receiveModifier));
classDeclaration = classDeclaration.withModifiers(ctx.receiveNonNullNodes(classDeclaration.getModifiers(), ctx::receiveTree));
classDeclaration = classDeclaration.getPadding().withKind(ctx.receiveNonNullNode(classDeclaration.getPadding().getKind(), JavaScriptReceiver::receiveClassDeclarationKind));
classDeclaration = classDeclaration.withName(ctx.receiveNonNullNode(classDeclaration.getName(), ctx::receiveTree));
classDeclaration = classDeclaration.getPadding().withTypeParameters(ctx.receiveNode(classDeclaration.getPadding().getTypeParameters(), JavaScriptReceiver::receiveContainer));
Expand Down Expand Up @@ -1116,7 +1116,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
methodDeclaration = methodDeclaration.withPrefix(ctx.receiveNonNullNode(methodDeclaration.getPrefix(), JavaScriptReceiver::receiveSpace));
methodDeclaration = methodDeclaration.withMarkers(ctx.receiveNonNullNode(methodDeclaration.getMarkers(), ctx::receiveMarkers));
methodDeclaration = methodDeclaration.withLeadingAnnotations(ctx.receiveNonNullNodes(methodDeclaration.getLeadingAnnotations(), ctx::receiveTree));
methodDeclaration = methodDeclaration.withModifiers(ctx.receiveNonNullNodes(methodDeclaration.getModifiers(), JavaScriptReceiver::receiveModifier));
methodDeclaration = methodDeclaration.withModifiers(ctx.receiveNonNullNodes(methodDeclaration.getModifiers(), ctx::receiveTree));
methodDeclaration = methodDeclaration.getAnnotations().withTypeParameters(ctx.receiveNode(methodDeclaration.getAnnotations().getTypeParameters(), JavaScriptReceiver::receiveMethodTypeParameters));
methodDeclaration = methodDeclaration.withReturnTypeExpression(ctx.receiveNode(methodDeclaration.getReturnTypeExpression(), ctx::receiveTree));
methodDeclaration = methodDeclaration.getAnnotations().withName(ctx.receiveNonNullNode(methodDeclaration.getAnnotations().getName(), JavaScriptReceiver::receiveMethodIdentifierWithAnnotations));
Expand All @@ -1141,6 +1141,17 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
return methodInvocation;
}

@Override
public J.Modifier visitModifier(J.Modifier modifier, ReceiverContext ctx) {
modifier = modifier.withId(ctx.receiveNonNullValue(modifier.getId(), UUID.class));
modifier = modifier.withPrefix(ctx.receiveNonNullNode(modifier.getPrefix(), JavaScriptReceiver::receiveSpace));
modifier = modifier.withMarkers(ctx.receiveNonNullNode(modifier.getMarkers(), ctx::receiveMarkers));
modifier = modifier.withKeyword(ctx.receiveValue(modifier.getKeyword(), String.class));
modifier = modifier.withType(ctx.receiveNonNullValue(modifier.getType(), J.Modifier.Type.class));
modifier = modifier.withAnnotations(ctx.receiveNonNullNodes(modifier.getAnnotations(), ctx::receiveTree));
return modifier;
}

@Override
public J.MultiCatch visitMultiCatch(J.MultiCatch multiCatch, ReceiverContext ctx) {
multiCatch = multiCatch.withId(ctx.receiveNonNullValue(multiCatch.getId(), UUID.class));
Expand Down Expand Up @@ -1351,7 +1362,7 @@ public J.TypeParameter visitTypeParameter(J.TypeParameter typeParameter, Receive
typeParameter = typeParameter.withPrefix(ctx.receiveNonNullNode(typeParameter.getPrefix(), JavaScriptReceiver::receiveSpace));
typeParameter = typeParameter.withMarkers(ctx.receiveNonNullNode(typeParameter.getMarkers(), ctx::receiveMarkers));
typeParameter = typeParameter.withAnnotations(ctx.receiveNonNullNodes(typeParameter.getAnnotations(), ctx::receiveTree));
typeParameter = typeParameter.withModifiers(ctx.receiveNonNullNodes(typeParameter.getModifiers(), JavaScriptReceiver::receiveModifier));
typeParameter = typeParameter.withModifiers(ctx.receiveNonNullNodes(typeParameter.getModifiers(), ctx::receiveTree));
typeParameter = typeParameter.withName(ctx.receiveNonNullNode(typeParameter.getName(), ctx::receiveTree));
typeParameter = typeParameter.getPadding().withBounds(ctx.receiveNode(typeParameter.getPadding().getBounds(), JavaScriptReceiver::receiveContainer));
return typeParameter;
Expand All @@ -1374,7 +1385,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
variableDeclarations = variableDeclarations.withPrefix(ctx.receiveNonNullNode(variableDeclarations.getPrefix(), JavaScriptReceiver::receiveSpace));
variableDeclarations = variableDeclarations.withMarkers(ctx.receiveNonNullNode(variableDeclarations.getMarkers(), ctx::receiveMarkers));
variableDeclarations = variableDeclarations.withLeadingAnnotations(ctx.receiveNonNullNodes(variableDeclarations.getLeadingAnnotations(), ctx::receiveTree));
variableDeclarations = variableDeclarations.withModifiers(ctx.receiveNonNullNodes(variableDeclarations.getModifiers(), JavaScriptReceiver::receiveModifier));
variableDeclarations = variableDeclarations.withModifiers(ctx.receiveNonNullNodes(variableDeclarations.getModifiers(), ctx::receiveTree));
variableDeclarations = variableDeclarations.withTypeExpression(ctx.receiveNode(variableDeclarations.getTypeExpression(), ctx::receiveTree));
variableDeclarations = variableDeclarations.withVarargs(ctx.receiveNode(variableDeclarations.getVarargs(), JavaScriptReceiver::receiveSpace));
variableDeclarations = variableDeclarations.withDimensionsBeforeName(ctx.receiveNonNullNodes(variableDeclarations.getDimensionsBeforeName(), leftPaddedNodeReceiver(org.openrewrite.java.tree.Space.class)));
Expand Down Expand Up @@ -2388,7 +2399,7 @@ private static J.ClassDeclaration createJClassDeclaration(ReceiverContext ctx) {
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNodes(null, JavaScriptReceiver::receiveModifier),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveClassDeclarationKind),
ctx.receiveNonNullNode(null, ctx::receiveTree),
ctx.receiveNode(null, JavaScriptReceiver::receiveContainer),
Expand Down Expand Up @@ -2649,7 +2660,7 @@ private static J.MethodDeclaration createJMethodDeclaration(ReceiverContext ctx)
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNodes(null, JavaScriptReceiver::receiveModifier),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNode(null, JavaScriptReceiver::receiveMethodTypeParameters),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveMethodIdentifierWithAnnotations),
Expand Down Expand Up @@ -2895,7 +2906,7 @@ private static J.TypeParameter createJTypeParameter(ReceiverContext ctx) {
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNodes(null, JavaScriptReceiver::receiveModifier),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNode(null, ctx::receiveTree),
ctx.receiveNode(null, JavaScriptReceiver::receiveContainer)
);
Expand Down Expand Up @@ -2928,7 +2939,7 @@ private static J.VariableDeclarations createJVariableDeclarations(ReceiverContex
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNonNullNodes(null, JavaScriptReceiver::receiveModifier),
ctx.receiveNonNullNodes(null, ctx::receiveTree),
ctx.receiveNode(null, ctx::receiveTree),
ctx.receiveNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNodes(null, leftPaddedNodeReceiver(org.openrewrite.java.tree.Space.class)),
Expand Down Expand Up @@ -3049,27 +3060,6 @@ private static J.MethodDeclaration.IdentifierWithAnnotations receiveMethodIdenti
return identifierWithAnnotations;
}

private static J.Modifier receiveModifier(J.@Nullable Modifier modifier, @Nullable Class<?> type, ReceiverContext ctx) {
if (modifier != null) {
modifier = modifier.withId(ctx.receiveNonNullValue(modifier.getId(), UUID.class));
modifier = modifier.withPrefix(ctx.receiveNonNullNode(modifier.getPrefix(), JavaScriptReceiver::receiveSpace));
modifier = modifier.withMarkers(ctx.receiveNonNullNode(modifier.getMarkers(), ctx::receiveMarkers));
modifier = modifier.withKeyword(ctx.receiveValue(modifier.getKeyword(), String.class));
modifier = modifier.withType(ctx.receiveNonNullValue(modifier.getType(), J.Modifier.Type.class));
modifier = modifier.withAnnotations(ctx.receiveNonNullNodes(modifier.getAnnotations(), ctx::receiveTree));
} else {
modifier = new J.Modifier(
ctx.receiveNonNullValue(null, UUID.class),
ctx.receiveNonNullNode(null, JavaScriptReceiver::receiveSpace),
ctx.receiveNonNullNode(null, ctx::receiveMarkers),
ctx.receiveValue(null, String.class),
ctx.receiveNonNullValue(null, J.Modifier.Type.class),
ctx.receiveNonNullNodes(null, ctx::receiveTree)
);
}
return modifier;
}

private static J.TypeParameters receiveMethodTypeParameters(J.@Nullable TypeParameters typeParameters, @Nullable Class<?> type, ReceiverContext ctx) {
if (typeParameters != null) {
typeParameters = typeParameters.withId(ctx.receiveNonNullValue(typeParameters.getId(), UUID.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclarat
ctx.sendNode(classDeclaration, J.ClassDeclaration::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(classDeclaration, J.ClassDeclaration::getMarkers, ctx::sendMarkers);
ctx.sendNodes(classDeclaration, J.ClassDeclaration::getLeadingAnnotations, ctx::sendTree, Tree::getId);
ctx.sendNodes(classDeclaration, J.ClassDeclaration::getModifiers, this::sendModifier, Tree::getId);
ctx.sendNodes(classDeclaration, J.ClassDeclaration::getModifiers, ctx::sendTree, Tree::getId);
ctx.sendNode(classDeclaration, e -> e.getPadding().getKind(), this::sendClassDeclarationKind);
ctx.sendNode(classDeclaration, J.ClassDeclaration::getName, ctx::sendTree);
ctx.sendNode(classDeclaration, e -> e.getPadding().getTypeParameters(), JavaScriptSender::sendContainer);
Expand Down Expand Up @@ -1114,7 +1114,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
ctx.sendNode(methodDeclaration, J.MethodDeclaration::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(methodDeclaration, J.MethodDeclaration::getMarkers, ctx::sendMarkers);
ctx.sendNodes(methodDeclaration, J.MethodDeclaration::getLeadingAnnotations, ctx::sendTree, Tree::getId);
ctx.sendNodes(methodDeclaration, J.MethodDeclaration::getModifiers, this::sendModifier, Tree::getId);
ctx.sendNodes(methodDeclaration, J.MethodDeclaration::getModifiers, ctx::sendTree, Tree::getId);
ctx.sendNode(methodDeclaration, e -> e.getAnnotations().getTypeParameters(), this::sendMethodTypeParameters);
ctx.sendNode(methodDeclaration, J.MethodDeclaration::getReturnTypeExpression, ctx::sendTree);
ctx.sendNode(methodDeclaration, e -> e.getAnnotations().getName(), this::sendMethodIdentifierWithAnnotations);
Expand Down Expand Up @@ -1144,13 +1144,15 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
return methodInvocation;
}

private void sendModifier(J.Modifier modifier, SenderContext ctx) {
@Override
public J.Modifier visitModifier(J.Modifier modifier, SenderContext ctx) {
ctx.sendValue(modifier, J.Modifier::getId);
ctx.sendNode(modifier, J.Modifier::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(modifier, J.Modifier::getMarkers, ctx::sendMarkers);
ctx.sendValue(modifier, J.Modifier::getKeyword);
ctx.sendValue(modifier, J.Modifier::getType);
ctx.sendNodes(modifier, J.Modifier::getAnnotations, ctx::sendTree, Tree::getId);
return modifier;
}

@Override
Expand Down Expand Up @@ -1363,7 +1365,7 @@ public J.TypeParameter visitTypeParameter(J.TypeParameter typeParameter, SenderC
ctx.sendNode(typeParameter, J.TypeParameter::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(typeParameter, J.TypeParameter::getMarkers, ctx::sendMarkers);
ctx.sendNodes(typeParameter, J.TypeParameter::getAnnotations, ctx::sendTree, Tree::getId);
ctx.sendNodes(typeParameter, J.TypeParameter::getModifiers, this::sendModifier, Tree::getId);
ctx.sendNodes(typeParameter, J.TypeParameter::getModifiers, ctx::sendTree, Tree::getId);
ctx.sendNode(typeParameter, J.TypeParameter::getName, ctx::sendTree);
ctx.sendNode(typeParameter, e -> e.getPadding().getBounds(), JavaScriptSender::sendContainer);
return typeParameter;
Expand Down Expand Up @@ -1394,7 +1396,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations v
ctx.sendNode(variableDeclarations, J.VariableDeclarations::getPrefix, JavaScriptSender::sendSpace);
ctx.sendNode(variableDeclarations, J.VariableDeclarations::getMarkers, ctx::sendMarkers);
ctx.sendNodes(variableDeclarations, J.VariableDeclarations::getLeadingAnnotations, ctx::sendTree, Tree::getId);
ctx.sendNodes(variableDeclarations, J.VariableDeclarations::getModifiers, this::sendModifier, Tree::getId);
ctx.sendNodes(variableDeclarations, J.VariableDeclarations::getModifiers, ctx::sendTree, Tree::getId);
ctx.sendNode(variableDeclarations, J.VariableDeclarations::getTypeExpression, ctx::sendTree);
ctx.sendNode(variableDeclarations, J.VariableDeclarations::getVarargs, JavaScriptSender::sendSpace);
ctx.sendNodes(variableDeclarations, J.VariableDeclarations::getDimensionsBeforeName, JavaScriptSender::sendLeftPadded, Function.identity());
Expand Down
Loading

0 comments on commit 1fca27c

Please sign in to comment.