Class: JsxRosetta::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsx_rosetta/parser.rb

Overview

Public entry point for JSX → AST parsing.

Returns a typed AST::Node tree (rooted at AST::File) that mirrors the Babel AST shape with Ruby ergonomics: snake_case field accessors, source location preservation, traversal helpers, and pattern-matching support.

Instance Method Summary collapse

Constructor Details

#initialize(node_bridge: NodeBridge.new) ⇒ Parser

Returns a new instance of Parser.



15
16
17
# File 'lib/jsx_rosetta/parser.rb', line 15

def initialize(node_bridge: NodeBridge.new)
  @node_bridge = node_bridge
end

Instance Method Details

#parse(source, typescript: false, source_filename: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/jsx_rosetta/parser.rb', line 19

def parse(source, typescript: false, source_filename: nil)
  response = @node_bridge.parse(source, typescript: typescript, source_filename: source_filename)

  if response["ok"]
    AST.build(response["ast"])
  else
    error = response["error"] || {}
    raise ParseError.new(error["message"] || "JSX parse failed", line: error["line"], column: error["column"])
  end
end