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.
- checksums.yaml +5 -5
- data/.gitignore +0 -1
- data/.travis.yml +2 -3
- data/CHANGELOG.md +10 -11
- data/Gemfile +3 -5
- data/Gemfile.lock +125 -0
- data/Guardfile +2 -0
- data/README.md +5 -1
- data/Rakefile +2 -17
- data/assets/result.html.erb +2 -0
- data/lib/rails_best_practices.rb +4 -2
- data/lib/rails_best_practices/analyzer.rb +63 -51
- data/lib/rails_best_practices/cli.rb +22 -0
- data/lib/rails_best_practices/command.rb +1 -131
- data/lib/rails_best_practices/core/check.rb +63 -55
- data/lib/rails_best_practices/core/checks_loader.rb +24 -23
- data/lib/rails_best_practices/core/configs.rb +1 -2
- data/lib/rails_best_practices/core/controllers.rb +1 -2
- data/lib/rails_best_practices/core/error.rb +1 -1
- data/lib/rails_best_practices/core/helpers.rb +1 -2
- data/lib/rails_best_practices/core/mailers.rb +1 -2
- data/lib/rails_best_practices/core/methods.rb +27 -21
- data/lib/rails_best_practices/core/model_associations.rb +10 -5
- data/lib/rails_best_practices/core/models.rb +1 -2
- data/lib/rails_best_practices/core/modules.rb +1 -1
- data/lib/rails_best_practices/core/routes.rb +2 -2
- data/lib/rails_best_practices/core/runner.rb +87 -72
- data/lib/rails_best_practices/inline_disables.rb +3 -0
- data/lib/rails_best_practices/inline_disables/comment_ripper.rb +19 -0
- data/lib/rails_best_practices/inline_disables/inline_disable.rb +50 -0
- data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
- data/lib/rails_best_practices/option_parser.rb +156 -0
- data/lib/rails_best_practices/prepares.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +23 -17
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +2 -2
- data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +3 -3
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +2 -1
- data/lib/rails_best_practices/prepares/model_prepare.rb +63 -23
- data/lib/rails_best_practices/prepares/route_prepare.rb +28 -21
- data/lib/rails_best_practices/prepares/schema_prepare.rb +1 -1
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +38 -34
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +94 -88
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +15 -5
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +20 -8
- data/lib/rails_best_practices/reviews/default_scope_is_evil_review.rb +1 -1
- data/lib/rails_best_practices/reviews/dry_bundler_in_capistrano_review.rb +1 -1
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +16 -16
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +12 -12
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +10 -11
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +25 -24
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +4 -4
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +9 -10
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +10 -11
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +24 -22
- data/lib/rails_best_practices/reviews/not_rescue_exception_review.rb +1 -1
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
- data/lib/rails_best_practices/reviews/not_use_time_ago_in_words_review.rb +1 -1
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +8 -8
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +35 -32
- data/lib/rails_best_practices/reviews/remove_empty_helpers_review.rb +4 -4
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +20 -17
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +12 -10
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +38 -18
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +11 -11
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +77 -74
- data/lib/rails_best_practices/reviews/review.rb +2 -1
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +2 -3
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +12 -12
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +14 -10
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +15 -15
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +24 -22
- data/lib/rails_best_practices/reviews/use_observer_review.rb +28 -28
- data/lib/rails_best_practices/reviews/use_parentheses_in_method_def_review.rb +6 -6
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +63 -60
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +9 -8
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +16 -14
- data/lib/rails_best_practices/reviews/use_turbo_sprockets_rails3_review.rb +2 -1
- data/lib/rails_best_practices/version.rb +1 -1
- data/rails_best_practices.gemspec +48 -49
- data/spec/fixtures/lib/rails_best_practices/plugins/reviews/not_use_rails_root_review.rb +1 -2
- data/spec/rails_best_practices/analyzer_spec.rb +73 -42
- data/spec/rails_best_practices/core/check_spec.rb +5 -5
- data/spec/rails_best_practices/core/checks_loader_spec.rb +3 -3
- data/spec/rails_best_practices/core/configs_spec.rb +1 -1
- data/spec/rails_best_practices/core/controllers_spec.rb +1 -1
- data/spec/rails_best_practices/core/error_spec.rb +21 -18
- data/spec/rails_best_practices/core/except_methods_spec.rb +7 -7
- data/spec/rails_best_practices/core/gems_spec.rb +4 -4
- data/spec/rails_best_practices/core/helpers_spec.rb +1 -1
- data/spec/rails_best_practices/core/klasses_spec.rb +3 -3
- data/spec/rails_best_practices/core/mailers_spec.rb +1 -1
- data/spec/rails_best_practices/core/methods_spec.rb +6 -6
- data/spec/rails_best_practices/core/model_associations_spec.rb +10 -6
- data/spec/rails_best_practices/core/model_attributes_spec.rb +4 -4
- data/spec/rails_best_practices/core/models_spec.rb +1 -1
- data/spec/rails_best_practices/core/modules_spec.rb +5 -5
- data/spec/rails_best_practices/core/routes_spec.rb +5 -5
- data/spec/rails_best_practices/core/runner_spec.rb +9 -7
- data/spec/rails_best_practices/core_ext/erubis_spec.rb +10 -10
- data/spec/rails_best_practices/inline_disables/inline_disable_spec.rb +62 -0
- data/spec/rails_best_practices/lexicals/long_line_check_spec.rb +32 -31
- data/spec/rails_best_practices/lexicals/remove_tab_check_spec.rb +6 -6
- data/spec/rails_best_practices/lexicals/remove_trailing_whitespace_check_spec.rb +6 -6
- data/spec/rails_best_practices/prepares/config_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/controller_prepare_spec.rb +18 -10
- data/spec/rails_best_practices/prepares/gemfile_prepare_spec.rb +17 -17
- data/spec/rails_best_practices/prepares/helper_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/initializer_prepare_spec.rb +3 -3
- data/spec/rails_best_practices/prepares/mailer_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/prepares/model_prepare_spec.rb +79 -43
- data/spec/rails_best_practices/prepares/route_prepare_spec.rb +141 -76
- data/spec/rails_best_practices/prepares/schema_prepare_spec.rb +2 -2
- data/spec/rails_best_practices/reviews/add_model_virtual_attribute_review_spec.rb +18 -12
- data/spec/rails_best_practices/reviews/always_add_db_index_review_spec.rb +28 -22
- data/spec/rails_best_practices/reviews/check_destroy_return_value_review_spec.rb +15 -13
- data/spec/rails_best_practices/reviews/check_save_return_value_review_spec.rb +31 -21
- data/spec/rails_best_practices/reviews/default_scope_is_evil_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/dry_bundler_in_capistrano_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/hash_syntax_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/isolate_seed_data_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/keep_finders_on_their_own_model_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/law_of_demeter_review_spec.rb +29 -22
- data/spec/rails_best_practices/reviews/move_code_into_controller_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/move_code_into_helper_review_spec.rb +11 -6
- data/spec/rails_best_practices/reviews/move_code_into_model_review_spec.rb +32 -22
- data/spec/rails_best_practices/reviews/move_finder_to_named_scope_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/move_model_logic_into_model_review_spec.rb +9 -7
- data/spec/rails_best_practices/reviews/needless_deep_nesting_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/not_rescue_exception_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/not_use_default_route_review_spec.rb +5 -5
- data/spec/rails_best_practices/reviews/not_use_time_ago_in_words_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/overuse_route_customizations_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/protect_mass_assignment_review_spec.rb +24 -17
- data/spec/rails_best_practices/reviews/remove_empty_helpers_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_controllers_review_spec.rb +47 -32
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +21 -14
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +57 -53
- data/spec/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review_spec.rb +10 -8
- data/spec/rails_best_practices/reviews/replace_instance_variable_with_local_variable_review_spec.rb +20 -14
- data/spec/rails_best_practices/reviews/restrict_auto_generated_routes_review_spec.rb +54 -31
- data/spec/rails_best_practices/reviews/simplify_render_in_controllers_review_spec.rb +9 -9
- data/spec/rails_best_practices/reviews/simplify_render_in_views_review_spec.rb +13 -13
- data/spec/rails_best_practices/reviews/use_before_filter_review_spec.rb +11 -9
- data/spec/rails_best_practices/reviews/use_model_association_review_spec.rb +7 -7
- data/spec/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review_spec.rb +35 -31
- data/spec/rails_best_practices/reviews/use_observer_review_spec.rb +6 -6
- data/spec/rails_best_practices/reviews/use_parentheses_in_method_def_review_spec.rb +10 -8
- data/spec/rails_best_practices/reviews/use_query_attribute_review_spec.rb +31 -24
- data/spec/rails_best_practices/reviews/use_say_with_time_in_migrations_review_spec.rb +15 -11
- data/spec/rails_best_practices/reviews/use_scope_access_review_spec.rb +14 -14
- data/spec/rails_best_practices/reviews/use_turbo_sprockets_rails3_review_spec.rb +61 -59
- metadata +21 -14
@@ -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
|
@@ -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
|
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
|
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
|
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
|
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
|
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
|
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
|
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([])
|
127
|
-
unused_methods +=
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
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] = {
|
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
|
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
|
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
|
-
|
53
|
-
|
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
|
@@ -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 =
|
28
|
-
action_name =
|
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
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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 ||=
|
70
|
-
|
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 ||=
|
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
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
168
|
+
# load all prepares.
|
169
|
+
def load_prepares
|
170
|
+
Prepares.constants.map { |prepare| Prepares.const_get(prepare).new }
|
171
|
+
end
|
157
172
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
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,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
|