rubocop-rails 2.14.2 → 2.15.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks if the value of the option `class_name`, in
6
+ # Checks if the value of the option `class_name`, in
7
7
  # the definition of a reflection is a string.
8
8
  #
9
9
  # @safety
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks whether constant value isn't relative date.
6
+ # Checks whether constant value isn't relative date.
7
7
  # Because the relative date will be evaluated only once.
8
8
  #
9
9
  # @safety
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop looks for inline rendering within controller actions.
6
+ # Looks for inline rendering within controller actions.
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 identifies places where `render text:` can be
6
+ # Identifies places where `render text:` can be
7
7
  # replaced with `render plain:`.
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 consistent uses of `request.referer` or
6
+ # Checks for consistent uses of `request.referer` or
7
7
  # `request.referrer`, depending on the cop's configuration.
8
8
  #
9
9
  # @example EnforcedStyle: referer (default)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the usage of `require_dependency`.
6
+ # Checks for the usage of `require_dependency`.
7
7
  #
8
8
  # `require_dependency` is an obsolete method for Rails applications running in Zeitwerk mode.
9
9
  # In Zeitwerk mode, the semantics should match Ruby's and no need to be defensive with load order,
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks whether the change method of the migration file is
6
+ # Checks whether the change method of the migration file is
7
7
  # reversible.
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 whether the migration implements
6
+ # Checks whether the migration implements
7
7
  # either a `change` method or both an `up` and a `down`
8
8
  # method.
9
9
  #
@@ -49,15 +49,15 @@ module RuboCop
49
49
  'both an `up` and a `down` method.'
50
50
 
51
51
  def_node_matcher :change_method?, <<~PATTERN
52
- [ #migration_class? `(def :change (args) _) ]
52
+ `(def :change (args) _)
53
53
  PATTERN
54
54
 
55
55
  def_node_matcher :up_and_down_methods?, <<~PATTERN
56
- [ #migration_class? `(def :up (args) _) `(def :down (args) _) ]
56
+ [`(def :up (args) _) `(def :down (args) _)]
57
57
  PATTERN
58
58
 
59
59
  def on_class(node)
60
- return if change_method?(node) || up_and_down_methods?(node)
60
+ return if !migration_class?(node) || change_method?(node) || up_and_down_methods?(node)
61
61
 
62
62
  add_offense(node)
63
63
  end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Favor `Rails.public_path` over `Rails.root` with `'public'`
7
+ #
8
+ # @example
9
+ # # bad
10
+ # Rails.root.join('public')
11
+ # Rails.root.join('public/file.pdf')
12
+ # Rails.root.join('public', 'file.pdf')
13
+ #
14
+ # # good
15
+ # Rails.public_path
16
+ # Rails.public_path.join('file.pdf')
17
+ # Rails.public_path.join('file.pdf')
18
+ #
19
+ class RootPublicPath < Base
20
+ extend AutoCorrector
21
+
22
+ MSG = 'Use `Rails.public_path`.'
23
+
24
+ RESTRICT_ON_SEND = %i[join].to_set.freeze
25
+
26
+ PATTERN = %r{\Apublic(/|\z)}.freeze
27
+
28
+ def_node_matcher :rails_root_public, <<~PATTERN
29
+ (send
30
+ (send
31
+ $(const {nil? cbase} :Rails) :root) :join
32
+ (str $#public_path?) $...)
33
+ PATTERN
34
+
35
+ def on_send(node)
36
+ return unless (rails, maybe_public_path, other_args = rails_root_public(node))
37
+
38
+ add_offense(node) do |corrector|
39
+ first_args = maybe_public_path.gsub(PATTERN, '')
40
+
41
+ args = other_args.map(&:source)
42
+ args.unshift("'#{first_args}'") unless first_args.empty?
43
+
44
+ replacement = "#{rails.source}.public_path"
45
+ replacement += ".join(#{args.join(', ')})" unless args.empty?
46
+
47
+ corrector.replace(node, replacement)
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def public_path?(string)
54
+ PATTERN.match?(string)
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -3,8 +3,9 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop converts usages of `try!` to `&.`. It can also be configured
7
- # to convert `try`. It will convert code to use safe navigation.
6
+ # Converts usages of `try!` to `&.`. It can also be configured
7
+ # to convert `try`. It will convert code to use safe navigation
8
+ # if the target Ruby version is set to 2.3+
8
9
  #
9
10
  # @example ConvertTry: false (default)
10
11
  # # bad
@@ -39,6 +40,9 @@ module RuboCop
39
40
  class SafeNavigation < Base
40
41
  include RangeHelp
41
42
  extend AutoCorrector
43
+ extend TargetRubyVersion
44
+
45
+ minimum_target_ruby_version 2.3
42
46
 
43
47
  MSG = 'Use safe navigation (`&.`) instead of `%<try>s`.'
44
48
  RESTRICT_ON_SEND = %i[try try!].freeze
@@ -47,15 +51,6 @@ module RuboCop
47
51
  (send _ ${:try :try!} $_ ...)
48
52
  PATTERN
49
53
 
50
- # Monkey patching for `Style/RedundantSelf` of RuboCop core.
51
- # rubocop:disable Style/ClassAndModuleChildren
52
- class Style::RedundantSelf
53
- def self.autocorrect_incompatible_with
54
- [Rails::SafeNavigation]
55
- end
56
- end
57
- # rubocop:enable Style/ClassAndModuleChildren
58
-
59
54
  def self.autocorrect_incompatible_with
60
55
  [Style::RedundantSelf]
61
56
  end
@@ -75,7 +70,7 @@ module RuboCop
75
70
 
76
71
  def autocorrect(corrector, node)
77
72
  method_node, *params = *node.arguments
78
- method = method_node.source[1..-1]
73
+ method = method_node.source[1..]
79
74
 
80
75
  range = if node.receiver
81
76
  range_between(node.loc.dot.begin_pos, node.loc.expression.end_pos)
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks to make sure safe navigation isn't used with `blank?` in
6
+ # Checks to make sure safe navigation isn't used with `blank?` in
7
7
  # a conditional.
8
8
  #
9
9
  # @safety
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop identifies possible cases where Active Record save! or related
6
+ # Identifies possible cases where Active Record save! or related
7
7
  # should be used instead of save because the model might have failed to
8
8
  # save and an exception is better than unhandled failure.
9
9
  #
@@ -37,7 +37,7 @@ module RuboCop
37
37
  #
38
38
  # update_attributes
39
39
  #
40
- # # After running rubocop --safe-auto-correct
40
+ # # After running rubocop --safe-autocorrect
41
41
  # def update_attributes
42
42
  # end
43
43
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces the use of the `comment` option when adding a new table or column
6
+ # Enforces the use of the `comment` option when adding a new table or column
7
7
  # to the database during a migration.
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 scope calls where it was passed
6
+ # Checks for scope calls where it was passed
7
7
  # a method (usually a scope) instead of a lambda/proc.
8
8
  #
9
9
  # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces that short forms of `I18n` methods are used:
6
+ # Enforces that short forms of `I18n` methods are used:
7
7
  # `t` instead of `translate` and `l` instead of `localize`.
8
8
  #
9
9
  # This cop has two different enforcement modes. When the EnforcedStyle
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the use of methods which skip
6
+ # Checks for the use of methods which skip
7
7
  # validations which are listed in
8
8
  # https://guides.rubyonrails.org/active_record_validations.html#skipping-validations
9
9
  #
@@ -8,7 +8,7 @@ module RuboCop
8
8
  #
9
9
  # @safety
10
10
  # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
11
- # to be preserved in order to work, thus auto-correction for this cop is not safe.
11
+ # to be preserved in order to work, thus autocorrection for this cop is not safe.
12
12
  #
13
13
  # @example
14
14
  # # bad
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces the use of squiggly heredoc over `strip_heredoc`.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # <<EOS.strip_heredoc
12
+ # some text
13
+ # EOS
14
+ #
15
+ # # bad
16
+ # <<-EOS.strip_heredoc
17
+ # some text
18
+ # EOS
19
+ #
20
+ # # good
21
+ # <<~EOS
22
+ # some text
23
+ # EOS
24
+ #
25
+ class StripHeredoc < Base
26
+ extend AutoCorrector
27
+ extend TargetRubyVersion
28
+
29
+ MSG = 'Use squiggly heredoc (`<<~`) instead of `strip_heredoc`.'
30
+ RESTRICT_ON_SEND = %i[strip_heredoc].freeze
31
+
32
+ minimum_target_ruby_version 2.3
33
+
34
+ def on_send(node)
35
+ return unless (receiver = node.receiver)
36
+ return unless receiver.str_type? || receiver.dstr_type?
37
+ return unless receiver.respond_to?(:heredoc?) && receiver.heredoc?
38
+
39
+ register_offense(node, receiver)
40
+ end
41
+
42
+ private
43
+
44
+ def register_offense(node, heredoc)
45
+ add_offense(node) do |corrector|
46
+ squiggly_heredoc = heredoc.source.sub(/\A<<(-|~)?/, '<<~')
47
+
48
+ corrector.replace(heredoc, squiggly_heredoc)
49
+ corrector.remove(node.loc.dot)
50
+ corrector.remove(node.loc.selector)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces the absence of explicit table name assignment.
6
+ # Enforces the absence of explicit table name assignment.
7
7
  #
8
8
  # `self.table_name=` should only be used for very good reasons,
9
9
  # such as not having control over the database, or working
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the use of Time methods without zone.
6
+ # Checks for the use of Time methods without zone.
7
7
  #
8
8
  # Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time)
9
9
  # and the article http://danilenko.org/2012/7/6/rails_timezones/
@@ -28,17 +28,17 @@ module RuboCop
28
28
  # Time.zone.parse('2015-03-02T19:05:37')
29
29
  # Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
30
30
  #
31
- # @example EnforcedStyle: strict
32
- # # `strict` means that `Time` should be used with `zone`.
33
- #
34
- # # bad
35
- # Time.at(timestamp).in_time_zone
36
- #
37
31
  # @example EnforcedStyle: flexible (default)
38
32
  # # `flexible` allows usage of `in_time_zone` instead of `zone`.
39
33
  #
40
34
  # # good
41
35
  # Time.at(timestamp).in_time_zone
36
+ #
37
+ # @example EnforcedStyle: strict
38
+ # # `strict` means that `Time` should be used with `zone`.
39
+ #
40
+ # # bad
41
+ # Time.at(timestamp).in_time_zone
42
42
  class TimeZone < Base
43
43
  include ConfigurableEnforcedStyle
44
44
  extend AutoCorrector
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the use of `Time.zone=` method.
6
+ # Checks for the use of `Time.zone=` method.
7
7
  #
8
8
  # The `zone` attribute persists for the rest of the Ruby runtime, potentially causing
9
9
  # unexpected behavior at a later time.
@@ -22,12 +22,12 @@ module RuboCop
22
22
  MSG = 'Use `Time.use_zone` with block instead of `Time.zone=`.'
23
23
  RESTRICT_ON_SEND = %i[zone=].freeze
24
24
 
25
- def_node_matcher :time_zone_assignement?, <<~PATTERN
25
+ def_node_matcher :time_zone_assignment?, <<~PATTERN
26
26
  (send (const nil? :Time) :zone= ...)
27
27
  PATTERN
28
28
 
29
29
  def on_send(node)
30
- return unless time_zone_assignement?(node)
30
+ return unless time_zone_assignment?(node)
31
31
 
32
32
  add_offense(node)
33
33
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks for consistent uses of `to_fs` or `to_formatted_s`,
7
+ # depending on the cop's configuration.
8
+ #
9
+ # @example EnforcedStyle: to_fs (default)
10
+ #
11
+ # # bad
12
+ # time.to_formatted_s(:db)
13
+ #
14
+ # # good
15
+ # time.to_fs(:db)
16
+ #
17
+ # @example EnforcedStyle: to_formatted_s
18
+ #
19
+ # # bad
20
+ # time.to_fs(:db)
21
+ #
22
+ # # good
23
+ # time.to_formatted_s(:db)
24
+ #
25
+ class ToFormattedS < Base
26
+ include ConfigurableEnforcedStyle
27
+ extend AutoCorrector
28
+ extend TargetRailsVersion
29
+
30
+ minimum_target_rails_version 7.0
31
+
32
+ MSG = 'Use `%<prefer>s` instead.'
33
+ RESTRICT_ON_SEND = %i[to_formatted_s to_fs].freeze
34
+
35
+ def on_send(node)
36
+ return if node.method?(style)
37
+
38
+ add_offense(node.loc.selector, message: format(MSG, prefer: style)) do |corrector|
39
+ corrector.replace(node.loc.selector, style)
40
+ end
41
+ end
42
+ alias on_csend on_send
43
+ end
44
+ end
45
+ end
46
+ end
@@ -3,10 +3,10 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the use of exit statements (namely `return`,
6
+ # Checks for the use of exit statements (namely `return`,
7
7
  # `break` and `throw`) in transactions. This is due to the eventual
8
8
  # unexpected behavior when using ActiveRecord >= 7, where transactions
9
- # exitted using these statements are being rollbacked rather than
9
+ # exited using these statements are being rollbacked rather than
10
10
  # committed (pre ActiveRecord 7 behavior).
11
11
  #
12
12
  # As alternatives, it would be more intuitive to explicitly raise an
@@ -29,6 +29,11 @@ module RuboCop
29
29
  # throw if user.active?
30
30
  # end
31
31
  #
32
+ # # bad, as `with_lock` implicitly opens a transaction too
33
+ # user.with_lock do
34
+ # throw if user.active?
35
+ # end
36
+ #
32
37
  # # good
33
38
  # ApplicationRecord.transaction do
34
39
  # # Rollback
@@ -47,18 +52,26 @@ module RuboCop
47
52
  Exit statement `%<statement>s` is not allowed. Use `raise` (rollback) or `next` (commit).
48
53
  MSG
49
54
 
50
- RESTRICT_ON_SEND = %i[transaction].freeze
55
+ RESTRICT_ON_SEND = %i[transaction with_lock].freeze
51
56
 
52
57
  def_node_search :exit_statements, <<~PATTERN
53
58
  ({return | break | send nil? :throw} ...)
54
59
  PATTERN
55
60
 
61
+ def_node_matcher :rescue_body_return_node?, <<~PATTERN
62
+ (:resbody ...
63
+ ...
64
+ ({return | break | send nil? :throw} ...)
65
+ ...
66
+ )
67
+ PATTERN
68
+
56
69
  def on_send(node)
57
70
  return unless (parent = node.parent)
58
71
  return unless parent.block_type? && parent.body
59
72
 
60
73
  exit_statements(parent.body).each do |statement_node|
61
- next if in_rescue?(statement_node) || nested_block?(statement_node)
74
+ next if statement_node.break_type? && nested_block?(statement_node)
62
75
 
63
76
  statement = statement(statement_node)
64
77
  message = format(MSG, statement: statement)
@@ -79,13 +92,7 @@ module RuboCop
79
92
  end
80
93
  end
81
94
 
82
- def in_rescue?(statement_node)
83
- statement_node.ancestors.find(&:rescue_type?)
84
- end
85
-
86
95
  def nested_block?(statement_node)
87
- return false unless statement_node.break_type?
88
-
89
96
  !statement_node.ancestors.find(&:block_type?).method?(:transaction)
90
97
  end
91
98
  end
@@ -56,9 +56,9 @@ module RuboCop
56
56
 
57
57
  def with_index?(klass, table, names)
58
58
  # Compatibility for Rails 4.2.
59
- add_indicies = schema.add_indicies_by(table_name: table_name(klass))
59
+ add_indices = schema.add_indices_by(table_name: table_name(klass))
60
60
 
61
- (table.indices + add_indicies).any? do |index|
61
+ (table.indices + add_indices).any? do |index|
62
62
  index.unique &&
63
63
  (index.columns.to_set == names ||
64
64
  include_column_names_in_expression_index?(index, names))
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks that environments called with `Rails.env` predicates
6
+ # Checks that environments called with `Rails.env` predicates
7
7
  # exist.
8
8
  # By default the cop allows three environments which Rails ships with:
9
9
  # `development`, `test`, and `production`.
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop suggests you remove a column that does not exist in the schema from `ignored_columns`.
6
+ # Suggests you remove a column that does not exist in the schema from `ignored_columns`.
7
7
  # `ignored_columns` is necessary to drop a column from RDBMS, but you don't need it after the migration
8
8
  # to drop the column. You avoid forgetting to remove `ignored_columns` by this cop.
9
9
  #
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop checks for the use of old-style attribute validation macros.
6
+ # Checks for the use of old-style attribute validation macros.
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 identifies places where manually constructed SQL
6
+ # Identifies places where manually constructed SQL
7
7
  # in `where` can be replaced with `where(attribute: value)`.
8
8
  #
9
9
  # @safety
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop enforces consistent style when using `exists?`.
6
+ # Enforces consistent style when using `exists?`.
7
7
  #
8
8
  # Two styles are supported for this cop. When EnforcedStyle is 'exists'
9
9
  # then the cop enforces `exists?(...)` over `where(...).exists?`.
@@ -12,7 +12,7 @@ module RuboCop
12
12
  # `where(...).exists?` over `exists?(...)`.
13
13
  #
14
14
  # @safety
15
- # This cop is unsafe for auto-correction because the behavior may change on the following case:
15
+ # This cop is unsafe for autocorrection because the behavior may change on the following case:
16
16
  #
17
17
  # [source,ruby]
18
18
  # ----
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # This cop identifies places where manually constructed SQL
6
+ # Identifies places where manually constructed SQL
7
7
  # in `where` can be replaced with `where.not(...)`.
8
8
  #
9
9
  # @example
@@ -34,6 +34,7 @@ require_relative 'rails/default_scope'
34
34
  require_relative 'rails/delegate'
35
35
  require_relative 'rails/delegate_allow_blank'
36
36
  require_relative 'rails/deprecated_active_model_errors_methods'
37
+ require_relative 'rails/dot_separated_keys'
37
38
  require_relative 'rails/duplicate_association'
38
39
  require_relative 'rails/duplicate_scope'
39
40
  require_relative 'rails/duration_arithmetic'
@@ -96,6 +97,7 @@ require_relative 'rails/require_dependency'
96
97
  require_relative 'rails/reversible_migration'
97
98
  require_relative 'rails/reversible_migration_method_definition'
98
99
  require_relative 'rails/root_join_chain'
100
+ require_relative 'rails/root_public_path'
99
101
  require_relative 'rails/safe_navigation'
100
102
  require_relative 'rails/safe_navigation_with_blank'
101
103
  require_relative 'rails/save_bang'
@@ -104,9 +106,11 @@ require_relative 'rails/scope_args'
104
106
  require_relative 'rails/short_i18n'
105
107
  require_relative 'rails/skips_model_validations'
106
108
  require_relative 'rails/squished_sql_heredocs'
109
+ require_relative 'rails/strip_heredoc'
107
110
  require_relative 'rails/table_name_assignment'
108
111
  require_relative 'rails/time_zone'
109
112
  require_relative 'rails/time_zone_assignment'
113
+ require_relative 'rails/to_formatted_s'
110
114
  require_relative 'rails/transaction_exit_statement'
111
115
  require_relative 'rails/uniq_before_pluck'
112
116
  require_relative 'rails/unique_validation_without_index'