rubocop-rails 2.20.2 → 2.22.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -2
  3. data/config/default.yml +68 -8
  4. data/lib/rubocop/cop/mixin/database_type_resolvable.rb +66 -0
  5. data/lib/rubocop/cop/mixin/index_method.rb +2 -2
  6. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +1 -1
  7. data/lib/rubocop/cop/rails/action_controller_test_case.rb +2 -2
  8. data/lib/rubocop/cop/rails/action_filter.rb +3 -0
  9. data/lib/rubocop/cop/rails/bulk_change_table.rb +5 -38
  10. data/lib/rubocop/cop/rails/dangerous_column_names.rb +447 -0
  11. data/lib/rubocop/cop/rails/date.rb +1 -1
  12. data/lib/rubocop/cop/rails/duplicate_association.rb +69 -12
  13. data/lib/rubocop/cop/rails/dynamic_find_by.rb +3 -3
  14. data/lib/rubocop/cop/rails/env_local.rb +46 -0
  15. data/lib/rubocop/cop/rails/file_path.rb +4 -1
  16. data/lib/rubocop/cop/rails/freeze_time.rb +1 -1
  17. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +1 -1
  18. data/lib/rubocop/cop/rails/helper_instance_variable.rb +1 -1
  19. data/lib/rubocop/cop/rails/http_status.rb +4 -3
  20. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +63 -13
  21. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +7 -8
  22. data/lib/rubocop/cop/rails/not_null_column.rb +13 -3
  23. data/lib/rubocop/cop/rails/output.rb +3 -2
  24. data/lib/rubocop/cop/rails/rake_environment.rb +20 -4
  25. data/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +207 -0
  26. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +7 -0
  27. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -1
  28. data/lib/rubocop/cop/rails/root_pathname_methods.rb +38 -4
  29. data/lib/rubocop/cop/rails/save_bang.rb +9 -4
  30. data/lib/rubocop/cop/rails/schema_comment.rb +16 -10
  31. data/lib/rubocop/cop/rails/select_map.rb +78 -0
  32. data/lib/rubocop/cop/rails/time_zone.rb +12 -5
  33. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +29 -10
  34. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +1 -1
  35. data/lib/rubocop/cop/rails/unknown_env.rb +5 -1
  36. data/lib/rubocop/cop/rails/unused_render_content.rb +67 -0
  37. data/lib/rubocop/cop/rails/where_exists.rb +0 -1
  38. data/lib/rubocop/cop/rails_cops.rb +6 -0
  39. data/lib/rubocop/rails/schema_loader.rb +1 -1
  40. data/lib/rubocop/rails/version.rb +1 -1
  41. data/lib/rubocop-rails.rb +8 -0
  42. metadata +9 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 227ce5b698e456412c24917c5644b2a938531440ab8c64e2b5036d6f2857a548
4
- data.tar.gz: 41c6157da73215cca0b5d56ffa254711c2f9f4f5399c8aa3d85ac7c719958b28
3
+ metadata.gz: a4d08f111893c16ea5c725b8da3b488a85d193366cdec6c876e780e794d66ba3
4
+ data.tar.gz: 40bd526fbf58ea8b3b45ab82ff5b7f809dcf29c7377991023fb30157dc66cc0d
5
5
  SHA512:
6
- metadata.gz: afafd2f9293f4592cc914fdb42c6742900681574d5d0a27dc2031eddb3446b419ac04a77a149e0169ab89a0bb7afe4e0481b3ff889631345a0227b0d24a2c185
7
- data.tar.gz: a90108eed94c1cedbe1b9a059b60cae51465cffd5a8612f56a0548182b6f992e3e626125e4a4f2f74ceda491a91cdcacb0bd8206ebf94d7d245841572639b556
6
+ metadata.gz: 6dd91e7f4068bba386e8b66ce86ec18961bc6e346d000947220af3fccdaf076907a11d7367786702124370aa070aacadba96bde0c200303ad64eaf1b5de6f8d5
7
+ data.tar.gz: 783d1a9013524550b3bbd2271f3d244b13565038b1f0e73b7726565a6ea79e6e409b19f748e043fd7d850c623ea3f56c831f81f7d5bafd2cbd5e08d4181b1172
data/README.md CHANGED
@@ -56,6 +56,8 @@ Note: `--rails` option is required while `rubocop` command supports `--rails` op
56
56
  ### Rake task
57
57
 
58
58
  ```ruby
59
+ require 'rubocop/rake_task'
60
+
59
61
  RuboCop::RakeTask.new do |task|
60
62
  task.requires << 'rubocop-rails'
61
63
  end
@@ -72,13 +74,15 @@ module YourCoolApp
72
74
  class Application < Rails::Application
73
75
  config.generators.after_generate do |files|
74
76
  parsable_files = files.filter { |file| file.end_with?('.rb') }
75
- system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
77
+ unless parsable_files.empty?
78
+ system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
79
+ end
76
80
  end
77
81
  end
78
82
  end
79
83
  ```
80
84
 
81
- It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsafe autocorretion cops.
85
+ It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsafe autocorrection cops.
82
86
  `rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to
83
87
  be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead.
84
88
 
data/config/default.yml CHANGED
@@ -61,6 +61,18 @@ Lint/RedundantSafeNavigation:
61
61
  - presence
62
62
  - present?
63
63
 
64
+ Lint/SafeNavigationChain:
65
+ # Add `presence_in` method to the default of the RuboCop core:
66
+ # https://github.com/rubocop/rubocop/blob/v1.56.0/config/default.yml#L2265-L2271
67
+ AllowedMethods:
68
+ - present?
69
+ - blank?
70
+ - presence
71
+ - presence_in
72
+ - try
73
+ - try!
74
+ - in?
75
+
64
76
  Rails:
65
77
  Enabled: true
66
78
  DocumentationBaseURL: https://docs.rubocop.org/rubocop-rails
@@ -83,8 +95,9 @@ Rails/ActionControllerTestCase:
83
95
 
84
96
  Rails/ActionFilter:
85
97
  Description: 'Enforces consistent use of action filter methods.'
86
- Enabled: true
98
+ Enabled: false
87
99
  VersionAdded: '0.19'
100
+ VersionChanged: '2.22'
88
101
  EnforcedStyle: action
89
102
  SupportedStyles:
90
103
  - action
@@ -296,6 +309,15 @@ Rails/CreateTableWithTimestamps:
296
309
  - db/**/*_create_active_storage_tables.active_storage.rb
297
310
  - db/**/*_create_active_storage_variant_records.active_storage.rb
298
311
 
312
+ Rails/DangerousColumnNames:
313
+ Description: >-
314
+ Avoid dangerous column names.
315
+ Enabled: pending
316
+ Severity: warning
317
+ VersionAdded: '2.21'
318
+ Include:
319
+ - 'db/**/*.rb'
320
+
299
321
  Rails/Date:
300
322
  Description: >-
301
323
  Checks the correct usage of date aware methods,
@@ -317,7 +339,6 @@ Rails/Date:
317
339
 
318
340
  Rails/DefaultScope:
319
341
  Description: 'Avoid use of `default_scope`.'
320
- StyleGuide: 'https://rails.rubystyle.guide#avoid-default-scope'
321
342
  Enabled: false
322
343
  VersionAdded: '2.7'
323
344
 
@@ -409,6 +430,11 @@ Rails/EnumUniqueness:
409
430
  Include:
410
431
  - app/models/**/*.rb
411
432
 
433
+ Rails/EnvLocal:
434
+ Description: 'Use `Rails.env.local?` instead of `Rails.env.development? || Rails.env.test?`.'
435
+ Enabled: pending
436
+ VersionAdded: '2.22'
437
+
412
438
  Rails/EnvironmentComparison:
413
439
  Description: "Favor `Rails.env.production?` over `Rails.env == 'production'`."
414
440
  Enabled: true
@@ -463,10 +489,8 @@ Rails/FindBy:
463
489
  StyleGuide: 'https://rails.rubystyle.guide#find_by'
464
490
  Enabled: true
465
491
  VersionAdded: '0.30'
466
- VersionChanged: '2.11'
492
+ VersionChanged: '2.21'
467
493
  IgnoreWhereFirst: true
468
- Include:
469
- - app/models/**/*.rb
470
494
 
471
495
  Rails/FindById:
472
496
  Description: >-
@@ -482,9 +506,7 @@ Rails/FindEach:
482
506
  Enabled: true
483
507
  Safe: false
484
508
  VersionAdded: '0.30'
485
- VersionChanged: '2.19'
486
- Include:
487
- - app/models/**/*.rb
509
+ VersionChanged: '2.21'
488
510
  AllowedMethods:
489
511
  # Methods that don't work well with `find_each`.
490
512
  - order
@@ -547,6 +569,10 @@ Rails/I18nLazyLookup:
547
569
  Reference: 'https://guides.rubyonrails.org/i18n.html#lazy-lookup'
548
570
  Enabled: pending
549
571
  VersionAdded: '2.14'
572
+ EnforcedStyle: lazy
573
+ SupportedStyles:
574
+ - lazy
575
+ - explicit
550
576
  Include:
551
577
  - 'app/controllers/**/*.rb'
552
578
 
@@ -669,6 +695,9 @@ Rails/NotNullColumn:
669
695
  Enabled: true
670
696
  VersionAdded: '0.43'
671
697
  VersionChanged: '2.20'
698
+ Database: null
699
+ SupportedDatabases:
700
+ - mysql
672
701
  Include:
673
702
  - db/**/*.rb
674
703
 
@@ -775,6 +804,16 @@ Rails/ReadWriteAttribute:
775
804
  Include:
776
805
  - app/models/**/*.rb
777
806
 
807
+ Rails/RedundantActiveRecordAllMethod:
808
+ Description: Detect redundant `all` used as a receiver for Active Record query methods.
809
+ StyleGuide: 'https://rails.rubystyle.guide/#redundant-all'
810
+ Enabled: pending
811
+ Safe: false
812
+ AllowedReceivers:
813
+ - ActionMailer::Preview
814
+ - ActiveSupport::TimeZone
815
+ VersionAdded: '2.21'
816
+
778
817
  Rails/RedundantAllowNil:
779
818
  Description: >-
780
819
  Finds redundant use of `allow_nil` when `allow_blank` is set to
@@ -955,6 +994,12 @@ Rails/ScopeArgs:
955
994
  Include:
956
995
  - app/models/**/*.rb
957
996
 
997
+ Rails/SelectMap:
998
+ Description: 'Checks for uses of `select(:column_name)` with `map(&:column_name)`.'
999
+ Enabled: pending
1000
+ Safe: false
1001
+ VersionAdded: '2.21'
1002
+
958
1003
  Rails/ShortI18n:
959
1004
  Description: 'Use the short form of the I18n methods: `t` instead of `translate` and `l` instead of `localize`.'
960
1005
  StyleGuide: 'https://rails.rubystyle.guide/#short-i18n'
@@ -1083,6 +1128,7 @@ Rails/TransactionExitStatement:
1083
1128
  - https://github.com/rails/rails/commit/15aa4200e083
1084
1129
  Enabled: pending
1085
1130
  VersionAdded: '2.14'
1131
+ TransactionMethods: []
1086
1132
 
1087
1133
  Rails/UniqBeforePluck:
1088
1134
  Description: 'Prefer the use of uniq or distinct before pluck.'
@@ -1120,6 +1166,12 @@ Rails/UnusedIgnoredColumns:
1120
1166
  Include:
1121
1167
  - app/models/**/*.rb
1122
1168
 
1169
+ Rails/UnusedRenderContent:
1170
+ Description: 'Do not specify body content for a response with a non-content status code.'
1171
+ Enabled: pending
1172
+ Severity: warning
1173
+ VersionAdded: '2.21'
1174
+
1123
1175
  Rails/Validation:
1124
1176
  Description: 'Use validates :attribute, hash of validations.'
1125
1177
  Enabled: true
@@ -1175,6 +1227,14 @@ Style/FormatStringToken:
1175
1227
  AllowedMethods:
1176
1228
  - redirect
1177
1229
 
1230
+ Style/InvertibleUnlessCondition:
1231
+ InverseMethods:
1232
+ # Active Support defines some common inverse methods. They are listed below:
1233
+ :present?: :blank?
1234
+ :blank?: :present?
1235
+ :include?: :exclude?
1236
+ :exclude?: :include?
1237
+
1178
1238
  Style/SymbolProc:
1179
1239
  AllowedMethods:
1180
1240
  - define_method
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # A mixin to extend cops in order to determine the database type.
6
+ #
7
+ # This module automatically detect an adapter from `development` environment
8
+ # in `config/database.yml` or the environment variable `DATABASE_URL`
9
+ # when the `Database` option is not set.
10
+ module DatabaseTypeResolvable
11
+ MYSQL = 'mysql'
12
+ POSTGRESQL = 'postgresql'
13
+
14
+ def database
15
+ cop_config['Database'] || database_from_yaml || database_from_env
16
+ end
17
+
18
+ private
19
+
20
+ def database_from_yaml
21
+ return unless database_yaml
22
+
23
+ case database_adapter
24
+ when 'mysql2', 'trilogy'
25
+ MYSQL
26
+ when 'postgresql'
27
+ POSTGRESQL
28
+ end
29
+ end
30
+
31
+ def database_from_env
32
+ url = ENV['DATABASE_URL'].presence
33
+ return unless url
34
+
35
+ case url
36
+ when %r{\A(mysql2|trilogy)://}
37
+ MYSQL
38
+ when %r{\Apostgres(ql)?://}
39
+ POSTGRESQL
40
+ end
41
+ end
42
+
43
+ def database_yaml
44
+ return unless File.exist?('config/database.yml')
45
+
46
+ yaml = if YAML.respond_to?(:unsafe_load_file)
47
+ YAML.unsafe_load_file('config/database.yml')
48
+ else
49
+ YAML.load_file('config/database.yml')
50
+ end
51
+ return unless yaml.is_a? Hash
52
+
53
+ config = yaml['development']
54
+ return unless config.is_a?(Hash)
55
+
56
+ config
57
+ rescue Psych::SyntaxError
58
+ # noop
59
+ end
60
+
61
+ def database_adapter
62
+ database_yaml['adapter'] || database_yaml.first.last['adapter']
63
+ end
64
+ end
65
+ end
66
+ end
@@ -102,7 +102,7 @@ module RuboCop
102
102
  end
103
103
 
104
104
  # Internal helper class to hold match data
105
- Captures = Struct.new(
105
+ Captures = ::Struct.new(
106
106
  :transformed_argname,
107
107
  :transforming_body_expr
108
108
  ) do
@@ -112,7 +112,7 @@ module RuboCop
112
112
  end
113
113
 
114
114
  # Internal helper class to hold autocorrect data
115
- Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do
115
+ Autocorrection = ::Struct.new(:match, :block_node, :leading, :trailing) do
116
116
  def self.from_each_with_object(node, match)
117
117
  new(match, node, 0, 0)
118
118
  end
@@ -85,7 +85,7 @@ module RuboCop
85
85
 
86
86
  def inherit_action_controller_base?(node)
87
87
  class_node = find_ancestor(node, type: :class)
88
- return unless class_node
88
+ return false unless class_node
89
89
 
90
90
  action_controller?(class_node)
91
91
  end
@@ -3,8 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # Using `ActionController::TestCase`` is discouraged and should be replaced by
7
- # `ActionDispatch::IntegrationTest``. Controller tests are too close to the
6
+ # Using `ActionController::TestCase` is discouraged and should be replaced by
7
+ # `ActionDispatch::IntegrationTest`. Controller tests are too close to the
8
8
  # internals of a controller whereas integration tests mimic the browser/user.
9
9
  #
10
10
  # @safety
@@ -8,6 +8,9 @@ module RuboCop
8
8
  # The cop is configurable and can enforce the use of the older
9
9
  # something_filter methods or the newer something_action methods.
10
10
  #
11
+ # IMPORTANT: This cop is deprecated. Because the `*_filter` methods were removed in Rails 4.2,
12
+ # and that Rails version is no longer supported by RuboCop Rails. This cop will be removed in RuboCop Rails 3.0.
13
+ #
11
14
  # @example EnforcedStyle: action (default)
12
15
  # # bad
13
16
  # after_filter :do_stuff
@@ -12,8 +12,9 @@ module RuboCop
12
12
  # The `bulk` option is only supported on the MySQL and
13
13
  # the PostgreSQL (5.2 later) adapter; thus it will
14
14
  # automatically detect an adapter from `development` environment
15
- # in `config/database.yml` when the `Database` option is not set.
16
- # If the adapter is not `mysql2` or `postgresql`,
15
+ # in `config/database.yml` or the environment variable `DATABASE_URL`
16
+ # when the `Database` option is not set.
17
+ # If the adapter is not `mysql2`, `trilogy`, or `postgresql`,
17
18
  # this Cop ignores offenses.
18
19
  #
19
20
  # @example
@@ -63,6 +64,8 @@ module RuboCop
63
64
  # end
64
65
  # end
65
66
  class BulkChangeTable < Base
67
+ include DatabaseTypeResolvable
68
+
66
69
  MSG_FOR_CHANGE_TABLE = <<~MSG.chomp
67
70
  You can combine alter queries using `bulk: true` options.
68
71
  MSG
@@ -70,9 +73,6 @@ module RuboCop
70
73
  You can use `change_table :%<table>s, bulk: true` to combine alter queries.
71
74
  MSG
72
75
 
73
- MYSQL = 'mysql'
74
- POSTGRESQL = 'postgresql'
75
-
76
76
  MIGRATION_METHODS = %i[change up down].freeze
77
77
 
78
78
  COMBINABLE_TRANSFORMATIONS = %i[
@@ -174,39 +174,6 @@ module RuboCop
174
174
  options.hash_type? && options.keys.any? { |key| key.sym_type? && key.value == :bulk }
175
175
  end
176
176
 
177
- def database
178
- cop_config['Database'] || database_from_yaml
179
- end
180
-
181
- def database_from_yaml
182
- return nil unless database_yaml
183
-
184
- case database_yaml['adapter']
185
- when 'mysql2'
186
- MYSQL
187
- when 'postgresql'
188
- POSTGRESQL
189
- end
190
- end
191
-
192
- def database_yaml
193
- return nil unless File.exist?('config/database.yml')
194
-
195
- yaml = if YAML.respond_to?(:unsafe_load_file)
196
- YAML.unsafe_load_file('config/database.yml')
197
- else
198
- YAML.load_file('config/database.yml')
199
- end
200
- return nil unless yaml.is_a? Hash
201
-
202
- config = yaml['development']
203
- return nil unless config.is_a?(Hash)
204
-
205
- config
206
- rescue Psych::SyntaxError
207
- nil
208
- end
209
-
210
177
  def support_bulk_alter?
211
178
  case database
212
179
  when MYSQL