PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review.rb

http://github.com/flyerhzm/rails_best_practices
Ruby | 28 lines | 14 code | 2 blank | 12 comment | 1 complexity | 0aa0d556ed9a8c0057c6295e1da4e6d0 MD5 | raw file
 1# frozen_string_literal: true
 2
 3module RailsBestPractices
 4  module Reviews
 5    # Review a partail view file to make sure there is no instance variable.
 6    #
 7    # See the best practice details here https://rails-bestpractices.com/posts/2010/07/24/replace-instance-variable-with-local-variable/
 8    #
 9    # Implementation:
10    #
11    # Review process:
12    #   check all instance variable in partial view files,
13    #   if exist, then they should be replaced with local variable
14    class ReplaceInstanceVariableWithLocalVariableReview < Review
15      interesting_nodes :var_ref, :vcall
16      interesting_files PARTIAL_VIEW_FILES
17      url 'https://rails-bestpractices.com/posts/2010/07/24/replace-instance-variable-with-local-variable/'
18
19      # check ivar node in partial view file,
20      # it is an instance variable, and should be replaced with local variable.
21      add_callback :start_var_ref, :start_vcall do |node|
22        if node.to_s.start_with?('@')
23          add_error 'replace instance variable with local variable'
24        end
25      end
26    end
27  end
28end