rails_best_practices 1.19.3 → 1.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +0 -1
  3. data/.travis.yml +2 -3
  4. data/CHANGELOG.md +10 -11
  5. data/Gemfile +3 -5
  6. data/Gemfile.lock +125 -0
  7. data/Guardfile +2 -0
  8. data/README.md +5 -1
  9. data/Rakefile +2 -17
  10. data/assets/result.html.erb +2 -0
  11. data/lib/rails_best_practices.rb +4 -2
  12. data/lib/rails_best_practices/analyzer.rb +63 -51
  13. data/lib/rails_best_practices/cli.rb +22 -0
  14. data/lib/rails_best_practices/command.rb +1 -131
  15. data/lib/rails_best_practices/core/check.rb +63 -55
  16. data/lib/rails_best_practices/core/checks_loader.rb +24 -23
  17. data/lib/rails_best_practices/core/configs.rb +1 -2
  18. data/lib/rails_best_practices/core/controllers.rb +1 -2
  19. data/lib/rails_best_practices/core/error.rb +1 -1
  20. data/lib/rails_best_practices/core/helpers.rb +1 -2
  21. data/lib/rails_best_practices/core/mailers.rb +1 -2
  22. data/lib/rails_best_practices/core/methods.rb +27 -21
  23. data/lib/rails_best_practices/core/model_associations.rb +10 -5
  24. data/lib/rails_best_practices/core/models.rb +1 -2
  25. data/lib/rails_best_practices/core/modules.rb +1 -1
  26. data/lib/rails_best_practices/core/routes.rb +2 -2
  27. data/lib/rails_best_practices/core/runner.rb +87 -72
  28. data/lib/rails_best_practices/inline_disables.rb +3 -0
  29. data/lib/rails_best_practices/inline_disables/comment_ripper.rb +19 -0
  30. data/lib/rails_best_practices/inline_disables/inline_disable.rb +50 -0
  31. data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
  32. data/lib/rails_best_practices/option_parser.rb +156 -0
  33. data/lib/rails_best_practices/prepares.rb +1 -1
  34. data/lib/rails_best_practices/prepares/controller_prepare.rb +23 -17
  35. data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -2
  36. data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
  37. data/lib/rails_best_practices/prepares/initializer_prepare.rb +3 -3
  38. data/lib/rails_best_practices/prepares/mailer_prepare.rb +2 -1
  39. data/lib/rails_best_practices/prepares/model_prepare.rb +63 -23
  40. data/lib/rails_best_practices/prepares/route_prepare.rb +28 -21
  41. data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
  42. data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +38 -34
  43. data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +94 -88
  44. data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +15 -5
  45. data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +20 -8
  46. data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +1 -1
  47. data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +1 -1
  48. data/lib/rails_best_practices/reviews/hash_syntax_review.rb +16 -16
  49. data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +12 -12
  50. data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +10 -11
  51. data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +25 -24
  52. data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +4 -4
  53. data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +9 -10
  54. data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +10 -11
  55. data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +24 -22
  56. data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +1 -1
  57. data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
  58. data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +1 -1
  59. data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +8 -8
  60. data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +35 -32
  61. data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +4 -4
  62. data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +20 -17
  63. data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +12 -10
  64. data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +38 -18
  65. data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +11 -11
  66. data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +77 -74
  67. data/lib/rails_best_practices/reviews/review.rb +2 -1
  68. data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -3
  69. data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +12 -12
  70. data/lib/rails_best_practices/reviews/use_before_filter_review.rb +14 -10
  71. data/lib/rails_best_practices/reviews/use_model_association_review.rb +15 -15
  72. data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +24 -22
  73. data/lib/rails_best_practices/reviews/use_observer_review.rb +28 -28
  74. data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +6 -6
  75. data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +63 -60
  76. data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -8
  77. data/lib/rails_best_practices/reviews/use_scope_access_review.rb +16 -14
  78. data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
  79. data/lib/rails_best_practices/version.rb +1 -1
  80. data/rails_best_practices.gemspec +48 -49
  81. data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +1 -2
  82. data/spec/rails_best_practices/analyzer_spec.rb +73 -42
  83. data/spec/rails_best_practices/core/check_spec.rb +5 -5
  84. data/spec/rails_best_practices/core/checks_loader_spec.rb +3 -3
  85. data/spec/rails_best_practices/core/configs_spec.rb +1 -1
  86. data/spec/rails_best_practices/core/controllers_spec.rb +1 -1
  87. data/spec/rails_best_practices/core/error_spec.rb +21 -18
  88. data/spec/rails_best_practices/core/except_methods_spec.rb +7 -7
  89. data/spec/rails_best_practices/core/gems_spec.rb +4 -4
  90. data/spec/rails_best_practices/core/helpers_spec.rb +1 -1
  91. data/spec/rails_best_practices/core/klasses_spec.rb +3 -3
  92. data/spec/rails_best_practices/core/mailers_spec.rb +1 -1
  93. data/spec/rails_best_practices/core/methods_spec.rb +6 -6
  94. data/spec/rails_best_practices/core/model_associations_spec.rb +10 -6
  95. data/spec/rails_best_practices/core/model_attributes_spec.rb +4 -4
  96. data/spec/rails_best_practices/core/models_spec.rb +1 -1
  97. data/spec/rails_best_practices/core/modules_spec.rb +5 -5
  98. data/spec/rails_best_practices/core/routes_spec.rb +5 -5
  99. data/spec/rails_best_practices/core/runner_spec.rb +9 -7
  100. data/spec/rails_best_practices/core_ext/erubis_spec.rb +10 -10
  101. data/spec/rails_best_practices/inline_disables/inline_disable_spec.rb +62 -0
  102. data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +32 -31
  103. data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
  104. data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
  105. data/spec/rails_best_practices/prepares/config_prepare_spec.rb +2 -2
  106. data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +18 -10
  107. data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +17 -17
  108. data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +3 -3
  109. data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +3 -3
  110. data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
  111. data/spec/rails_best_practices/prepares/model_prepare_spec.rb +79 -43
  112. data/spec/rails_best_practices/prepares/route_prepare_spec.rb +141 -76
  113. data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +2 -2
  114. data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +18 -12
  115. data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +28 -22
  116. data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +15 -13
  117. data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +31 -21
  118. data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
  119. data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +5 -5
  120. data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +13 -13
  121. data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +7 -7
  122. data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
  123. data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +29 -22
  124. data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
  125. data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +11 -6
  126. data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +32 -22
  127. data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
  128. data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +9 -7
  129. data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +9 -9
  130. data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -9
  131. data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +5 -5
  132. data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -7
  133. data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
  134. data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +24 -17
  135. data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -6
  136. data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +47 -32
  137. data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +21 -14
  138. data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +57 -53
  139. data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +10 -8
  140. data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +20 -14
  141. data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +54 -31
  142. data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -9
  143. data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -13
  144. data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +11 -9
  145. data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
  146. data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +35 -31
  147. data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
  148. data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +10 -8
  149. data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +31 -24
  150. data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +15 -11
  151. data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +14 -14
  152. data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +61 -59
  153. metadata +21 -14
@@ -5,7 +5,6 @@ require_rel 'klasses'
5
5
  module RailsBestPractices
6
6
  module Core
7
7
  # Controller classes.
8
- class Controllers < Klasses
9
- end
8
+ class Controllers < Klasses; end
10
9
  end
11
10
  end
@@ -20,7 +20,7 @@ module RailsBestPractices
20
20
  end
21
21
 
22
22
  def short_filename
23
- File.expand_path(filename)[File.expand_path(Core::Runner.base_path).size..-1].sub(/^\//, '')
23
+ File.expand_path(filename)[File.expand_path(Core::Runner.base_path).size..-1].sub(%r{^/}, '')
24
24
  end
25
25
 
26
26
  def first_line_number
@@ -5,7 +5,6 @@ require_rel 'modules'
5
5
  module RailsBestPractices
6
6
  module Core
7
7
  # Helper moduels.
8
- class Helpers < Modules
9
- end
8
+ class Helpers < Modules; end
10
9
  end
11
10
  end
@@ -3,7 +3,6 @@
3
3
  module RailsBestPractices
4
4
  module Core
5
5
  # Mailer classes.
6
- class Mailers < Klasses
7
- end
6
+ class Mailers < Klasses; end
8
7
  end
9
8
  end
@@ -18,6 +18,7 @@ module RailsBestPractices
18
18
  def add_method(class_name, method_name, meta = {}, access_control = 'public')
19
19
  return if class_name == ''
20
20
  return if has_method?(class_name, method_name)
21
+
21
22
  methods(class_name) << Method.new(class_name, method_name, access_control, meta)
22
23
  if access_control == 'public'
23
24
  @possible_methods[method_name] = false
@@ -45,7 +46,9 @@ module RailsBestPractices
45
46
  # @return [Boolean] has a method or not
46
47
  def has_method?(class_name, method_name, access_control = nil)
47
48
  if access_control
48
- !!methods(class_name).find { |method| method.method_name == method_name && method.access_control == access_control }
49
+ !!methods(class_name).find do |method|
50
+ method.method_name == method_name && method.access_control == access_control
51
+ end
49
52
  else
50
53
  !!methods(class_name).find { |method| method.method_name == method_name }
51
54
  end
@@ -57,10 +60,10 @@ module RailsBestPractices
57
60
  # @param [String] method name
58
61
  def mark_parent_class_method_used(class_name, method_name)
59
62
  klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
60
- if klass && klass.extend_class_name
63
+ if klass&.extend_class_name
61
64
  mark_parent_class_method_used(klass.extend_class_name, method_name)
62
65
  method = get_method(klass.extend_class_name, method_name)
63
- method.mark_used if method
66
+ method&.mark_used
64
67
  end
65
68
  end
66
69
 
@@ -72,7 +75,7 @@ module RailsBestPractices
72
75
  Prepares.klasses.select { |klass| klass.extend_class_name == class_name }.each do |klass|
73
76
  mark_subclasses_method_used(klass.to_s, method_name)
74
77
  method = get_method(klass.to_s, method_name)
75
- method.mark_used if method
78
+ method&.mark_used
76
79
  end
77
80
  end
78
81
 
@@ -82,7 +85,7 @@ module RailsBestPractices
82
85
  # @param [String] method name
83
86
  def mark_publicize(class_name, method_name)
84
87
  method = get_method(class_name, method_name)
85
- method.publicize if method
88
+ method&.publicize
86
89
  end
87
90
 
88
91
  # Mark parent classs' method as public.
@@ -91,7 +94,7 @@ module RailsBestPractices
91
94
  # @param [String] method name
92
95
  def mark_parent_class_methods_publicize(class_name, method_name)
93
96
  klass = Prepares.klasses.find { |klass| klass.to_s == class_name }
94
- if klass && klass.extend_class_name
97
+ if klass&.extend_class_name
95
98
  mark_parent_class_methods_publicize(klass.extend_class_name, method_name)
96
99
  mark_publicize(class_name, method_name)
97
100
  end
@@ -112,7 +115,9 @@ module RailsBestPractices
112
115
  # @return [Method] Method object
113
116
  def get_method(class_name, method_name, access_control = nil)
114
117
  if access_control
115
- methods(class_name).find { |method| method.method_name == method_name && method.access_control == access_control }
118
+ methods(class_name).find do |method|
119
+ method.method_name == method_name && method.access_control == access_control
120
+ end
116
121
  else
117
122
  methods(class_name).find { |method| method.method_name == method_name }
118
123
  end
@@ -123,24 +128,25 @@ module RailsBestPractices
123
128
  # @param [String] access control
124
129
  # @return [Array] array of Method
125
130
  def get_all_unused_methods(access_control = nil)
126
- @methods.inject([]) { |unused_methods, (_class_name, methods)|
127
- unused_methods += if access_control
128
- methods.select { |method| method.access_control == access_control && !method.used }
129
- else
130
- methods.reject { |method| method.used }
131
- end
132
- }.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
131
+ @methods.inject([]) do |unused_methods, (_class_name, methods)|
132
+ unused_methods +=
133
+ if access_control
134
+ methods.select { |method| method.access_control == access_control && !method.used }
135
+ else
136
+ methods.reject(&:used)
137
+ end
138
+ end.reject { |method| method.access_control == 'public' && @possible_methods[method.method_name] }
133
139
  end
134
140
 
135
141
  private
136
142
 
137
- # Methods of a class.
138
- #
139
- # @param [String] class name
140
- # @return [Array] array of methods
141
- def methods(class_name)
142
- @methods[class_name] ||= []
143
- end
143
+ # Methods of a class.
144
+ #
145
+ # @param [String] class name
146
+ # @return [Array] array of methods
147
+ def methods(class_name)
148
+ @methods[class_name] ||= []
149
+ end
144
150
  end
145
151
 
146
152
  # Method info includes class name, method name, access control, file, line_number, used.
@@ -15,7 +15,9 @@ module RailsBestPractices
15
15
  # @param [String] association class name
16
16
  def add_association(model_name, association_name, association_meta, association_class = nil)
17
17
  @associations[model_name] ||= {}
18
- @associations[model_name][association_name] = { 'meta' => association_meta, 'class_name' => association_class || association_name.classify }
18
+ @associations[model_name][association_name] = {
19
+ 'meta' => association_meta, 'class_name' => association_class || association_name.classify
20
+ }
19
21
  end
20
22
 
21
23
  # Get a model association.
@@ -25,7 +27,7 @@ module RailsBestPractices
25
27
  # @return [Hash] {"meta" => association_meta, "class_name" => association_class}
26
28
  def get_association(model_name, association_name)
27
29
  associations = @associations[model_name]
28
- associations and associations[association_name]
30
+ associations && associations[association_name]
29
31
  end
30
32
 
31
33
  # If it is a model's association.
@@ -39,7 +41,7 @@ module RailsBestPractices
39
41
  end
40
42
 
41
43
  # delegate each to @associations.
42
- def each(&block)
44
+ def each
43
45
  @associations.each { |model, model_associations| yield model, model_associations }
44
46
  end
45
47
 
@@ -49,8 +51,11 @@ module RailsBestPractices
49
51
  # @param [String] association_name
50
52
  # @return [String] association's class name
51
53
  def get_association_class_name(table_name, association_name)
52
- associations = @associations.select { |model, _model_associations| model.gsub('::', '').tableize == table_name }.values.first and
53
- association_meta = associations.select { |name, _meta| name == association_name }.values.first and
54
+ (
55
+ associations =
56
+ @associations.select { |model, _model_associations| model.gsub('::', '').tableize == table_name }.values
57
+ .first
58
+ ) && (association_meta = associations.select { |name, _meta| name == association_name }.values.first) &&
54
59
  association_meta['class_name']
55
60
  end
56
61
  end
@@ -3,7 +3,6 @@
3
3
  module RailsBestPractices
4
4
  module Core
5
5
  # Model classes.
6
- class Models < Klasses
7
- end
6
+ class Models < Klasses; end
8
7
  end
9
8
  end
@@ -10,7 +10,7 @@ module RailsBestPractices
10
10
  # @param [String] descendant name
11
11
  def add_module_descendant(module_name, descendant)
12
12
  mod = find { |mod| mod.to_s == module_name }
13
- mod.add_descendant(descendant) if mod
13
+ mod&.add_descendant(descendant)
14
14
  end
15
15
  end
16
16
 
@@ -24,8 +24,8 @@ module RailsBestPractices
24
24
  # mappings can be specified by e.g.
25
25
  # post 'some/:pattern' => 'controller#action'
26
26
  if action_name.is_a?(String) && action_name =~ /\A(\w+)#(\w+)\z/
27
- controller_name = $1
28
- action_name = $2
27
+ controller_name = Regexp.last_match(1)
28
+ action_name = Regexp.last_match(2)
29
29
  end
30
30
 
31
31
  if controller_name
@@ -20,33 +20,23 @@ module RailsBestPractices
20
20
  class Runner
21
21
  attr_reader :checks
22
22
 
23
- # set the base path.
24
- #
25
- # @param [String] path the base path
26
- def self.base_path=(path)
27
- @base_path = path
28
- end
29
-
30
- # get the base path, by default, the base path is current path.
31
- #
32
- # @return [String] the base path
33
- def self.base_path
34
- @base_path || '.'
35
- end
23
+ class << self
24
+ attr_writer :base_path, :config_path
36
25
 
37
- # set the configuration path
38
- #
39
- # @param path [String] path to rbc config file
40
- def self.config_path=(path)
41
- @config_path = path
42
- end
26
+ # get the base path, by default, the base path is current path.
27
+ #
28
+ # @return [String] the base path
29
+ def base_path
30
+ @base_path || '.'
31
+ end
43
32
 
44
- # get the configuration path, if will default to config/rails_best_practices.yml
45
- #
46
- # @return [String] the config path
47
- def self.config_path
48
- custom_config = @config_path || File.join(Runner.base_path, 'config/rails_best_practices.yml')
49
- File.exist?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG
33
+ # get the configuration path, if will default to config/rails_best_practices.yml
34
+ #
35
+ # @return [String] the config path
36
+ def config_path
37
+ custom_config = @config_path || File.join(Runner.base_path, 'config/rails_best_practices.yml')
38
+ File.exist?(custom_config) ? custom_config : RailsBestPractices::Analyzer::DEFAULT_CONFIG
39
+ end
50
40
  end
51
41
 
52
42
  # initialize the runner.
@@ -66,9 +56,18 @@ module RailsBestPractices
66
56
  load_plugin_reviews if reviews.empty?
67
57
 
68
58
  @lexical_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @lexicals)
69
- @plain_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: @prepares.select { |checker| checker.is_a? Prepares::GemfilePrepare })
70
- @default_prepare_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @prepares.reject { |checker| checker.is_a? Prepares::GemfilePrepare })
59
+ @plain_prepare_checker ||=
60
+ CodeAnalyzer::CheckingVisitor::Plain.new(
61
+ checkers: @prepares.select { |checker| checker.is_a? Prepares::GemfilePrepare }
62
+ )
63
+ @default_prepare_checker ||=
64
+ CodeAnalyzer::CheckingVisitor::Default.new(
65
+ checkers: @prepares.reject { |checker| checker.is_a? Prepares::GemfilePrepare }
66
+ )
71
67
  @review_checker ||= CodeAnalyzer::CheckingVisitor::Default.new(checkers: @reviews)
68
+
69
+ @inlnie_disable ||= InlineDisables::InlineDisable.new
70
+ @inline_disable_checker ||= CodeAnalyzer::CheckingVisitor::Plain.new(checkers: [@inlnie_disable])
72
71
  end
73
72
 
74
73
  # lexical analysis the file.
@@ -110,65 +109,81 @@ module RailsBestPractices
110
109
  @review_checker.after_check
111
110
  end
112
111
 
112
+ # disable check by inline comment the file.
113
+ #
114
+ # @param [String] filename of the file
115
+ # @param [String] content of the file
116
+ def inline_disable(filename, content)
117
+ content = parse_html_template(filename, content)
118
+ @inline_disable_checker.check(filename, content)
119
+ end
120
+
121
+ def after_inline_disable
122
+ @inline_disable_checker.after_check
123
+ end
124
+
113
125
  # get all errors from lexicals and reviews.
114
126
  #
115
127
  # @return [Array] all errors from lexicals and reviews
116
128
  def errors
117
- @errors ||= (@reviews + @lexicals).collect(&:errors).flatten
129
+ @errors ||= begin
130
+ reported_errors = (@reviews + @lexicals).collect(&:errors).flatten
131
+ reported_errors.reject { |error| @inlnie_disable.disabled?(error) }
132
+ end
118
133
  end
119
134
 
120
135
  private
121
136
 
122
- # parse html template code, erb, haml and slim.
123
- #
124
- # @param [String] filename is the filename of the erb, haml or slim code.
125
- # @param [String] content is the source code of erb, haml or slim file.
126
- def parse_html_template(filename, content)
127
- if filename =~ /.*\.erb$|.*\.rhtml$/
128
- content = Erubis::OnlyRuby.new(content).src
129
- elsif filename =~ /.*\.haml$/
130
- begin
131
- require 'haml'
132
- content = Haml::Engine.new(content).precompiled
133
- # remove \xxx characters
134
- content.gsub!(/\\\d{3}/, '')
135
- rescue LoadError
136
- raise "In order to parse #{filename}, please install the haml gem"
137
- rescue Haml::Error, SyntaxError
138
- # do nothing, just ignore the wrong haml files.
139
- end
140
- elsif filename =~ /.*\.slim$/
141
- begin
142
- require 'slim'
143
- content = Slim::Engine.new.call(content)
144
- rescue LoadError
145
- raise "In order to parse #{filename}, please install the slim gem"
146
- rescue SyntaxError
147
- # do nothing, just ignore the wrong slim files
148
- end
137
+ # parse html template code, erb, haml and slim.
138
+ #
139
+ # @param [String] filename is the filename of the erb, haml or slim code.
140
+ # @param [String] content is the source code of erb, haml or slim file.
141
+ def parse_html_template(filename, content)
142
+ if filename =~ /.*\.erb$|.*\.rhtml$/
143
+ content = Erubis::OnlyRuby.new(content).src
144
+ elsif filename =~ /.*\.haml$/
145
+ begin
146
+ require 'haml'
147
+ content = Haml::Engine.new(content).precompiled
148
+ # remove \xxx characters
149
+ content.gsub!(/\\\d{3}/, '')
150
+ rescue LoadError
151
+ raise "In order to parse #{filename}, please install the haml gem"
152
+ rescue Haml::Error, SyntaxError
153
+ # do nothing, just ignore the wrong haml files.
154
+ end
155
+ elsif filename =~ /.*\.slim$/
156
+ begin
157
+ require 'slim'
158
+ content = Slim::Engine.new.call(content)
159
+ rescue LoadError
160
+ raise "In order to parse #{filename}, please install the slim gem"
161
+ rescue SyntaxError
162
+ # do nothing, just ignore the wrong slim files
149
163
  end
150
- content
151
164
  end
165
+ content
166
+ end
152
167
 
153
- # load all prepares.
154
- def load_prepares
155
- Prepares.constants.map { |prepare| Prepares.const_get(prepare).new }
156
- end
168
+ # load all prepares.
169
+ def load_prepares
170
+ Prepares.constants.map { |prepare| Prepares.const_get(prepare).new }
171
+ end
157
172
 
158
- # load all plugin reviews.
159
- def load_plugin_reviews
160
- plugins = File.join(Runner.base_path, 'lib', 'rails_best_practices', 'plugins', 'reviews')
161
- if File.directory?(plugins)
162
- Dir[File.expand_path(File.join(plugins, '*.rb'))].each do |review|
163
- require review
164
- end
165
- if RailsBestPractices.constants.map(&:to_sym).include? :Plugins
166
- RailsBestPractices::Plugins::Reviews.constants.each do |review|
167
- @reviews << RailsBestPractices::Plugins::Reviews.const_get(review).new
168
- end
169
- end
173
+ # load all plugin reviews.
174
+ def load_plugin_reviews
175
+ plugins = File.join(Runner.base_path, 'lib', 'rails_best_practices', 'plugins', 'reviews')
176
+ if File.directory?(plugins)
177
+ Dir[File.expand_path(File.join(plugins, '*.rb'))].each do |review|
178
+ require review
179
+ end
180
+ if RailsBestPractices.constants.map(&:to_sym).include? :Plugins
181
+ RailsBestPractices::Plugins::Reviews.constants.each do |review|
182
+ @reviews << RailsBestPractices::Plugins::Reviews.const_get(review).new
170
183
  end
184
+ end
171
185
  end
186
+ end
172
187
  end
173
188
  end
174
189
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_rel 'inline_disables'
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsBestPractices
4
+ module InlineDisables
5
+ class CommentRipper < Ripper::SexpBuilder
6
+ attr_reader :comments
7
+
8
+ def initialize(*arg)
9
+ super
10
+ @comments = []
11
+ end
12
+
13
+ def on_comment(*arg)
14
+ # [sexp_type, statement, [lineno, column]] = super
15
+ comments << super
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsBestPractices
4
+ module InlineDisables
5
+ class InlineDisable < Core::Check
6
+ interesting_files ALL_FILES
7
+ url '#'
8
+
9
+ def initialize(*args)
10
+ super
11
+ @disabled_errors = []
12
+ end
13
+
14
+ def check(filename, content)
15
+ comments = CommentRipper.new(content).tap(&:parse).comments
16
+ comments.each do |_sexp_type, statement, (line_number, _column)|
17
+ add_as_disable_errors(filename, statement, line_number)
18
+ end
19
+ end
20
+
21
+ def disabled?(error)
22
+ error_key = [error.filename, error.line_number, error.type.split('::').last].join('-')
23
+ disabled_error_keys.include?(error_key)
24
+ end
25
+
26
+ private
27
+
28
+ def disabled_error_keys
29
+ @disabled_error_keys ||= Set.new(@disabled_errors.map { |e| [e.filename, e.line_number, e.type].join('-') })
30
+ end
31
+
32
+ def add_as_disable_errors(filename, statement, line_number)
33
+ match = statement.match(/rails_b(?:est_)?p(?:ractices)?:disable (.*)/)
34
+ return unless match
35
+
36
+ check_names = match[1].split(',')
37
+ check_names.each do |check_name|
38
+ add_as_disable_error(filename, check_name.gsub(/Check$/, 'Review'), line_number)
39
+ end
40
+ end
41
+
42
+ def add_as_disable_error(filename, check_name, line_number)
43
+ @disabled_errors <<
44
+ RailsBestPractices::Core::Error.new(
45
+ filename: filename, line_number: line_number, message: 'disable by inline comment', type: check_name, url: url
46
+ )
47
+ end
48
+ end
49
+ end
50
+ end