8000 Fixing parsing of method invocations in Javascript by greg-at-moderne · Pull Request #5484 · openrewrite/rewrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixing parsing of method invocations in Javascript #5484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from

Conversation

greg-at-modern
8000
e
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?

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
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.

2 participants
0