Sha256: 7106c399f50e902822ab1ea7b14314d677644e3243a9448a6dc2e334297d1b35

Contents?: true

Size: 1.85 KB

Versions: 15

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `array` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `array` nodes within RuboCop.
    class ArrayNode < Node
      PERCENT_LITERAL_TYPES = {
        string: /^%[wW]/,
        symbol: /^%[iI]/
      }.freeze

      # Returns an array of all value nodes in the `array` literal.
      #
      # @return [Array<Node>] an array of value nodes
      alias values children

      # @deprecated Use `values.each` (a.k.a. `children.each`)
      def each_value(&block)
        return to_enum(__method__) unless block_given?

        values.each(&block)

        self
      end

      # Checks whether the `array` literal is delimited by square brackets.
      #
      # @return [Boolean] whether the array is enclosed in square brackets
      def square_brackets?
        loc.begin&.is?('[')
      end

      # Checks whether the `array` literal is delimited by percent brackets.
      #
      # @overload percent_literal?
      #   Check for any percent literal.
      #
      # @overload percent_literal?(type)
      #   Check for percent literal of type `type`.
      #
      #   @param type [Symbol] an optional percent literal type
      #
      # @return [Boolean] whether the array is enclosed in percent brackets
      def percent_literal?(type = nil)
        if type
          loc.begin && loc.begin.source =~ PERCENT_LITERAL_TYPES[type]
        else
          loc.begin&.source&.start_with?('%')
        end
      end

      # Checks whether the `array` literal is delimited by either percent or
      # square brackets
      #
      # @return [Boolean] whether the array is enclosed in percent or square
      # brackets
      def bracketed?
        square_brackets? || percent_literal?
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/array_node.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/array_node.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/array_node.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/array_node.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/array_node.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.4.2/lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.6.0 lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.5.1 lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.5.0 lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.4.2 lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.4.1 lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.4.0 lib/rubocop/ast/node/array_node.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-ast-0.3.0/lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.3.0 lib/rubocop/ast/node/array_node.rb
rubocop-ast-0.2.0 lib/rubocop/ast/node/array_node.rb