rubocop-rails 2.0.1 → 2.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +52 -5
  4. data/config/default.yml +726 -32
  5. data/config/obsoletion.yml +17 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +106 -0
  7. data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +32 -0
  8. data/lib/rubocop/cop/mixin/class_send_node_helper.rb +20 -0
  9. data/lib/rubocop/cop/mixin/enforce_superclass.rb +40 -0
  10. data/lib/rubocop/cop/mixin/index_method.rb +165 -0
  11. data/lib/rubocop/cop/mixin/migrations_helper.rb +26 -0
  12. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +112 -0
  13. data/lib/rubocop/cop/rails/action_controller_test_case.rb +47 -0
  14. data/lib/rubocop/cop/rails/action_filter.rb +11 -21
  15. data/lib/rubocop/cop/rails/action_order.rb +116 -0
  16. data/lib/rubocop/cop/rails/active_record_aliases.rb +23 -24
  17. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +143 -0
  18. data/lib/rubocop/cop/rails/active_record_override.rb +3 -6
  19. data/lib/rubocop/cop/rails/active_support_aliases.rb +13 -22
  20. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  21. data/lib/rubocop/cop/rails/add_column_index.rb +61 -0
  22. data/lib/rubocop/cop/rails/after_commit_override.rb +81 -0
  23. data/lib/rubocop/cop/rails/application_controller.rb +36 -0
  24. data/lib/rubocop/cop/rails/application_job.rb +9 -4
  25. data/lib/rubocop/cop/rails/application_mailer.rb +39 -0
  26. data/lib/rubocop/cop/rails/application_record.rb +9 -9
  27. data/lib/rubocop/cop/rails/arel_star.rb +47 -0
  28. data/lib/rubocop/cop/rails/assert_not.rb +8 -10
  29. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +90 -0
  30. data/lib/rubocop/cop/rails/belongs_to.rb +12 -24
  31. data/lib/rubocop/cop/rails/blank.rb +40 -36
  32. data/lib/rubocop/cop/rails/bulk_change_table.rb +40 -35
  33. data/lib/rubocop/cop/rails/compact_blank.rb +111 -0
  34. data/lib/rubocop/cop/rails/content_tag.rb +93 -0
  35. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +22 -15
  36. data/lib/rubocop/cop/rails/date.rb +41 -36
  37. data/lib/rubocop/cop/rails/default_scope.rb +61 -0
  38. data/lib/rubocop/cop/rails/delegate.rb +33 -29
  39. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +9 -10
  40. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +168 -0
  41. data/lib/rubocop/cop/rails/dot_separated_keys.rb +71 -0
  42. data/lib/rubocop/cop/rails/duplicate_association.rb +56 -0
  43. data/lib/rubocop/cop/rails/duplicate_scope.rb +46 -0
  44. data/lib/rubocop/cop/rails/duration_arithmetic.rb +98 -0
  45. data/lib/rubocop/cop/rails/dynamic_find_by.rb +76 -31
  46. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +82 -0
  47. data/lib/rubocop/cop/rails/enum_hash.rb +75 -0
  48. data/lib/rubocop/cop/rails/enum_uniqueness.rb +30 -12
  49. data/lib/rubocop/cop/rails/environment_comparison.rb +70 -22
  50. data/lib/rubocop/cop/rails/environment_variable_access.rb +67 -0
  51. data/lib/rubocop/cop/rails/exit.rb +7 -13
  52. data/lib/rubocop/cop/rails/expanded_date_range.rb +102 -0
  53. data/lib/rubocop/cop/rails/file_path.rb +48 -31
  54. data/lib/rubocop/cop/rails/find_by.rb +43 -24
  55. data/lib/rubocop/cop/rails/find_by_id.rb +94 -0
  56. data/lib/rubocop/cop/rails/find_each.rb +42 -18
  57. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  58. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +4 -3
  59. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +62 -25
  60. data/lib/rubocop/cop/rails/helper_instance_variable.rb +32 -4
  61. data/lib/rubocop/cop/rails/http_positional_arguments.rb +61 -32
  62. data/lib/rubocop/cop/rails/http_status.rb +27 -23
  63. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +96 -0
  64. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +37 -0
  65. data/lib/rubocop/cop/rails/i18n_locale_texts.rb +110 -0
  66. data/lib/rubocop/cop/rails/ignored_columns_assignment.rb +50 -0
  67. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +9 -16
  68. data/lib/rubocop/cop/rails/index_by.rb +65 -0
  69. data/lib/rubocop/cop/rails/index_with.rb +68 -0
  70. data/lib/rubocop/cop/rails/inquiry.rb +39 -0
  71. data/lib/rubocop/cop/rails/inverse_of.rb +33 -27
  72. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +62 -32
  73. data/lib/rubocop/cop/rails/link_to_blank.rb +31 -32
  74. data/lib/rubocop/cop/rails/mailer_name.rb +90 -0
  75. data/lib/rubocop/cop/rails/match_route.rb +120 -0
  76. data/lib/rubocop/cop/rails/migration_class_name.rb +63 -0
  77. data/lib/rubocop/cop/rails/negate_include.rb +42 -0
  78. data/lib/rubocop/cop/rails/not_null_column.rb +16 -12
  79. data/lib/rubocop/cop/rails/order_by_id.rb +51 -0
  80. data/lib/rubocop/cop/rails/output.rb +29 -10
  81. data/lib/rubocop/cop/rails/output_safety.rb +9 -4
  82. data/lib/rubocop/cop/rails/pick.rb +64 -0
  83. data/lib/rubocop/cop/rails/pluck.rb +96 -0
  84. data/lib/rubocop/cop/rails/pluck_id.rb +59 -0
  85. data/lib/rubocop/cop/rails/pluck_in_where.rb +71 -0
  86. data/lib/rubocop/cop/rails/pluralization_grammar.rb +14 -19
  87. data/lib/rubocop/cop/rails/presence.rb +54 -26
  88. data/lib/rubocop/cop/rails/present.rb +40 -37
  89. data/lib/rubocop/cop/rails/rake_environment.rb +112 -0
  90. data/lib/rubocop/cop/rails/read_write_attribute.rb +56 -18
  91. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +33 -45
  92. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +77 -0
  93. data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +257 -0
  94. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +34 -32
  95. data/lib/rubocop/cop/rails/redundant_travel_back.rb +57 -0
  96. data/lib/rubocop/cop/rails/reflection_class_name.rb +56 -7
  97. data/lib/rubocop/cop/rails/refute_methods.rb +56 -35
  98. data/lib/rubocop/cop/rails/relative_date_constant.rb +52 -33
  99. data/lib/rubocop/cop/rails/render_inline.rb +41 -0
  100. data/lib/rubocop/cop/rails/render_plain_text.rb +71 -0
  101. data/lib/rubocop/cop/rails/request_referer.rb +10 -11
  102. data/lib/rubocop/cop/rails/require_dependency.rb +38 -0
  103. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  104. data/lib/rubocop/cop/rails/reversible_migration.rb +122 -82
  105. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +66 -0
  106. data/lib/rubocop/cop/rails/root_join_chain.rb +72 -0
  107. data/lib/rubocop/cop/rails/root_pathname_methods.rb +238 -0
  108. data/lib/rubocop/cop/rails/root_public_path.rb +59 -0
  109. data/lib/rubocop/cop/rails/safe_navigation.rb +55 -43
  110. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +50 -0
  111. data/lib/rubocop/cop/rails/save_bang.rb +89 -63
  112. data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
  113. data/lib/rubocop/cop/rails/scope_args.rb +8 -3
  114. data/lib/rubocop/cop/rails/short_i18n.rb +71 -0
  115. data/lib/rubocop/cop/rails/skips_model_validations.rb +53 -16
  116. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +87 -0
  117. data/lib/rubocop/cop/rails/strip_heredoc.rb +56 -0
  118. data/lib/rubocop/cop/rails/table_name_assignment.rb +44 -0
  119. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +73 -0
  120. data/lib/rubocop/cop/rails/time_zone.rb +83 -67
  121. data/lib/rubocop/cop/rails/time_zone_assignment.rb +37 -0
  122. data/lib/rubocop/cop/rails/to_formatted_s.rb +46 -0
  123. data/lib/rubocop/cop/rails/to_s_with_argument.rb +78 -0
  124. data/lib/rubocop/cop/rails/top_level_hash_with_indifferent_access.rb +49 -0
  125. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +99 -0
  126. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +40 -49
  127. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +172 -0
  128. data/lib/rubocop/cop/rails/unknown_env.rb +52 -21
  129. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +76 -0
  130. data/lib/rubocop/cop/rails/validation.rb +54 -23
  131. data/lib/rubocop/cop/rails/where_equals.rb +102 -0
  132. data/lib/rubocop/cop/rails/where_exists.rb +138 -0
  133. data/lib/rubocop/cop/rails/where_missing.rb +118 -0
  134. data/lib/rubocop/cop/rails/where_not.rb +101 -0
  135. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  136. data/lib/rubocop/cop/rails_cops.rb +78 -8
  137. data/lib/rubocop/rails/inject.rb +1 -1
  138. data/lib/rubocop/rails/schema_loader/schema.rb +191 -0
  139. data/lib/rubocop/rails/schema_loader.rb +61 -0
  140. data/lib/rubocop/rails/version.rb +5 -1
  141. data/lib/rubocop/rails.rb +3 -1
  142. data/lib/rubocop-rails.rb +22 -0
  143. metadata +120 -19
  144. data/bin/setup +0 -7
@@ -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,73 +3,85 @@
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
- # @example
10
- # # ConvertTry: false
11
- # # bad
12
- # foo.try!(:bar)
13
- # foo.try!(:bar, baz)
14
- # foo.try!(:bar) { |e| e.baz }
10
+ # @example ConvertTry: false (default)
11
+ # # bad
12
+ # foo.try!(:bar)
13
+ # foo.try!(:bar, baz)
14
+ # foo.try!(:bar) { |e| e.baz }
15
15
  #
16
- # foo.try!(:[], 0)
16
+ # foo.try!(:[], 0)
17
17
  #
18
- # # good
19
- # foo.try(:bar)
20
- # foo.try(:bar, baz)
21
- # foo.try(:bar) { |e| e.baz }
18
+ # # good
19
+ # foo.try(:bar)
20
+ # foo.try(:bar, baz)
21
+ # foo.try(:bar) { |e| e.baz }
22
22
  #
23
- # foo&.bar
24
- # foo&.bar(baz)
25
- # foo&.bar { |e| e.baz }
23
+ # foo&.bar
24
+ # foo&.bar(baz)
25
+ # foo&.bar { |e| e.baz }
26
26
  #
27
+ # @example ConvertTry: true
28
+ # # bad
29
+ # foo.try!(:bar)
30
+ # foo.try!(:bar, baz)
31
+ # foo.try!(:bar) { |e| e.baz }
32
+ # foo.try(:bar)
33
+ # foo.try(:bar, baz)
34
+ # foo.try(:bar) { |e| e.baz }
27
35
  #
28
- # # ConvertTry: true
29
- # # bad
30
- # foo.try!(:bar)
31
- # foo.try!(:bar, baz)
32
- # foo.try!(:bar) { |e| e.baz }
33
- # foo.try(:bar)
34
- # foo.try(:bar, baz)
35
- # foo.try(:bar) { |e| e.baz }
36
- #
37
- # # good
38
- # foo&.bar
39
- # foo&.bar(baz)
40
- # foo&.bar { |e| e.baz }
41
- class SafeNavigation < Cop
36
+ # # good
37
+ # foo&.bar
38
+ # foo&.bar(baz)
39
+ # foo&.bar { |e| e.baz }
40
+ class SafeNavigation < Base
42
41
  include RangeHelp
42
+ extend AutoCorrector
43
+ extend TargetRubyVersion
44
+
45
+ minimum_target_ruby_version 2.3
43
46
 
44
47
  MSG = 'Use safe navigation (`&.`) instead of `%<try>s`.'
48
+ RESTRICT_ON_SEND = %i[try try!].freeze
45
49
 
46
- def_node_matcher :try_call, <<-PATTERN
47
- (send !nil? ${:try :try!} $_ ...)
50
+ def_node_matcher :try_call, <<~PATTERN
51
+ (send _ ${:try :try!} $_ ...)
48
52
  PATTERN
49
53
 
54
+ def self.autocorrect_incompatible_with
55
+ [Style::RedundantSelf]
56
+ end
57
+
50
58
  def on_send(node)
51
59
  try_call(node) do |try_method, dispatch|
52
60
  return if try_method == :try && !cop_config['ConvertTry']
53
- return unless dispatch.sym_type? && dispatch.value =~ /\w+[=!?]?/
61
+ return unless dispatch.sym_type? && dispatch.value.match?(/\w+[=!?]?/)
54
62
 
55
- add_offense(node, message: format(MSG, try: try_method))
63
+ add_offense(node, message: format(MSG, try: try_method)) do |corrector|
64
+ autocorrect(corrector, node)
65
+ end
56
66
  end
57
67
  end
58
68
 
59
- def autocorrect(node)
69
+ private
70
+
71
+ def autocorrect(corrector, node)
60
72
  method_node, *params = *node.arguments
61
- method = method_node.source[1..-1]
73
+ method = method_node.source[1..]
62
74
 
63
- range = range_between(node.loc.dot.begin_pos,
64
- node.loc.expression.end_pos)
75
+ range = if node.receiver
76
+ range_between(node.loc.dot.begin_pos, node.source_range.end_pos)
77
+ else
78
+ corrector.insert_before(node, 'self')
79
+ node
80
+ end
65
81
 
66
- lambda do |corrector|
67
- corrector.replace(range, replacement(method, params))
68
- end
82
+ corrector.replace(range, replacement(method, params))
69
83
  end
70
84
 
71
- private
72
-
73
85
  def replacement(method, params)
74
86
  new_params = params.map(&:source).join(', ')
75
87
 
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Checks to make sure safe navigation isn't used with `blank?` in
7
+ # a conditional.
8
+ #
9
+ # @safety
10
+ # While the safe navigation operator is generally a good idea, when
11
+ # checking `foo&.blank?` in a conditional, `foo` being `nil` will actually
12
+ # do the opposite of what the author intends.
13
+ #
14
+ # For example:
15
+ #
16
+ # [source,ruby]
17
+ # ----
18
+ # foo&.blank? #=> nil
19
+ # foo.blank? #=> true
20
+ # ----
21
+ #
22
+ # @example
23
+ # # bad
24
+ # do_something if foo&.blank?
25
+ # do_something unless foo&.blank?
26
+ #
27
+ # # good
28
+ # do_something if foo.blank?
29
+ # do_something unless foo.blank?
30
+ #
31
+ class SafeNavigationWithBlank < Base
32
+ extend AutoCorrector
33
+
34
+ MSG = 'Avoid calling `blank?` with the safe navigation operator in conditionals.'
35
+
36
+ def_node_matcher :safe_navigation_blank_in_conditional?, <<~PATTERN
37
+ (if $(csend ... :blank?) ...)
38
+ PATTERN
39
+
40
+ def on_if(node)
41
+ return unless safe_navigation_blank_in_conditional?(node)
42
+
43
+ add_offense(node) do |corrector|
44
+ corrector.replace(safe_navigation_blank_in_conditional?(node).location.dot, '.')
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -3,18 +3,20 @@
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
  #
10
10
  # This will allow:
11
- # - update or save calls, assigned to a variable,
11
+ #
12
+ # * update or save calls, assigned to a variable,
12
13
  # or used as a condition in an if/unless/case statement.
13
- # - create calls, assigned to a variable that then has a
14
- # call to `persisted?`.
15
- # - calls if the result is explicitly returned from methods and blocks,
14
+ # * create calls, assigned to a variable that then has a
15
+ # call to `persisted?`, or whose return value is checked by
16
+ # `persisted?` immediately
17
+ # * calls if the result is explicitly returned from methods and blocks,
16
18
  # or provided as arguments.
17
- # - calls whose signature doesn't look like an ActiveRecord
19
+ # * calls whose signature doesn't look like an ActiveRecord
18
20
  # persistence method.
19
21
  #
20
22
  # By default it will also allow implicit returns from methods and blocks.
@@ -23,6 +25,25 @@ module RuboCop
23
25
  # You can permit receivers that are giving false positives with
24
26
  # `AllowedReceivers: []`
25
27
  #
28
+ # @safety
29
+ # This cop's autocorrection is unsafe because a custom `update` method call would be changed to `update!`,
30
+ # but the method name in the definition would be unchanged.
31
+ #
32
+ # [source,ruby]
33
+ # ----
34
+ # # Original code
35
+ # def update_attributes
36
+ # end
37
+ #
38
+ # update_attributes
39
+ #
40
+ # # After running rubocop --safe-autocorrect
41
+ # def update_attributes
42
+ # end
43
+ #
44
+ # update
45
+ # ----
46
+ #
26
47
  # @example
27
48
  #
28
49
  # # bad
@@ -96,26 +117,20 @@ module RuboCop
96
117
  # Services::Service::Mailer.update(message: 'Message')
97
118
  # Service::Mailer::update
98
119
  #
99
- class SaveBang < Cop
120
+ class SaveBang < Base
100
121
  include NegativeConditional
122
+ extend AutoCorrector
123
+
124
+ MSG = 'Use `%<prefer>s` instead of `%<current>s` if the return value is not checked.'
125
+ CREATE_MSG = "#{MSG} Or check `persisted?` on model returned from `%<current>s`."
126
+ CREATE_CONDITIONAL_MSG = '`%<current>s` returns a model which is always truthy.'
127
+
128
+ CREATE_PERSIST_METHODS = %i[create create_or_find_by first_or_create find_or_create_by].freeze
129
+ MODIFY_PERSIST_METHODS = %i[save update update_attributes destroy].freeze
130
+ RESTRICT_ON_SEND = (CREATE_PERSIST_METHODS + MODIFY_PERSIST_METHODS).freeze
101
131
 
102
- MSG = 'Use `%<prefer>s` instead of `%<current>s` if the return ' \
103
- 'value is not checked.'
104
- CREATE_MSG = (MSG +
105
- ' Or check `persisted?` on model returned from ' \
106
- '`%<current>s`.').freeze
107
- CREATE_CONDITIONAL_MSG = '`%<current>s` returns a model which is ' \
108
- 'always truthy.'
109
-
110
- CREATE_PERSIST_METHODS = %i[create
111
- first_or_create find_or_create_by].freeze
112
- MODIFY_PERSIST_METHODS = %i[save
113
- update update_attributes destroy].freeze
114
- PERSIST_METHODS = (CREATE_PERSIST_METHODS +
115
- MODIFY_PERSIST_METHODS).freeze
116
-
117
- def join_force?(force_class)
118
- force_class == VariableForce
132
+ def self.joining_forces
133
+ VariableForce
119
134
  end
120
135
 
121
136
  def after_leaving_scope(scope, _variable_table)
@@ -133,35 +148,35 @@ module RuboCop
133
148
  return unless persist_method?(node, CREATE_PERSIST_METHODS)
134
149
  return if persisted_referenced?(assignment)
135
150
 
136
- add_offense_for_node(node, CREATE_MSG)
151
+ register_offense(node, CREATE_MSG)
137
152
  end
138
153
 
139
- def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
154
+ # rubocop:disable Metrics/CyclomaticComplexity
155
+ def on_send(node)
140
156
  return unless persist_method?(node)
141
157
  return if return_value_assigned?(node)
142
- return if check_used_in_conditional(node)
143
- return if argument?(node)
144
158
  return if implicit_return?(node)
159
+ return if check_used_in_condition_or_compound_boolean(node)
160
+ return if argument?(node)
145
161
  return if explicit_return?(node)
162
+ return if checked_immediately?(node)
146
163
 
147
- add_offense_for_node(node)
164
+ register_offense(node, MSG)
148
165
  end
166
+ # rubocop:enable Metrics/CyclomaticComplexity
149
167
  alias on_csend on_send
150
168
 
151
- def autocorrect(node)
152
- save_loc = node.loc.selector
153
- new_method = "#{node.method_name}!"
154
-
155
- ->(corrector) { corrector.replace(save_loc, new_method) }
156
- end
157
-
158
169
  private
159
170
 
160
- def add_offense_for_node(node, msg = MSG)
161
- name = node.method_name
162
- full_message = format(msg, prefer: "#{name}!", current: name.to_s)
171
+ def register_offense(node, msg)
172
+ current_method = node.method_name
173
+ bang_method = "#{current_method}!"
174
+ full_message = format(msg, prefer: bang_method, current: current_method)
163
175
 
164
- add_offense(node, location: :selector, message: full_message)
176
+ range = node.loc.selector
177
+ add_offense(range, message: full_message) do |corrector|
178
+ corrector.replace(range, bang_method)
179
+ end
165
180
  end
166
181
 
167
182
  def right_assignment_node(assignment)
@@ -210,29 +225,37 @@ module RuboCop
210
225
  array
211
226
  end
212
227
 
213
- def check_used_in_conditional(node)
214
- return false unless conditional?(node)
228
+ def check_used_in_condition_or_compound_boolean(node)
229
+ return false unless in_condition_or_compound_boolean?(node)
215
230
 
216
- unless MODIFY_PERSIST_METHODS.include?(node.method_name)
217
- add_offense_for_node(node, CREATE_CONDITIONAL_MSG)
218
- end
231
+ register_offense(node, CREATE_CONDITIONAL_MSG) unless MODIFY_PERSIST_METHODS.include?(node.method_name)
219
232
 
220
233
  true
221
234
  end
222
235
 
223
- def conditional?(node) # rubocop:disable Metrics/CyclomaticComplexity
236
+ def in_condition_or_compound_boolean?(node)
224
237
  node = node.block_node || node
238
+ parent = node.parent
239
+ return false unless parent
225
240
 
226
- condition = node.parent
227
- return false unless condition
241
+ operator_or_single_negative?(parent) || (conditional?(parent) && node == parent.condition)
242
+ end
243
+
244
+ def operator_or_single_negative?(node)
245
+ node.or_type? || node.and_type? || single_negative?(node)
246
+ end
228
247
 
229
- condition.if_type? || condition.case_type? ||
230
- condition.or_type? || condition.and_type? ||
231
- single_negative?(condition)
248
+ def conditional?(parent)
249
+ parent.if_type? || parent.case_type?
250
+ end
251
+
252
+ def checked_immediately?(node)
253
+ node.parent && call_to_persisted?(node.parent)
232
254
  end
233
255
 
234
256
  def allowed_receiver?(node)
235
257
  return false unless node.receiver
258
+ return true if node.receiver.const_name == 'ENV'
236
259
  return false unless cop_config['AllowedReceivers']
237
260
 
238
261
  cop_config['AllowedReceivers'].any? do |allowed_receiver|
@@ -248,7 +271,7 @@ module RuboCop
248
271
  if node.variable?
249
272
  node.node_parts.first == receiver_part.to_sym
250
273
  elsif node.send_type?
251
- node.method_name == receiver_part.to_sym
274
+ node.method?(receiver_part.to_sym)
252
275
  elsif node.const_type?
253
276
  const_matches?(node.const_name, receiver_part)
254
277
  end
@@ -264,9 +287,7 @@ module RuboCop
264
287
  # NameSpace::Const != ::Const
265
288
  # Const != NameSpace::Const
266
289
  def const_matches?(const, allowed_const)
267
- parts = allowed_const.split('::').reverse.zip(
268
- const.split('::').reverse
269
- )
290
+ parts = allowed_const.split('::').reverse.zip(const.split('::').reverse)
270
291
  parts.all? do |(allowed_part, const_part)|
271
292
  allowed_part == const_part.to_s
272
293
  end
@@ -276,10 +297,18 @@ module RuboCop
276
297
  return false unless cop_config['AllowImplicitReturn']
277
298
 
278
299
  node = assignable_node(node)
279
- method = node.parent
300
+ method, sibling_index = find_method_with_sibling_index(node.parent)
280
301
  return unless method && (method.def_type? || method.block_type?)
281
302
 
282
- method.children.size == node.sibling_index + 1
303
+ method.children.size == node.sibling_index + sibling_index
304
+ end
305
+
306
+ def find_method_with_sibling_index(node, sibling_index = 1)
307
+ return node, sibling_index unless node&.or_type?
308
+
309
+ sibling_index += 1
310
+
311
+ find_method_with_sibling_index(node.parent, sibling_index)
283
312
  end
284
313
 
285
314
  def argument?(node)
@@ -296,10 +325,8 @@ module RuboCop
296
325
  assignment&.lvasgn_type?
297
326
  end
298
327
 
299
- def persist_method?(node, methods = PERSIST_METHODS)
300
- methods.include?(node.method_name) &&
301
- expected_signature?(node) &&
302
- !allowed_receiver?(node)
328
+ def persist_method?(node, methods = RESTRICT_ON_SEND)
329
+ methods.include?(node.method_name) && expected_signature?(node) && !allowed_receiver?(node)
303
330
  end
304
331
 
305
332
  # Check argument signature as no arguments or one hash
@@ -307,8 +334,7 @@ module RuboCop
307
334
  !node.arguments? ||
308
335
  (node.arguments.one? &&
309
336
  node.method_name != :destroy &&
310
- (node.first_argument.hash_type? ||
311
- !node.first_argument.literal?))
337
+ (node.first_argument.hash_type? || !node.first_argument.literal?))
312
338
  end
313
339
  end
314
340
  end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces the use of the `comment` option when adding a new table or column
7
+ # to the database during a migration.
8
+ #
9
+ # @example
10
+ # # bad (no comment for a new column or table)
11
+ # add_column :table, :column, :integer
12
+ #
13
+ # create_table :table do |t|
14
+ # t.type :column
15
+ # end
16
+ #
17
+ # # good
18
+ # add_column :table, :column, :integer, comment: 'Number of offenses'
19
+ #
20
+ # create_table :table, comment: 'Table of offenses data' do |t|
21
+ # t.type :column, comment: 'Number of offenses'
22
+ # end
23
+ #
24
+ class SchemaComment < Base
25
+ include ActiveRecordMigrationsHelper
26
+
27
+ COLUMN_MSG = 'New database column without `comment`.'
28
+ TABLE_MSG = 'New database table without `comment`.'
29
+ RESTRICT_ON_SEND = %i[add_column create_table].freeze
30
+ CREATE_TABLE_COLUMN_METHODS = Set[
31
+ *(
32
+ RAILS_ABSTRACT_SCHEMA_DEFINITIONS |
33
+ RAILS_ABSTRACT_SCHEMA_DEFINITIONS_HELPERS |
34
+ POSTGRES_SCHEMA_DEFINITIONS |
35
+ MYSQL_SCHEMA_DEFINITIONS
36
+ )
37
+ ].freeze
38
+
39
+ # @!method comment_present?(node)
40
+ def_node_matcher :comment_present?, <<~PATTERN
41
+ (hash <(pair {(sym :comment) (str "comment")} (_ [present?])) ...>)
42
+ PATTERN
43
+
44
+ # @!method add_column?(node)
45
+ def_node_matcher :add_column?, <<~PATTERN
46
+ (send nil? :add_column _table _column _type _?)
47
+ PATTERN
48
+
49
+ # @!method add_column_with_comment?(node)
50
+ def_node_matcher :add_column_with_comment?, <<~PATTERN
51
+ (send nil? :add_column _table _column _type #comment_present?)
52
+ PATTERN
53
+
54
+ # @!method create_table?(node)
55
+ def_node_matcher :create_table?, <<~PATTERN
56
+ (send nil? :create_table _table _?)
57
+ PATTERN
58
+
59
+ # @!method create_table?(node)
60
+ def_node_matcher :create_table_with_comment?, <<~PATTERN
61
+ (send nil? :create_table _table #comment_present? ...)
62
+ PATTERN
63
+
64
+ # @!method t_column?(node)
65
+ def_node_matcher :t_column?, <<~PATTERN
66
+ (send _var CREATE_TABLE_COLUMN_METHODS ...)
67
+ PATTERN
68
+
69
+ # @!method t_column_with_comment?(node)
70
+ def_node_matcher :t_column_with_comment?, <<~PATTERN
71
+ (send _var CREATE_TABLE_COLUMN_METHODS _column _type? #comment_present?)
72
+ PATTERN
73
+
74
+ def on_send(node)
75
+ if add_column_without_comment?(node)
76
+ add_offense(node, message: COLUMN_MSG)
77
+ elsif create_table?(node)
78
+ if create_table_without_comment?(node)
79
+ add_offense(node, message: TABLE_MSG)
80
+ elsif create_table_column_call_without_comment?(node)
81
+ add_offense(node.parent.body, message: COLUMN_MSG)
82
+ end
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def add_column_without_comment?(node)
89
+ add_column?(node) && !add_column_with_comment?(node)
90
+ end
91
+
92
+ def create_table_without_comment?(node)
93
+ create_table?(node) && !create_table_with_comment?(node)
94
+ end
95
+
96
+ def create_table_column_call_without_comment?(node)
97
+ create_table_with_block?(node.parent) &&
98
+ t_column?(node.parent.body) &&
99
+ !t_column_with_comment?(node.parent.body)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -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
@@ -13,14 +13,19 @@ module RuboCop
13
13
  #
14
14
  # # good
15
15
  # scope :something, -> { where(something: true) }
16
- class ScopeArgs < Cop
16
+ class ScopeArgs < Base
17
+ extend AutoCorrector
18
+
17
19
  MSG = 'Use `lambda`/`proc` instead of a plain method call.'
20
+ RESTRICT_ON_SEND = %i[scope].freeze
18
21
 
19
22
  def_node_matcher :scope?, '(send nil? :scope _ $send)'
20
23
 
21
24
  def on_send(node)
22
25
  scope?(node) do |second_arg|
23
- add_offense(second_arg)
26
+ add_offense(second_arg) do |corrector|
27
+ corrector.replace(second_arg, "-> { #{second_arg.source} }")
28
+ end
24
29
  end
25
30
  end
26
31
  end