Conversation
Oxc::MutationVisitor to rewrite what it walkedOxc::MutationVisitor to rewrite the source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on the visitor, and gives it a way to change what it walked.
replace,remove,insert_before,insert_afterandwrapeach take a node. Edits are collected during the walk and spliced into the original text at the end, so everything untouched survives byte for byte, comments and indentation included. Spans are exact, so removingdebugger;removes what the node covered and leaves the newline after it alone.There is no AST to hand back. oxc's ESTree output is a projection of its Rust AST, not a serialization of it, so nothing can turn one back into the other. Splicing the source is also what the JavaScript ecosystem settled on for the same reason, since reprinting a whole file loses everything the author wrote.
Two things that make it safe
Replacing a node stops descent into it. Walking into what was just replaced would edit text that is about to disappear, and descending is exactly what a visitor does next, so
visit_childrenanswers without doing anything there. Two edits over the same span raiseOxc::MutationVisitor::Overlapinstead of quietly producing something broken.What goes in is text, so a node can become anything, including several statements or nothing at all. Nothing checks it on the way, so the result is read back afterwards and refused if it stopped being JavaScript.
verify: falseskips that for a fragment that was never going to parse on its own.Driving from symbols
parsedreaches what the source parsed to, so a rewrite can ask for symbols and work from references.Rewriting every
Identifiernamedcountalso rewrites the declaration, a shadowed local, and a same-named property. Working from a symbol's own references does not, because oxc reports a shadowed binding as a separate symbol with its own references.