Class: Ast::Merge::AstNode Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/ast_node.rb

Overview

This class is abstract.

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

Comment::Block, Comment::Empty, Comment::Line

Defined Under Namespace

Classes: Location

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#locationLocation (readonly)



30
31
32
# File 'lib/ast/merge/ast_node.rb', line 30

def location
  @location
end

#sliceString (readonly)



33
34
35
# File 'lib/ast/merge/ast_node.rb', line 33

def slice
  @slice
end

Instance Method Details

#childrenArray<AstNode>



45
46
47
# File 'lib/ast/merge/ast_node.rb', line 45

def children
  []
end

#inspectString



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_contentString



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

#signatureArray

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_sString



70
71
72
# File 'lib/ast/merge/ast_node.rb', line 70

def to_s
  slice.to_s
end

#unwrapAstNode

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