Sha256: 705f52ed3f72cdaa071adfdd0acb1e658be86bc178331a2abf7e79c163cca9ca

Contents?: true

Size: 1.1 KB

Versions: 125

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # Checks for spaces inside range literals.
      #
      # @example
      #   # bad
      #   1 .. 3
      #
      #   # good
      #   1..3
      #
      #   # bad
      #   'a' .. 'z'
      #
      #   # good
      #   'a'..'z'
      class SpaceInsideRangeLiteral < Base
        extend AutoCorrector

        MSG = 'Space inside range literal.'

        def on_irange(node)
          check(node)
        end

        def on_erange(node)
          check(node)
        end

        private

        def check(node)
          expression = node.source
          op = node.loc.operator.source
          escaped_op = op.gsub(/\./, '\.')

          # account for multiline range literals
          expression.sub!(/#{escaped_op}\n\s*/, op)

          return unless /(\s#{escaped_op})|(#{escaped_op}\s)/.match?(expression)

          add_offense(node) do |corrector|
            corrector.replace(
              node, expression.sub(/\s+#{escaped_op}/, op).sub(/#{escaped_op}\s+/, op)
            )
          end
        end
      end
    end
  end
end

Version data entries

125 entries across 117 versions & 13 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_range_literal.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_range_literal.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/layout/space_inside_range_literal.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.52.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.52.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.51.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.50.2 lib/rubocop/cop/layout/space_inside_range_literal.rb