Sha256: 53ed8b02718f8817ee8b2de6d1c979050f315c1bb6262a6f652a1a56c9f88828

Contents?: true

Size: 1.82 KB

Versions: 36

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks for whitespace within string interpolations.
      #
      # @example EnforcedStyle: no_space (default)
      #   # bad
      #      var = "This is the #{ space } example"
      #
      #   # good
      #      var = "This is the #{no_space} example"
      #
      # @example EnforcedStyle: space
      #   # bad
      #      var = "This is the #{no_space} example"
      #
      #   # good
      #      var = "This is the #{ space } example"
      class SpaceInsideStringInterpolation < Cop
        include Interpolation
        include SurroundingSpace
        include ConfigurableEnforcedStyle
        include RangeHelp

        NO_SPACE_MSG = 'Space inside string interpolation detected.'
        SPACE_MSG = 'Missing space inside string interpolation detected.'

        def on_interpolation(begin_node)
          return if begin_node.multiline?

          delims = delimiters(begin_node)
          return if empty_brackets?(*delims)

          if style == :no_space
            no_space_offenses(begin_node, *delims, NO_SPACE_MSG)
          else
            space_offenses(begin_node, *delims, SPACE_MSG)
          end
        end

        def autocorrect(begin_node)
          lambda do |corrector|
            delims = delimiters(begin_node)

            if style == :no_space
              SpaceCorrector.remove_space(processed_source, corrector, *delims)
            else
              SpaceCorrector.add_space(processed_source, corrector, *delims)
            end
          end
        end

        private

        def delimiters(begin_node)
          left = processed_source.tokens[index_of_first_token(begin_node)]
          right = processed_source.tokens[index_of_last_token(begin_node)]
          [left, right]
        end
      end
    end
  end
end

Version data entries

36 entries across 29 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.89.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.89.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.88.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.87.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.87.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.86.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.85.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.85.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.84.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.83.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.82.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.81.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.80.1 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
rubocop-0.80.0 lib/rubocop/cop/layout/space_inside_string_interpolation.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_inside_string_interpolation.rb