File: binary_operator_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 (43 lines) | stat: -rw-r--r-- 1,217 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module RuboCop
  module AST
    # Common functionality for nodes that are binary operations:
    # `or`, `and` ...
    module BinaryOperatorNode
      # Returns the left hand side node of the binary operation.
      #
      # @return [Node] the left hand side of the binary operation
      def lhs
        node_parts[0]
      end

      # Returns the right hand side node of the binary operation.
      #
      # @return [Node] the right hand side of the binary operation
      def rhs
        node_parts[1]
      end

      # Returns all of the conditions, including nested conditions,
      # of the binary operation.
      #
      # @return [Array<Node>] the left and right hand side of the binary
      # operation and the let and right hand side of any nested binary
      # operators
      def conditions
        lhs, rhs = *self
        lhs = lhs.children.first if lhs.begin_type?
        rhs = rhs.children.first if rhs.begin_type?

        [lhs, rhs].each_with_object([]) do |side, collection|
          if side.operator_keyword?
            collection.concat(side.conditions)
          else
            collection << side
          end
        end
      end
    end
  end
end