Sha256: 9641ff55ba4bfa4130cc769f47afe51035a809cab9a6da08e5ae081f08996b25

Contents?: true

Size: 717 Bytes

Versions: 5

Compression:

Stored size: 717 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for variable interpolation (like "#@ivar").
      class VariableInterpolation < Cop
        MSG = 'Replace interpolated var %s with expression #{%s}.'

        def on_dstr(node)
          var_nodes(node.children).each do |v|
            var = (v.type == :nth_ref ? '$' : '') + v.to_a[0].to_s

            if node.loc.expression.source.include?("##{var}")
              convention(v, :expression, sprintf(MSG, var, var))
            end
          end
        end

        private

        def var_nodes(nodes)
          nodes.select { |n| [:ivar, :cvar, :gvar, :nth_ref].include?(n.type) }
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.15.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.14.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.14.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.13.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.13.0 lib/rubocop/cop/style/variable_interpolation.rb