class RuboCop::Cop::Style::RedundantInterpolation
Checks for strings that are just an interpolated expression.
@safety
Autocorrection is unsafe because when calling a destructive method to string, the resulting string may have different behavior or raise `FrozenError`. [source,ruby] ---- x = 'a' y = "#{x}" y << 'b' # return 'ab' x # return 'a' y = x.to_s y << 'b' # return 'ab' x # return 'ab' x = 'a'.freeze y = "#{x}" y << 'b' # return 'ab'. y = x.to_s y << 'b' # raise `FrozenError`. ----
@example
# bad "#{@var}" # good @var.to_s # good if @var is already a String @var
Constants
- MSG
Public Class Methods
autocorrect_incompatible_with()
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 45 def self.autocorrect_incompatible_with [Style::LineEndConcatenation] end
Public Instance Methods
on_dstr(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 49 def on_dstr(node) return unless single_interpolation?(node) add_offense(node) do |corrector| embedded_node = node.children.first if variable_interpolation?(embedded_node) autocorrect_variable_interpolation(corrector, embedded_node, node) elsif single_variable_interpolation?(embedded_node) autocorrect_single_variable_interpolation(corrector, embedded_node, node) else autocorrect_other(corrector, embedded_node, node) end end end
Private Instance Methods
autocorrect_other(corrector, embedded_node, node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 122 def autocorrect_other(corrector, embedded_node, node) loc = node.loc embedded_loc = embedded_node.loc corrector.replace(loc.begin, '') corrector.replace(loc.end, '') corrector.replace(embedded_loc.begin, '(') corrector.replace(embedded_loc.end, ').to_s') end
autocorrect_single_variable_interpolation(corrector, embedded_node, node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 105 def autocorrect_single_variable_interpolation(corrector, embedded_node, node) embedded_var = embedded_node.children.first source = if require_parentheses?(embedded_var) receiver = range_between( embedded_var.loc.expression.begin_pos, embedded_var.loc.selector.end_pos ) arguments = embedded_var.arguments.map(&:source).join(', ') "#{receiver.source}(#{arguments})" else embedded_var.source end corrector.replace(node, "#{source}.to_s") end
autocorrect_variable_interpolation(corrector, embedded_node, node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 99 def autocorrect_variable_interpolation(corrector, embedded_node, node) replacement = "#{embedded_node.loc.expression.source}.to_s" corrector.replace(node, replacement) end
embedded_in_percent_array?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 95 def embedded_in_percent_array?(node) node.parent&.array_type? && percent_literal?(node.parent) end
implicit_concatenation?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 91 def implicit_concatenation?(node) node.parent&.dstr_type? end
interpolation?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 83 def interpolation?(node) variable_interpolation?(node) || node.begin_type? end
require_parentheses?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 132 def require_parentheses?(node) node.send_type? && !node.arguments.count.zero? && !node.parenthesized_call? end
single_interpolation?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 67 def single_interpolation?(node) node.children.one? && interpolation?(node.children.first) && !implicit_concatenation?(node) && !embedded_in_percent_array?(node) end
single_variable_interpolation?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 74 def single_variable_interpolation?(node) return false unless node.children.one? first_child = node.children.first variable_interpolation?(first_child) || (first_child.send_type? && !first_child.operator_method?) end
variable_interpolation?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_interpolation.rb, line 87 def variable_interpolation?(node) node.variable? || node.reference? end