File: in_pattern_node.rb

package info (click to toggle)
ruby-rubocop-ast 1.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,256 kB
  • sloc: ruby: 15,071; yacc: 90; makefile: 9
file content (38 lines) | stat: -rw-r--r-- 1,025 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
# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `in` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `in` nodes within RuboCop.
    class InPatternNode < Node
      # Returns a node of the pattern in the `in` branch.
      #
      # @return [Node] a pattern node
      def pattern
        node_parts.first
      end

      # Returns the index of the `in` branch within the `case` statement.
      #
      # @return [Integer] the index of the `in` branch
      def branch_index
        parent.in_pattern_branches.index(self)
      end

      # Checks whether the `in` node has a `then` keyword.
      #
      # @return [Boolean] whether the `in` node has a `then` keyword
      def then?
        loc.begin&.is?('then')
      end

      # Returns the body of the `in` node.
      #
      # @return [Node, nil] the body of the `in` node
      def body
        node_parts[-1]
      end
    end
  end
end