Sha256: ca92e635e5b1c05ea89e072735ba2bc66686e235006e4446910da95ab91f6e7d

Contents?: true

Size: 1.63 KB

Versions: 35

Compression:

Stored size: 1.63 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&.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)
        line_span = node.source_range.first_line...node.source_range.last_line
        processed_source.each_comment_in_lines(line_span).any?
      end

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

        return unless style == :brackets

        add_offense(node) do |corrector|
          correct_bracketed(corrector, node)
        end
      end

      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) do |corrector|
          percent_literal_corrector = PercentLiteralCorrector.new(@config, @preferred_delimiters)
          percent_literal_corrector.correct(corrector, node, literal_prefix)
        end
      end
    end
  end
end

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/percent_array.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/percent_array.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/percent_array.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/percent_array.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/percent_array.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.12.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.12.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.11.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.10.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.9.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.9.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.8.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.8.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.7.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.6.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.6.0 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.5.2 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.5.1 lib/rubocop/cop/mixin/percent_array.rb
rubocop-1.5.0 lib/rubocop/cop/mixin/percent_array.rb