123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::PercentArray

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/percent_array.rb

Overview

Common functionality for handling percent arrays.

Instance Method Summary

Instance Method Details

#allowed_bracket_array?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 26

def allowed_bracket_array?(node)
  comments_in_array?(node) || below_array_length?(node) ||
    invalid_percent_array_context?(node)
end

#build_bracketed_array_with_appropriate_whitespace(elements:, node:) ⇒ String (private)

Parameters:

  • node (RuboCop::AST::ArrayNode)
  • elements (Array<String>)
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 83

def build_bracketed_array_with_appropriate_whitespace(elements:, node:)
  [
    '[',
    whitespace_leading(node),
    elements.join(",#{whitespace_between(node)}"),
    whitespace_trailing(node),
    ']'
  ].join
end

#build_message_for_bracketed_array(preferred_array_code) ⇒ String (private)

Parameters:

  • preferred_array_code (String)
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 56

def build_message_for_bracketed_array(preferred_array_code)
  format(
    self.class::ARRAY_MSG,
    prefer: if preferred_array_code.include?("\n")
              'an array literal `[...]`'
            else
              "`#{preferred_array_code}`"
            end
  )
end

#check_bracketed_array(node, literal_prefix) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 67

def check_bracketed_array(node, literal_prefix)
  return if allowed_bracket_array?(node)

  array_style_detected(:brackets, node.values.size)

  return unless style == :percent

  add_offense(node, message: self.class::PERCENT_MSG) do |corrector|
    percent_literal_corrector = PercentLiteralCorrector.new(@config, @preferred_delimiters)
    percent_literal_corrector.correct(corrector, node, literal_prefix)
  end
end

#check_percent_array(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 36

def check_percent_array(node)
  array_style_detected(:percent, node.values.size)

  brackets_required = invalid_percent_array_contents?(node)
  return unless style == :brackets || brackets_required

  # If in percent style but brackets are required due to
  # string content, the file should be excluded in auto-gen-config
  no_acceptable_style! if brackets_required

  bracketed_array = build_bracketed_array(node)
  message = build_message_for_bracketed_array(bracketed_array)

  add_offense(node, message: message) do |corrector|
    corrector.replace(node, bracketed_array)
  end
end

#comments_in_array?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 31

def comments_in_array?(node)
  line_span = node.source_range.first_line...node.source_range.last_line
  processed_source.each_comment_in_lines(line_span).any?
end

#invalid_percent_array_contents?(_node) ⇒ Boolean (private)

Override to determine values that are invalid in a percent array

[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 22

def invalid_percent_array_contents?(_node)
  false
end

#invalid_percent_array_context?(node) ⇒ Boolean (private)

Ruby does not allow percent arrays in an ambiguous block context.

Examples:

foo %i[bar baz] { qux }
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 14

def invalid_percent_array_context?(node)
  parent = node.parent

  parent&.send_type? && parent.arguments.include?(node) &&
    !parent.parenthesized? && parent.block_literal?
end

#whitespace_between(node) ⇒ String (private)

Provides whitespace between elements for building a bracketed array. %w[ a b c ] ^

Parameters:

  • node (RuboCop::AST::ArrayNode)
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 98

def whitespace_between(node)
  if node.children.length >= 2
    node.children[0].source_range.end.join(node.children[1].source_range.begin).source
  else
    ' '
  end
end

#whitespace_leading(node) ⇒ String (private)

Provides leading whitespace for building a bracketed array. %w[ a b c ] ^^

Parameters:

  • node (RuboCop::AST::ArrayNode)
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 111

def whitespace_leading(node)
  node.loc.begin.end.join(node.children[0].source_range.begin).source
end

#whitespace_trailing(node) ⇒ String (private)

Provides trailing whitespace for building a bracketed array. %w[ a b c ] ^^

Parameters:

  • node (RuboCop::AST::ArrayNode)
[ GitHub ]

  
# File 'lib/rubocop/cop/mixin/percent_array.rb', line 120

def whitespace_trailing(node)
  node.children[-1].source_range.end.join(node.loc.end.begin).source
end