Sha256: a83784d4663687331b97a6f9cc76407114771407783224ebc54618d05980fb5f

Contents?: true

Size: 1.01 KB

Versions: 17

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

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}`.'.freeze

        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.source
            add_offense(v, :expression, format(MSG, var, var))
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(node.source_range, "{#{node.source}}")
          end
        end

        def var_nodes(nodes)
          nodes.select { |n| n.variable? || n.reference? }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.49.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.49.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.48.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.48.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.47.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.47.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.46.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.45.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.44.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.44.0 lib/rubocop/cop/style/variable_interpolation.rb