Class: Bash::Merge::NodeWrapper
- Inherits:
-
Ast::Merge::NodeWrapperBase
- Object
- Ast::Merge::NodeWrapperBase
- Bash::Merge::NodeWrapper
- 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
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
Instance Method Summary collapse
-
#case_statement? ⇒ Boolean
Check if this is a case statement.
-
#command? ⇒ Boolean
Check if this is a command.
-
#command_name ⇒ String?
Get the command name if this is a command.
-
#comment? ⇒ Boolean
Check if this is a comment.
- #end_byte ⇒ Integer
-
#find_child_by_field(field_name) ⇒ TreeSitter::Node?
Find a child by field name.
-
#find_child_by_type(type_name) ⇒ TreeSitter::Node?
Find a child by type.
-
#for_statement? ⇒ Boolean
Check if this is a for loop.
-
#function_definition? ⇒ Boolean
Check if this is a function definition.
-
#function_name ⇒ String?
Get the function name if this is a function definition.
- #heredoc? ⇒ Boolean
-
#if_statement? ⇒ Boolean
Check if this is an if statement.
-
#pipeline? ⇒ Boolean
Check if this is a pipeline.
- #semantic_tree ⇒ Array[untyped]
- #source_text ⇒ String
- #start_byte ⇒ Integer
-
#variable_assignment? ⇒ Boolean
Check if this is a variable assignment.
-
#variable_name ⇒ String?
Get the variable name if this is a variable assignment.
-
#while_statement? ⇒ Boolean
Check if this is a while loop.
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
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
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
62 63 64 |
# File 'lib/bash/merge/node_wrapper.rb', line 62 def command? @node.type.to_s == 'command' end |
#command_name ⇒ String?
Get the command name if this is a command
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
74 75 76 |
# File 'lib/bash/merge/node_wrapper.rb', line 74 def comment? @node.type.to_s == 'comment' end |
#end_byte ⇒ 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
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
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
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
26 27 28 |
# File 'lib/bash/merge/node_wrapper.rb', line 26 def function_definition? @node.type.to_s == 'function_definition' end |
#function_name ⇒ String?
Get the function name if this is a function definition
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
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
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
68 69 70 |
# File 'lib/bash/merge/node_wrapper.rb', line 68 def pipeline? @node.type.to_s == 'pipeline' end |
#semantic_tree ⇒ Array[untyped]
116 117 118 |
# File 'lib/bash/merge/node_wrapper.rb', line 116 def semantic_tree semantic_node(node) end |
#source_text ⇒ String
114 |
# File 'lib/bash/merge/node_wrapper.rb', line 114 def source_text = @source.byteslice(start_byte...end_byte).to_s |
#start_byte ⇒ 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
32 33 34 |
# File 'lib/bash/merge/node_wrapper.rb', line 32 def variable_assignment? @node.type.to_s == 'variable_assignment' end |
#variable_name ⇒ String?
Get the variable name if this is a variable assignment
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
50 51 52 |
# File 'lib/bash/merge/node_wrapper.rb', line 50 def while_statement? @node.type.to_s == 'while_statement' end |