class RuboCop::Cop::Layout::SpaceInsideStringInterpolation

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"

Constants

NO_SPACE_MSG
SPACE_MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/layout/space_inside_string_interpolation.rb, line 34
def autocorrect(node)
  new_source = style == :no_space ? node.source : " #{node.source} "
  lambda do |corrector|
    corrector.replace(
      range_with_surrounding_space(range: node.source_range),
      new_source
    )
  end
end
on_dstr(node) click to toggle source
# File lib/rubocop/cop/layout/space_inside_string_interpolation.rb, line 28
def on_dstr(node)
  each_style_violation(node) do |final_node|
    add_offense(final_node)
  end
end

Private Instance Methods

each_style_violation(node) { |final_node| ... } click to toggle source
# File lib/rubocop/cop/layout/space_inside_string_interpolation.rb, line 46
def each_style_violation(node)
  node.each_child_node(:begin) do |begin_node|
    final_node = begin_node.children.last
    next unless final_node

    if style == :no_space && space_on_any_side?(final_node)
      yield final_node
    elsif style == :space && !space_on_each_side?(final_node)
      yield final_node
    end
  end
end
message(_node) click to toggle source
# File lib/rubocop/cop/layout/space_inside_string_interpolation.rb, line 59
def message(_node)
  style == :no_space ? NO_SPACE_MSG : SPACE_MSG
end
space_on_any_side?(node) click to toggle source
# File lib/rubocop/cop/layout/space_inside_string_interpolation.rb, line 63
def space_on_any_side?(node)
  interp = node.source_range
  interp_with_surrounding_space =
    range_with_surrounding_space(range: interp)

  interp_with_surrounding_space != interp
end
space_on_each_side?(node) click to toggle source
# File lib/rubocop/cop/layout/space_inside_string_interpolation.rb, line 71
def space_on_each_side?(node)
  interp = node.source_range
  interp_with_surrounding_space =
    range_with_surrounding_space(range: interp)

  interp_with_surrounding_space.source == " #{interp.source} "
end