Sha256: c7baad6d57aed4232ccab22190810e3c5ccbaa6075e20b65e55b026dfec0754f

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

# encoding: utf-8

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.'
        SPACE_MSG = 'Missing space around string interpolation detected.'

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

            interp = final_node.loc.expression
            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)
          if style == :no_space
            lambda do |corrector|
              corrector.replace(
                range_with_surrounding_space(node.loc.expression),
                node.loc.expression.source
              )
            end
          elsif style == :space
            lambda do |corrector|
              corrector.replace(
                range_with_surrounding_space(node.loc.expression),
                " #{node.loc.expression.source} "
              )
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.33.0 lib/rubocop/cop/style/space_inside_string_interpolation.rb