Sha256: 608effb0c05630168ced1a7fe82dd9d0b424970385ce9f5de382bc7b47ddc379

Contents?: true

Size: 1.15 KB

Versions: 35

Compression:

Stored size: 1.15 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 < Base
        include MatchRange
        include PercentLiteral
        extend AutoCorrector

        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(range) do |corrector|
              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

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/layout/space_inside_array_percent_literal.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.12.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.12.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.11.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.10.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.9.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.9.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.8.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.8.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.7.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.6.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.6.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.5.2 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.5.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.5.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb