rubocop-rails 2.14.2 → 2.15.2

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 (108) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +38 -6
  3. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  4. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +1 -1
  5. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  6. data/lib/rubocop/cop/rails/add_column_index.rb +1 -1
  7. data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
  8. data/lib/rubocop/cop/rails/application_controller.rb +1 -1
  9. data/lib/rubocop/cop/rails/application_job.rb +1 -1
  10. data/lib/rubocop/cop/rails/application_mailer.rb +1 -1
  11. data/lib/rubocop/cop/rails/application_record.rb +1 -1
  12. data/lib/rubocop/cop/rails/arel_star.rb +1 -1
  13. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +1 -1
  14. data/lib/rubocop/cop/rails/belongs_to.rb +1 -1
  15. data/lib/rubocop/cop/rails/blank.rb +4 -4
  16. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
  17. data/lib/rubocop/cop/rails/compact_blank.rb +1 -1
  18. data/lib/rubocop/cop/rails/content_tag.rb +1 -1
  19. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +1 -1
  20. data/lib/rubocop/cop/rails/date.rb +8 -8
  21. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  22. data/lib/rubocop/cop/rails/delegate.rb +5 -10
  23. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  24. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +58 -2
  25. data/lib/rubocop/cop/rails/dot_separated_keys.rb +71 -0
  26. data/lib/rubocop/cop/rails/duplicate_association.rb +2 -2
  27. data/lib/rubocop/cop/rails/duplicate_scope.rb +1 -1
  28. data/lib/rubocop/cop/rails/duration_arithmetic.rb +1 -1
  29. data/lib/rubocop/cop/rails/dynamic_find_by.rb +1 -1
  30. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +1 -1
  31. data/lib/rubocop/cop/rails/enum_hash.rb +2 -3
  32. data/lib/rubocop/cop/rails/enum_uniqueness.rb +1 -1
  33. data/lib/rubocop/cop/rails/environment_comparison.rb +1 -1
  34. data/lib/rubocop/cop/rails/environment_variable_access.rb +1 -1
  35. data/lib/rubocop/cop/rails/exit.rb +1 -1
  36. data/lib/rubocop/cop/rails/expanded_date_range.rb +39 -23
  37. data/lib/rubocop/cop/rails/file_path.rb +12 -9
  38. data/lib/rubocop/cop/rails/find_by.rb +1 -1
  39. data/lib/rubocop/cop/rails/find_by_id.rb +1 -1
  40. data/lib/rubocop/cop/rails/find_each.rb +1 -1
  41. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  42. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +10 -2
  43. data/lib/rubocop/cop/rails/helper_instance_variable.rb +2 -2
  44. data/lib/rubocop/cop/rails/http_positional_arguments.rb +1 -1
  45. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +1 -1
  46. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +1 -1
  47. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +2 -2
  48. data/lib/rubocop/cop/rails/index_by.rb +1 -1
  49. data/lib/rubocop/cop/rails/index_with.rb +1 -1
  50. data/lib/rubocop/cop/rails/inquiry.rb +1 -1
  51. data/lib/rubocop/cop/rails/inverse_of.rb +1 -1
  52. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +3 -3
  53. data/lib/rubocop/cop/rails/link_to_blank.rb +1 -1
  54. data/lib/rubocop/cop/rails/mailer_name.rb +1 -1
  55. data/lib/rubocop/cop/rails/match_route.rb +1 -1
  56. data/lib/rubocop/cop/rails/migration_class_name.rb +1 -1
  57. data/lib/rubocop/cop/rails/negate_include.rb +1 -1
  58. data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
  59. data/lib/rubocop/cop/rails/order_by_id.rb +2 -3
  60. data/lib/rubocop/cop/rails/output.rb +1 -1
  61. data/lib/rubocop/cop/rails/output_safety.rb +1 -1
  62. data/lib/rubocop/cop/rails/pick.rb +1 -1
  63. data/lib/rubocop/cop/rails/pluck.rb +1 -1
  64. data/lib/rubocop/cop/rails/pluck_id.rb +1 -1
  65. data/lib/rubocop/cop/rails/pluck_in_where.rb +1 -1
  66. data/lib/rubocop/cop/rails/pluralization_grammar.rb +1 -1
  67. data/lib/rubocop/cop/rails/presence.rb +1 -1
  68. data/lib/rubocop/cop/rails/present.rb +2 -2
  69. data/lib/rubocop/cop/rails/rake_environment.rb +1 -1
  70. data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -1
  71. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
  72. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -1
  73. data/lib/rubocop/cop/rails/redundant_travel_back.rb +1 -1
  74. data/lib/rubocop/cop/rails/reflection_class_name.rb +1 -1
  75. data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
  76. data/lib/rubocop/cop/rails/render_inline.rb +1 -1
  77. data/lib/rubocop/cop/rails/render_plain_text.rb +1 -1
  78. data/lib/rubocop/cop/rails/request_referer.rb +1 -1
  79. data/lib/rubocop/cop/rails/require_dependency.rb +1 -1
  80. data/lib/rubocop/cop/rails/reversible_migration.rb +1 -1
  81. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +4 -4
  82. data/lib/rubocop/cop/rails/root_public_path.rb +59 -0
  83. data/lib/rubocop/cop/rails/safe_navigation.rb +7 -12
  84. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +1 -1
  85. data/lib/rubocop/cop/rails/save_bang.rb +2 -2
  86. data/lib/rubocop/cop/rails/schema_comment.rb +1 -1
  87. data/lib/rubocop/cop/rails/scope_args.rb +1 -1
  88. data/lib/rubocop/cop/rails/short_i18n.rb +1 -1
  89. data/lib/rubocop/cop/rails/skips_model_validations.rb +1 -1
  90. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +1 -1
  91. data/lib/rubocop/cop/rails/strip_heredoc.rb +56 -0
  92. data/lib/rubocop/cop/rails/table_name_assignment.rb +1 -1
  93. data/lib/rubocop/cop/rails/time_zone.rb +7 -7
  94. data/lib/rubocop/cop/rails/time_zone_assignment.rb +3 -3
  95. data/lib/rubocop/cop/rails/to_formatted_s.rb +46 -0
  96. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +17 -10
  97. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +2 -2
  98. data/lib/rubocop/cop/rails/unknown_env.rb +1 -1
  99. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +1 -1
  100. data/lib/rubocop/cop/rails/validation.rb +1 -1
  101. data/lib/rubocop/cop/rails/where_equals.rb +1 -1
  102. data/lib/rubocop/cop/rails/where_exists.rb +2 -2
  103. data/lib/rubocop/cop/rails/where_not.rb +1 -1
  104. data/lib/rubocop/cop/rails_cops.rb +4 -0
  105. data/lib/rubocop/rails/schema_loader/schema.rb +8 -5
  106. data/lib/rubocop/rails/version.rb +1 -1
  107. data/lib/rubocop-rails.rb +8 -0
  108. metadata +9 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: babf21479a37aa37cba8b6b8401b23ddb6532e14afd679095d456f749aca3ca0
4
- data.tar.gz: 11a94f8219a5b9d241d68af1c3ecd69afb0a47ccf988db59d00f48caaf73dfbf
3
+ metadata.gz: 169ed04b1f5378e9e08fbffdca151afaf4b84fd54b1867a173740aadaede60b3
4
+ data.tar.gz: 1a2ac75c313cfcdeec1814d7e3aff4777c5013527d2cf4a2aa4f95aeb45446bf
5
5
  SHA512:
6
- metadata.gz: 02b2646080a54c50c8708cd6feeed501b5aafd7ae77d04e42ed0bf5b8779f43fa778bc3f46daec2c06474ac76f40ae144bf0afed35e5acf996c4b2213df53f89
7
- data.tar.gz: 4b29b44608474ead97651327e5c896bfb403eadcc1deffd10730853f303e75682e3ebebed80f8b3b05c1f9f0862441845bea89135d694ecf70ee3d6c776f2a97
6
+ metadata.gz: 51d495edd3e825db49ece8f51bfc118c73887681f414b86d31cd47d85dc5a1a22a385a2a17ed4ebf7d1c6332989795e2cde999a364d911c60f452993dc926c4b
7
+ data.tar.gz: 4546785b39af93e6c7fe5a6a3b637fa5c21d4b358c38ea8ad3ebd5c272d1d0a504e8206e8201cfe3771af9199c336649d033e32b324f16b06fc7d150054dc488
data/config/default.yml CHANGED
@@ -60,6 +60,7 @@ Rails/ActionFilter:
60
60
  - filter
61
61
  Include:
62
62
  - app/controllers/**/*.rb
63
+ - app/mailers/**/*.rb
63
64
 
64
65
  Rails/ActiveRecordAliases:
65
66
  Description: >-
@@ -107,7 +108,7 @@ Rails/AddColumnIndex:
107
108
 
108
109
  Rails/AfterCommitOverride:
109
110
  Description: >-
110
- This cop enforces that there is only one call to `after_commit`
111
+ Enforces that there is only one call to `after_commit`
111
112
  (and its aliases - `after_create_commit`, `after_update_commit`,
112
113
  and `after_destroy_commit`) with the same callback name per model.
113
114
  Enabled: 'pending'
@@ -159,7 +160,7 @@ Rails/AttributeDefaultBlockValue:
159
160
  Enabled: pending
160
161
  VersionAdded: '2.9'
161
162
  Include:
162
- - 'models/**/*'
163
+ - 'app/models/**/*'
163
164
 
164
165
  Rails/BelongsTo:
165
166
  Description: >-
@@ -271,6 +272,13 @@ Rails/DeprecatedActiveModelErrorsMethods:
271
272
  Enabled: pending
272
273
  Safe: false
273
274
  VersionAdded: '2.14'
275
+ VersionChanged: '2.15'
276
+
277
+ Rails/DotSeparatedKeys:
278
+ Description: 'Enforces the use of dot-separated keys instead of `:scope` options in `I18n` translation methods.'
279
+ StyleGuide: 'https://rails.rubystyle.guide/#dot-separated-keys'
280
+ Enabled: pending
281
+ VersionAdded: '2.15'
274
282
 
275
283
  Rails/DuplicateAssociation:
276
284
  Description: "Don't repeat associations in a model."
@@ -359,6 +367,7 @@ Rails/Exit:
359
367
 
360
368
  Rails/ExpandedDateRange:
361
369
  Description: 'Checks for expanded date range.'
370
+ StyleGuide: 'https://rails.rubystyle.guide/#date-time-range'
362
371
  Enabled: pending
363
372
  VersionAdded: '2.11'
364
373
 
@@ -476,6 +485,7 @@ Rails/IgnoredSkipActionFilterOption:
476
485
  VersionAdded: '0.63'
477
486
  Include:
478
487
  - app/controllers/**/*.rb
488
+ - app/mailers/**/*.rb
479
489
 
480
490
  Rails/IndexBy:
481
491
  Description: 'Prefer `index_by` over `each_with_object`, `to_h`, or `map`.'
@@ -504,13 +514,14 @@ Rails/InverseOf:
504
514
  - app/models/**/*.rb
505
515
 
506
516
  Rails/LexicallyScopedActionFilter:
507
- Description: "Checks that methods specified in the filter's `only` or `except` options are explicitly defined in the controller."
517
+ Description: "Checks that methods specified in the filter's `only` or `except` options are explicitly defined in the class."
508
518
  StyleGuide: 'https://rails.rubystyle.guide#lexically-scoped-action-filter'
509
519
  Enabled: true
510
520
  Safe: false
511
521
  VersionAdded: '0.52'
512
522
  Include:
513
523
  - app/controllers/**/*.rb
524
+ - app/mailers/**/*.rb
514
525
 
515
526
  Rails/LinkToBlank:
516
527
  Description: 'Checks that `link_to` with a `target: "_blank"` have a `rel: "noopener"` option passed to them.'
@@ -690,7 +701,7 @@ Rails/RedundantReceiverInWithOptions:
690
701
  VersionAdded: '0.52'
691
702
 
692
703
  Rails/RedundantTravelBack:
693
- Description: This cop checks for redundant `travel_back` calls.
704
+ Description: Checks for redundant `travel_back` calls.
694
705
  Enabled: pending
695
706
  VersionAdded: '2.12'
696
707
  Include:
@@ -774,6 +785,11 @@ Rails/RootJoinChain:
774
785
  Enabled: pending
775
786
  VersionAdded: '2.13'
776
787
 
788
+ Rails/RootPublicPath:
789
+ Description: "Favor `Rails.public_path` over `Rails.root` with `'public'`."
790
+ Enabled: pending
791
+ VersionAdded: '2.15'
792
+
777
793
  Rails/SafeNavigation:
778
794
  Description: "Use Ruby's safe navigation operator (`&.`) instead of `try!`."
779
795
  Enabled: true
@@ -808,7 +824,7 @@ Rails/SaveBang:
808
824
 
809
825
  Rails/SchemaComment:
810
826
  Description: >-
811
- This cop enforces the use of the `comment` option when adding a new table or column
827
+ Enforces the use of the `comment` option when adding a new table or column
812
828
  to the database during a migration.
813
829
  Enabled: false
814
830
  VersionAdded: '2.13'
@@ -867,9 +883,15 @@ Rails/SquishedSQLHeredocs:
867
883
  VersionAdded: '2.8'
868
884
  VersionChanged: '2.9'
869
885
  # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
870
- # to be preserved in order to work, thus auto-correction is not safe.
886
+ # to be preserved in order to work, thus autocorrection is not safe.
871
887
  SafeAutoCorrect: false
872
888
 
889
+ Rails/StripHeredoc:
890
+ Description: 'Enforces the use of squiggly heredoc over `strip_heredoc`.'
891
+ StyleGuide: 'https://rails.rubystyle.guide/#prefer-squiggly-heredoc'
892
+ Enabled: pending
893
+ VersionAdded: '2.15'
894
+
873
895
  Rails/TableNameAssignment:
874
896
  Description: >-
875
897
  Do not use `self.table_name =`. Use Inflections or `table_name_prefix` instead.
@@ -905,6 +927,16 @@ Rails/TimeZoneAssignment:
905
927
  - spec/**/*.rb
906
928
  - test/**/*.rb
907
929
 
930
+ Rails/ToFormattedS:
931
+ Description: 'Checks for consistent uses of `to_fs` or `to_formatted_s`.'
932
+ StyleGuide: 'https://rails.rubystyle.guide/#prefer-to-fs'
933
+ Enabled: pending
934
+ EnforcedStyle: to_fs
935
+ SupportedStyles:
936
+ - to_fs
937
+ - to_formatted_s
938
+ VersionAdded: '2.15'
939
+
908
940
  Rails/TransactionExitStatement:
909
941
  Description: 'Avoid the usage of `return`, `break` and `throw` in transaction blocks.'
910
942
  Enabled: pending
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces the consistent use of action filter methods.
6
+ # Enforces the consistent use of action filter methods.
7
7
  #
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.
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that Active Record callbacks are declared
6
+ # Checks that Active Record callbacks are declared
7
7
  # in the order in which they will be executed.
8
8
  #
9
9
  # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that ActiveSupport aliases to core ruby methods
6
+ # Checks that ActiveSupport aliases to core ruby methods
7
7
  # are not used.
8
8
  #
9
9
  # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for migrations using `add_column` that have an `index`
6
+ # Checks for migrations using `add_column` that have an `index`
7
7
  # key. `add_column` does not accept `index`, but also does not raise an
8
8
  # error for extra keys, so it is possible to mistakenly add the key without
9
9
  # realizing it will not actually add an index.
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces that there is only one call to `after_commit`
6
+ # Enforces that there is only one call to `after_commit`
7
7
  # (and its aliases - `after_create_commit`, `after_update_commit`,
8
8
  # and `after_destroy_commit`) with the same callback name per model.
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that controllers subclass `ApplicationController`.
6
+ # Checks that controllers subclass `ApplicationController`.
7
7
  #
8
8
  # @safety
9
9
  # This cop's autocorrection is unsafe because it may let the logic from `ApplicationController`
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that jobs subclass `ApplicationJob` with Rails 5.0.
6
+ # Checks that jobs subclass `ApplicationJob` with Rails 5.0.
7
7
  #
8
8
  # @safety
9
9
  # This cop's autocorrection is unsafe because it may let the logic from `ApplicationJob`
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that mailers subclass `ApplicationMailer` with Rails 5.0.
6
+ # Checks that mailers subclass `ApplicationMailer` with Rails 5.0.
7
7
  #
8
8
  # @safety
9
9
  # This cop's autocorrection is unsafe because it may let the logic from `ApplicationMailer`
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that models subclass `ApplicationRecord` with Rails 5.0.
6
+ # Checks that models subclass `ApplicationRecord` with Rails 5.0.
7
7
  #
8
8
  # @safety
9
9
  # This cop's autocorrection is unsafe because it may let the logic from `ApplicationRecord`
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop prevents usage of `"*"` on an Arel::Table column reference.
6
+ # Prevents usage of `"*"` on an Arel::Table column reference.
7
7
  #
8
8
  # Using `arel_table["*"]` causes the outputted string to be a literal
9
9
  # quoted asterisk (e.g. <tt>`my_model`.`*`</tt>). This causes the
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for `attribute` class methods that specify a `:default` option
6
+ # Looks for `attribute` class methods that specify a `:default` option
7
7
  # which value is an array, string literal or method call without a block.
8
8
  # It will accept all other values, such as string, symbol, integer and float literals
9
9
  # as well as constants.
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for belongs_to associations where we control whether the
6
+ # Looks for belongs_to associations where we control whether the
7
7
  # association is required via the deprecated `required` option instead.
8
8
  #
9
9
  # Since Rails 5, belongs_to associations are required by default and this
@@ -3,17 +3,17 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for code that can be written with simpler conditionals
6
+ # Checks for code that can be written with simpler conditionals
7
7
  # using `Object#blank?` defined by Active Support.
8
8
  #
9
9
  # Interaction with `Style/UnlessElse`:
10
10
  # The configuration of `NotPresent` will not produce an offense in the
11
11
  # context of `unless else` if `Style/UnlessElse` is inabled. This is
12
- # to prevent interference between the auto-correction of the two cops.
12
+ # to prevent interference between the autocorrection of the two cops.
13
13
  #
14
14
  # @safety
15
- # This cop is unsafe auto-correction, because `' '.empty?` returns false,
16
- # but `' '.blank?` returns true. Therefore, auto-correction is not compatible
15
+ # This cop is unsafe autocorrection, because `' '.empty?` returns false,
16
+ # but `' '.blank?` returns true. Therefore, autocorrection is not compatible
17
17
  # if the receiver is a non-empty blank string, tab, or newline meta characters.
18
18
  #
19
19
  # @example NilOrEmpty: true (default)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This Cop checks whether alter queries are combinable.
6
+ # Checks whether alter queries are combinable.
7
7
  # If combinable queries are detected, it suggests to you
8
8
  # to use `change_table` with `bulk: true` instead.
9
9
  # This option causes the migration to generate a single
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # `ActionController::Parameters`.
18
18
  # `Array#compact_blank!`, `Hash#compact_blank!` are equivalent to `delete_if(&:blank?)`.
19
19
  # `ActionController::Parameters#compact_blank!` is equivalent to `reject!(&:blank?)`.
20
- # If the cop makes a mistake, auto-corrected code may get unexpected behavior.
20
+ # If the cop makes a mistake, autocorrected code may get unexpected behavior.
21
21
  #
22
22
  # @example
23
23
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks legacy syntax usage of `tag`
6
+ # Checks legacy syntax usage of `tag`
7
7
  #
8
8
  # NOTE: Allow `tag` when the first argument is a variable because
9
9
  # `tag(name)` is simpler rather than `tag.public_send(name)`.
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks the migration for which timestamps are not included
6
+ # Checks the migration for which timestamps are not included
7
7
  # when creating a new table.
8
8
  # In many cases, timestamps are useful information and should be added.
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the correct use of Date methods,
6
+ # Checks for the correct use of Date methods,
7
7
  # such as Date.today, Date.current etc.
8
8
  #
9
9
  # Using `Date.today` is dangerous, because it doesn't know anything about
@@ -22,26 +22,26 @@ module RuboCop
22
22
  # And you can set a warning for `to_time` with `AllowToTime: false`.
23
23
  # `AllowToTime` is `true` by default to prevent false positive on `DateTime` object.
24
24
  #
25
- # @example EnforcedStyle: strict
25
+ # @example EnforcedStyle: flexible (default)
26
26
  # # bad
27
- # Date.current
28
- # Date.yesterday
29
27
  # Date.today
30
28
  #
31
29
  # # good
32
30
  # Time.zone.today
33
31
  # Time.zone.today - 1.day
32
+ # Date.current
33
+ # Date.yesterday
34
+ # date.in_time_zone
34
35
  #
35
- # @example EnforcedStyle: flexible (default)
36
+ # @example EnforcedStyle: strict
36
37
  # # bad
38
+ # Date.current
39
+ # Date.yesterday
37
40
  # Date.today
38
41
  #
39
42
  # # good
40
43
  # Time.zone.today
41
44
  # Time.zone.today - 1.day
42
- # Date.current
43
- # Date.yesterday
44
- # date.in_time_zone
45
45
  #
46
46
  # @example AllowToTime: true (default)
47
47
  # # good
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for uses of `default_scope`.
6
+ # Looks for uses of `default_scope`.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for delegations that could have been created
6
+ # Looks for delegations that could have been created
7
7
  # automatically with the `delegate` method.
8
8
  #
9
9
  # Safe navigation `&.` is ignored because Rails' `allow_nil`
@@ -54,6 +54,7 @@ module RuboCop
54
54
  # delegate :bar, to: :foo, prefix: true
55
55
  class Delegate < Base
56
56
  extend AutoCorrector
57
+ include VisibilityHelp
57
58
 
58
59
  MSG = 'Use `delegate` to define delegations.'
59
60
 
@@ -112,17 +113,11 @@ module RuboCop
112
113
  end
113
114
 
114
115
  def private_or_protected_delegation(node)
115
- line = node.first_line
116
- private_or_protected_before(line) ||
117
- private_or_protected_inline(line)
116
+ private_or_protected_inline(node) || node_visibility(node) != :public
118
117
  end
119
118
 
120
- def private_or_protected_before(line)
121
- (processed_source[0..line].map(&:strip) & %w[private protected]).any?
122
- end
123
-
124
- def private_or_protected_inline(line)
125
- processed_source[line - 1].strip.match?(/\A(private )|(protected )/)
119
+ def private_or_protected_inline(node)
120
+ processed_source[node.first_line - 1].strip.match?(/\A(private )|(protected )/)
126
121
  end
127
122
  end
128
123
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for delegations that pass :allow_blank as an option
6
+ # Looks for delegations that pass :allow_blank as an option
7
7
  # instead of :allow_nil. :allow_blank is not a valid option to pass
8
8
  # to ActiveSupport#delegate.
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks direct manipulation of ActiveModel#errors as hash.
6
+ # Checks direct manipulation of ActiveModel#errors as hash.
7
7
  # These operations are deprecated in Rails 6.1 and will not work in Rails 7.
8
8
  #
9
9
  # @safety
@@ -26,8 +26,18 @@ module RuboCop
26
26
  # # good
27
27
  # user.errors.delete(:name)
28
28
  #
29
+ # # bad
30
+ # user.errors.keys.include?(:attr)
31
+ #
32
+ # # good
33
+ # user.errors.attribute_names.include?(:attr)
34
+ #
29
35
  class DeprecatedActiveModelErrorsMethods < Base
36
+ include RangeHelp
37
+ extend AutoCorrector
38
+
30
39
  MSG = 'Avoid manipulating ActiveModel errors as hash directly.'
40
+ AUTOCORECTABLE_METHODS = %i[<< clear keys].freeze
31
41
 
32
42
  MANIPULATIVE_METHODS = Set[
33
43
  *%i[
@@ -45,6 +55,7 @@ module RuboCop
45
55
  {
46
56
  #root_manipulation?
47
57
  #root_assignment?
58
+ #errors_keys?
48
59
  #messages_details_manipulation?
49
60
  #messages_details_assignment?
50
61
  }
@@ -66,6 +77,12 @@ module RuboCop
66
77
  ...)
67
78
  PATTERN
68
79
 
80
+ def_node_matcher :errors_keys?, <<~PATTERN
81
+ (send
82
+ (send #receiver_matcher :errors)
83
+ :keys)
84
+ PATTERN
85
+
69
86
  def_node_matcher :messages_details_manipulation?, <<~PATTERN
70
87
  (send
71
88
  (send
@@ -89,12 +106,51 @@ module RuboCop
89
106
 
90
107
  def on_send(node)
91
108
  any_manipulation?(node) do
92
- add_offense(node)
109
+ next if node.method?(:keys) && target_rails_version <= 6.0
110
+
111
+ add_offense(node) do |corrector|
112
+ next unless AUTOCORECTABLE_METHODS.include?(node.method_name)
113
+
114
+ autocorrect(corrector, node)
115
+ end
93
116
  end
94
117
  end
95
118
 
96
119
  private
97
120
 
121
+ def autocorrect(corrector, node)
122
+ receiver = node.receiver
123
+
124
+ if receiver.receiver.send_type? && receiver.receiver.method?(:messages)
125
+ corrector.remove(receiver.receiver.loc.dot)
126
+ corrector.remove(receiver.receiver.loc.selector)
127
+ end
128
+
129
+ range = offense_range(node, receiver)
130
+ replacement = replacement(node, receiver)
131
+
132
+ corrector.replace(range, replacement)
133
+ end
134
+
135
+ def offense_range(node, receiver)
136
+ range_between(receiver.receiver.source_range.end_pos, node.source_range.end_pos)
137
+ end
138
+
139
+ def replacement(node, receiver)
140
+ return '.errors.attribute_names' if node.method?(:keys)
141
+
142
+ key = receiver.first_argument.source
143
+
144
+ case node.method_name
145
+ when :<<
146
+ value = node.first_argument.source
147
+
148
+ ".add(#{key}, #{value})"
149
+ when :clear
150
+ ".delete(#{key})"
151
+ end
152
+ end
153
+
98
154
  def receiver_matcher(node)
99
155
  model_file? ? receiver_matcher_inside_model(node) : receiver_matcher_outside_model(node)
100
156
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces the use of dot-separated locale keys instead of specifying the `:scope` option
7
+ # with an array or a single symbol in `I18n` translation methods.
8
+ # Dot-separated notation is easier to read and trace the hierarchy.
9
+ #
10
+ # @example
11
+ # # bad
12
+ # I18n.t :record_invalid, scope: [:activerecord, :errors, :messages]
13
+ # I18n.t :title, scope: :invitation
14
+ #
15
+ # # good
16
+ # I18n.t 'activerecord.errors.messages.record_invalid'
17
+ # I18n.t :record_invalid, scope: 'activerecord.errors.messages'
18
+ #
19
+ class DotSeparatedKeys < Base
20
+ include RangeHelp
21
+ extend AutoCorrector
22
+
23
+ MSG = 'Use the dot-separated keys instead of specifying the `:scope` option.'
24
+ TRANSLATE_METHODS = %i[translate t].freeze
25
+
26
+ def_node_matcher :translate_with_scope?, <<~PATTERN
27
+ (send {nil? (const nil? :I18n)} {:translate :t} ${sym_type? str_type?}
28
+ (hash <$(pair (sym :scope) ${array_type? sym_type?}) ...>)
29
+ )
30
+ PATTERN
31
+
32
+ def on_send(node)
33
+ return unless TRANSLATE_METHODS.include?(node.method_name)
34
+
35
+ translate_with_scope?(node) do |key_node, scope_node|
36
+ return unless should_convert_scope?(scope_node)
37
+
38
+ add_offense(scope_node) do |corrector|
39
+ # Eat the comma on the left.
40
+ range = range_with_surrounding_space(range: scope_node.source_range, side: :left)
41
+ range = range_with_surrounding_comma(range, :left)
42
+ corrector.remove(range)
43
+
44
+ corrector.replace(key_node, new_key(key_node, scope_node))
45
+ end
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ def should_convert_scope?(scope_node)
52
+ scopes(scope_node).all?(&:basic_literal?)
53
+ end
54
+
55
+ def new_key(key_node, scope_node)
56
+ "'#{scopes(scope_node).map(&:value).join('.')}.#{key_node.value}'".squeeze('.')
57
+ end
58
+
59
+ def scopes(scope_node)
60
+ value = scope_node.value
61
+
62
+ if value.array_type?
63
+ value.values
64
+ else
65
+ [value]
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for associations that have been defined multiple times in the same file.
6
+ # Looks for associations that have been defined multiple times in the same file.
7
7
  #
8
8
  # When an association is defined multiple times on a model, Active Record overrides the
9
9
  # previously defined association with the new one. Because of this, this cop's autocorrection
@@ -35,7 +35,7 @@ module RuboCop
35
35
  offenses(class_node).each do |name, nodes|
36
36
  nodes.each do |node|
37
37
  add_offense(node, message: format(MSG, name: name)) do |corrector|
38
- next if nodes.last == node
38
+ next if same_line?(nodes.last, node)
39
39
 
40
40
  corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
41
41
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for multiple scopes in a model that have the same `where` clause. This
6
+ # Checks for multiple scopes in a model that have the same `where` clause. This
7
7
  # often means you copy/pasted a scope, updated the name, and forgot to change the condition.
8
8
  #
9
9
  # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks if a duration is added to or subtracted from `Time.current`.
6
+ # Checks if a duration is added to or subtracted from `Time.current`.
7
7
  #
8
8
  # @example
9
9
  # # bad
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks dynamic `find_by_*` methods.
6
+ # Checks dynamic `find_by_*` methods.
7
7
  # Use `find_by` instead of dynamic method.
8
8
  # See. https://rails.rubystyle.guide#find_by
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that blocks are used for interpolated strings passed to
6
+ # Checks that blocks are used for interpolated strings passed to
7
7
  # `Rails.logger.debug`.
8
8
  #
9
9
  # By default, Rails production environments use the `:info` log level.