Visitar URL original
Fix mapping of tokens with generated nodes in between by nicolo-ribaudo · Pull Request #16948 · babel/babel · GitHub
Skip to content

Fix mapping of tokens with generated nodes in between - #16948

Merged
nicolo-ribaudo merged 2 commits into
babel:mainfrom
nicolo-ribaudo:preserve-format-middle-node-injection
Oct 30, 2024
Merged

nicolo-ribaudo merged 2 commits into
babel:mainfrom
nicolo-ribaudo:preserve-format-middle-node-injection

Conversation

@nicolo-ribaudo

Copy link
Copy Markdown
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
Copy Markdown
Collaborator

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

@kriskowal kriskowal left a comment

Copy link
Copy Markdown
Contributor

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;

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
@nicolo-ribaudo
nicolo-ribaudo deleted the preserve-format-middle-node-injection branch October 30, 2024 16:29
@nicolo-ribaudo

Copy link
Copy Markdown
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

fisker commented Oct 31, 2024 •

Copy link
Copy Markdown
Contributor

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

@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 31, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jan 31, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue 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