rubocop 0.70.0 → 0.72.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -10
- data/config/default.yml +50 -491
- data/lib/rubocop.rb +5 -53
- data/lib/rubocop/ast/builder.rb +2 -0
- data/lib/rubocop/ast/node.rb +1 -1
- data/lib/rubocop/ast/node/float_node.rb +12 -0
- data/lib/rubocop/ast/node/int_node.rb +12 -0
- data/lib/rubocop/ast/node/mixin/numeric_node.rb +21 -0
- data/lib/rubocop/ast/node/resbody_node.rb +1 -6
- data/lib/rubocop/cached_data.rb +1 -1
- data/lib/rubocop/config.rb +35 -6
- data/lib/rubocop/config_loader.rb +2 -2
- data/lib/rubocop/config_loader_resolver.rb +0 -6
- data/lib/rubocop/cop/cop.rb +0 -4
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +55 -0
- data/lib/rubocop/cop/layout/class_structure.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +3 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -0
- data/lib/rubocop/cop/layout/indent_first_argument.rb +6 -2
- data/lib/rubocop/cop/layout/indent_first_parameter.rb +7 -3
- data/lib/rubocop/cop/layout/indent_heredoc.rb +0 -1
- data/lib/rubocop/cop/layout/indentation_consistency.rb +13 -12
- data/lib/rubocop/cop/layout/indentation_width.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +2 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_alignment.rb +4 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +20 -22
- data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -1
- data/lib/rubocop/cop/style/float_division.rb +94 -0
- data/lib/rubocop/cop/style/format_string.rb +7 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +42 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +12 -2
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -0
- data/lib/rubocop/cop/style/word_array.rb +2 -2
- data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
- data/lib/rubocop/node_pattern.rb +84 -5
- data/lib/rubocop/options.rb +0 -2
- data/lib/rubocop/processed_source.rb +5 -1
- data/lib/rubocop/rspec/cop_helper.rb +0 -1
- data/lib/rubocop/rspec/shared_contexts.rb +0 -17
- data/lib/rubocop/rspec/support.rb +0 -1
- data/lib/rubocop/runner.rb +6 -7
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop/yaml_duplication_checker.rb +8 -2
- metadata +7 -69
- data/lib/rubocop/cop/mixin/target_rails_version.rb +0 -16
- data/lib/rubocop/cop/rails/action_filter.rb +0 -117
- data/lib/rubocop/cop/rails/active_record_aliases.rb +0 -48
- data/lib/rubocop/cop/rails/active_record_override.rb +0 -82
- data/lib/rubocop/cop/rails/active_support_aliases.rb +0 -69
- data/lib/rubocop/cop/rails/application_job.rb +0 -40
- data/lib/rubocop/cop/rails/application_record.rb +0 -40
- data/lib/rubocop/cop/rails/assert_not.rb +0 -44
- data/lib/rubocop/cop/rails/belongs_to.rb +0 -102
- data/lib/rubocop/cop/rails/blank.rb +0 -164
- data/lib/rubocop/cop/rails/bulk_change_table.rb +0 -289
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +0 -91
- data/lib/rubocop/cop/rails/date.rb +0 -161
- data/lib/rubocop/cop/rails/delegate.rb +0 -132
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +0 -37
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +0 -91
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +0 -45
- data/lib/rubocop/cop/rails/environment_comparison.rb +0 -68
- data/lib/rubocop/cop/rails/exit.rb +0 -67
- data/lib/rubocop/cop/rails/file_path.rb +0 -108
- data/lib/rubocop/cop/rails/find_by.rb +0 -55
- data/lib/rubocop/cop/rails/find_each.rb +0 -51
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +0 -25
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +0 -106
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +0 -117
- data/lib/rubocop/cop/rails/http_status.rb +0 -179
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +0 -94
- data/lib/rubocop/cop/rails/inverse_of.rb +0 -246
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +0 -175
- data/lib/rubocop/cop/rails/link_to_blank.rb +0 -98
- data/lib/rubocop/cop/rails/not_null_column.rb +0 -67
- data/lib/rubocop/cop/rails/output.rb +0 -49
- data/lib/rubocop/cop/rails/output_safety.rb +0 -99
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +0 -107
- data/lib/rubocop/cop/rails/presence.rb +0 -124
- data/lib/rubocop/cop/rails/present.rb +0 -153
- data/lib/rubocop/cop/rails/read_write_attribute.rb +0 -74
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +0 -111
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +0 -136
- data/lib/rubocop/cop/rails/reflection_class_name.rb +0 -37
- data/lib/rubocop/cop/rails/refute_methods.rb +0 -76
- data/lib/rubocop/cop/rails/relative_date_constant.rb +0 -93
- data/lib/rubocop/cop/rails/request_referer.rb +0 -56
- data/lib/rubocop/cop/rails/reversible_migration.rb +0 -286
- data/lib/rubocop/cop/rails/safe_navigation.rb +0 -87
- data/lib/rubocop/cop/rails/save_bang.rb +0 -316
- data/lib/rubocop/cop/rails/scope_args.rb +0 -29
- data/lib/rubocop/cop/rails/skips_model_validations.rb +0 -87
- data/lib/rubocop/cop/rails/time_zone.rb +0 -238
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +0 -105
- data/lib/rubocop/cop/rails/unknown_env.rb +0 -63
- data/lib/rubocop/cop/rails/validation.rb +0 -109
- data/lib/rubocop/rspec/shared_examples.rb +0 -59
data/lib/rubocop/options.rb
CHANGED
@@ -159,7 +159,6 @@ module RuboCop
|
|
159
159
|
option(opts, '-D', '--[no-]display-cop-names')
|
160
160
|
option(opts, '-E', '--extra-details')
|
161
161
|
option(opts, '-S', '--display-style-guide')
|
162
|
-
option(opts, '-R', '--rails')
|
163
162
|
option(opts, '-a', '--auto-correct')
|
164
163
|
option(opts, '--ignore-disable-comments')
|
165
164
|
|
@@ -429,7 +428,6 @@ module RuboCop
|
|
429
428
|
'Default is true.'],
|
430
429
|
display_style_guide: 'Display style guide URLs in offense messages.',
|
431
430
|
extra_details: 'Display extra details in offense messages.',
|
432
|
-
rails: 'Run extra Rails cops.',
|
433
431
|
lint: 'Run only lint cops.',
|
434
432
|
safe: 'Run only safe cops.',
|
435
433
|
list_target_files: 'List all files RuboCop will inspect.',
|
@@ -183,8 +183,12 @@ module RuboCop
|
|
183
183
|
when 2.6
|
184
184
|
require 'parser/ruby26'
|
185
185
|
Parser::Ruby26
|
186
|
+
when 2.7
|
187
|
+
require 'parser/ruby27'
|
188
|
+
Parser::Ruby27
|
186
189
|
else
|
187
|
-
raise ArgumentError,
|
190
|
+
raise ArgumentError,
|
191
|
+
"RuboCop found unknown Ruby version: #{ruby_version.inspect}"
|
188
192
|
end
|
189
193
|
end
|
190
194
|
# rubocop:enable Metrics/MethodLength
|
@@ -47,7 +47,6 @@ RSpec.shared_context 'config', :config do
|
|
47
47
|
end
|
48
48
|
|
49
49
|
hash = { 'AllCops' => { 'TargetRubyVersion' => ruby_version } }
|
50
|
-
hash['Rails'] = { 'Enabled' => true } if enabled_rails
|
51
50
|
hash['AllCops']['TargetRailsVersion'] = rails_version if rails_version
|
52
51
|
if respond_to?(:cop_config)
|
53
52
|
cop_name = described_class.cop_name
|
@@ -77,19 +76,3 @@ end
|
|
77
76
|
RSpec.shared_context 'ruby 2.6', :ruby26 do
|
78
77
|
let(:ruby_version) { 2.6 }
|
79
78
|
end
|
80
|
-
|
81
|
-
RSpec.shared_context 'with Rails', :enabled_rails do
|
82
|
-
let(:enabled_rails) { true }
|
83
|
-
end
|
84
|
-
|
85
|
-
RSpec.shared_context 'with Rails 3', :rails3 do
|
86
|
-
let(:rails_version) { 3.0 }
|
87
|
-
end
|
88
|
-
|
89
|
-
RSpec.shared_context 'with Rails 4', :rails4 do
|
90
|
-
let(:rails_version) { 4.0 }
|
91
|
-
end
|
92
|
-
|
93
|
-
RSpec.shared_context 'with Rails 5', :rails5 do
|
94
|
-
let(:rails_version) { 5.0 }
|
95
|
-
end
|
data/lib/rubocop/runner.rb
CHANGED
@@ -72,7 +72,12 @@ module RuboCop
|
|
72
72
|
|
73
73
|
each_inspected_file(files) { |file| inspected_files << file }
|
74
74
|
ensure
|
75
|
-
ResultCache.cleanup
|
75
|
+
# OPTIMIZE: Calling `ResultCache.cleanup` takes time. This optimization
|
76
|
+
# mainly targets editors that integrates RuboCop. When RuboCop is run
|
77
|
+
# by an editor, it should be inspecting only one file.
|
78
|
+
if files.size > 1 && cached_run?
|
79
|
+
ResultCache.cleanup(@config_store, @options[:debug])
|
80
|
+
end
|
76
81
|
formatter_set.finished(inspected_files.freeze)
|
77
82
|
formatter_set.close_output_files
|
78
83
|
end
|
@@ -268,7 +273,6 @@ module RuboCop
|
|
268
273
|
|
269
274
|
def inspect_file(processed_source)
|
270
275
|
config = @config_store.for(processed_source.path)
|
271
|
-
enable_rails_cops(config) if @options[:rails]
|
272
276
|
team = Cop::Team.new(mobilized_cop_classes(config), config, @options)
|
273
277
|
offenses = team.inspect_file(processed_source)
|
274
278
|
@errors.concat(team.errors)
|
@@ -276,11 +280,6 @@ module RuboCop
|
|
276
280
|
[offenses, team.updated_source_file?]
|
277
281
|
end
|
278
282
|
|
279
|
-
def enable_rails_cops(config)
|
280
|
-
config['Rails'] ||= {}
|
281
|
-
config['Rails']['Enabled'] = true
|
282
|
-
end
|
283
|
-
|
284
283
|
def mobilized_cop_classes(config)
|
285
284
|
@mobilized_cop_classes ||= {}
|
286
285
|
@mobilized_cop_classes[config.object_id] ||= begin
|
data/lib/rubocop/version.rb
CHANGED
@@ -4,8 +4,14 @@ module RuboCop
|
|
4
4
|
# Find duplicated keys from YAML.
|
5
5
|
module YAMLDuplicationChecker
|
6
6
|
def self.check(yaml_string, filename, &on_duplicated)
|
7
|
-
#
|
8
|
-
tree =
|
7
|
+
# Ruby 2.6+
|
8
|
+
tree = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
|
9
|
+
# Specify filename to display helpful message when it raises
|
10
|
+
# an error.
|
11
|
+
YAML.parse(yaml_string, filename: filename)
|
12
|
+
else
|
13
|
+
YAML.parse(yaml_string, filename)
|
14
|
+
end
|
9
15
|
return unless tree
|
10
16
|
|
11
17
|
traverse(tree, &on_duplicated)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.72.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: exe
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-
|
13
|
+
date: 2019-06-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: jaro_winkler
|
@@ -128,20 +128,6 @@ dependencies:
|
|
128
128
|
- - "<"
|
129
129
|
- !ruby/object:Gem::Version
|
130
130
|
version: '3.0'
|
131
|
-
- !ruby/object:Gem::Dependency
|
132
|
-
name: rack
|
133
|
-
requirement: !ruby/object:Gem::Requirement
|
134
|
-
requirements:
|
135
|
-
- - ">="
|
136
|
-
- !ruby/object:Gem::Version
|
137
|
-
version: '2.0'
|
138
|
-
type: :development
|
139
|
-
prerelease: false
|
140
|
-
version_requirements: !ruby/object:Gem::Requirement
|
141
|
-
requirements:
|
142
|
-
- - ">="
|
143
|
-
- !ruby/object:Gem::Version
|
144
|
-
version: '2.0'
|
145
131
|
description: |2
|
146
132
|
Automatic Ruby code style checking tool.
|
147
133
|
Aims to enforce the community-driven Ruby Style Guide.
|
@@ -175,9 +161,11 @@ files:
|
|
175
161
|
- lib/rubocop/ast/node/def_node.rb
|
176
162
|
- lib/rubocop/ast/node/defined_node.rb
|
177
163
|
- lib/rubocop/ast/node/ensure_node.rb
|
164
|
+
- lib/rubocop/ast/node/float_node.rb
|
178
165
|
- lib/rubocop/ast/node/for_node.rb
|
179
166
|
- lib/rubocop/ast/node/hash_node.rb
|
180
167
|
- lib/rubocop/ast/node/if_node.rb
|
168
|
+
- lib/rubocop/ast/node/int_node.rb
|
181
169
|
- lib/rubocop/ast/node/keyword_splat_node.rb
|
182
170
|
- lib/rubocop/ast/node/mixin/basic_literal_node.rb
|
183
171
|
- lib/rubocop/ast/node/mixin/binary_operator_node.rb
|
@@ -187,6 +175,7 @@ files:
|
|
187
175
|
- lib/rubocop/ast/node/mixin/method_dispatch_node.rb
|
188
176
|
- lib/rubocop/ast/node/mixin/method_identifier_predicates.rb
|
189
177
|
- lib/rubocop/ast/node/mixin/modifier_node.rb
|
178
|
+
- lib/rubocop/ast/node/mixin/numeric_node.rb
|
190
179
|
- lib/rubocop/ast/node/mixin/parameterized_node.rb
|
191
180
|
- lib/rubocop/ast/node/mixin/predicate_operator_node.rb
|
192
181
|
- lib/rubocop/ast/node/module_node.rb
|
@@ -242,6 +231,7 @@ files:
|
|
242
231
|
- lib/rubocop/cop/gemspec/duplicated_assignment.rb
|
243
232
|
- lib/rubocop/cop/gemspec/ordered_dependencies.rb
|
244
233
|
- lib/rubocop/cop/gemspec/required_ruby_version.rb
|
234
|
+
- lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb
|
245
235
|
- lib/rubocop/cop/generator.rb
|
246
236
|
- lib/rubocop/cop/generator/configuration_injector.rb
|
247
237
|
- lib/rubocop/cop/generator/require_file_injector.rb
|
@@ -488,7 +478,6 @@ files:
|
|
488
478
|
- lib/rubocop/cop/mixin/string_help.rb
|
489
479
|
- lib/rubocop/cop/mixin/string_literals_help.rb
|
490
480
|
- lib/rubocop/cop/mixin/surrounding_space.rb
|
491
|
-
- lib/rubocop/cop/mixin/target_rails_version.rb
|
492
481
|
- lib/rubocop/cop/mixin/target_ruby_version.rb
|
493
482
|
- lib/rubocop/cop/mixin/too_many_lines.rb
|
494
483
|
- lib/rubocop/cop/mixin/trailing_body.rb
|
@@ -512,57 +501,6 @@ files:
|
|
512
501
|
- lib/rubocop/cop/naming/variable_name.rb
|
513
502
|
- lib/rubocop/cop/naming/variable_number.rb
|
514
503
|
- lib/rubocop/cop/offense.rb
|
515
|
-
- lib/rubocop/cop/rails/action_filter.rb
|
516
|
-
- lib/rubocop/cop/rails/active_record_aliases.rb
|
517
|
-
- lib/rubocop/cop/rails/active_record_override.rb
|
518
|
-
- lib/rubocop/cop/rails/active_support_aliases.rb
|
519
|
-
- lib/rubocop/cop/rails/application_job.rb
|
520
|
-
- lib/rubocop/cop/rails/application_record.rb
|
521
|
-
- lib/rubocop/cop/rails/assert_not.rb
|
522
|
-
- lib/rubocop/cop/rails/belongs_to.rb
|
523
|
-
- lib/rubocop/cop/rails/blank.rb
|
524
|
-
- lib/rubocop/cop/rails/bulk_change_table.rb
|
525
|
-
- lib/rubocop/cop/rails/create_table_with_timestamps.rb
|
526
|
-
- lib/rubocop/cop/rails/date.rb
|
527
|
-
- lib/rubocop/cop/rails/delegate.rb
|
528
|
-
- lib/rubocop/cop/rails/delegate_allow_blank.rb
|
529
|
-
- lib/rubocop/cop/rails/dynamic_find_by.rb
|
530
|
-
- lib/rubocop/cop/rails/enum_uniqueness.rb
|
531
|
-
- lib/rubocop/cop/rails/environment_comparison.rb
|
532
|
-
- lib/rubocop/cop/rails/exit.rb
|
533
|
-
- lib/rubocop/cop/rails/file_path.rb
|
534
|
-
- lib/rubocop/cop/rails/find_by.rb
|
535
|
-
- lib/rubocop/cop/rails/find_each.rb
|
536
|
-
- lib/rubocop/cop/rails/has_and_belongs_to_many.rb
|
537
|
-
- lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
|
538
|
-
- lib/rubocop/cop/rails/http_positional_arguments.rb
|
539
|
-
- lib/rubocop/cop/rails/http_status.rb
|
540
|
-
- lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb
|
541
|
-
- lib/rubocop/cop/rails/inverse_of.rb
|
542
|
-
- lib/rubocop/cop/rails/lexically_scoped_action_filter.rb
|
543
|
-
- lib/rubocop/cop/rails/link_to_blank.rb
|
544
|
-
- lib/rubocop/cop/rails/not_null_column.rb
|
545
|
-
- lib/rubocop/cop/rails/output.rb
|
546
|
-
- lib/rubocop/cop/rails/output_safety.rb
|
547
|
-
- lib/rubocop/cop/rails/pluralization_grammar.rb
|
548
|
-
- lib/rubocop/cop/rails/presence.rb
|
549
|
-
- lib/rubocop/cop/rails/present.rb
|
550
|
-
- lib/rubocop/cop/rails/read_write_attribute.rb
|
551
|
-
- lib/rubocop/cop/rails/redundant_allow_nil.rb
|
552
|
-
- lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb
|
553
|
-
- lib/rubocop/cop/rails/reflection_class_name.rb
|
554
|
-
- lib/rubocop/cop/rails/refute_methods.rb
|
555
|
-
- lib/rubocop/cop/rails/relative_date_constant.rb
|
556
|
-
- lib/rubocop/cop/rails/request_referer.rb
|
557
|
-
- lib/rubocop/cop/rails/reversible_migration.rb
|
558
|
-
- lib/rubocop/cop/rails/safe_navigation.rb
|
559
|
-
- lib/rubocop/cop/rails/save_bang.rb
|
560
|
-
- lib/rubocop/cop/rails/scope_args.rb
|
561
|
-
- lib/rubocop/cop/rails/skips_model_validations.rb
|
562
|
-
- lib/rubocop/cop/rails/time_zone.rb
|
563
|
-
- lib/rubocop/cop/rails/uniq_before_pluck.rb
|
564
|
-
- lib/rubocop/cop/rails/unknown_env.rb
|
565
|
-
- lib/rubocop/cop/rails/validation.rb
|
566
504
|
- lib/rubocop/cop/registry.rb
|
567
505
|
- lib/rubocop/cop/security/eval.rb
|
568
506
|
- lib/rubocop/cop/security/json_load.rb
|
@@ -616,6 +554,7 @@ files:
|
|
616
554
|
- lib/rubocop/cop/style/eval_with_location.rb
|
617
555
|
- lib/rubocop/cop/style/even_odd.rb
|
618
556
|
- lib/rubocop/cop/style/expand_path_arguments.rb
|
557
|
+
- lib/rubocop/cop/style/float_division.rb
|
619
558
|
- lib/rubocop/cop/style/for.rb
|
620
559
|
- lib/rubocop/cop/style/format_string.rb
|
621
560
|
- lib/rubocop/cop/style/format_string_token.rb
|
@@ -783,7 +722,6 @@ files:
|
|
783
722
|
- lib/rubocop/rspec/expect_offense.rb
|
784
723
|
- lib/rubocop/rspec/host_environment_simulation_helper.rb
|
785
724
|
- lib/rubocop/rspec/shared_contexts.rb
|
786
|
-
- lib/rubocop/rspec/shared_examples.rb
|
787
725
|
- lib/rubocop/rspec/support.rb
|
788
726
|
- lib/rubocop/runner.rb
|
789
727
|
- lib/rubocop/string_interpreter.rb
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
# Common functionality for checking target rails version.
|
6
|
-
module TargetRailsVersion
|
7
|
-
def minimum_target_rails_version(version)
|
8
|
-
@minimum_target_rails_version = version
|
9
|
-
end
|
10
|
-
|
11
|
-
def support_target_rails_version?(version)
|
12
|
-
@minimum_target_rails_version <= version
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,117 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Rails
|
6
|
-
# This cop enforces the consistent use of action filter methods.
|
7
|
-
#
|
8
|
-
# The cop is configurable and can enforce the use of the older
|
9
|
-
# something_filter methods or the newer something_action methods.
|
10
|
-
#
|
11
|
-
# If the TargetRailsVersion is set to less than 4.0, the cop will enforce
|
12
|
-
# the use of filter methods.
|
13
|
-
#
|
14
|
-
# @example EnforcedStyle: action (default)
|
15
|
-
# # bad
|
16
|
-
# after_filter :do_stuff
|
17
|
-
# append_around_filter :do_stuff
|
18
|
-
# skip_after_filter :do_stuff
|
19
|
-
#
|
20
|
-
# # good
|
21
|
-
# after_action :do_stuff
|
22
|
-
# append_around_action :do_stuff
|
23
|
-
# skip_after_action :do_stuff
|
24
|
-
#
|
25
|
-
# @example EnforcedStyle: filter
|
26
|
-
# # bad
|
27
|
-
# after_action :do_stuff
|
28
|
-
# append_around_action :do_stuff
|
29
|
-
# skip_after_action :do_stuff
|
30
|
-
#
|
31
|
-
# # good
|
32
|
-
# after_filter :do_stuff
|
33
|
-
# append_around_filter :do_stuff
|
34
|
-
# skip_after_filter :do_stuff
|
35
|
-
class ActionFilter < Cop
|
36
|
-
extend TargetRailsVersion
|
37
|
-
include ConfigurableEnforcedStyle
|
38
|
-
|
39
|
-
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
|
40
|
-
|
41
|
-
FILTER_METHODS = %i[
|
42
|
-
after_filter
|
43
|
-
append_after_filter
|
44
|
-
append_around_filter
|
45
|
-
append_before_filter
|
46
|
-
around_filter
|
47
|
-
before_filter
|
48
|
-
prepend_after_filter
|
49
|
-
prepend_around_filter
|
50
|
-
prepend_before_filter
|
51
|
-
skip_after_filter
|
52
|
-
skip_around_filter
|
53
|
-
skip_before_filter
|
54
|
-
skip_filter
|
55
|
-
].freeze
|
56
|
-
|
57
|
-
ACTION_METHODS = %i[
|
58
|
-
after_action
|
59
|
-
append_after_action
|
60
|
-
append_around_action
|
61
|
-
append_before_action
|
62
|
-
around_action
|
63
|
-
before_action
|
64
|
-
prepend_after_action
|
65
|
-
prepend_around_action
|
66
|
-
prepend_before_action
|
67
|
-
skip_after_action
|
68
|
-
skip_around_action
|
69
|
-
skip_before_action
|
70
|
-
skip_action_callback
|
71
|
-
].freeze
|
72
|
-
|
73
|
-
minimum_target_rails_version 4.0
|
74
|
-
|
75
|
-
def on_block(node)
|
76
|
-
check_method_node(node.send_node)
|
77
|
-
end
|
78
|
-
|
79
|
-
def on_send(node)
|
80
|
-
check_method_node(node) unless node.receiver
|
81
|
-
end
|
82
|
-
|
83
|
-
def autocorrect(node)
|
84
|
-
lambda do |corrector|
|
85
|
-
corrector.replace(node.loc.selector,
|
86
|
-
preferred_method(node.loc.selector.source).to_s)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
|
92
|
-
def check_method_node(node)
|
93
|
-
return unless bad_methods.include?(node.method_name)
|
94
|
-
|
95
|
-
add_offense(node, location: :selector)
|
96
|
-
end
|
97
|
-
|
98
|
-
def message(node)
|
99
|
-
format(MSG, prefer: preferred_method(node.method_name),
|
100
|
-
current: node.method_name)
|
101
|
-
end
|
102
|
-
|
103
|
-
def bad_methods
|
104
|
-
style == :action ? FILTER_METHODS : ACTION_METHODS
|
105
|
-
end
|
106
|
-
|
107
|
-
def good_methods
|
108
|
-
style == :action ? ACTION_METHODS : FILTER_METHODS
|
109
|
-
end
|
110
|
-
|
111
|
-
def preferred_method(method)
|
112
|
-
good_methods[bad_methods.index(method.to_sym)]
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Rails
|
6
|
-
# Checks that ActiveRecord aliases are not used. The direct method names
|
7
|
-
# are more clear and easier to read.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# #bad
|
11
|
-
# Book.update_attributes!(author: 'Alice')
|
12
|
-
#
|
13
|
-
# #good
|
14
|
-
# Book.update!(author: 'Alice')
|
15
|
-
class ActiveRecordAliases < Cop
|
16
|
-
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'
|
17
|
-
|
18
|
-
ALIASES = {
|
19
|
-
update_attributes: :update,
|
20
|
-
update_attributes!: :update!
|
21
|
-
}.freeze
|
22
|
-
|
23
|
-
def on_send(node)
|
24
|
-
ALIASES.each do |bad, good|
|
25
|
-
next unless node.method?(bad)
|
26
|
-
|
27
|
-
add_offense(node,
|
28
|
-
message: format(MSG, prefer: good, current: bad),
|
29
|
-
location: :selector,
|
30
|
-
severity: :warning)
|
31
|
-
break
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
alias on_csend on_send
|
36
|
-
|
37
|
-
def autocorrect(node)
|
38
|
-
lambda do |corrector|
|
39
|
-
corrector.replace(
|
40
|
-
node.loc.selector,
|
41
|
-
ALIASES[node.method_name].to_s
|
42
|
-
)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|