rubocop-rails 2.14.2 → 2.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/config/default.yml +36 -5
  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 +1 -1
  23. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  24. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +56 -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 +1 -1
  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 +1 -1
  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 +1 -1
  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 +1 -1
  95. data/lib/rubocop/cop/rails/to_formatted_s.rb +45 -0
  96. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +16 -9
  97. data/lib/rubocop/cop/rails/unknown_env.rb +1 -1
  98. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +1 -1
  99. data/lib/rubocop/cop/rails/validation.rb +1 -1
  100. data/lib/rubocop/cop/rails/where_equals.rb +1 -1
  101. data/lib/rubocop/cop/rails/where_exists.rb +2 -2
  102. data/lib/rubocop/cop/rails/where_not.rb +1 -1
  103. data/lib/rubocop/cop/rails_cops.rb +4 -0
  104. data/lib/rubocop/rails/schema_loader/schema.rb +3 -0
  105. data/lib/rubocop/rails/version.rb +1 -1
  106. data/lib/rubocop-rails.rb +8 -0
  107. 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: 6e850e3f6c08903dd860f5865cb24cab1037189bbd7418c0db047d36094808d8
4
+ data.tar.gz: a91a77471ae5e9a4c7a188d5fb958402844ba04c3641f14a6bf82161ea43ebf7
5
5
  SHA512:
6
- metadata.gz: 02b2646080a54c50c8708cd6feeed501b5aafd7ae77d04e42ed0bf5b8779f43fa778bc3f46daec2c06474ac76f40ae144bf0afed35e5acf996c4b2213df53f89
7
- data.tar.gz: 4b29b44608474ead97651327e5c896bfb403eadcc1deffd10730853f303e75682e3ebebed80f8b3b05c1f9f0862441845bea89135d694ecf70ee3d6c776f2a97
6
+ metadata.gz: 753768200576274520f9f6a8d8ab959436e0b87c44a7b51a6eb20803fee0ec842fb1d0debb4c4fef997e8b51f7caee4198692bb8de3e1603ce16fdadf386692f
7
+ data.tar.gz: eeabca82a842cea89cb8f3b5fbb54abe83c4d6405112a6493e30aeaa9ebb4df39b67c71a6e43342e77d80b2ca901978d48b77e7a52952254f20545ab9eaaf74a
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'
@@ -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."
@@ -476,6 +484,7 @@ Rails/IgnoredSkipActionFilterOption:
476
484
  VersionAdded: '0.63'
477
485
  Include:
478
486
  - app/controllers/**/*.rb
487
+ - app/mailers/**/*.rb
479
488
 
480
489
  Rails/IndexBy:
481
490
  Description: 'Prefer `index_by` over `each_with_object`, `to_h`, or `map`.'
@@ -504,13 +513,14 @@ Rails/InverseOf:
504
513
  - app/models/**/*.rb
505
514
 
506
515
  Rails/LexicallyScopedActionFilter:
507
- Description: "Checks that methods specified in the filter's `only` or `except` options are explicitly defined in the controller."
516
+ Description: "Checks that methods specified in the filter's `only` or `except` options are explicitly defined in the class."
508
517
  StyleGuide: 'https://rails.rubystyle.guide#lexically-scoped-action-filter'
509
518
  Enabled: true
510
519
  Safe: false
511
520
  VersionAdded: '0.52'
512
521
  Include:
513
522
  - app/controllers/**/*.rb
523
+ - app/mailers/**/*.rb
514
524
 
515
525
  Rails/LinkToBlank:
516
526
  Description: 'Checks that `link_to` with a `target: "_blank"` have a `rel: "noopener"` option passed to them.'
@@ -690,7 +700,7 @@ Rails/RedundantReceiverInWithOptions:
690
700
  VersionAdded: '0.52'
691
701
 
692
702
  Rails/RedundantTravelBack:
693
- Description: This cop checks for redundant `travel_back` calls.
703
+ Description: Checks for redundant `travel_back` calls.
694
704
  Enabled: pending
695
705
  VersionAdded: '2.12'
696
706
  Include:
@@ -774,6 +784,11 @@ Rails/RootJoinChain:
774
784
  Enabled: pending
775
785
  VersionAdded: '2.13'
776
786
 
787
+ Rails/RootPublicPath:
788
+ Description: "Favor `Rails.public_path` over `Rails.root` with `'public'`."
789
+ Enabled: pending
790
+ VersionAdded: '2.15'
791
+
777
792
  Rails/SafeNavigation:
778
793
  Description: "Use Ruby's safe navigation operator (`&.`) instead of `try!`."
779
794
  Enabled: true
@@ -808,7 +823,7 @@ Rails/SaveBang:
808
823
 
809
824
  Rails/SchemaComment:
810
825
  Description: >-
811
- This cop enforces the use of the `comment` option when adding a new table or column
826
+ Enforces the use of the `comment` option when adding a new table or column
812
827
  to the database during a migration.
813
828
  Enabled: false
814
829
  VersionAdded: '2.13'
@@ -867,9 +882,15 @@ Rails/SquishedSQLHeredocs:
867
882
  VersionAdded: '2.8'
868
883
  VersionChanged: '2.9'
869
884
  # 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.
885
+ # to be preserved in order to work, thus autocorrection is not safe.
871
886
  SafeAutoCorrect: false
872
887
 
888
+ Rails/StripHeredoc:
889
+ Description: 'Enforces the use of squiggly heredoc over `strip_heredoc`.'
890
+ StyleGuide: 'https://rails.rubystyle.guide/#prefer-squiggly-heredoc'
891
+ Enabled: pending
892
+ VersionAdded: '2.15'
893
+
873
894
  Rails/TableNameAssignment:
874
895
  Description: >-
875
896
  Do not use `self.table_name =`. Use Inflections or `table_name_prefix` instead.
@@ -905,6 +926,16 @@ Rails/TimeZoneAssignment:
905
926
  - spec/**/*.rb
906
927
  - test/**/*.rb
907
928
 
929
+ Rails/ToFormattedS:
930
+ Description: 'Checks for consistent uses of `to_fs` or `to_formatted_s`.'
931
+ StyleGuide: 'https://rails.rubystyle.guide/#prefer-to-fs'
932
+ Enabled: pending
933
+ EnforcedStyle: to_fs
934
+ SupportedStyles:
935
+ - to_fs
936
+ - to_formatted_s
937
+ VersionAdded: '2.15'
938
+
908
939
  Rails/TransactionExitStatement:
909
940
  Description: 'Avoid the usage of `return`, `break` and `throw` in transaction blocks.'
910
941
  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`
@@ -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,49 @@ module RuboCop
89
106
 
90
107
  def on_send(node)
91
108
  any_manipulation?(node) do
92
- add_offense(node)
109
+ add_offense(node) do |corrector|
110
+ next unless AUTOCORECTABLE_METHODS.include?(node.method_name)
111
+
112
+ autocorrect(corrector, node)
113
+ end
93
114
  end
94
115
  end
95
116
 
96
117
  private
97
118
 
119
+ def autocorrect(corrector, node)
120
+ receiver = node.receiver
121
+
122
+ if receiver.receiver.method?(:messages)
123
+ corrector.remove(receiver.receiver.loc.dot)
124
+ corrector.remove(receiver.receiver.loc.selector)
125
+ end
126
+
127
+ range = offense_range(node, receiver)
128
+ replacement = replacement(node, receiver)
129
+
130
+ corrector.replace(range, replacement)
131
+ end
132
+
133
+ def offense_range(node, receiver)
134
+ range_between(receiver.receiver.source_range.end_pos, node.source_range.end_pos)
135
+ end
136
+
137
+ def replacement(node, receiver)
138
+ return '.errors.attribute_names' if node.method?(:keys)
139
+
140
+ key = receiver.first_argument.source
141
+
142
+ case node.method_name
143
+ when :<<
144
+ value = node.first_argument.source
145
+
146
+ ".add(#{key}, #{value})"
147
+ when :clear
148
+ ".delete(#{key})"
149
+ end
150
+ end
151
+
98
152
  def receiver_matcher(node)
99
153
  model_file? ? receiver_matcher_inside_model(node) : receiver_matcher_outside_model(node)
100
154
  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}'"
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.
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for enums written with array syntax.
6
+ # Looks for enums written with array syntax.
7
7
  #
8
8
  # When using array syntax, adding an element in a
9
9
  # position other than the last causes all previous
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for duplicate values in enum declarations.
6
+ # Looks for duplicate values in enum declarations.
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 that Rails.env is compared using `.production?`-like
6
+ # Checks that Rails.env is compared using `.production?`-like
7
7
  # methods instead of equality against a string or symbol.
8
8
  #
9
9
  # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for direct access to environment variables through the
6
+ # Looks for direct access to environment variables through the
7
7
  # `ENV` variable within the application code. This can lead to runtime
8
8
  # errors due to misconfiguration that could have been discovered at boot
9
9
  # time if the environment variables were loaded as part of initialization