Skip to content

Conversation

@greg-at-moderne
Copy link
Contributor

@greg-at-moderne greg-at-moderne commented May 22, 2025

What's changed?

Fixing parsing of method and other function invocations in Javascript. Currently console.log("something") gets parsed as MethodInvocation(select=FieldAccess(console.log), name="") which is understandably not desired.

The design decision has been to:

  • parse "normal" method invocations (with proper select and proper name) as J.MethodInvocation
  • and any other function calls as a new type - called JS.FunctionCall

What's your motivation?

Avoiding wrong parsing. And providing reasonable level of compatibility with Java parsing.

@knutwannheden
Copy link
Contributor

@greg-at-moderne Did we decide what to do about this?

@greg-at-moderne
Copy link
Contributor Author

We have not, forgot about this one. It was several weeks ago.

@greg-at-moderne greg-at-moderne marked this pull request as ready for review June 18, 2025 16:50
@greg-at-moderne greg-at-moderne marked this pull request as draft June 24, 2025 07:58
@greg-at-moderne greg-at-moderne marked this pull request as ready for review June 24, 2025 11:27
Copy link
Contributor

@knutwannheden knutwannheden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Left a few small comments.

if (functionCall.function) {
await this.visitRightPadded(functionCall.function, p);
if (functionCall.function.element.markers.markers.find(m => m.kind === JS.Markers.Optional)) {
p.append("?.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we could detangle this a bit and have the ? be printed by the presence of the marker and then only here print the .. Would that make sense?

Copy link
Contributor Author

@greg-at-moderne greg-at-moderne Jun 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it like you suggest before in my code, but then the Space handling is wrong:

- const result0 = func/*a*/?./*b*/("TS");
+ const result0 = func?/*a*/./*b*/("TS");

For that to work, the /*a*/ comment would need to land as Optional.prefix.
That in turn would make it inconsistent with how the /*a*/ comment is placed in other similar syntaxes.
See call.test.ts / with optional chaining operator.

I suggest to keep it like it is. After all the ?. is a single token with its own meaning, thus it feels OK to print it in one go.
Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. In that case I think it probably makes sense to keep it this way, unless we want to add some kind a dedicated optionalChaining property to the JS.FunctionCall type. That would then however require parsing any calls using ?. into a JS.FunctionCall.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

github-actions[bot]

This comment was marked as outdated.

@greg-at-moderne greg-at-moderne marked this pull request as draft June 26, 2025 10:33
@greg-at-moderne greg-at-moderne marked this pull request as ready for review June 27, 2025 11:40
@greg-at-moderne greg-at-moderne merged commit 9b41310 into main Jul 2, 2025
2 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Jul 2, 2025
@greg-at-moderne greg-at-moderne deleted the greg-parsing-method-invocation branch July 2, 2025 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants