Sha256: 5b55df9c4ca1e271e121a90ff81ce0bb84bb4ab0a4507bb75bd1d726123cfcf6

Contents?: true

Size: 1.37 KB

Versions: 6838

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for handling percent arrays.
    module PercentArray
      private

      # Ruby does not allow percent arrays in an ambiguous block context.
      #
      # @example
      #
      #   foo %i[bar baz] { qux }
      def invalid_percent_array_context?(node)
        parent = node.parent

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

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

      def message(_node)
        style == :percent ? self.class::PERCENT_MSG : self.class::ARRAY_MSG
      end

      def comments_in_array?(node)
        comments = processed_source.comments
        array_range = node.source_range.to_a

        comments.any? do |comment|
          !(comment.loc.expression.to_a & array_range).empty?
        end
      end

      def check_percent_array(node)
        array_style_detected(:percent, node.values.size)
        add_offense(node) if style == :brackets
      end

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

        array_style_detected(:brackets, node.values.size)
        add_offense(node) if style == :percent
      end
    end
  end
end

Version data entries

6,838 entries across 6,832 versions & 26 rubygems

Version Path
rubocop-0.59.2 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.59.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.59.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.58.2 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.58.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.58.0 lib/rubocop/cop/mixin/percent_array.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.57.2 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.57.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.57.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.56.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.55.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.54.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.53.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.52.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.52.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.51.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-0.50.0 lib/rubocop/cop/mixin/percent_array.rb