Class: Bash::Merge::NodeWrapper

Inherits:
Ast::Merge::NodeWrapperBase
  • Object
show all
Defined in:
lib/bash/merge/node_wrapper.rb,
sig/bash/merge.rbs

Overview

Wraps TreeHaver nodes with comment associations, line information, and signatures. This provides a unified interface for working with Bash AST nodes during merging.

Inherits common functionality from Ast::Merge::NodeWrapperBase:

  • Source context (lines, source, comments)
  • Line info extraction
  • Basic methods: #type, #type?, #text, #content, #signature

Examples:

Basic usage

parser = TreeHaver::Parser.new
parser.language = TreeHaver::Language.bash
tree = parser.parse(source)
wrapper = NodeWrapper.new(tree.root_node, lines: source.lines, source: source)
wrapper.signature # => [:program, ...]

See Also:

  • Ast::Merge::NodeWrapperBase

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.

Returns:

  • (Object)


22
23
24
# File 'lib/bash/merge/node_wrapper.rb', line 22

def node
  @node
end

Instance Method Details

#case_statement?Boolean

Check if this is a case statement

Returns:

  • (Boolean)


56
57
58
# File 'lib/bash/merge/node_wrapper.rb', line 56

def case_statement?
  @node.type.to_s == 'case_statement'
end

#command?Boolean

Check if this is a command

Returns:

  • (Boolean)


62
63
64
# File 'lib/bash/merge/node_wrapper.rb', line 62

def command?
  @node.type.to_s == 'command'
end

#command_nameString?

Get the command name if this is a command

Returns:

  • (String, nil)


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bash/merge/node_wrapper.rb', line 100

def command_name
  return unless command?

  # First child that is a word or simple_expansion
  @node.each do |child|
    next if %w[comment file_redirect heredoc_redirect].include?(child.type.to_s)

    return node_text(child) if %w[word command_name].include?(child.type.to_s)
  end
  nil
end

#comment?Boolean

Check if this is a comment

Returns:

  • (Boolean)


74
75
76
# File 'lib/bash/merge/node_wrapper.rb', line 74

def comment?
  @node.type.to_s == 'comment'
end

#end_byteInteger

Returns:

  • (Integer)


113
# File 'lib/bash/merge/node_wrapper.rb', line 113

def end_byte = node.end_byte

#find_child_by_field(field_name) ⇒ TreeSitter::Node?

Find a child by field name

Parameters:

  • field_name (String)

    Field name to look for

Returns:

  • (TreeSitter::Node, nil)


127
128
129
130
131
# File 'lib/bash/merge/node_wrapper.rb', line 127

def find_child_by_field(field_name)
  return unless @node.respond_to?(:child_by_field_name)

  @node.child_by_field_name(field_name)
end

#find_child_by_type(type_name) ⇒ TreeSitter::Node?

Find a child by type

Parameters:

  • type_name (String)

    Type name to look for

Returns:

  • (TreeSitter::Node, nil)


136
137
138
139
140
141
142
143
# File 'lib/bash/merge/node_wrapper.rb', line 136

def find_child_by_type(type_name)
  return unless @node.respond_to?(:each)

  @node.each do |child|
    return child if child.type.to_s == type_name
  end
  nil
end

#for_statement?Boolean

Check if this is a for loop

Returns:

  • (Boolean)


44
45
46
# File 'lib/bash/merge/node_wrapper.rb', line 44

def for_statement?
  %w[for_statement c_style_for_statement].include?(@node.type.to_s)
end

#function_definition?Boolean

Check if this is a function definition

Returns:

  • (Boolean)


26
27
28
# File 'lib/bash/merge/node_wrapper.rb', line 26

def function_definition?
  @node.type.to_s == 'function_definition'
end

#function_nameString?

Get the function name if this is a function definition

Returns:

  • (String, nil)


80
81
82
83
84
85
86
# File 'lib/bash/merge/node_wrapper.rb', line 80

def function_name
  return unless function_definition?

  # In bash tree-sitter, function name is in a 'name' or 'word' child
  name_node = find_child_by_type('word') || find_child_by_field('name')
  node_text(name_node) if name_node
end

#heredoc?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/bash/merge/node_wrapper.rb', line 120

def heredoc?
  descendant_type?(node, %w[heredoc_redirect heredoc_body])
end

#if_statement?Boolean

Check if this is an if statement

Returns:

  • (Boolean)


38
39
40
# File 'lib/bash/merge/node_wrapper.rb', line 38

def if_statement?
  @node.type.to_s == 'if_statement'
end

#pipeline?Boolean

Check if this is a pipeline

Returns:

  • (Boolean)


68
69
70
# File 'lib/bash/merge/node_wrapper.rb', line 68

def pipeline?
  @node.type.to_s == 'pipeline'
end

#semantic_treeArray[untyped]

Returns:

  • (Array[untyped])


116
117
118
# File 'lib/bash/merge/node_wrapper.rb', line 116

def semantic_tree
  semantic_node(node)
end

#source_textString

Returns:

  • (String)


114
# File 'lib/bash/merge/node_wrapper.rb', line 114

def source_text = @source.byteslice(start_byte...end_byte).to_s

#start_byteInteger

Returns:

  • (Integer)


112
# File 'lib/bash/merge/node_wrapper.rb', line 112

def start_byte = node.start_byte

#variable_assignment?Boolean

Check if this is a variable assignment

Returns:

  • (Boolean)


32
33
34
# File 'lib/bash/merge/node_wrapper.rb', line 32

def variable_assignment?
  @node.type.to_s == 'variable_assignment'
end

#variable_nameString?

Get the variable name if this is a variable assignment

Returns:

  • (String, nil)


90
91
92
93
94
95
96
# File 'lib/bash/merge/node_wrapper.rb', line 90

def variable_name
  return unless variable_assignment?

  # In bash tree-sitter, variable name is a child of type 'variable_name'
  name_node = find_child_by_type('variable_name')
  node_text(name_node) if name_node
end

#while_statement?Boolean

Check if this is a while loop

Returns:

  • (Boolean)


50
51
52
# File 'lib/bash/merge/node_wrapper.rb', line 50

def while_statement?
  @node.type.to_s == 'while_statement'
end