Class: Solargraph::Convention::StructDefinition::NodeProcessors::StructNode

Inherits:
Parser::NodeProcessor::Base show all
Defined in:
lib/solargraph/convention/struct_definition.rb

Instance Attribute Summary

Attributes inherited from Parser::NodeProcessor::Base

#locals, #node, #pins, #region

Instance Method Summary collapse

Methods inherited from Parser::NodeProcessor::Base

#initialize

Constructor Details

This class inherits a constructor from Solargraph::Parser::NodeProcessor::Base

Instance Method Details

#processBoolean

Returns continue processing the next processor of the same node.

Returns:

  • (Boolean)

    continue processing the next processor of the same node.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/solargraph/convention/struct_definition.rb', line 12

def process
  return true if struct_definition_node.nil?

  loc = get_node_location(node)
  nspin = Solargraph::Pin::Namespace.new(
    type: :class,
    location: loc,
    closure: region.closure,
    name: struct_definition_node.class_name,
    docstring: docstring,
    visibility: :public,
    gates: region.closure.gates.freeze,
    source: :struct_definition
  )
  pins.push nspin

  # define initialize method

  initialize_method_pin = Pin::Method.new(
    name: 'initialize',
    parameters: [],
    scope: :instance,
    location: get_node_location(node),
    closure: nspin,
    visibility: :private,
    docstring: docstring,
    source: :struct_definition
  )

  pins.push initialize_method_pin

  struct_definition_node.attributes.map do |attribute_node, attribute_name|
    initialize_method_pin.parameters.push(
      Pin::Parameter.new(
        name: attribute_name,
        decl: struct_definition_node.keyword_init? ? :kwarg : :arg,
        location: get_node_location(attribute_node),
        closure: initialize_method_pin,
        source: :struct_definition
      )
    )
  end

  # define attribute accessors and instance variables

  struct_definition_node.attributes.each do |attribute_node, attribute_name|
    [attribute_name, "#{attribute_name}="].each do |name|
      docs = docstring.tags.find { |t| t.tag_name == 'param' && t.name == attribute_name }

      attribute_type = ComplexType.parse(tag_string(docs))
      return_type_comment = attribute_comment(docs, false)
      param_comment = attribute_comment(docs, true)

      method_pin = Pin::Method.new(
        name: name,
        parameters: [],
        scope: :instance,
        location: get_node_location(attribute_node),
        closure: nspin,
        docstring: YARD::Docstring.new(return_type_comment),
        # even assignments return the value

        comments: return_type_comment,
        return_type: attribute_type,
        visibility: :public,
        source: :struct_definition
      )

      if name.end_with?('=')
        method_pin.parameters << Pin::Parameter.new(
          name: attribute_name,
          location: get_node_location(attribute_node),
          closure: method_pin,
          return_type: attribute_type,
          comments: param_comment,
          source: :struct_definition
        )

        pins.push Pin::InstanceVariable.new(name: "@#{attribute_name}",
                                            closure: method_pin,
                                            location: get_node_location(attribute_node),
                                            return_type: attribute_type,
                                            comments: "@type [#{attribute_type.rooted_tags}]",
                                            source: :struct_definition)
      end

      pins.push method_pin
    end
  end

  process_children region.update(closure: nspin, visibility: :public)
  false
end