Class: Go::Merge::NodeWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/go/merge/node_wrapper.rb,
sig/go/merge.rbs

Overview

Structural identity and source-range adapter for a top-level Go AST node. rubocop:disable Metrics/AbcSize, Metrics/ClassLength, Metrics/CyclomaticComplexity, Metrics/MethodLength -- Go declaration forms share one structural identity boundary

Constant Summary collapse

DECLARATION_TYPES =

Returns:

  • (Array[String])
%w[
  const_declaration
  function_declaration
  import_declaration
  method_declaration
  package_clause
  type_declaration
  var_declaration
].freeze
GROUP_SPEC_TYPES =

Returns:

  • (Array[String])
%w[const_spec import_spec type_spec var_spec].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, source:, leading_comments: []) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.

Parameters:

  • node (Object)
  • source: (String)
  • leading_comments: (Array[untyped]) (defaults to: [])


21
22
23
24
25
26
# File 'lib/go/merge/node_wrapper.rb', line 21

def initialize(node, source:, leading_comments: [])
  @node = node
  @source = source
  @leading_comments = leading_comments.freeze
  @trailing_comments = []
end

Instance Attribute Details

#leading_commentsArray[untyped] (readonly)

Returns the value of attribute leading_comments.

Returns:

  • (Array[untyped])


19
20
21
# File 'lib/go/merge/node_wrapper.rb', line 19

def leading_comments
  @leading_comments
end

#nodeObject (readonly)

Returns the value of attribute node.

Returns:

  • (Object)


19
20
21
# File 'lib/go/merge/node_wrapper.rb', line 19

def node
  @node
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


19
20
21
# File 'lib/go/merge/node_wrapper.rb', line 19

def source
  @source
end

#trailing_commentsArray[untyped] (readonly)

Returns the value of attribute trailing_comments.

Returns:

  • (Array[untyped])


19
20
21
# File 'lib/go/merge/node_wrapper.rb', line 19

def trailing_comments
  @trailing_comments
end

Instance Method Details

#attach_trailing_comment!(comment) ⇒ Object

Parameters:

  • comment (Object)

Returns:

  • (Object)


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

def attach_trailing_comment!(comment)
  trailing_comments << comment
end

#end_byteInteger

Returns:

  • (Integer)


70
# File 'lib/go/merge/node_wrapper.rb', line 70

def end_byte = trailing_comments.last&.end_byte || node.end_byte

#end_lineInteger

Returns:

  • (Integer)


72
# File 'lib/go/merge/node_wrapper.rb', line 72

def end_line = line_for_byte(end_byte.zero? ? 0 : end_byte - 1)

#grouped?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/go/merge/node_wrapper.rb', line 49

def grouped?
  case node.type
  when 'import_declaration'
    !find_descendant(node, ['import_spec_list']).nil?
  when 'type_declaration', 'const_declaration'
    node.children.any? { |child| child.type == '(' }
  when 'var_declaration'
    !find_descendant(node, ['var_spec_list']).nil?
  else
    false
  end
end

#semantic_treeArray[untyped]

Returns:

  • (Array[untyped])


75
76
77
# File 'lib/go/merge/node_wrapper.rb', line 75

def semantic_tree
  semantic_node(node)
end

#signatureArray[untyped]

Returns:

  • (Array[untyped])


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/go/merge/node_wrapper.rb', line 28

def signature
  case node.type
  when 'package_clause'
    [:package, required_descendant_text(node, 'package_identifier')]
  when 'import_declaration'
    import_signature
  when 'function_declaration'
    [:function, required_direct_child_text(node, 'identifier')]
  when 'method_declaration'
    [:method, receiver_type_name, required_direct_child_text(node, 'field_identifier')]
  when 'type_declaration'
    declaration_signature(:type, 'type_spec', 'type_identifier')
  when 'const_declaration'
    declaration_signature(:const, 'const_spec', 'identifier')
  when 'var_declaration'
    declaration_signature(:var, 'var_spec', 'identifier')
  else
    raise ArgumentError, "Unsupported top-level Go node #{node.type.inspect}"
  end.freeze
end

#source_textString

Returns:

  • (String)


73
# File 'lib/go/merge/node_wrapper.rb', line 73

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

#start_byteInteger

Returns:

  • (Integer)


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

def start_byte
  leading_comments.first&.start_byte || node.start_byte
end

#start_lineInteger

Returns:

  • (Integer)


71
# File 'lib/go/merge/node_wrapper.rb', line 71

def start_line = line_for_byte(start_byte)