Sha256: 76846b34e8d4828ece500893c0b78b80f54c151f2af45e0f0f78d5d09f90d3c4

Contents?: true

Size: 1.71 KB

Versions: 17

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Checks for use of the helper methods which reference
      # instance variables.
      #
      # Relying on instance variables makes it difficult to re-use helper
      # methods.
      #
      # If it seems awkward to explicitly pass in each dependent
      # variable, consider moving the behavior elsewhere, for
      # example to a model, decorator or presenter.
      #
      # Provided that a class inherits `ActionView::Helpers::FormBuilder`,
      # an offense will not be registered.
      #
      # @example
      #   # bad
      #   def welcome_message
      #     "Hello #{@user.name}"
      #   end
      #
      #   # good
      #   def welcome_message(user)
      #     "Hello #{user.name}"
      #   end
      #
      #   # good
      #   class MyFormBuilder < ActionView::Helpers::FormBuilder
      #     @template.do_something
      #   end
      class HelperInstanceVariable < Base
        MSG = 'Do not use instance variables in helpers.'

        def_node_matcher :form_builder_class?, <<~PATTERN
          (const
            (const
               (const {nil? cbase} :ActionView) :Helpers) :FormBuilder)
        PATTERN

        def on_ivar(node)
          return if inherit_form_builder?(node)

          add_offense(node)
        end

        def on_ivasgn(node)
          return if node.parent.or_asgn_type? || inherit_form_builder?(node)

          add_offense(node.loc.name)
        end

        private

        def inherit_form_builder?(node)
          node.each_ancestor(:class) do |class_node|
            return true if form_builder_class?(class_node.parent_class)
          end

          false
        end
      end
    end
  end
end

Version data entries

17 entries across 16 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.21.2 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.21.1 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.21.0 lib/rubocop/cop/rails/helper_instance_variable.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/helper_instance_variable.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.20.2/lib/rubocop/cop/rails/helper_instance_variable.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/helper_instance_variable.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.20.2 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.20.1 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.20.0 lib/rubocop/cop/rails/helper_instance_variable.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.19.1/lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.19.1 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.19.0 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.18.0 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.17.4 lib/rubocop/cop/rails/helper_instance_variable.rb
rubocop-rails-2.17.3 lib/rubocop/cop/rails/helper_instance_variable.rb