File: if_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 (168 lines) | stat: -rw-r--r-- 5,210 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `if` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `if` nodes within RuboCop.
    class IfNode < Node
      include ConditionalNode
      include ModifierNode

      # Checks whether this node is an `if` statement. (This is not true of
      # ternary operators and `unless` statements.)
      #
      # @return [Boolean] whether the node is an `if` statement
      def if?
        keyword == 'if'
      end

      # Checks whether this node is an `unless` statement. (This is not true
      # of ternary operators and `if` statements.)
      #
      # @return [Boolean] whether the node is an `unless` statement
      def unless?
        keyword == 'unless'
      end

      # Checks whether the `if` is an `elsif`. Parser handles these by nesting
      # `if` nodes in the `else` branch.
      #
      # @return [Boolean] whether the node is an `elsif`
      def elsif?
        keyword == 'elsif'
      end

      # Checks whether the `if` node has an `else` clause.
      #
      # @note This returns `true` for nodes containing an `elsif` clause.
      #       This is legacy behavior, and many cops rely on it.
      #
      # @return [Boolean] whether the node has an `else` clause
      def else?
        loc.respond_to?(:else) && loc.else
      end

      # Checks whether the `if` node is a ternary operator.
      #
      # @return [Boolean] whether the `if` node is a ternary operator
      def ternary?
        loc.respond_to?(:question)
      end

      # Returns the keyword of the `if` statement as a string. Returns an empty
      # string for ternary operators.
      #
      # @return [String] the keyword of the `if` statement
      def keyword
        ternary? ? '' : loc.keyword.source
      end

      # Returns the inverse keyword of the `if` node as a string. Returns `if`
      # for `unless` nodes and vice versa. Returns an empty string for ternary
      # operators.
      #
      # @return [String] the inverse keyword of the `if` statement
      def inverse_keyword
        case keyword
        when 'if' then 'unless'
        when 'unless' then 'if'
        else
          ''
        end
      end

      # Checks whether the `if` node is in a modifier form, i.e. a condition
      # trailing behind an expression. Only `if` and `unless` nodes without
      # other branches can be modifiers.
      #
      # @return [Boolean] whether the `if` node is a modifier
      def modifier_form?
        (if? || unless?) && super
      end

      # Chacks whether the `if` node has nested `if` nodes in any of its
      # branches.
      #
      # @note This performs a shallow search.
      #
      # @return [Boolean] whether the `if` node contains nested conditionals
      def nested_conditional?
        node_parts[1..2].compact.each do |branch|
          branch.each_node(:if) do |nested|
            return true unless nested.elsif?
          end
        end

        false
      end

      # Checks whether the `if` node has at least one `elsif` branch. Returns
      # true if this `if` node itself is an `elsif`.
      #
      # @return [Boolean] whether the `if` node has at least one `elsif` branch
      def elsif_conditional?
        else_branch&.if_type? && else_branch&.elsif?
      end

      # Returns the branch of the `if` node that gets evaluated when its
      # condition is truthy.
      #
      # @note This is normalized for `unless` nodes.
      #
      # @return [Node] the truthy branch node of the `if` node
      # @return [nil] if the truthy branch is empty
      def if_branch
        node_parts[1]
      end

      # Returns the branch of the `if` node that gets evaluated when its
      # condition is falsey.
      #
      # @note This is normalized for `unless` nodes.
      #
      # @return [Node] the falsey branch node of the `if` node
      # @return [nil] when there is no else branch
      def else_branch
        node_parts[2]
      end

      # Custom destructuring method. This is used to normalize the branches
      # for `if` and `unless` nodes, to aid comparisons and conversions.
      #
      # @return [Array<Node>] the different parts of the `if` statement
      def node_parts
        if unless?
          condition, false_branch, true_branch = *self
        else
          condition, true_branch, false_branch = *self
        end

        [condition, true_branch, false_branch]
      end

      # Returns an array of all the branches in the conditional statement.
      #
      # @return [Array<Node>] an array of branch nodes
      def branches
        branches = [if_branch]

        return branches unless else?

        other_branches = if elsif_conditional?
                           else_branch.branches
                         else
                           [else_branch]
                         end
        branches.concat(other_branches)
      end

      # @deprecated Use `branches.each`
      def each_branch(&block)
        return branches.to_enum(__method__) unless block_given?

        branches.each(&block)
      end
    end
  end
end