Sha256: 7da22fc8a69a3459d82ee5bf0ae3dba3cc55f99492627cb88ac6ed6a0779d65f

Contents?: true

Size: 1.38 KB

Versions: 97

Compression:

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

        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

        private

        def add_offenses_for_unnecessary_spaces(node)
          return unless node.single_line?

          regex_matches(node) do |match_range|
            add_offense(match_range) do |corrector|
              corrector.remove(match_range)
            end
          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

97 entries across 91 versions & 9 rubygems

Version Path
rubocop-1.5.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.5.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.4.2 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.4.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.4.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.3.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.3.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.2.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.1.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.0.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.93.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.93.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.92.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.91.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.91.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-0.90.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb