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
rubocop-1.37.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.37.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.36.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.35.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.35.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.34.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.34.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.33.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.32.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.31.2 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.31.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.31.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.30.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.30.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.29.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.29.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.28.2 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.28.1 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.28.0 lib/rubocop/cop/layout/space_inside_range_literal.rb
rubocop-1.27.0 lib/rubocop/cop/layout/space_inside_range_literal.rb