Sha256: b6abe73a336b440366a2ee3f984cda25611328cd8c1f457a421925b238d35652

Contents?: true

Size: 1.69 KB

Versions: 10

Compression:

Stored size: 1.69 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for whitespace within string interpolations.
      #
      # @example
      #   # Good if EnforcedStyle is no_space, bad if space.
      #      var = "This is the #{no_space} example"
      #
      #   # Good if EnforceStyle is space, bad if no_space.
      #      var = "This is the #{ space } example"
      class SpaceInsideStringInterpolation < Cop
        include ConfigurableEnforcedStyle

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

        def on_dstr(node)
          node.children.select { |n| n.type == :begin }.each do |begin_node|
            final_node = begin_node.children.last
            next unless final_node

            interp = final_node.source_range
            interp_with_surrounding_space = range_with_surrounding_space(interp)
            if style == :no_space
              if interp_with_surrounding_space != interp
                add_offense(final_node, :expression, NO_SPACE_MSG)
              end
            elsif style == :space
              if interp_with_surrounding_space.source != " #{interp.source} "
                add_offense(final_node, :expression, SPACE_MSG)
              end
            end
          end
        end

        private

        def autocorrect(node)
          new_source = (style == :no_space) ? node.source : " #{node.source} "
          lambda do |corrector|
            corrector.replace(range_with_surrounding_space(node.source_range),
                              new_source)
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rubocop-0.41.2 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.41.1 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.41.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.40.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.39.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.38.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.37.2 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.37.1 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.37.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb
rubocop-0.36.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb