Skip to content

Fix ChangeType UOE on JS.ComputedPropertyMethodDeclaration#7418

Draft
knutwannheden wants to merge 1 commit intomainfrom
fix-changetype-uoe-on-js-computedpropertymethoddeclaration
Draft

Fix ChangeType UOE on JS.ComputedPropertyMethodDeclaration#7418
knutwannheden wants to merge 1 commit intomainfrom
fix-changetype-uoe-on-js-computedpropertymethoddeclaration

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

Motivation

ChangeType.postVisit crashes with UnsupportedOperationException when it encounters JS.ComputedPropertyMethodDeclaration:

java.lang.UnsupportedOperationException: To change the return type of this method declaration, use withMethodType(..)
  org.openrewrite.javascript.tree.JS$ComputedPropertyMethodDeclaration.withType(JS.java:3899)
  org.openrewrite.java.ChangeType$JavaChangeTypeVisitor.postVisit(ChangeType.java:229)

This is the third variant of the same underlying issue:

Observed volume: ~13 per-file errors/day on production runs of NoGuavaPredicate (a sub-recipe of "Java best practices") against TypeScript repositories.

Summary

  • New interface org.openrewrite.java.tree.MethodDeclarationLike (parallel to MethodCall) exposes getMethodType() / withMethodType(..).
  • J.MethodDeclaration and JS.ComputedPropertyMethodDeclaration now implement it (signatures were already there).
  • ChangeType.postVisit has a new branch for MethodDeclarationLike, placed after the MethodCall branch and before the generic TypedTree fallback.
  • New test ChangeTypeAdaptabilityTest#computedPropertyMethodDeclarationDoesNotThrow exercises the path end-to-end.

Known follow-up

K.SpreadArgument and K.StatementExpression also throw UOE from withType(..), but their semantics ("cannot be changed directly" / "cannot have a type") do not fit MethodDeclarationLike. Those are not addressed here and would need separate handling.

Test plan

  • New ChangeTypeAdaptabilityTest#computedPropertyMethodDeclarationDoesNotThrow reproduces the UOE on main and passes on this branch.
  • ./gradlew :rewrite-java-test:test --tests org.openrewrite.java.ChangeTypeTest — 81 tests, all pass.
  • ./gradlew :rewrite-javascript:integTest --tests org.openrewrite.javascript.ChangeTypeAdaptabilityTest — 2 tests, all pass.
  • ./gradlew assemble — clean build across all modules.
  • ./gradlew :rewrite-javascript:test — green.

`ChangeType.postVisit` walks `TypedTree` and calls `withType(..)`. For
method-declaration-shaped nodes that redirect the return type through
`withMethodType(..)`, `withType(..)` throws `UnsupportedOperationException`.
`J.MethodDeclaration` had a dedicated branch, but `JS.ComputedPropertyMethodDeclaration`
(`JS, Statement, TypedTree`) fell through to the generic `TypedTree` branch
and crashed.

Introduce `MethodDeclarationLike` (parallel to `MethodCall`) implemented by
`J.MethodDeclaration` and `JS.ComputedPropertyMethodDeclaration`, and add a
catch-all branch in `ChangeType.postVisit` before the `TypedTree` fallback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant