Sha256: 1aef98d5dc2653c37b81b17ba33b8eaa92e81999ee8269ea2962dbd8828ea5b0

Contents?: true

Size: 1.49 KB

Versions: 62

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Checks for unnecessary additional spaces inside the delimiters of
      # %i/%w/%x literals.
      #
      # @example
      #
      #   # good
      #   %i(foo bar baz)
      #
      #   # bad
      #   %w( foo bar baz )
      #
      #   # bad
      #   %x(  ls -l )
      class SpaceInsidePercentLiteralDelimiters < Cop
        include MatchRange
        include PercentLiteral

        MSG = 'Do not use spaces inside percent literal delimiters.'
        BEGIN_REGEX = /\A( +)/.freeze
        END_REGEX = /(?<!\\)( +)\z/.freeze

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

        def on_xstr(node)
          process(node, '%x')
        end

        def on_percent_literal(node)
          add_offenses_for_unnecessary_spaces(node)
        end

        def autocorrect(node)
          lambda do |corrector|
            regex_matches(node) do |match_range|
              corrector.remove(match_range)
            end
          end
        end

        private

        def add_offenses_for_unnecessary_spaces(node)
          return unless node.single_line?

          regex_matches(node) do |match_range|
            add_offense(node, location: match_range)
          end
        end

        def regex_matches(node, &blk)
          [BEGIN_REGEX, END_REGEX].each do |regex|
            each_match_range(contents_range(node), regex, &blk)
          end
        end
      end
    end
  end
end

Version data entries

62 entries across 43 versions & 5 rubygems

Version Path
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.75.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.75.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.74.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.73.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.72.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.71.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb