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.

Parameters:

  • slice (String)

    The source text for this node

  • location (Location, #start_line)

    Location object or anything responding to start_line/end_line



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)

Returns The location of this node in source.

Returns:

  • (Location)

    The location of this node in source



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

def location
  @location
end

#sliceString (readonly)

Returns The source text for this node.

Returns:

  • (String)

    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

#childrenArray<AstNode>

Returns Child nodes (empty for leaf nodes).

Returns:

  • (Array<AstNode>)

    Child nodes (empty for leaf nodes)



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

def children
  []
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    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_contentString

Returns Normalized content for signature comparison.

Returns:

  • (String)

    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

Returns:

  • (Boolean)

    true



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.

Returns:

  • (Array)

    Signature array for matching



55
56
57
# File 'lib/ast/merge/ast_node.rb', line 55

def signature
  [self.class.name, normalized_content]
end

#to_sString

Returns The source text.

Returns:

  • (String)

    The source text



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)

Returns:



76
77
78
# File 'lib/ast/merge/ast_node.rb', line 76

def unwrap
  self
end