Sha256: 3cf06bcc85fa9326bf7a9a1bf4ba6572b179a02cb6398273bfa3016e17933cd1

Contents?: true

Size: 1.25 KB

Versions: 13

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

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

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

        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, range, MSG)
          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

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.48.1 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.48.0 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.47.1 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.47.0 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.46.0 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.45.0 lib/rubocop/cop/style/space_inside_array_percent_literal.rb