Sha256: 4c10ae4526fe84574e4c76ca21bc0a2d172aed8bea6abe55754b315ee252a021
Contents?: true
Size: 1.05 KB
Versions: 12
Compression:
Stored size: 1.05 KB
Contents
# encoding: utf-8 module RuboCop module Cop module Style # This cop checks for variable interpolation (like "#@ivar"). class VariableInterpolation < Cop MSG = 'Replace interpolated variable `%s` with expression `#{%s}`.' def on_dstr(node) check_for_interpolation(node) end def on_regexp(node) check_for_interpolation(node) end def on_xstr(node) check_for_interpolation(node) end private def check_for_interpolation(node) var_nodes(node.children).each do |v| var = v.loc.expression.source add_offense(v, :expression, format(MSG, var, var)) end end def autocorrect(node) @corrections << lambda do |corrector| expr = node.loc.expression corrector.replace(expr, "{#{expr.source}}") end end def var_nodes(nodes) nodes.select { |n| [:ivar, :cvar, :gvar, :nth_ref].include?(n.type) } end end end end end
Version data entries
12 entries across 12 versions & 2 rubygems