Skip to content
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

Fix mapping of tokens with generated nodes in between #16948

Merged

Conversation

nicolo-ribaudo
Copy link
Member

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

See the first commit to see the wrong output. The problem was that a null start was flowing to the binary search, failing both < and > comparisons, and thus considering it as "matched" and returning the middle point.

cc @kriskowal

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator labels Oct 30, 2024
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58292

Copy link

@kriskowal kriskowal left a comment

Choose a reason for hiding this comment

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

This is very likely the trouble I ran into, though I have not yet isolated. Thank you!

Comment on lines +161 to +162
if (child == null) continue;
if (child.start == null || child.end == null) continue;

Choose a reason for hiding this comment

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

Suggested change
if (child == null) continue;
if (child.start == null || child.end == null) continue;
if (child == null || child.start == null || child.end == null) continue;

Copy link
Member Author

Choose a reason for hiding this comment

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

Heheh unfortunately typescript-eslint complains that child == null || child.start == null should be rewritten to child?.start == null, but I find child?.start == null || child.end == null to be terrible so this two-lines solution was the compromise I reached with the linter :P

@nicolo-ribaudo nicolo-ribaudo merged commit ae77628 into babel:main Oct 30, 2024
54 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the preserve-format-middle-node-injection branch October 30, 2024 16:29
@nicolo-ribaudo
Copy link
Member Author

Whops, wrong commit message 😓 I'll at least rename the PR for the changelog

@nicolo-ribaudo nicolo-ribaudo changed the title Add test for current wrong behavior Fix mapping of tokens with generated nodes in between Oct 30, 2024
@fisker
Copy link
Contributor

fisker commented Oct 31, 2024

@nicolo-ribaudo

I tried to use experimental_preserveFormat in Prettier repo.
I feel the output for generated node can be improved.

Before this PR,

-__arrayFindLast(/* isOptionalObject */false, foo, callback);
+__arrayFindLast(/* isOptionalObject */false,foo,callback)

Space between arguments are removed, comma not printed.

See prettier/prettier#16804

After this PR, the output seems getting worse

- __at(/* isOptionalObject */true,foo?.bar.baz,-1)
+ __at           (/* isOptionalObject */true,foo?.bar.baz,-1)

See prettier/prettier#16811

The call expression is generated by this function https://github.com/prettier/prettier/blob/1abee7352fe98f8d484a0e813ebe531fffb8503e/scripts/build/transform/transforms/create-method-call-transform.js#L30

I'm not sure how this works, and not really care about the output that much, but I guess worth to point it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants