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
|
# 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: /\A%[wW]/,
symbol: /\A%[iI]/
}.freeze
private_constant :PERCENT_LITERAL_TYPES
# Returns an array of all value nodes in the `array` literal.
#
# @return [Array<Node>] an array of value nodes
alias values children
# Calls the given block for each `value` node in the `array` literal.
# If no block is given, an `Enumerator` is returned.
#
# @return [self] if a block is given
# @return [Enumerator] if no block is given
def each_value(&block)
return to_enum(__method__) unless block
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_is?(:begin, '[')
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&.source&.match?(PERCENT_LITERAL_TYPES.fetch(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
|