rails_best_practices 1.20.0 → 1.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/Gemfile.lock +30 -26
- data/Guardfile +2 -0
- data/Rakefile +2 -0
- data/lib/rails_best_practices.rb +1 -2
- data/lib/rails_best_practices/analyzer.rb +56 -46
- data/lib/rails_best_practices/core/check.rb +39 -32
- data/lib/rails_best_practices/core/checks_loader.rb +8 -6
- 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 +21 -16
- data/lib/rails_best_practices/core/model_associations.rb +9 -4
- 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 +29 -33
- data/lib/rails_best_practices/lexicals/long_line_check.rb +7 -3
- data/lib/rails_best_practices/option_parser.rb +17 -6
- data/lib/rails_best_practices/prepares.rb +1 -1
- data/lib/rails_best_practices/prepares/controller_prepare.rb +15 -3
- data/lib/rails_best_practices/prepares/gemfile_prepare.rb +1 -1
- data/lib/rails_best_practices/prepares/helper_prepare.rb +6 -1
- data/lib/rails_best_practices/prepares/initializer_prepare.rb +2 -2
- data/lib/rails_best_practices/prepares/mailer_prepare.rb +1 -0
- data/lib/rails_best_practices/prepares/model_prepare.rb +52 -12
- data/lib/rails_best_practices/prepares/route_prepare.rb +16 -10
- data/lib/rails_best_practices/reviews/add_model_virtual_attribute_review.rb +15 -13
- data/lib/rails_best_practices/reviews/always_add_db_index_review.rb +34 -29
- data/lib/rails_best_practices/reviews/check_destroy_return_value_review.rb +14 -5
- data/lib/rails_best_practices/reviews/check_save_return_value_review.rb +19 -8
- data/lib/rails_best_practices/reviews/hash_syntax_review.rb +5 -5
- data/lib/rails_best_practices/reviews/isolate_seed_data_review.rb +4 -4
- data/lib/rails_best_practices/reviews/keep_finders_on_their_own_model_review.rb +7 -8
- data/lib/rails_best_practices/reviews/law_of_demeter_review.rb +6 -6
- data/lib/rails_best_practices/reviews/move_code_into_controller_review.rb +1 -1
- data/lib/rails_best_practices/reviews/move_code_into_helper_review.rb +6 -7
- data/lib/rails_best_practices/reviews/move_finder_to_named_scope_review.rb +7 -8
- data/lib/rails_best_practices/reviews/needless_deep_nesting_review.rb +12 -10
- data/lib/rails_best_practices/reviews/not_use_default_route_review.rb +1 -2
- data/lib/rails_best_practices/reviews/overuse_route_customizations_review.rb +5 -5
- data/lib/rails_best_practices/reviews/protect_mass_assignment_review.rb +5 -2
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_controllers_review.rb +6 -3
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_helpers_review.rb +6 -4
- data/lib/rails_best_practices/reviews/remove_unused_methods_in_models_review.rb +29 -9
- data/lib/rails_best_practices/reviews/replace_complex_creation_with_factory_method_review.rb +3 -3
- data/lib/rails_best_practices/reviews/restrict_auto_generated_routes_review.rb +17 -15
- data/lib/rails_best_practices/reviews/simplify_render_in_controllers_review.rb +1 -2
- data/lib/rails_best_practices/reviews/simplify_render_in_views_review.rb +3 -3
- data/lib/rails_best_practices/reviews/use_before_filter_review.rb +2 -1
- data/lib/rails_best_practices/reviews/use_model_association_review.rb +5 -5
- data/lib/rails_best_practices/reviews/use_multipart_alternative_as_content_type_of_email_review.rb +9 -8
- data/lib/rails_best_practices/reviews/use_observer_review.rb +9 -9
- data/lib/rails_best_practices/reviews/use_query_attribute_review.rb +26 -26
- data/lib/rails_best_practices/reviews/use_say_with_time_in_migrations_review.rb +8 -7
- data/lib/rails_best_practices/reviews/use_scope_access_review.rb +17 -15
- 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 +35 -36
- 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 -21
- 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/lexicals/long_line_check_spec.rb +11 -10
- 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 +2 -2
- 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 +138 -77
- 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 +9 -9
- 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 +21 -14
- 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 +26 -16
- 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 -19
- 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 +44 -31
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_helpers_review_spec.rb +17 -12
- data/spec/rails_best_practices/reviews/remove_unused_methods_in_models_review_spec.rb +46 -44
- 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 +16 -10
- 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 +21 -17
- 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 +9 -7
- 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 +10 -8
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e7a2b67944c27b7a11e48f2c490a4ea1080fc4df39ad1d59b20cdeaedfb4b083
|
4
|
+
data.tar.gz: d96f6d7da9909bac895162f70ae1247a98a8df39da7fd2ce17365e959568c4f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 244acdd4067af5f87364236d4fe5bc40d5a4d68b98d1c6786aaf0e53c7766626ef670c4aaa61d793a47bba643fddcd2cbd545a4697dff8d0a3d6f7238912c2b5
|
7
|
+
data.tar.gz: 9b4ca4c95172317d9017691399fcb9211295bad98959477fa8fa24a054a7a6283dc7cdbabd926e925ccf0f3ea9bfe1dde3e594cc42a2db7bd3b37758cb7bdc6e
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rails_best_practices (1.20.
|
4
|
+
rails_best_practices (1.20.1)
|
5
5
|
activesupport
|
6
|
-
code_analyzer (>= 0.5.
|
6
|
+
code_analyzer (>= 0.5.2)
|
7
7
|
erubis
|
8
8
|
i18n
|
9
9
|
json
|
@@ -13,17 +13,17 @@ PATH
|
|
13
13
|
GEM
|
14
14
|
remote: https://rubygems.org/
|
15
15
|
specs:
|
16
|
-
activesupport (6.
|
16
|
+
activesupport (6.1.3.2)
|
17
17
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
-
i18n (>=
|
19
|
-
minitest (
|
20
|
-
tzinfo (~>
|
21
|
-
zeitwerk (~> 2.
|
18
|
+
i18n (>= 1.6, < 2)
|
19
|
+
minitest (>= 5.1)
|
20
|
+
tzinfo (~> 2.0)
|
21
|
+
zeitwerk (~> 2.3)
|
22
22
|
awesome_print (1.8.0)
|
23
|
-
code_analyzer (0.5.
|
23
|
+
code_analyzer (0.5.2)
|
24
24
|
sexp_processor
|
25
25
|
coderay (1.1.2)
|
26
|
-
concurrent-ruby (1.1.
|
26
|
+
concurrent-ruby (1.1.8)
|
27
27
|
coveralls (0.8.23)
|
28
28
|
json (>= 1.8, < 3)
|
29
29
|
simplecov (~> 0.16.1)
|
@@ -50,7 +50,7 @@ GEM
|
|
50
50
|
haml (5.1.2)
|
51
51
|
temple (>= 0.8.0)
|
52
52
|
tilt
|
53
|
-
i18n (1.
|
53
|
+
i18n (1.8.10)
|
54
54
|
concurrent-ruby (~> 1.0)
|
55
55
|
json (2.3.0)
|
56
56
|
listen (3.2.1)
|
@@ -58,7 +58,7 @@ GEM
|
|
58
58
|
rb-inotify (~> 0.9, >= 0.9.10)
|
59
59
|
lumberjack (1.0.13)
|
60
60
|
method_source (0.9.2)
|
61
|
-
minitest (5.
|
61
|
+
minitest (5.14.4)
|
62
62
|
nenv (0.3.0)
|
63
63
|
notiffany (0.1.3)
|
64
64
|
nenv (~> 0.1)
|
@@ -71,16 +71,21 @@ GEM
|
|
71
71
|
rb-inotify (0.10.1)
|
72
72
|
ffi (~> 1.0)
|
73
73
|
require_all (3.0.0)
|
74
|
-
rspec (
|
75
|
-
rspec-core (~>
|
76
|
-
rspec-expectations (~>
|
77
|
-
rspec-mocks (~>
|
78
|
-
rspec-core (
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
74
|
+
rspec (3.9.0)
|
75
|
+
rspec-core (~> 3.9.0)
|
76
|
+
rspec-expectations (~> 3.9.0)
|
77
|
+
rspec-mocks (~> 3.9.0)
|
78
|
+
rspec-core (3.9.1)
|
79
|
+
rspec-support (~> 3.9.1)
|
80
|
+
rspec-expectations (3.9.1)
|
81
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
82
|
+
rspec-support (~> 3.9.0)
|
83
|
+
rspec-mocks (3.9.1)
|
84
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
85
|
+
rspec-support (~> 3.9.0)
|
86
|
+
rspec-support (3.9.2)
|
87
|
+
ruby-progressbar (1.11.0)
|
88
|
+
sexp_processor (4.15.2)
|
84
89
|
shellany (0.0.1)
|
85
90
|
simplecov (0.16.1)
|
86
91
|
docile (~> 1.1)
|
@@ -94,12 +99,11 @@ GEM
|
|
94
99
|
term-ansicolor (1.7.1)
|
95
100
|
tins (~> 1.0)
|
96
101
|
thor (1.0.1)
|
97
|
-
thread_safe (0.3.6)
|
98
102
|
tilt (2.0.10)
|
99
103
|
tins (1.22.2)
|
100
|
-
tzinfo (
|
101
|
-
|
102
|
-
zeitwerk (2.
|
104
|
+
tzinfo (2.0.4)
|
105
|
+
concurrent-ruby (~> 1.0)
|
106
|
+
zeitwerk (2.4.2)
|
103
107
|
|
104
108
|
PLATFORMS
|
105
109
|
ruby
|
@@ -114,7 +118,7 @@ DEPENDENCIES
|
|
114
118
|
pry
|
115
119
|
rails_best_practices!
|
116
120
|
rake
|
117
|
-
rspec
|
121
|
+
rspec
|
118
122
|
slim
|
119
123
|
|
120
124
|
BUNDLED WITH
|
data/Guardfile
CHANGED
data/Rakefile
CHANGED
data/lib/rails_best_practices.rb
CHANGED
@@ -104,30 +104,31 @@ module RailsBestPractices
|
|
104
104
|
#
|
105
105
|
# @return [Array] all files for parsing
|
106
106
|
def parse_files
|
107
|
-
@parse_files ||=
|
108
|
-
|
109
|
-
|
107
|
+
@parse_files ||=
|
108
|
+
begin
|
109
|
+
files = expand_dirs_to_files(@path)
|
110
|
+
files = file_sort(files)
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
if @options['only'].present?
|
113
|
+
files = file_accept(files, @options['only'])
|
114
|
+
end
|
114
115
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
116
|
+
# By default, tmp, vender, spec, test, features are ignored.
|
117
|
+
%w[vendor spec test features tmp].each do |dir|
|
118
|
+
files = file_ignore(files, File.join(@path, dir)) unless @options[dir]
|
119
|
+
end
|
119
120
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
121
|
+
# Exclude files based on exclude regexes if the option is set.
|
122
|
+
@options['exclude'].each do |pattern|
|
123
|
+
files = file_ignore(files, pattern)
|
124
|
+
end
|
124
125
|
|
125
|
-
|
126
|
-
|
127
|
-
|
126
|
+
%w[Capfile Gemfile Gemfile.lock].each do |file|
|
127
|
+
files.unshift File.join(@path, file)
|
128
|
+
end
|
128
129
|
|
129
|
-
|
130
|
-
|
130
|
+
files.compact
|
131
|
+
end
|
131
132
|
end
|
132
133
|
|
133
134
|
# expand all files with extenstion rb, erb, haml, slim, builder and rxml under the dirs
|
@@ -158,7 +159,10 @@ module RailsBestPractices
|
|
158
159
|
models = files.find_all { |file| file =~ Core::Check::MODEL_FILES }
|
159
160
|
mailers = files.find_all { |file| file =~ Core::Check::MAILER_FILES }
|
160
161
|
helpers = files.find_all { |file| file =~ Core::Check::HELPER_FILES }
|
161
|
-
others =
|
162
|
+
others =
|
163
|
+
files.find_all do |file|
|
164
|
+
file !~ Core::Check::MAILER_FILES && file !~ Core::Check::MODEL_FILES && file !~ Core::Check::HELPER_FILES
|
165
|
+
end
|
162
166
|
models + mailers + helpers + others
|
163
167
|
end
|
164
168
|
|
@@ -194,7 +198,10 @@ module RailsBestPractices
|
|
194
198
|
def load_hg_info
|
195
199
|
hg_progressbar = ProgressBar.create(title: 'Hg Info', total: errors.size) if display_bar?
|
196
200
|
errors.each do |error|
|
197
|
-
|
201
|
+
info_command = "cd #{@runner.class.base_path}"
|
202
|
+
info_command += " && hg blame -lvcu #{error.filename[@runner.class.base_path.size..-1].gsub(%r{^/}, '')}"
|
203
|
+
info_command += " | sed -n /:#{error.line_number.split(',').first}:/p"
|
204
|
+
hg_info = system(info_command)
|
198
205
|
unless hg_info == ''
|
199
206
|
hg_commit_username = hg_info.split(':')[0].strip
|
200
207
|
error.hg_username = hg_commit_username.split(/\ /)[0..-2].join(' ')
|
@@ -208,9 +215,11 @@ module RailsBestPractices
|
|
208
215
|
# load git commit and git username info.
|
209
216
|
def load_git_info
|
210
217
|
git_progressbar = ProgressBar.create(title: 'Git Info', total: errors.size) if display_bar?
|
211
|
-
start = @runner.class.base_path =~
|
218
|
+
start = @runner.class.base_path =~ %r{/$} ? @runner.class.base_path.size : @runner.class.base_path.size + 1
|
212
219
|
errors.each do |error|
|
213
|
-
|
220
|
+
info_command = "cd #{@runner.class.base_path}"
|
221
|
+
info_command += " && git blame -L #{error.line_number.split(',').first},+1 #{error.filename[start..-1]}"
|
222
|
+
git_info = system(info_command)
|
214
223
|
unless git_info == ''
|
215
224
|
git_commit, git_username = git_info.split(/\d{4}-\d{2}-\d{2}/).first.split('(')
|
216
225
|
error.git_commit = git_commit.split(' ').first.strip
|
@@ -224,7 +233,10 @@ module RailsBestPractices
|
|
224
233
|
# output errors with html format.
|
225
234
|
def output_html_errors
|
226
235
|
require 'erubis'
|
227
|
-
template =
|
236
|
+
template =
|
237
|
+
@options['template'] ?
|
238
|
+
File.read(File.expand_path(@options['template'])) :
|
239
|
+
File.read(File.join(File.dirname(__FILE__), '..', '..', 'assets', 'result.html.erb'))
|
228
240
|
|
229
241
|
if @options['with-github']
|
230
242
|
last_commit_id = @options['last-commit-id'] || `cd #{@runner.class.base_path} && git rev-parse HEAD`.chomp
|
@@ -235,27 +247,28 @@ module RailsBestPractices
|
|
235
247
|
File.open(@options['output-file'], 'w+') do |file|
|
236
248
|
eruby = Erubis::Eruby.new(template)
|
237
249
|
file.puts eruby.evaluate(
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
+
errors: errors,
|
251
|
+
error_types: error_types,
|
252
|
+
textmate: @options['with-textmate'],
|
253
|
+
vscode: @options['with-vscode'],
|
254
|
+
sublime: @options['with-sublime'],
|
255
|
+
mvim: @options['with-mvim'],
|
256
|
+
github: @options['with-github'],
|
257
|
+
github_name: @options['github-name'],
|
258
|
+
last_commit_id: last_commit_id,
|
259
|
+
git: @options['with-git'],
|
260
|
+
hg: @options['with-hg']
|
261
|
+
)
|
250
262
|
end
|
251
263
|
end
|
252
264
|
|
253
265
|
def output_xml_errors
|
254
266
|
require 'rexml/document'
|
255
267
|
|
256
|
-
document =
|
257
|
-
|
258
|
-
|
268
|
+
document =
|
269
|
+
REXML::Document.new.tap do |d|
|
270
|
+
d << REXML::XMLDecl.new
|
271
|
+
end
|
259
272
|
|
260
273
|
checkstyle = REXML::Element.new('checkstyle', document)
|
261
274
|
|
@@ -289,13 +302,10 @@ module RailsBestPractices
|
|
289
302
|
|
290
303
|
# output errors with json format.
|
291
304
|
def output_json_errors
|
292
|
-
errors_as_hashes =
|
293
|
-
|
294
|
-
filename:
|
295
|
-
|
296
|
-
message: err.message
|
297
|
-
}
|
298
|
-
end
|
305
|
+
errors_as_hashes =
|
306
|
+
errors.map do |err|
|
307
|
+
{ filename: err.filename, line_number: err.line_number, message: err.message }
|
308
|
+
end
|
299
309
|
|
300
310
|
File.open(@options['output-file'], 'w+') do |file|
|
301
311
|
file.write JSON.dump(errors_as_hashes)
|
@@ -3,24 +3,25 @@
|
|
3
3
|
module RailsBestPractices
|
4
4
|
module Core
|
5
5
|
# A Check class that takes charge of checking the sexp.
|
6
|
+
|
6
7
|
class Check < CodeAnalyzer::Checker
|
7
|
-
ALL_FILES =
|
8
|
-
CONTROLLER_FILES = /
|
9
|
-
MIGRATION_FILES = /
|
10
|
-
MODEL_FILES = /
|
11
|
-
MAILER_FILES = /
|
12
|
-
VIEW_FILES = /
|
13
|
-
PARTIAL_VIEW_FILES = /
|
14
|
-
ROUTE_FILES = /
|
15
|
-
SCHEMA_FILE = /
|
16
|
-
HELPER_FILES = /
|
17
|
-
DEPLOY_FILES = /
|
18
|
-
CONFIG_FILES = /
|
19
|
-
INITIALIZER_FILES = /
|
20
|
-
CAPFILE = /Capfile
|
21
|
-
GEMFILE_LOCK = /Gemfile\.lock
|
22
|
-
|
23
|
-
SKIP_FILES = /
|
8
|
+
ALL_FILES = /.*/.freeze
|
9
|
+
CONTROLLER_FILES = %r{app/(controllers|cells)/.*\.rb$}.freeze
|
10
|
+
MIGRATION_FILES = %r{db/migrate/.*\.rb$}.freeze
|
11
|
+
MODEL_FILES = %r{app/models/.*\.rb$}.freeze
|
12
|
+
MAILER_FILES = %r{app/models/.*mailer\.rb$|app/mailers/.*\.rb}.freeze
|
13
|
+
VIEW_FILES = %r{app/(views|cells)/.*\.(erb|haml|slim|builder|rxml)$}.freeze
|
14
|
+
PARTIAL_VIEW_FILES = %r{app/(views|cells)/.*/_.*\.(erb|haml|slim|builder|rxml)$}.freeze
|
15
|
+
ROUTE_FILES = %r{config/routes.*\.rb}.freeze
|
16
|
+
SCHEMA_FILE = %r{db/schema\.rb}.freeze
|
17
|
+
HELPER_FILES = %r{app/helpers/.*\.rb$}.freeze
|
18
|
+
DEPLOY_FILES = %r{config/deploy.*\.rb}.freeze
|
19
|
+
CONFIG_FILES = %r{config/(application|environment|environments/.*)\.rb}.freeze
|
20
|
+
INITIALIZER_FILES = %r{config/initializers/.*\.rb}.freeze
|
21
|
+
CAPFILE = /Capfile/.freeze
|
22
|
+
GEMFILE_LOCK = /Gemfile\.lock/.freeze
|
23
|
+
|
24
|
+
SKIP_FILES = %r{db/schema.rb}.freeze
|
24
25
|
|
25
26
|
def initialize(options = {})
|
26
27
|
options.each do |key, value|
|
@@ -33,7 +34,7 @@ module RailsBestPractices
|
|
33
34
|
# @param [String] the file name of node.
|
34
35
|
# @return [Boolean] true if the check will need to parse the file.
|
35
36
|
def parse_file?(node_file)
|
36
|
-
is_interesting_file?(node_file)
|
37
|
+
node_file.is_a?(String) && is_interesting_file?(node_file) && !is_ignored?(node_file)
|
37
38
|
end
|
38
39
|
|
39
40
|
def is_interesting_file?(node_file)
|
@@ -60,13 +61,10 @@ module RailsBestPractices
|
|
60
61
|
# @param [String] filename, is the filename of source code
|
61
62
|
# @param [Integer] line_number, is the line number of the source code which is reviewing
|
62
63
|
def add_error(message, filename = @node.file, line_number = @node.line_number)
|
63
|
-
errors <<
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
type: self.class.to_s,
|
68
|
-
url: url
|
69
|
-
)
|
64
|
+
errors <<
|
65
|
+
RailsBestPractices::Core::Error.new(
|
66
|
+
filename: filename, line_number: line_number, message: message, type: self.class.to_s, url: url
|
67
|
+
)
|
70
68
|
end
|
71
69
|
|
72
70
|
# errors that violate the rails best practices.
|
@@ -99,9 +97,9 @@ module RailsBestPractices
|
|
99
97
|
end
|
100
98
|
end
|
101
99
|
|
102
|
-
class <<self
|
100
|
+
class << self
|
103
101
|
def url(url = nil)
|
104
|
-
url ?
|
102
|
+
url ? @url = url : @url
|
105
103
|
end
|
106
104
|
|
107
105
|
def debug?
|
@@ -114,6 +112,7 @@ module RailsBestPractices
|
|
114
112
|
end
|
115
113
|
|
116
114
|
# Helper to parse the class name.
|
115
|
+
|
117
116
|
module Classable
|
118
117
|
def self.included(base)
|
119
118
|
base.class_eval do
|
@@ -165,6 +164,7 @@ module RailsBestPractices
|
|
165
164
|
end
|
166
165
|
|
167
166
|
# Helper to parse the module name.
|
167
|
+
|
168
168
|
module Moduleable
|
169
169
|
def self.included(base)
|
170
170
|
base.class_eval do
|
@@ -197,7 +197,15 @@ module RailsBestPractices
|
|
197
197
|
module Callable
|
198
198
|
def self.included(base)
|
199
199
|
base.class_eval do
|
200
|
-
interesting_nodes :call,
|
200
|
+
interesting_nodes :call,
|
201
|
+
:fcall,
|
202
|
+
:var_ref,
|
203
|
+
:vcall,
|
204
|
+
:command_call,
|
205
|
+
:command,
|
206
|
+
:alias,
|
207
|
+
:bare_assoc_hash,
|
208
|
+
:method_add_arg
|
201
209
|
|
202
210
|
# remembe the message of call node.
|
203
211
|
add_callback :start_call do |node|
|
@@ -371,10 +379,8 @@ module RailsBestPractices
|
|
371
379
|
class_name = '.*' if class_name == '*'
|
372
380
|
class_expression = Regexp.new class_name
|
373
381
|
|
374
|
-
class_names =
|
375
|
-
|
376
|
-
.map(&:extend_class_name)
|
377
|
-
.compact
|
382
|
+
class_names =
|
383
|
+
Prepares.klasses.select { |klass| klass.class_name == method.class_name }.map(&:extend_class_name).compact
|
378
384
|
|
379
385
|
class_names.unshift method.class_name
|
380
386
|
matched = class_names.any? { |name| name =~ class_expression }
|
@@ -385,6 +391,7 @@ module RailsBestPractices
|
|
385
391
|
end
|
386
392
|
|
387
393
|
# Helper to parse the access control.
|
394
|
+
|
388
395
|
module Accessable
|
389
396
|
def self.included(base)
|
390
397
|
base.class_eval do
|
@@ -14,17 +14,19 @@ module RailsBestPractices
|
|
14
14
|
|
15
15
|
# load all reviews according to configuration.
|
16
16
|
def load_reviews
|
17
|
-
load_checks_from_config
|
17
|
+
load_checks_from_config do |check_name|
|
18
|
+
RailsBestPractices::Reviews.const_get(check_name.gsub(/Check$/, 'Review'))
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
22
|
private
|
21
23
|
|
22
|
-
|
24
|
+
# read the checks from yaml config.
|
23
25
|
def checks_from_config
|
24
26
|
@checks ||= YAML.load_file @config
|
25
27
|
end
|
26
28
|
|
27
|
-
|
29
|
+
# load all checks from the configuration
|
28
30
|
def load_checks_from_config(&block)
|
29
31
|
checks_from_config.each_with_object([]) do |check, active_checks|
|
30
32
|
check_instance = instantiate_check(block, *check)
|
@@ -32,13 +34,13 @@ module RailsBestPractices
|
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
|
37
|
+
# instantiates a check
|
36
38
|
def instantiate_check(block, check_name, options)
|
37
39
|
check_class = load_check_class(check_name, &block)
|
38
|
-
check_class
|
40
|
+
check_class&.new(options || {})
|
39
41
|
end
|
40
42
|
|
41
|
-
|
43
|
+
# loads the class for a check by calling the given block
|
42
44
|
def load_check_class(check_name)
|
43
45
|
yield(check_name)
|
44
46
|
rescue NameError
|