Sha256: 7c576dbefccacb90b331252257b59dc1cc77f3dd1d9dc8a04ec807579e513ebc

Contents?: true

Size: 1.09 KB

Versions: 35

Compression:

Stored size: 1.09 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 < Base
        include Interpolation
        extend AutoCorrector

        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) do |corrector|
              corrector.replace(var_node, "{#{var_node.source}}")
            end
          end
        end

        private

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

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

Version data entries

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/variable_interpolation.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/variable_interpolation.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/variable_interpolation.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/variable_interpolation.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/variable_interpolation.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.12.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.12.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.11.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.10.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.9.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.9.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.8.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.8.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.7.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.6.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.6.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.5.2 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.5.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.5.0 lib/rubocop/cop/style/variable_interpolation.rb