rubocop-rails 2.14.2 → 2.15.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +37 -6
- data/lib/rubocop/cop/rails/action_filter.rb +1 -1
- data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +1 -1
- data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
- data/lib/rubocop/cop/rails/add_column_index.rb +1 -1
- data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
- data/lib/rubocop/cop/rails/application_controller.rb +1 -1
- data/lib/rubocop/cop/rails/application_job.rb +1 -1
- data/lib/rubocop/cop/rails/application_mailer.rb +1 -1
- data/lib/rubocop/cop/rails/application_record.rb +1 -1
- data/lib/rubocop/cop/rails/arel_star.rb +1 -1
- data/lib/rubocop/cop/rails/attribute_default_block_value.rb +1 -1
- data/lib/rubocop/cop/rails/belongs_to.rb +1 -1
- data/lib/rubocop/cop/rails/blank.rb +4 -4
- data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -1
- data/lib/rubocop/cop/rails/compact_blank.rb +1 -1
- data/lib/rubocop/cop/rails/content_tag.rb +1 -1
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +1 -1
- data/lib/rubocop/cop/rails/date.rb +8 -8
- data/lib/rubocop/cop/rails/default_scope.rb +1 -1
- data/lib/rubocop/cop/rails/delegate.rb +1 -1
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
- data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +56 -2
- data/lib/rubocop/cop/rails/dot_separated_keys.rb +71 -0
- data/lib/rubocop/cop/rails/duplicate_association.rb +2 -2
- data/lib/rubocop/cop/rails/duplicate_scope.rb +1 -1
- data/lib/rubocop/cop/rails/duration_arithmetic.rb +1 -1
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +1 -1
- data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +1 -1
- data/lib/rubocop/cop/rails/enum_hash.rb +2 -3
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +1 -1
- data/lib/rubocop/cop/rails/environment_comparison.rb +1 -1
- data/lib/rubocop/cop/rails/environment_variable_access.rb +1 -1
- data/lib/rubocop/cop/rails/exit.rb +1 -1
- data/lib/rubocop/cop/rails/expanded_date_range.rb +1 -1
- data/lib/rubocop/cop/rails/file_path.rb +12 -9
- data/lib/rubocop/cop/rails/find_by.rb +1 -1
- data/lib/rubocop/cop/rails/find_by_id.rb +1 -1
- data/lib/rubocop/cop/rails/find_each.rb +1 -1
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +10 -2
- data/lib/rubocop/cop/rails/helper_instance_variable.rb +2 -2
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +1 -1
- data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +1 -1
- data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +1 -1
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +2 -2
- data/lib/rubocop/cop/rails/index_by.rb +1 -1
- data/lib/rubocop/cop/rails/index_with.rb +1 -1
- data/lib/rubocop/cop/rails/inquiry.rb +1 -1
- data/lib/rubocop/cop/rails/inverse_of.rb +1 -1
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +3 -3
- data/lib/rubocop/cop/rails/link_to_blank.rb +1 -1
- data/lib/rubocop/cop/rails/mailer_name.rb +1 -1
- data/lib/rubocop/cop/rails/match_route.rb +1 -1
- data/lib/rubocop/cop/rails/migration_class_name.rb +1 -1
- data/lib/rubocop/cop/rails/negate_include.rb +1 -1
- data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
- data/lib/rubocop/cop/rails/order_by_id.rb +2 -3
- data/lib/rubocop/cop/rails/output.rb +1 -1
- data/lib/rubocop/cop/rails/output_safety.rb +1 -1
- data/lib/rubocop/cop/rails/pick.rb +1 -1
- data/lib/rubocop/cop/rails/pluck.rb +1 -1
- data/lib/rubocop/cop/rails/pluck_id.rb +1 -1
- data/lib/rubocop/cop/rails/pluck_in_where.rb +1 -1
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +1 -1
- data/lib/rubocop/cop/rails/presence.rb +1 -1
- data/lib/rubocop/cop/rails/present.rb +2 -2
- data/lib/rubocop/cop/rails/rake_environment.rb +1 -1
- data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_travel_back.rb +1 -1
- data/lib/rubocop/cop/rails/reflection_class_name.rb +1 -1
- data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
- data/lib/rubocop/cop/rails/render_inline.rb +1 -1
- data/lib/rubocop/cop/rails/render_plain_text.rb +1 -1
- data/lib/rubocop/cop/rails/request_referer.rb +1 -1
- data/lib/rubocop/cop/rails/require_dependency.rb +1 -1
- data/lib/rubocop/cop/rails/reversible_migration.rb +1 -1
- data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +4 -4
- data/lib/rubocop/cop/rails/root_public_path.rb +59 -0
- data/lib/rubocop/cop/rails/safe_navigation.rb +7 -12
- data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +1 -1
- data/lib/rubocop/cop/rails/save_bang.rb +2 -2
- data/lib/rubocop/cop/rails/schema_comment.rb +1 -1
- data/lib/rubocop/cop/rails/scope_args.rb +1 -1
- data/lib/rubocop/cop/rails/short_i18n.rb +1 -1
- data/lib/rubocop/cop/rails/skips_model_validations.rb +1 -1
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +1 -1
- data/lib/rubocop/cop/rails/strip_heredoc.rb +56 -0
- data/lib/rubocop/cop/rails/table_name_assignment.rb +1 -1
- data/lib/rubocop/cop/rails/time_zone.rb +7 -7
- data/lib/rubocop/cop/rails/time_zone_assignment.rb +3 -3
- data/lib/rubocop/cop/rails/to_formatted_s.rb +46 -0
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +17 -10
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +2 -2
- data/lib/rubocop/cop/rails/unknown_env.rb +1 -1
- data/lib/rubocop/cop/rails/unused_ignored_columns.rb +1 -1
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/rails/where_equals.rb +1 -1
- data/lib/rubocop/cop/rails/where_exists.rb +2 -2
- data/lib/rubocop/cop/rails/where_not.rb +1 -1
- data/lib/rubocop/cop/rails_cops.rb +4 -0
- data/lib/rubocop/rails/schema_loader/schema.rb +8 -5
- data/lib/rubocop/rails/version.rb +1 -1
- data/lib/rubocop-rails.rb +8 -0
- metadata +9 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dc39965bea5c1a04b65aed496d749cc59a8ed9eeccef6d010ff8ce1970dabb9
|
4
|
+
data.tar.gz: f827dd6365f66a1464ae9433985960a99a90a8ba35b1b7c683c9dc3d7a0209ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eea7efbff824ab7a3bdae13bdbbdb2c33b4cd04ba74c2f9a7f89971ddf2da0d7a3f68a4508e9ca81314425fe9ba593f6675e014a5b385923e3790a7357bba6f5
|
7
|
+
data.tar.gz: 28990bc579868dcd5ce06332558f1bd5bb97ae13895bdf690b46650e86f7e4cf37e1d64e5a18c681f3cfa17347c338f30511b1d34788617bbaf311c5fea37126
|
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
|
-
|
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."
|
@@ -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
|
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:
|
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
|
-
|
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
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
12
|
+
# to prevent interference between the autocorrection of the two cops.
|
13
13
|
#
|
14
14
|
# @safety
|
15
|
-
# This cop is unsafe
|
16
|
-
# but `' '.blank?` returns true. Therefore,
|
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
|
-
#
|
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,
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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:
|
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:
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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.send_type? && 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}'".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
|
-
#
|
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
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
@@ -20,8 +20,7 @@ module RuboCop
|
|
20
20
|
class EnumHash < Base
|
21
21
|
extend AutoCorrector
|
22
22
|
|
23
|
-
MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '
|
24
|
-
'Use hash syntax instead.'
|
23
|
+
MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. Use hash syntax instead.'
|
25
24
|
RESTRICT_ON_SEND = %i[enum].freeze
|
26
25
|
|
27
26
|
def_node_matcher :enum?, <<~PATTERN
|