File: parameterized_node.rb

package info (click to toggle)
ruby-rubocop-ast 0.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 892 kB
  • sloc: ruby: 10,886; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 3,709 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

module RuboCop
  module AST
    # Requires implementing `arguments`.
    #
    # Common functionality for nodes that are parameterized:
    # `send`, `super`, `zsuper`, `def`, `defs`
    # and (modern only): `index`, `indexasgn`, `lambda`
    module ParameterizedNode
      # Checks whether this node's arguments are wrapped in parentheses.
      #
      # @return [Boolean] whether this node's arguments are
      #                   wrapped in parentheses
      def parenthesized?
        loc.end&.is?(')')
      end

      # A shorthand for getting the first argument of the node.
      # Equivalent to `arguments.first`.
      #
      # @return [Node, nil] the first argument of the node,
      #                     or `nil` if there are no arguments
      def first_argument
        arguments[0]
      end

      # A shorthand for getting the last argument of the node.
      # Equivalent to `arguments.last`.
      #
      # @return [Node, nil] the last argument of the node,
      #                     or `nil` if there are no arguments
      def last_argument
        arguments[-1]
      end

      # Checks whether this node has any arguments.
      #
      # @return [Boolean] whether this node has any arguments
      def arguments?
        !arguments.empty?
      end

      # Checks whether any argument of the node is a splat
      # argument, i.e. `*splat`.
      #
      # @return [Boolean] whether the node is a splat argument
      def splat_argument?
        arguments? &&
          (arguments.any?(&:splat_type?) || arguments.any?(&:restarg_type?))
      end
      alias rest_argument? splat_argument?

      # Whether the last argument of the node is a block pass,
      # i.e. `&block`.
      #
      # @return [Boolean] whether the last argument of the node is a block pass
      def block_argument?
        arguments? &&
          (last_argument.block_pass_type? || last_argument.blockarg_type?)
      end

      # A specialized `ParameterizedNode` for node that have a single child
      # containing either `nil`, an argument, or a `begin` node with all the
      # arguments
      module WrappedArguments
        include ParameterizedNode
        # @return [Array] The arguments of the node.
        def arguments
          first = children.first
          if first&.begin_type?
            first.children
          else
            children
          end
        end
      end

      # A specialized `ParameterizedNode`.
      # Requires implementing `first_argument_index`
      # Implements `arguments` as `children[first_argument_index..-1]`
      # and optimizes other calls
      module RestArguments
        include ParameterizedNode
        # @return [Array] arguments, if any
        def arguments
          children[first_argument_index..-1]
        end

        # A shorthand for getting the first argument of the node.
        # Equivalent to `arguments.first`.
        #
        # @return [Node, nil] the first argument of the node,
        #                     or `nil` if there are no arguments
        def first_argument
          children[first_argument_index]
        end

        # A shorthand for getting the last argument of the node.
        # Equivalent to `arguments.last`.
        #
        # @return [Node, nil] the last argument of the node,
        #                     or `nil` if there are no arguments
        def last_argument
          children[-1] if arguments?
        end

        # Checks whether this node has any arguments.
        #
        # @return [Boolean] whether this node has any arguments
        def arguments?
          children.size > first_argument_index
        end
      end
    end
  end
end