Sha256: 46e53d0a51c60139c38cb7d3baff59171c22de0cdcf8a1d736b1c43d17e49b2b

Contents?: true

Size: 1.26 KB

Versions: 62

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Checks for unnecessary additional spaces inside array percent literals
      # (i.e. %i/%w).
      #
      # @example
      #
      #   # bad
      #   %w(foo  bar  baz)
      #   # good
      #   %i(foo bar baz)
      class SpaceInsideArrayPercentLiteral < Cop
        include MatchRange
        include PercentLiteral

        MSG = 'Use only a single space inside array percent literal.'
        MULTIPLE_SPACES_BETWEEN_ITEMS_REGEX =
          /(?:[\S&&[^\\]](?:\\ )*)( {2,})(?=\S)/.freeze

        def on_array(node)
          process(node, '%i', '%I', '%w', '%W')
        end

        def on_percent_literal(node)
          each_unnecessary_space_match(node) do |range|
            add_offense(node, location: range)
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            each_unnecessary_space_match(node) do |range|
              corrector.replace(range, ' ')
            end
          end
        end

        private

        def each_unnecessary_space_match(node, &blk)
          each_match_range(
            contents_range(node),
            MULTIPLE_SPACES_BETWEEN_ITEMS_REGEX,
            &blk
          )
        end
      end
    end
  end
end

Version data entries

62 entries across 43 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.89.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.89.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.88.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.87.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.87.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.86.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.85.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.85.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.84.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.83.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.82.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.81.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.80.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-0.80.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb