Class: JsxRosetta::IR::Lowering

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

Overview

Lowers a parsed AST::File into an IR::Component tree.

Phase 2 scope:

- Single function-declaration component per file.
- JSX elements with lowercase tags lower to IR::Element; others to
  IR::ComponentInvocation.
- className attributes lower to IR::StyleBinding; everything else
  to IR::Attribute (event handlers like onClick are passed through
  as Attribute for now and will be re-lowered to EventBinding in
  a later phase).
- JS expressions are preserved as opaque source text via
  IR::Interpolation. No JS-to-Ruby translation.
- Pure-whitespace JSXText between elements is dropped (matches
  JSX runtime behavior); other text is preserved verbatim.

Phase 4a additions:

- {children} where `children` is a prop lowers to IR::Slot.
- {cond && X}, {cond ? X : null}, and {cond ? X : Y} lower to
  IR::Conditional. Other LogicalExpression operators (||, ??) are
  left as opaque interpolations.

Defined Under Namespace

Classes: LoweringError

Constant Summary collapse

REACT_HOOKS =
%w[
  useState useEffect useRef useContext useMemo useCallback
  useReducer useImperativeHandle useLayoutEffect useDebugValue
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Lowering

Returns a new instance of Lowering.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jsx_rosetta/ir/lowering.rb', line 69

def initialize(source)
  @source = source
  @prop_names = []
  @local_jsx = {}
  @local_bindings = []
  @local_arrows = {}
  @local_polymorphic_tags = {}
  @stimulus_methods = []
  @stimulus_seen_names = {}
  @react_hooks = []
end

Class Method Details

.lower(file, source:) ⇒ Object



56
57
58
# File 'lib/jsx_rosetta/ir/lowering.rb', line 56

def self.lower(file, source:)
  new(source).lower_file(file)
end

.lower_all(file, source:) ⇒ Object



60
61
62
# File 'lib/jsx_rosetta/ir/lowering.rb', line 60

def self.lower_all(file, source:)
  new(source).lower_all_components(file)
end

Instance Method Details

#lower_all_components(file) ⇒ Object



89
90
91
92
93
94
# File 'lib/jsx_rosetta/ir/lowering.rb', line 89

def lower_all_components(file)
  candidates = find_component_functions(file.program)
  raise lowering_error("no component function found in module") if candidates.empty?

  candidates.map { |name, function| lower_component(name, function) }
end

#lower_file(file) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/jsx_rosetta/ir/lowering.rb', line 81

def lower_file(file)
  candidates = find_component_functions(file.program)
  raise lowering_error("no component function found in module") if candidates.empty?

  name, function = candidates.first
  lower_component(name, function)
end