Skip to content

Implement Oxc::MutationVisitor to rewrite the source - #4

Merged
marcoroth merged 1 commit into
mainfrom
rewriter
Aug 25, 2026
Merged

Implement Oxc::MutationVisitor to rewrite the source#4
marcoroth merged 1 commit into
mainfrom
rewriter

Conversation

@marcoroth

@marcoroth marcoroth commented Aug 25, 2026

Copy link
Copy Markdown
Owner

Builds on the visitor, and gives it a way to change what it walked.

class Renamer < Oxc::MutationVisitor
  def visit_identifier(node)
    replace(node, "renamed") if node["name"] == "count"
  end
end

Renamer.new.rewrite("let count = 1 // keep me")
#=> "let renamed = 1 // keep me"

replace, remove, insert_before, insert_after and wrap each 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 removing debugger; 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_children answers without doing anything there. Two edits over the same span raise Oxc::MutationVisitor::Overlap instead 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.

Breaker.new.rewrite("foo(data)")
#=> Oxc::MutationVisitor::Invalid: what was rewritten no longer reads as JavaScript: Unexpected token

verify: false skips that for a fragment that was never going to parse on its own.

Driving from symbols

parsed reaches what the source parsed to, so a rewrite can ask for symbols and work from references.

class ToState < Oxc::MutationVisitor
  def rewrite(source) = super(source, symbols: true)

  def visit_identifier(node)
    reference = parsed.symbols.fetch("declared")
                              .flat_map { |symbol| symbol["references"] }
                              .find { |found| found["start"] == node.start }

    replace(node, %(state.get("#{node["name"]}"))) if reference && !reference["write"]
  end
end

Rewriting every Identifier named count also 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.

@marcoroth marcoroth changed the title Implement Oxc::MutationVisitor to rewrite what it walked Implement Oxc::MutationVisitor to rewrite the source Aug 25, 2026
@marcoroth
marcoroth merged commit f8f8f5f into main Aug 25, 2026
11 checks passed
@marcoroth
marcoroth deleted the rewriter branch August 25, 2026 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant