Sha256: 77c587330d25906c96626f08bb00ffa7962143515a61c96979a1061e17cf1499

Contents?: true

Size: 1.09 KB

Versions: 62

Compression:

Stored size: 1.09 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

62 entries across 56 versions & 7 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.38.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.37.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.37.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.36.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.35.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.35.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.34.1 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.34.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb
rubocop-1.33.0 lib/rubocop/cop/layout/space_inside_array_percent_literal.rb