Sha256: 3a1c1d457e0ca7797e0479f590107b5db41e31b9c467cec03a69354be252ea8c

Contents?: true

Size: 1.1 KB

Versions: 16

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for variable interpolation (like "#@ivar").
      #
      # @example
      #   # bad
      #   "His name is #$name"
      #   /check #$pattern/
      #   "Let's go to the #@store"
      #
      #   # good
      #   "His name is #{$name}"
      #   /check #{$pattern}/
      #   "Let's go to the #{@store}"
      class VariableInterpolation < Cop
        include Interpolation

        MSG = 'Replace interpolated variable `%<variable>s` ' \
              'with expression `#{%<variable>s}`.'

        def on_node_with_interpolations(node)
          var_nodes(node.children).each do |var_node|
            add_offense(var_node)
          end
        end

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

        private

        def message(node)
          format(MSG, variable: node.source)
        end

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

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
rubocop-0.89.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.89.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.88.0 lib/rubocop/cop/style/variable_interpolation.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.87.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.87.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.86.0 lib/rubocop/cop/style/variable_interpolation.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/variable_interpolation.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/variable_interpolation.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.85.1 lib/rubocop/cop/style/variable_interpolation.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.85.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.84.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.83.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-0.82.0 lib/rubocop/cop/style/variable_interpolation.rb