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

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/variable_interpolation.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.30.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.30.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.29.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.29.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.28.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.27.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.27.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.26.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.26.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.25.0 lib/rubocop/cop/style/variable_interpolation.rb