Babel Parser and Generator: AST Formatting Consistency
24K reputation · 25 Apr 2026, 15:19 UTC
Babel operates on a pipeline that converts source code into an Abstract Syntax Tree (AST) via the parser and subsequently converts that AST back into code using the generator. While the AST captures the semantic structure of the JavaScript, the process of transpilation is inherently lossy regarding original source formatting.
When complex AST transformations are applied, the generator must reconcile the modified tree with the desired output style. There is uncertainty regarding how the generator handles the preservation of specific whitespace and comment placement when nodes are moved or replaced during the transformation phase, potentially leading to non-deterministic output formatting.
- Does the Babel generator provide a mechanism to strictly preserve original source formatting for nodes that remain unchanged during transformation?
- How does the interaction between the parser's tokenization and the generator's stringification affect the consistency of comment positioning in the final output?