Class: ASTTransform::KwargsBuilder

Inherits:
Prism::Translation::Parser::Builder
  • Object
show all
Defined in:
lib/ast_transform/kwargs_builder.rb

Overview

Extends the default Prism parser builder to distinguish keyword arguments from hash literals in the AST.

The upstream builder always emits :hash nodes for both foo(bar: 1) and foo({ bar: 1 }). Unparser uses the node type to decide whether to emit braces: :hash gets {}, :kwargs does not. Since Ruby 3.0+ treats these as semantically different (strict keyword/positional separation), we need the AST to preserve the distinction.

NOTE: parsed nodes deliberately stay plain Parser::AST::Node. Custom node classes exist only for registered custom types (see ASTTransform::Node), which are IR and never reach Unparser: AST::Node#eql? compares class, and Unparser verifies dynamic-string emission by re-parsing and comparing eql? against the freshly parsed (plain-class) node — custom-class nodes of standard types would fail that verification.

Instance Method Summary collapse

Instance Method Details

#associate(begin_t, pairs, end_t) ⇒ Object



17
18
19
20
21
22
# File 'lib/ast_transform/kwargs_builder.rb', line 17

def associate(begin_t, pairs, end_t)
  node = super
  return node unless begin_t.nil? && end_t.nil?

  node.updated(:kwargs)
end