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.32.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.31.2 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.31.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.31.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.30.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.30.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.29.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.29.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.28.2 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.28.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.28.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.27.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.26.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.26.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.25.1 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
rubocop-1.25.0 lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb