rubocop-rails 2.19.1 → 2.21.2

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 +3 -1
  3. data/config/default.yml +86 -12
  4. data/lib/rubocop/cop/mixin/index_method.rb +2 -2
  5. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +1 -1
  6. data/lib/rubocop/cop/rails/action_controller_test_case.rb +2 -2
  7. data/lib/rubocop/cop/rails/assert_not.rb +0 -1
  8. data/lib/rubocop/cop/rails/bulk_change_table.rb +20 -3
  9. data/lib/rubocop/cop/rails/dangerous_column_names.rb +439 -0
  10. data/lib/rubocop/cop/rails/date.rb +12 -3
  11. data/lib/rubocop/cop/rails/duplicate_association.rb +3 -0
  12. data/lib/rubocop/cop/rails/dynamic_find_by.rb +3 -3
  13. data/lib/rubocop/cop/rails/file_path.rb +129 -13
  14. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  15. data/lib/rubocop/cop/rails/freeze_time.rb +1 -1
  16. data/lib/rubocop/cop/rails/http_status.rb +4 -3
  17. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +63 -13
  18. data/lib/rubocop/cop/rails/i18n_locale_texts.rb +5 -1
  19. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +22 -2
  20. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +9 -10
  21. data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
  22. data/lib/rubocop/cop/rails/rake_environment.rb +20 -4
  23. data/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +168 -0
  24. data/lib/rubocop/cop/rails/refute_methods.rb +0 -1
  25. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -1
  26. data/lib/rubocop/cop/rails/root_pathname_methods.rb +38 -4
  27. data/lib/rubocop/cop/rails/save_bang.rb +2 -2
  28. data/lib/rubocop/cop/rails/schema_comment.rb +16 -10
  29. data/lib/rubocop/cop/rails/select_map.rb +78 -0
  30. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +0 -1
  31. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +2 -4
  32. data/lib/rubocop/cop/rails/time_zone.rb +12 -5
  33. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +35 -9
  34. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +15 -19
  35. data/lib/rubocop/cop/rails/unused_render_content.rb +67 -0
  36. data/lib/rubocop/cop/rails/where_exists.rb +0 -1
  37. data/lib/rubocop/cop/rails_cops.rb +4 -0
  38. data/lib/rubocop/rails/schema_loader/schema.rb +4 -4
  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 +8 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b020fcc0db6203c33dfe23032cc2c6ec0df139e6abb048409bdf7439ea2d4183
4
- data.tar.gz: fc6e892161124d21b9067e01e6b86178857df27d14ae687f3f0fdcceb2b3d078
3
+ metadata.gz: 0e154e69d10b43226db08e454d992d724dc7504d54cac804e597156ee5cf13e5
4
+ data.tar.gz: 60d4697e076620a134f48eff2f81be7b2004fd824796b1a1b15f5822239e3ce3
5
5
  SHA512:
6
- metadata.gz: f09636066a282111462ab5cc3b93463ec0cd8a111e2882b6e7c21889da3b36958766f722926418439e48933d8791ce41c0edadc3e4a590c5f8caeb82f35ad5b1
7
- data.tar.gz: 40586400ad94701ee39ee71ab7280f7328889245f4786503cf5a851d158a05e4f736eea42c53f61164a420af8fab7e19854c6302237115a29efd3bdd1f19cc68
6
+ metadata.gz: f0aa0f69a7aa3b90d1e402ab26bdf5a4d03680080d9b06ba157cd92f3c5c290f821e81d4a99636009e17ec4ea2de0cfd405cea00134ebccd4323d424fb790ec8
7
+ data.tar.gz: 6b6ddbe47c1b112638cc3693464d9509f589a6683317b0bb30925a27788e39351a3a10853ecf286b419d3ec3b17e13aee9fd09ae07581d209635f0a56a44065b
data/README.md CHANGED
@@ -72,7 +72,9 @@ module YourCoolApp
72
72
  class Application < Rails::Application
73
73
  config.generators.after_generate do |files|
74
74
  parsable_files = files.filter { |file| file.end_with?('.rb') }
75
- system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
75
+ unless parsable_files.empty?
76
+ system("bundle exec rubocop -A --fail-level=E #{parsable_files.shelljoin}", exception: true)
77
+ end
76
78
  end
77
79
  end
78
80
  end
data/config/default.yml CHANGED
@@ -6,6 +6,7 @@ inherit_mode:
6
6
 
7
7
  AllCops:
8
8
  Exclude:
9
+ - app/assets/**/*
9
10
  - bin/*
10
11
  # Exclude db/schema.rb and db/[CONFIGURATION_NAMESPACE]_schema.rb by default.
11
12
  # See: https://guides.rubyonrails.org/active_record_multiple_databases.html#setting-up-your-application
@@ -47,6 +48,31 @@ Lint/NumberConversion:
47
48
  - in_milliseconds
48
49
  AllowedPatterns: []
49
50
 
51
+ Lint/RedundantSafeNavigation:
52
+ # Add `presence` and `present?` methods to the default of the RuboCop core.
53
+ # https://github.com/rubocop/rubocop/blob/v1.51.0/config/default.yml#L2148-L2159
54
+ AllowedMethods:
55
+ - instance_of?
56
+ - kind_of?
57
+ - is_a?
58
+ - eql?
59
+ - respond_to?
60
+ - equal?
61
+ - presence
62
+ - present?
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
+
50
76
  Rails:
51
77
  Enabled: true
52
78
  DocumentationBaseURL: https://docs.rubocop.org/rubocop-rails
@@ -146,8 +172,9 @@ Rails/AddColumnIndex:
146
172
  index might be used.
147
173
  Enabled: pending
148
174
  VersionAdded: '2.11'
175
+ VersionChanged: '2.20'
149
176
  Include:
150
- - db/migrate/*.rb
177
+ - db/**/*.rb
151
178
 
152
179
  Rails/AfterCommitOverride:
153
180
  Description: >-
@@ -235,12 +262,13 @@ Rails/BulkChangeTable:
235
262
  - https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html
236
263
  Enabled: true
237
264
  VersionAdded: '0.57'
265
+ VersionChanged: '2.20'
238
266
  Database: null
239
267
  SupportedDatabases:
240
268
  - mysql
241
269
  - postgresql
242
270
  Include:
243
- - db/migrate/*.rb
271
+ - db/**/*.rb
244
272
 
245
273
  Rails/CompactBlank:
246
274
  Description: 'Checks if collection can be blank-compacted with `compact_blank`.'
@@ -270,18 +298,31 @@ Rails/CreateTableWithTimestamps:
270
298
  when creating a new table.
271
299
  Enabled: true
272
300
  VersionAdded: '0.52'
301
+ VersionChanged: '2.20'
273
302
  Include:
274
- - db/migrate/*.rb
303
+ - db/**/*.rb
275
304
  Exclude:
276
305
  # Respect the `active_storage_variant_records` table of `*_create_active_storage_tables.active_storage.rb`
306
+ # and `*_create_active_storage_variant_records.active_storage.rb`
277
307
  # auto-generated by `bin/rails active_storage:install` even if `created_at` is not specified.
278
- - db/migrate/*_create_active_storage_tables.active_storage.rb
308
+ - db/**/*_create_active_storage_tables.active_storage.rb
309
+ - db/**/*_create_active_storage_variant_records.active_storage.rb
310
+
311
+ Rails/DangerousColumnNames:
312
+ Description: >-
313
+ Avoid dangerous column names.
314
+ Enabled: pending
315
+ Severity: warning
316
+ VersionAdded: '2.21'
317
+ Include:
318
+ - 'db/**/*.rb'
279
319
 
280
320
  Rails/Date:
281
321
  Description: >-
282
322
  Checks the correct usage of date aware methods,
283
323
  such as Date.today, Date.current etc.
284
324
  Enabled: true
325
+ SafeAutoCorrect: false
285
326
  VersionAdded: '0.30'
286
327
  VersionChanged: '2.11'
287
328
  # The value `strict` disallows usage of `Date.today`, `Date.current`,
@@ -443,10 +484,8 @@ Rails/FindBy:
443
484
  StyleGuide: 'https://rails.rubystyle.guide#find_by'
444
485
  Enabled: true
445
486
  VersionAdded: '0.30'
446
- VersionChanged: '2.11'
487
+ VersionChanged: '2.21'
447
488
  IgnoreWhereFirst: true
448
- Include:
449
- - app/models/**/*.rb
450
489
 
451
490
  Rails/FindById:
452
491
  Description: >-
@@ -462,9 +501,7 @@ Rails/FindEach:
462
501
  Enabled: true
463
502
  Safe: false
464
503
  VersionAdded: '0.30'
465
- VersionChanged: '2.19'
466
- Include:
467
- - app/models/**/*.rb
504
+ VersionChanged: '2.21'
468
505
  AllowedMethods:
469
506
  # Methods that don't work well with `find_each`.
470
507
  - order
@@ -527,6 +564,10 @@ Rails/I18nLazyLookup:
527
564
  Reference: 'https://guides.rubyonrails.org/i18n.html#lazy-lookup'
528
565
  Enabled: pending
529
566
  VersionAdded: '2.14'
567
+ EnforcedStyle: lazy
568
+ SupportedStyles:
569
+ - lazy
570
+ - explicit
530
571
  Include:
531
572
  - 'app/controllers/**/*.rb'
532
573
 
@@ -632,8 +673,9 @@ Rails/MigrationClassName:
632
673
  Description: 'The class name of the migration should match its file name.'
633
674
  Enabled: pending
634
675
  VersionAdded: '2.14'
676
+ VersionChanged: '2.20'
635
677
  Include:
636
- - db/migrate/*.rb
678
+ - db/**/*.rb
637
679
 
638
680
  Rails/NegateInclude:
639
681
  Description: 'Prefer `collection.exclude?(obj)` over `!collection.include?(obj)`.'
@@ -647,8 +689,9 @@ Rails/NotNullColumn:
647
689
  Description: 'Do not add a NOT NULL column without a default value.'
648
690
  Enabled: true
649
691
  VersionAdded: '0.43'
692
+ VersionChanged: '2.20'
650
693
  Include:
651
- - db/migrate/*.rb
694
+ - db/**/*.rb
652
695
 
653
696
  Rails/OrderById:
654
697
  Description: >-
@@ -753,6 +796,16 @@ Rails/ReadWriteAttribute:
753
796
  Include:
754
797
  - app/models/**/*.rb
755
798
 
799
+ Rails/RedundantActiveRecordAllMethod:
800
+ Description: Detect redundant `all` used as a receiver for Active Record query methods.
801
+ StyleGuide: 'https://rails.rubystyle.guide/#redundant-all'
802
+ Enabled: pending
803
+ Safe: false
804
+ AllowedReceivers:
805
+ - ActionMailer::Preview
806
+ - ActiveSupport::TimeZone
807
+ VersionAdded: '2.21'
808
+
756
809
  Rails/RedundantAllowNil:
757
810
  Description: >-
758
811
  Finds redundant use of `allow_nil` when `allow_blank` is set to
@@ -933,6 +986,12 @@ Rails/ScopeArgs:
933
986
  Include:
934
987
  - app/models/**/*.rb
935
988
 
989
+ Rails/SelectMap:
990
+ Description: 'Checks for uses of `select(:column_name)` with `map(&:column_name)`.'
991
+ Enabled: pending
992
+ Safe: false
993
+ VersionAdded: '2.21'
994
+
936
995
  Rails/ShortI18n:
937
996
  Description: 'Use the short form of the I18n methods: `t` instead of `translate` and `l` instead of `localize`.'
938
997
  StyleGuide: 'https://rails.rubystyle.guide/#short-i18n'
@@ -1061,6 +1120,7 @@ Rails/TransactionExitStatement:
1061
1120
  - https://github.com/rails/rails/commit/15aa4200e083
1062
1121
  Enabled: pending
1063
1122
  VersionAdded: '2.14'
1123
+ TransactionMethods: []
1064
1124
 
1065
1125
  Rails/UniqBeforePluck:
1066
1126
  Description: 'Prefer the use of uniq or distinct before pluck.'
@@ -1098,6 +1158,12 @@ Rails/UnusedIgnoredColumns:
1098
1158
  Include:
1099
1159
  - app/models/**/*.rb
1100
1160
 
1161
+ Rails/UnusedRenderContent:
1162
+ Description: 'Do not specify body content for a response with a non-content status code.'
1163
+ Enabled: pending
1164
+ Severity: warning
1165
+ VersionAdded: '2.21'
1166
+
1101
1167
  Rails/Validation:
1102
1168
  Description: 'Use validates :attribute, hash of validations.'
1103
1169
  Enabled: true
@@ -1153,6 +1219,14 @@ Style/FormatStringToken:
1153
1219
  AllowedMethods:
1154
1220
  - redirect
1155
1221
 
1222
+ Style/InvertibleUnlessCondition:
1223
+ InverseMethods:
1224
+ # Active Support defines some common inverse methods. They are listed below:
1225
+ :present?: :blank?
1226
+ :blank?: :present?
1227
+ :include?: :exclude?
1228
+ :exclude?: :include?
1229
+
1156
1230
  Style/SymbolProc:
1157
1231
  AllowedMethods:
1158
1232
  - define_method
@@ -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
@@ -3,7 +3,6 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- #
7
6
  # Use `assert_not` instead of `assert !`.
8
7
  #
9
8
  # @example
@@ -12,7 +12,8 @@ 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.
15
+ # in `config/database.yml` or the environment variable `DATABASE_URL`
16
+ # when the `Database` option is not set.
16
17
  # If the adapter is not `mysql2` or `postgresql`,
17
18
  # this Cop ignores offenses.
18
19
  #
@@ -175,13 +176,13 @@ module RuboCop
175
176
  end
176
177
 
177
178
  def database
178
- cop_config['Database'] || database_from_yaml
179
+ cop_config['Database'] || database_from_yaml || database_from_env
179
180
  end
180
181
 
181
182
  def database_from_yaml
182
183
  return nil unless database_yaml
183
184
 
184
- case database_yaml['adapter']
185
+ case database_adapter
185
186
  when 'mysql2'
186
187
  MYSQL
187
188
  when 'postgresql'
@@ -189,6 +190,10 @@ module RuboCop
189
190
  end
190
191
  end
191
192
 
193
+ def database_adapter
194
+ database_yaml['adapter'] || database_yaml.first.last['adapter']
195
+ end
196
+
192
197
  def database_yaml
193
198
  return nil unless File.exist?('config/database.yml')
194
199
 
@@ -207,6 +212,18 @@ module RuboCop
207
212
  nil
208
213
  end
209
214
 
215
+ def database_from_env
216
+ url = ENV['DATABASE_URL'].presence
217
+ return nil unless url
218
+
219
+ case url
220
+ when %r{\Amysql2://}
221
+ MYSQL
222
+ when %r{\Apostgres(ql)?://}
223
+ POSTGRESQL
224
+ end
225
+ end
226
+
210
227
  def support_bulk_alter?
211
228
  case database
212
229
  when MYSQL