Repository navigation
Map Scala parameterless method calls to J.MethodInvocation - #9915
Conversation
|
This does require changes downstream in recipes link |
A qualified call to a method without an argument list, as xs.reverse, "abc".length or xs.sum, was a J.FieldAccess without a method type, so MethodMatcher and UsesMethod could not find it. It is now a J.MethodInvocation whose empty arguments carry OmitParentheses, with the method type instantiated as the typer applied it, which also types xs.sum as Int rather than its type parameter. Assignment targets and eta-expanded methods keep their current shape. Refs #9913
94c65f0 to
2e014bc
Compare
|
@greg-at-moderne this is the more opinionated LST structure change that needs more thorough review, where I might lack the knowledge of the Scala internals & semantics. I still think this improves how we map elements to what they represent, but it does mean we need to account for the change downstream as well. |
|
IMHO we should map method calls as |
greg-at-moderne
left a comment
There was a problem hiding this comment.
One open question stands out.
| class B { | ||
| val c = new Counter | ||
| c.count = 5 | ||
| val f: () => String = "abc".toUpperCase |
There was a problem hiding this comment.
This particular line I am not sure of whether it should be mapped as Method Invocation. This is similar to Object::toString method reference in Java. And this I am not sure how it's mapped (would need to check). I suggest to make sure this is mapped the same way in Scala (if possible).
There was a problem hiding this comment.
Good point. This line isn't mapped as a method invocation: the eta-expanded "abc".toUpperCase stays a J.FieldAccess with an EtaExpansion marker, as it was before this PR. I changed the test to assert that explicitly in 5940a56. Mapping eta-expansion like Java's method references would change all eta-expansions, not just parameterless ones, so I opened #9918 to follow up on that.
|
As discussed on Slack with Knut and Greg we'll go ahead with this change, as it's a clear fix, and make the downstream adjustments. |
… types in sync openrewrite/rewrite#9915 parses a typed call without parentheses, such as `5.seconds`, as a `J.MethodInvocation` whose arguments carry `OmitParentheses`. `ReplaceScalaPostfixDurations` now produces that instead of a `J.FieldAccess`, and matches typed units by `DurationConversions`, falling back to their names where the enclosing Gatling call leaves them untyped. The field access branch of `MigrateGatlingScalaDsl` stays for untyped sources, which are still parsed that way. As Scala calls into Gatling are now typed, the renamed calls get renamed method types, and `maxConnectionsPerHost` takes the `int` it is passed. `ignoreDefaultChecks` and `ignoreDefaultHeaders` are request builder methods, so the test now calls them on a request rather than on the protocol.
…Invocation (#10027) * Scala: map a parameterless method called by its bare name to J.MethodInvocation A call written without receiver or parentheses, as size inside a subclass of ArrayList or currentThread after a static import, was a J.Identifier typed with the method's return type, so ChangeMethodName and other MethodMatcher-based recipes never saw it. It is now a select-less J.MethodInvocation whose empty arguments carry OmitParentheses, as #9915 did for calls with a receiver. Fixes #10024 * Build both parameterless invocation shapes through one helper
Addresses the second part of #9913. Builds on #9914, which typed the method name of every Scala method invocation.
A qualified call to a method without an argument list, such as
xs.reverse,"abc".lengthorxs.sum, was parsed as aJ.FieldAccesswith no method type, soMethodMatcherandUsesMethodcould not find it. It is now aJ.MethodInvocationwhose empty argument list carriesOmitParentheses. Its method type is instantiated the way the compiler applied it, which also givesxs.sumthe typeIntinstead of its type parameterB. This also covers methods whose only parameter list is implicit (sum,sorted,flatten). Vals, fields, objects and packages are stillJ.FieldAccess, and so are the targets of setter assignments (c.count = 5) and methods the compiler eta-expands. Unqualified parameterless calls such as a barefooare still aJ.Identifier, which is out of scope here.This is a draft because it changes the tree shape that recipes see. Visitor recipes that recognise these calls in
visitFieldAccesswill silently stop matching until they are updated to handleJ.MethodInvocation, so those updates should be lined up before this merges.Verified with
./gradlew :rewrite-scala:test, where all 1117 tests pass.memberSelectionHasTypeandimplicitArgumentsPassedByTheCompilerAreMarkedwere updated for the new shape, and there are new tests for the issue's examples and for the cases that keep their shape.