Sha256: 4d660055f15315300d37d046817cb3d4e6f5b10911bcb0591fc3d1534396e8cc

Contents?: true

Size: 802 Bytes

Versions: 3

Compression:

Stored size: 802 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

          super
        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

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/style/variable_interpolation.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.9.0 lib/rubocop/cop/style/variable_interpolation.rb