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
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.68.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.67.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.66.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.66.0 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.65.1 lib/rubocop/cop/style/variable_interpolation.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.65.0 lib/rubocop/cop/style/variable_interpolation.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/variable_interpolation.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/variable_interpolation.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/variable_interpolation.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.64.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.63.4 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.63.3 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.63.2 lib/rubocop/cop/style/variable_interpolation.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.63.1 lib/rubocop/cop/style/variable_interpolation.rb
rubocop-1.63.0 lib/rubocop/cop/style/variable_interpolation.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/variable_interpolation.rb