Class: Bash::Merge::NodeWrapper

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

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 Method Summary collapse

Instance Method Details

#case_statement?Boolean

Check if this is a case statement

Returns:

  • (Boolean)


54
55
56
# File 'lib/bash/merge/node_wrapper.rb', line 54

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

#command?Boolean

Check if this is a command

Returns:

  • (Boolean)


60
61
62
# File 'lib/bash/merge/node_wrapper.rb', line 60

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

#command_nameString?

Get the command name if this is a command

Returns:

  • (String, nil)


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

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)


72
73
74
# File 'lib/bash/merge/node_wrapper.rb', line 72

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

#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)


113
114
115
116
117
# File 'lib/bash/merge/node_wrapper.rb', line 113

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)


122
123
124
125
126
127
128
129
# File 'lib/bash/merge/node_wrapper.rb', line 122

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)


42
43
44
# File 'lib/bash/merge/node_wrapper.rb', line 42

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)


24
25
26
# File 'lib/bash/merge/node_wrapper.rb', line 24

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)


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

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

#if_statement?Boolean

Check if this is an if statement

Returns:

  • (Boolean)


36
37
38
# File 'lib/bash/merge/node_wrapper.rb', line 36

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

#pipeline?Boolean

Check if this is a pipeline

Returns:

  • (Boolean)


66
67
68
# File 'lib/bash/merge/node_wrapper.rb', line 66

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

#variable_assignment?Boolean

Check if this is a variable assignment

Returns:

  • (Boolean)


30
31
32
# File 'lib/bash/merge/node_wrapper.rb', line 30

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)


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

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)


48
49
50
# File 'lib/bash/merge/node_wrapper.rb', line 48

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