Sha256: 9bfb6ae740c225ab9e9c2bf8de15cc168cedabd19df5d736c4f73776b8920e42

Contents?: true

Size: 1.49 KB

Versions: 6775

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.'.freeze
        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

6,775 entries across 6,769 versions & 24 rubygems

Version Path
cybrid_api_organization_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.143 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_bank_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_organization_ruby-0.123.142 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_bank_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_organization_ruby-0.123.140 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_bank_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_organization_ruby-0.123.139 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_organization_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_bank_ruby-0.123.137 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_organization_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_bank_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.135 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_bank_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_id_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb
cybrid_api_organization_ruby-0.123.134 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb