Sha256: 8c6d8cedac7ae9cc731d4efff8ea4dc11b60ce633e13080014da5704af251168

Contents?: true

Size: 1.12 KB

Versions: 187

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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}`.' # rubocop:disable Lint/InterpolationCheck

        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

187 entries across 180 versions & 18 rubygems

Version Path
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.99 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.98 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.97 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.96 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.95 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb