rubocop 0.71.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.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -8
  3. data/config/default.yml +42 -484
  4. data/lib/rubocop.rb +5 -53
  5. data/lib/rubocop/ast/builder.rb +2 -0
  6. data/lib/rubocop/ast/node.rb +1 -1
  7. data/lib/rubocop/ast/node/float_node.rb +12 -0
  8. data/lib/rubocop/ast/node/int_node.rb +12 -0
  9. data/lib/rubocop/ast/node/mixin/numeric_node.rb +21 -0
  10. data/lib/rubocop/ast/node/resbody_node.rb +1 -6
  11. data/lib/rubocop/cached_data.rb +1 -1
  12. data/lib/rubocop/config.rb +34 -5
  13. data/lib/rubocop/config_loader.rb +2 -6
  14. data/lib/rubocop/config_loader_resolver.rb +0 -14
  15. data/lib/rubocop/cop/cop.rb +0 -4
  16. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +55 -0
  17. data/lib/rubocop/cop/layout/class_structure.rb +1 -1
  18. data/lib/rubocop/cop/layout/indent_first_argument.rb +6 -2
  19. data/lib/rubocop/cop/layout/indent_first_parameter.rb +7 -3
  20. data/lib/rubocop/cop/layout/indent_heredoc.rb +0 -1
  21. data/lib/rubocop/cop/layout/indentation_consistency.rb +13 -12
  22. data/lib/rubocop/cop/layout/indentation_width.rb +8 -4
  23. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  24. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +21 -20
  25. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  26. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -1
  27. data/lib/rubocop/cop/style/float_division.rb +94 -0
  28. data/lib/rubocop/cop/style/format_string.rb +7 -3
  29. data/lib/rubocop/cop/style/if_inside_else.rb +42 -0
  30. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -1
  31. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  32. data/lib/rubocop/cop/style/ternary_parentheses.rb +12 -2
  33. data/lib/rubocop/cop/style/word_array.rb +2 -2
  34. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  35. data/lib/rubocop/options.rb +0 -2
  36. data/lib/rubocop/processed_source.rb +2 -1
  37. data/lib/rubocop/rspec/cop_helper.rb +0 -1
  38. data/lib/rubocop/rspec/shared_contexts.rb +0 -17
  39. data/lib/rubocop/rspec/support.rb +0 -1
  40. data/lib/rubocop/runner.rb +0 -17
  41. data/lib/rubocop/version.rb +1 -1
  42. data/lib/rubocop/yaml_duplication_checker.rb +8 -2
  43. metadata +8 -91
  44. data/lib/rubocop/cop/mixin/target_rails_version.rb +0 -16
  45. data/lib/rubocop/cop/rails/action_filter.rb +0 -117
  46. data/lib/rubocop/cop/rails/active_record_aliases.rb +0 -48
  47. data/lib/rubocop/cop/rails/active_record_override.rb +0 -82
  48. data/lib/rubocop/cop/rails/active_support_aliases.rb +0 -69
  49. data/lib/rubocop/cop/rails/application_job.rb +0 -40
  50. data/lib/rubocop/cop/rails/application_record.rb +0 -40
  51. data/lib/rubocop/cop/rails/assert_not.rb +0 -44
  52. data/lib/rubocop/cop/rails/belongs_to.rb +0 -102
  53. data/lib/rubocop/cop/rails/blank.rb +0 -164
  54. data/lib/rubocop/cop/rails/bulk_change_table.rb +0 -289
  55. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +0 -91
  56. data/lib/rubocop/cop/rails/date.rb +0 -161
  57. data/lib/rubocop/cop/rails/delegate.rb +0 -132
  58. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +0 -37
  59. data/lib/rubocop/cop/rails/dynamic_find_by.rb +0 -91
  60. data/lib/rubocop/cop/rails/enum_uniqueness.rb +0 -45
  61. data/lib/rubocop/cop/rails/environment_comparison.rb +0 -68
  62. data/lib/rubocop/cop/rails/exit.rb +0 -67
  63. data/lib/rubocop/cop/rails/file_path.rb +0 -108
  64. data/lib/rubocop/cop/rails/find_by.rb +0 -55
  65. data/lib/rubocop/cop/rails/find_each.rb +0 -51
  66. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +0 -25
  67. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +0 -106
  68. data/lib/rubocop/cop/rails/http_positional_arguments.rb +0 -117
  69. data/lib/rubocop/cop/rails/http_status.rb +0 -179
  70. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +0 -94
  71. data/lib/rubocop/cop/rails/inverse_of.rb +0 -246
  72. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +0 -175
  73. data/lib/rubocop/cop/rails/link_to_blank.rb +0 -98
  74. data/lib/rubocop/cop/rails/not_null_column.rb +0 -67
  75. data/lib/rubocop/cop/rails/output.rb +0 -49
  76. data/lib/rubocop/cop/rails/output_safety.rb +0 -99
  77. data/lib/rubocop/cop/rails/pluralization_grammar.rb +0 -107
  78. data/lib/rubocop/cop/rails/presence.rb +0 -124
  79. data/lib/rubocop/cop/rails/present.rb +0 -153
  80. data/lib/rubocop/cop/rails/read_write_attribute.rb +0 -74
  81. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +0 -111
  82. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +0 -136
  83. data/lib/rubocop/cop/rails/reflection_class_name.rb +0 -37
  84. data/lib/rubocop/cop/rails/refute_methods.rb +0 -76
  85. data/lib/rubocop/cop/rails/relative_date_constant.rb +0 -93
  86. data/lib/rubocop/cop/rails/request_referer.rb +0 -56
  87. data/lib/rubocop/cop/rails/reversible_migration.rb +0 -286
  88. data/lib/rubocop/cop/rails/safe_navigation.rb +0 -87
  89. data/lib/rubocop/cop/rails/save_bang.rb +0 -316
  90. data/lib/rubocop/cop/rails/scope_args.rb +0 -29
  91. data/lib/rubocop/cop/rails/skips_model_validations.rb +0 -87
  92. data/lib/rubocop/cop/rails/time_zone.rb +0 -238
  93. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +0 -105
  94. data/lib/rubocop/cop/rails/unknown_env.rb +0 -63
  95. data/lib/rubocop/cop/rails/validation.rb +0 -109
  96. data/lib/rubocop/rspec/shared_examples.rb +0 -59
@@ -5,7 +5,6 @@
5
5
  require_relative 'cop_helper'
6
6
  require_relative 'host_environment_simulation_helper'
7
7
  require_relative 'shared_contexts'
8
- require_relative 'shared_examples'
9
8
  require_relative 'expect_offense'
10
9
 
11
10
  RSpec.configure do |config|
@@ -24,14 +24,6 @@ module RuboCop
24
24
 
25
25
  def initialize(options, config_store)
26
26
  @options = options
27
-
28
- if @options.key?(:rails)
29
- warn <<~MESSAGE
30
- `-R/--rails` option and Rails cops will be removed from RuboCop 0.72. Use the `rubocop-rails` gem instead.
31
- https://github.com/rubocop-hq/rubocop/blob/master/manual/migrate_rails_cops.md
32
- MESSAGE
33
- end
34
-
35
27
  @config_store = config_store
36
28
  @errors = []
37
29
  @warnings = []
@@ -281,10 +273,6 @@ module RuboCop
281
273
 
282
274
  def inspect_file(processed_source)
283
275
  config = @config_store.for(processed_source.path)
284
- if @options[:rails] ||
285
- ConfigLoader.required_features.include?('rubocop-rails')
286
- enable_rails_cops(config)
287
- end
288
276
  team = Cop::Team.new(mobilized_cop_classes(config), config, @options)
289
277
  offenses = team.inspect_file(processed_source)
290
278
  @errors.concat(team.errors)
@@ -292,11 +280,6 @@ module RuboCop
292
280
  [offenses, team.updated_source_file?]
293
281
  end
294
282
 
295
- def enable_rails_cops(config)
296
- config['Rails'] ||= {}
297
- config['Rails']['Enabled'] = true
298
- end
299
-
300
283
  def mobilized_cop_classes(config)
301
284
  @mobilized_cop_classes ||= {}
302
285
  @mobilized_cop_classes[config.object_id] ||= begin
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  # This module holds the RuboCop version information.
5
5
  module Version
6
- STRING = '0.71.0'
6
+ STRING = '0.72.0'
7
7
 
8
8
  MSG = '%<version>s (using Parser %<parser_version>s, running on ' \
9
9
  '%<ruby_engine>s %<ruby_version>s %<ruby_platform>s)'
@@ -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
- # Specify filename to display helpful message when it raises an error.
8
- tree = YAML.parse(yaml_string, filename)
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.71.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-05-30 00:00:00.000000000 Z
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
@@ -802,28 +740,7 @@ metadata:
802
740
  source_code_uri: https://github.com/rubocop-hq/rubocop/
803
741
  documentation_uri: https://docs.rubocop.org/
804
742
  bug_tracker_uri: https://github.com/rubocop-hq/rubocop/issues
805
- post_install_message: |
806
- Rails cops will be removed from RuboCop 0.72. Use the `rubocop-rails` gem instead.
807
-
808
- Put this in your `Gemfile`.
809
-
810
- ```rb
811
- gem 'rubocop-rails'
812
- ```
813
-
814
- And then execute:
815
-
816
- ```sh
817
- $ bundle install
818
- ```
819
-
820
- Put this into your `.rubocop.yml`.
821
-
822
- ```yaml
823
- require: rubocop-rails
824
- ```
825
-
826
- More information: https://github.com/rubocop-hq/rubocop-rails
743
+ post_install_message:
827
744
  rdoc_options: []
828
745
  require_paths:
829
746
  - lib
@@ -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
@@ -1,82 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Rails
6
- # Checks for overriding built-in Active Record methods instead of using
7
- # callbacks.
8
- #
9
- # @example
10
- # # bad
11
- # class Book < ApplicationRecord
12
- # def save
13
- # self.title = title.upcase!
14
- # super
15
- # end
16
- # end
17
- #
18
- # # good
19
- # class Book < ApplicationRecord
20
- # before_save :upcase_title
21
- #
22
- # def upcase_title
23
- # self.title = title.upcase!
24
- # end
25
- # end
26
- #
27
- class ActiveRecordOverride < Cop
28
- MSG =
29
- 'Use %<prefer>s callbacks instead of overriding the Active Record ' \
30
- 'method `%<bad>s`.'
31
- BAD_METHODS = %i[create destroy save update].freeze
32
- ACTIVE_RECORD_CLASSES = %w[ApplicationRecord ActiveModel::Base
33
- ActiveRecord::Base].freeze
34
-
35
- def on_def(node)
36
- return unless BAD_METHODS.include?(node.method_name)
37
-
38
- parent_class_name = find_parent_class_name(node)
39
- return unless active_model?(parent_class_name)
40
-
41
- return unless node.descendants.any?(&:zsuper_type?)
42
-
43
- add_offense(node, message: message(node.method_name))
44
- end
45
-
46
- private
47
-
48
- def active_model?(parent_class_name)
49
- ACTIVE_RECORD_CLASSES.include?(parent_class_name)
50
- end
51
-
52
- def callback_names(method_name)
53
- names = %w[before_ around_ after_].map do |prefix|
54
- "`#{prefix}#{method_name}`"
55
- end
56
-
57
- names[-1] = "or #{names.last}"
58
-
59
- names.join(', ')
60
- end
61
-
62
- def message(method_name)
63
- format(MSG, prefer: callback_names(method_name), bad: method_name)
64
- end
65
-
66
- def find_parent_class_name(node)
67
- return nil unless node
68
-
69
- if node.class_type?
70
- parent_class_name = node.node_parts[1]
71
-
72
- return nil if parent_class_name.nil?
73
-
74
- return parent_class_name.source
75
- end
76
-
77
- find_parent_class_name(node.parent)
78
- end
79
- end
80
- end
81
- end
82
- end