Sha256: a28e08ee14b60edd56b5294da1353374ed37c31f3ff3f0453d0d51b5b227dc10

Contents?: true

Size: 785 Bytes

Versions: 4

Compression:

Stored size: 785 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}")
              add_offence(:convention,
                          v.loc.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

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.11.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.11.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.10.0 lib/rubocop/cop/style/variable_interpolation.rb