Skip to content

Catchup source map position in preserveFormat#18011

Merged
nicolo-ribaudo merged 2 commits into
babel:mainfrom
nicolo-ribaudo:preserve-format-fix-mapping-position
May 25, 2026
Merged

Catchup source map position in preserveFormat#18011
nicolo-ribaudo merged 2 commits into
babel:mainfrom
nicolo-ribaudo:preserve-format-fix-mapping-position

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

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

Consider the case where we strip types from var x: number = 1 in preserve format mode. Whenever we print a node, the generator automatically keeps the original source position up to date with the original node position.

This means that we would print the Identifier("x"), and then set the original source position to the space after number (because : number is part of the identifier node). Then, when printing =, we'd first print the necessary spaces to catch up with the correct location ( ). Before doing that, .append() would insert a mapping, thus mapping the space right after x in the generated code to the one right after number in the original code.

This PR changes two things:

  • when adding spaces for catching up, they do not cause a mapping to be injected
  • after adding spaces, we update the sourcePosition to actually point to where we catched up to thanks to the spaces

This change helps shaving multiple KBs off from the preserveFormat test cases in #18006, but it's also a correctness fix on its own.

@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label May 22, 2026
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented May 22, 2026

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 22, 2026

Open in StackBlitz

commit: da9d00c

@nicolo-ribaudo nicolo-ribaudo force-pushed the preserve-format-fix-mapping-position branch from f8b1470 to da9d00c Compare May 22, 2026 22:03
@nicolo-ribaudo nicolo-ribaudo merged commit 5e917ed into babel:main May 25, 2026
57 checks passed
@nicolo-ribaudo nicolo-ribaudo deleted the preserve-format-fix-mapping-position branch May 25, 2026 09:20
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.

4 participants