Class: Ast::Merge::AstNode Abstract
- Inherits:
-
Object
- Object
- Ast::Merge::AstNode
- Defined in:
- lib/ast/merge/ast_node.rb
Overview
Base class for AST nodes in the ast-merge framework.
This provides a common API that works across different AST implementations (Prism, TreeSitter, custom comment nodes, etc.) enabling uniform handling in merge operations.
Subclasses should implement:
-
#slice - returns the source text for the node
-
#location - returns an object responding to start_line/end_line
-
#children - returns child nodes (empty array for leaf nodes)
-
#signature - returns a signature array for matching (optional, can use default)
Direct Known Subclasses
Defined Under Namespace
Classes: Location
Instance Attribute Summary collapse
-
#location ⇒ Location
readonly
The location of this node in source.
-
#slice ⇒ String
readonly
The source text for this node.
Instance Method Summary collapse
-
#children ⇒ Array<AstNode>
Child nodes (empty for leaf nodes).
-
#initialize(slice:, location:) ⇒ AstNode
constructor
Initialize a new AstNode.
-
#inspect ⇒ String
Human-readable representation.
-
#normalized_content ⇒ String
Normalized content for signature comparison.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Check if this node responds to the Prism-style location API.
-
#signature ⇒ Array
Generate a signature for this node for matching purposes.
-
#to_s ⇒ String
The source text.
-
#unwrap ⇒ AstNode
Support unwrap protocol (returns self for non-wrapper nodes).
Constructor Details
#initialize(slice:, location:) ⇒ AstNode
Initialize a new AstNode.
39 40 41 42 |
# File 'lib/ast/merge/ast_node.rb', line 39 def initialize(slice:, location:) @slice = slice @location = location end |
Instance Attribute Details
#location ⇒ Location (readonly)
Returns The location of this node in source.
30 31 32 |
# File 'lib/ast/merge/ast_node.rb', line 30 def location @location end |
#slice ⇒ String (readonly)
Returns The source text for this node.
33 34 35 |
# File 'lib/ast/merge/ast_node.rb', line 33 def slice @slice end |
Instance Method Details
#children ⇒ Array<AstNode>
Returns Child nodes (empty for leaf nodes).
45 46 47 |
# File 'lib/ast/merge/ast_node.rb', line 45 def children [] end |
#inspect ⇒ String
Returns Human-readable representation.
65 66 67 |
# File 'lib/ast/merge/ast_node.rb', line 65 def inspect "#<#{self.class.name} lines=#{location.start_line}..#{location.end_line} slice=#{slice.to_s[0..50].inspect}>" end |
#normalized_content ⇒ String
Returns Normalized content for signature comparison.
60 61 62 |
# File 'lib/ast/merge/ast_node.rb', line 60 def normalized_content slice.to_s.strip end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Check if this node responds to the Prism-style location API
82 83 84 |
# File 'lib/ast/merge/ast_node.rb', line 82 def respond_to_missing?(method, include_private = false) [:location, :slice].include?(method) || super end |
#signature ⇒ Array
Generate a signature for this node for matching purposes.
Override in subclasses for custom signature logic. Default returns the node class name and a normalized form of the slice.
55 56 57 |
# File 'lib/ast/merge/ast_node.rb', line 55 def signature [self.class.name, normalized_content] end |
#to_s ⇒ String
Returns The source text.
70 71 72 |
# File 'lib/ast/merge/ast_node.rb', line 70 def to_s slice.to_s end |
#unwrap ⇒ AstNode
Support unwrap protocol (returns self for non-wrapper nodes)
76 77 78 |
# File 'lib/ast/merge/ast_node.rb', line 76 def unwrap self end |