File: conditional_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 (45 lines) | stat: -rw-r--r-- 1,447 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

module RuboCop
  module AST
    # Common functionality for nodes that have conditions:
    # `if`, `while`, `until`, `case`.
    # This currently doesn't include `when` nodes, because they have multiple
    # conditions, and need to be checked for that.
    module ConditionalNode
      # Checks whether the condition of the node is written on a single line.
      #
      # @return [Boolean] whether the condition is on a single line
      def single_line_condition?
        loc.keyword.line == condition.source_range.line
      end

      # Checks whether the condition of the node is written on more than
      # one line.
      #
      # @return [Boolean] whether the condition is on more than one line
      def multiline_condition?
        !single_line_condition?
      end

      # Returns the condition of the node. This works together with each node's
      # custom destructuring method to select the correct part of the node.
      #
      # @return [Node, nil] the condition of the node
      def condition
        node_parts[0]
      end

      # Returns the body associated with the condition. This works together with
      # each node's custom destructuring method to select the correct part of
      # the node.
      #
      # @note For `if` nodes, this is the truthy branch.
      #
      # @return [Node, nil] the body of the node
      def body
        node_parts[1]
      end
    end
  end
end