Sha256: 6c45ed22fc49f3f91ff8e0091eee56d5b457d3f725431c89f9949ed84927ee28

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for unnecessary additional spaces inside array percent literals
      # (i.e. %i/%w).
      #
      # @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, *%w(%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

6 entries across 6 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.42.0 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.41.2 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.41.1 lib/rubocop/cop/style/space_inside_array_percent_literal.rb
rubocop-0.41.0 lib/rubocop/cop/style/space_inside_array_percent_literal.rb