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.1.43 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rubocop-1.59.0/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.59.0 lib/rubocop/cop/style/variable_interpolation.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.58.0 lib/rubocop/cop/style/variable_interpolation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/variable_interpolation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/variable_interpolation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/variable_interpolation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.57.2 lib/rubocop/cop/style/variable_interpolation.rb