Conversation
- codegen: AST map → JavaScript source via OXC's Codegen - bind: substitute $placeholders in parsed AST - Rust NIF reads BEAM terms directly (no serde roundtrip) - Supports all ESTree node types: statements, expressions, declarations, patterns, modules, classes, template literals
…EADME
bind/2 now supports:
- {:expr, "ref(0)"} — parse JS expression
- {:literal, %{...}} — recursive map → JS object
- {:literal, [...]} — recursive list → JS array
splice/3 replaces $placeholder statements/properties/elements:
- Statement-level: $actions → list of statements
- Property-level: {$fields} → list of object properties
- Element-level: [$items] → list of array elements
- Accepts strings (auto-parsed) or raw AST nodes
README updated with codegen, bind, splice, and lint sections.
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.
Adds the ability to generate JavaScript source from AST maps, closing the parse↔codegen roundtrip. Three new functions:
codegen/1— AST → JavaScriptRust NIF that reads BEAM terms directly (no serde intermediary), reconstructs OXC's arena-allocated AST via
AstBuilder, and runsoxc_codegenfor proper operator precedence, formatting, and semicolons.Roundtrips with
parse:bind/2— substitute$placeholdersReplaces
$nameidentifiers in a parsed AST. Supports renaming, literals (including recursive maps/lists → JS objects/arrays), parsed expressions, and raw AST nodes."count"{:literal, 42}{:literal, %{a: 1}}{:literal, [1, 2]}{:expr, "ref(0)"}%{type: ...}splice/3— expand$placeholderinto multiple nodesReplaces a single
$placeholderstatement, object property, or array element with a list of nodes. Strings are auto-parsed as JS.Works in three contexts:
$actionsin a block/body → list of statements{$fields}in an object → list of properties[$items]in an array → list of elementsPassing
[]removes the placeholder entirely.Implementation
native/oxc_ex_nif/src/codegen.rs— 1069-line Rust NIF that converts BEAM terms → OXC AST usingAstBuilder→oxc_codegen. Reads terms directly via Rustler'sTerm::map_getwith pre-declared atoms, no serialization overhead.bind/2andsplice/3are pure Elixir using the existingpostwalk/2.