rubocop-rails 2.11.2 → 2.19.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +23 -2
  4. data/config/default.yml +316 -20
  5. data/config/obsoletion.yml +10 -0
  6. data/lib/rubocop/cop/mixin/active_record_helper.rb +17 -8
  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 +1 -1
  10. data/lib/rubocop/cop/mixin/index_method.rb +7 -17
  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 +2 -2
  15. data/lib/rubocop/cop/rails/action_order.rb +116 -0
  16. data/lib/rubocop/cop/rails/active_record_aliases.rb +9 -6
  17. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +8 -13
  18. data/lib/rubocop/cop/rails/active_record_override.rb +2 -5
  19. data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
  20. data/lib/rubocop/cop/rails/active_support_on_load.rb +70 -0
  21. data/lib/rubocop/cop/rails/add_column_index.rb +3 -6
  22. data/lib/rubocop/cop/rails/after_commit_override.rb +3 -13
  23. data/lib/rubocop/cop/rails/application_controller.rb +6 -2
  24. data/lib/rubocop/cop/rails/application_job.rb +7 -3
  25. data/lib/rubocop/cop/rails/application_mailer.rb +6 -2
  26. data/lib/rubocop/cop/rails/application_record.rb +7 -2
  27. data/lib/rubocop/cop/rails/arel_star.rb +8 -2
  28. data/lib/rubocop/cop/rails/assert_not.rb +1 -1
  29. data/lib/rubocop/cop/rails/attribute_default_block_value.rb +1 -1
  30. data/lib/rubocop/cop/rails/belongs_to.rb +2 -5
  31. data/lib/rubocop/cop/rails/blank.rb +13 -13
  32. data/lib/rubocop/cop/rails/bulk_change_table.rb +28 -31
  33. data/lib/rubocop/cop/rails/compact_blank.rb +111 -0
  34. data/lib/rubocop/cop/rails/content_tag.rb +42 -33
  35. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -8
  36. data/lib/rubocop/cop/rails/date.rb +12 -17
  37. data/lib/rubocop/cop/rails/default_scope.rb +1 -1
  38. data/lib/rubocop/cop/rails/delegate.rb +24 -18
  39. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +2 -2
  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 +31 -15
  46. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +6 -2
  47. data/lib/rubocop/cop/rails/enum_hash.rb +3 -4
  48. data/lib/rubocop/cop/rails/enum_uniqueness.rb +3 -6
  49. data/lib/rubocop/cop/rails/environment_comparison.rb +3 -4
  50. data/lib/rubocop/cop/rails/environment_variable_access.rb +1 -1
  51. data/lib/rubocop/cop/rails/exit.rb +1 -1
  52. data/lib/rubocop/cop/rails/expanded_date_range.rb +39 -23
  53. data/lib/rubocop/cop/rails/file_path.rb +41 -24
  54. data/lib/rubocop/cop/rails/find_by.rb +1 -1
  55. data/lib/rubocop/cop/rails/find_by_id.rb +3 -3
  56. data/lib/rubocop/cop/rails/find_each.rb +27 -4
  57. data/lib/rubocop/cop/rails/freeze_time.rb +79 -0
  58. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -1
  59. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +19 -9
  60. data/lib/rubocop/cop/rails/helper_instance_variable.rb +3 -3
  61. data/lib/rubocop/cop/rails/http_positional_arguments.rb +29 -11
  62. data/lib/rubocop/cop/rails/http_status.rb +6 -11
  63. data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +96 -0
  64. data/lib/rubocop/cop/rails/i18n_locale_assignment.rb +1 -1
  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 +5 -14
  68. data/lib/rubocop/cop/rails/index_by.rb +8 -8
  69. data/lib/rubocop/cop/rails/index_with.rb +8 -8
  70. data/lib/rubocop/cop/rails/inquiry.rb +1 -1
  71. data/lib/rubocop/cop/rails/inverse_of.rb +20 -10
  72. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +44 -16
  73. data/lib/rubocop/cop/rails/link_to_blank.rb +4 -7
  74. data/lib/rubocop/cop/rails/mailer_name.rb +9 -5
  75. data/lib/rubocop/cop/rails/match_route.rb +1 -1
  76. data/lib/rubocop/cop/rails/migration_class_name.rb +63 -0
  77. data/lib/rubocop/cop/rails/negate_include.rb +5 -4
  78. data/lib/rubocop/cop/rails/not_null_column.rb +10 -7
  79. data/lib/rubocop/cop/rails/order_by_id.rb +2 -3
  80. data/lib/rubocop/cop/rails/output.rb +26 -10
  81. data/lib/rubocop/cop/rails/output_safety.rb +6 -2
  82. data/lib/rubocop/cop/rails/pick.rb +8 -1
  83. data/lib/rubocop/cop/rails/pluck.rb +51 -11
  84. data/lib/rubocop/cop/rails/pluck_id.rb +5 -2
  85. data/lib/rubocop/cop/rails/pluck_in_where.rb +8 -7
  86. data/lib/rubocop/cop/rails/pluralization_grammar.rb +4 -5
  87. data/lib/rubocop/cop/rails/presence.rb +22 -13
  88. data/lib/rubocop/cop/rails/present.rb +10 -13
  89. data/lib/rubocop/cop/rails/rake_environment.rb +8 -3
  90. data/lib/rubocop/cop/rails/read_write_attribute.rb +52 -15
  91. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +7 -17
  92. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +3 -3
  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 +31 -27
  95. data/lib/rubocop/cop/rails/redundant_travel_back.rb +57 -0
  96. data/lib/rubocop/cop/rails/reflection_class_name.rb +39 -4
  97. data/lib/rubocop/cop/rails/refute_methods.rb +1 -5
  98. data/lib/rubocop/cop/rails/relative_date_constant.rb +9 -9
  99. data/lib/rubocop/cop/rails/render_inline.rb +1 -1
  100. data/lib/rubocop/cop/rails/render_plain_text.rb +1 -1
  101. data/lib/rubocop/cop/rails/request_referer.rb +2 -3
  102. data/lib/rubocop/cop/rails/require_dependency.rb +2 -2
  103. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  104. data/lib/rubocop/cop/rails/reversible_migration.rb +29 -67
  105. data/lib/rubocop/cop/rails/reversible_migration_method_definition.rb +6 -15
  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 +8 -13
  110. data/lib/rubocop/cop/rails/safe_navigation_with_blank.rb +14 -7
  111. data/lib/rubocop/cop/rails/save_bang.rb +30 -23
  112. data/lib/rubocop/cop/rails/schema_comment.rb +104 -0
  113. data/lib/rubocop/cop/rails/scope_args.rb +6 -2
  114. data/lib/rubocop/cop/rails/short_i18n.rb +3 -6
  115. data/lib/rubocop/cop/rails/skips_model_validations.rb +3 -4
  116. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +13 -8
  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 +37 -32
  121. data/lib/rubocop/cop/rails/time_zone_assignment.rb +4 -4
  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 +32 -41
  127. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +16 -9
  128. data/lib/rubocop/cop/rails/unknown_env.rb +3 -5
  129. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +9 -2
  130. data/lib/rubocop/cop/rails/validation.rb +5 -13
  131. data/lib/rubocop/cop/rails/where_equals.rb +6 -2
  132. data/lib/rubocop/cop/rails/where_exists.rb +11 -10
  133. data/lib/rubocop/cop/rails/where_missing.rb +118 -0
  134. data/lib/rubocop/cop/rails/where_not.rb +2 -2
  135. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +55 -0
  136. data/lib/rubocop/cop/rails_cops.rb +34 -0
  137. data/lib/rubocop/rails/schema_loader/schema.rb +8 -5
  138. data/lib/rubocop/rails/version.rb +1 -1
  139. data/lib/rubocop/rails.rb +1 -1
  140. data/lib/rubocop-rails.rb +19 -0
  141. metadata +42 -8
  142. data/bin/setup +0 -7
@@ -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
  #
@@ -25,6 +25,25 @@ module RuboCop
25
25
  # You can permit receivers that are giving false positives with
26
26
  # `AllowedReceivers: []`
27
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
+ #
28
47
  # @example
29
48
  #
30
49
  # # bad
@@ -102,18 +121,12 @@ module RuboCop
102
121
  include NegativeConditional
103
122
  extend AutoCorrector
104
123
 
105
- MSG = 'Use `%<prefer>s` instead of `%<current>s` if the return ' \
106
- 'value is not checked.'
107
- CREATE_MSG = (MSG +
108
- ' Or check `persisted?` on model returned from ' \
109
- '`%<current>s`.').freeze
110
- CREATE_CONDITIONAL_MSG = '`%<current>s` returns a model which is ' \
111
- 'always truthy.'
112
-
113
- CREATE_PERSIST_METHODS = %i[create create_or_find_by
114
- first_or_create find_or_create_by].freeze
115
- MODIFY_PERSIST_METHODS = %i[save
116
- update update_attributes destroy].freeze
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
117
130
  RESTRICT_ON_SEND = (CREATE_PERSIST_METHODS + MODIFY_PERSIST_METHODS).freeze
118
131
 
119
132
  def self.joining_forces
@@ -225,8 +238,7 @@ module RuboCop
225
238
  parent = node.parent
226
239
  return false unless parent
227
240
 
228
- operator_or_single_negative?(parent) ||
229
- (conditional?(parent) && node == parent.condition)
241
+ operator_or_single_negative?(parent) || (conditional?(parent) && node == parent.condition)
230
242
  end
231
243
 
232
244
  def operator_or_single_negative?(node)
@@ -275,9 +287,7 @@ module RuboCop
275
287
  # NameSpace::Const != ::Const
276
288
  # Const != NameSpace::Const
277
289
  def const_matches?(const, allowed_const)
278
- parts = allowed_const.split('::').reverse.zip(
279
- const.split('::').reverse
280
- )
290
+ parts = allowed_const.split('::').reverse.zip(const.split('::').reverse)
281
291
  parts.all? do |(allowed_part, const_part)|
282
292
  allowed_part == const_part.to_s
283
293
  end
@@ -316,9 +326,7 @@ module RuboCop
316
326
  end
317
327
 
318
328
  def persist_method?(node, methods = RESTRICT_ON_SEND)
319
- methods.include?(node.method_name) &&
320
- expected_signature?(node) &&
321
- !allowed_receiver?(node)
329
+ methods.include?(node.method_name) && expected_signature?(node) && !allowed_receiver?(node)
322
330
  end
323
331
 
324
332
  # Check argument signature as no arguments or one hash
@@ -326,8 +334,7 @@ module RuboCop
326
334
  !node.arguments? ||
327
335
  (node.arguments.one? &&
328
336
  node.method_name != :destroy &&
329
- (node.first_argument.hash_type? ||
330
- !node.first_argument.literal?))
337
+ (node.first_argument.hash_type? || !node.first_argument.literal?))
331
338
  end
332
339
  end
333
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
@@ -14,6 +14,8 @@ module RuboCop
14
14
  # # good
15
15
  # scope :something, -> { where(something: true) }
16
16
  class ScopeArgs < Base
17
+ extend AutoCorrector
18
+
17
19
  MSG = 'Use `lambda`/`proc` instead of a plain method call.'
18
20
  RESTRICT_ON_SEND = %i[scope].freeze
19
21
 
@@ -21,7 +23,9 @@ module RuboCop
21
23
 
22
24
  def on_send(node)
23
25
  scope?(node) do |second_arg|
24
- add_offense(second_arg)
26
+ add_offense(second_arg) do |corrector|
27
+ corrector.replace(second_arg, "-> { #{second_arg.source} }")
28
+ end
25
29
  end
26
30
  end
27
31
  end
@@ -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
@@ -44,15 +44,12 @@ module RuboCop
44
44
 
45
45
  MSG = 'Use `%<good_method>s` instead of `%<bad_method>s`.'
46
46
 
47
- PREFERRED_METHODS = {
48
- translate: :t,
49
- localize: :l
50
- }.freeze
47
+ PREFERRED_METHODS = { translate: :t, localize: :l }.freeze
51
48
 
52
49
  RESTRICT_ON_SEND = PREFERRED_METHODS.keys.freeze
53
50
 
54
51
  def_node_matcher :long_i18n?, <<~PATTERN
55
- (send {nil? (const nil? :I18n)} ${:translate :localize} ...)
52
+ (send {nil? (const {nil? cbase} :I18n)} ${:translate :localize} ...)
56
53
  PATTERN
57
54
 
58
55
  def on_send(node)
@@ -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
  #
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  def_node_matcher :good_touch?, <<~PATTERN
59
59
  {
60
- (send (const nil? :FileUtils) :touch ...)
60
+ (send (const {nil? cbase} :FileUtils) :touch ...)
61
61
  (send _ :touch {true false})
62
62
  }
63
63
  PATTERN
@@ -93,8 +93,7 @@ module RuboCop
93
93
  end
94
94
 
95
95
  def allowed_method?(node)
96
- METHODS_WITH_ARGUMENTS.include?(node.method_name.to_s) &&
97
- !node.arguments?
96
+ METHODS_WITH_ARGUMENTS.include?(node.method_name.to_s) && !node.arguments?
98
97
  end
99
98
 
100
99
  def forbidden_methods
@@ -5,8 +5,10 @@ module RuboCop
5
5
  module Rails
6
6
  #
7
7
  # Checks SQL heredocs to use `.squish`.
8
- # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
9
- # to be preserved in order to work, thus auto-correction for this cop is not safe.
8
+ #
9
+ # @safety
10
+ # Some SQL syntax (e.g. PostgreSQL comments and functions) requires newlines
11
+ # to be preserved in order to work, thus autocorrection for this cop is not safe.
10
12
  #
11
13
  # @example
12
14
  # # bad
@@ -46,6 +48,7 @@ module RuboCop
46
48
  SQL = 'SQL'
47
49
  SQUISH = '.squish'
48
50
  MSG = 'Use `%<expect>s` instead of `%<current>s`.'
51
+ SQL_IDENTIFIER_MARKERS = /(".+?")|('.+?')|(\[.+?\])/.freeze
49
52
 
50
53
  def on_heredoc(node)
51
54
  return unless offense_detected?(node)
@@ -58,7 +61,7 @@ module RuboCop
58
61
  private
59
62
 
60
63
  def offense_detected?(node)
61
- sql_heredoc?(node) && !using_squish?(node)
64
+ sql_heredoc?(node) && !using_squish?(node) && !singleline_comments_present?(node)
62
65
  end
63
66
 
64
67
  def sql_heredoc?(node)
@@ -69,12 +72,14 @@ module RuboCop
69
72
  node.parent&.send_type? && node.parent&.method?(:squish)
70
73
  end
71
74
 
75
+ def singleline_comments_present?(node)
76
+ sql = node.children.map { |c| c.is_a?(String) ? c : c.source }.join('\n')
77
+
78
+ sql.gsub(SQL_IDENTIFIER_MARKERS, '').include?('--')
79
+ end
80
+
72
81
  def message(node)
73
- format(
74
- MSG,
75
- expect: "#{node.source}#{SQUISH}",
76
- current: node.source
77
- )
82
+ format(MSG, expect: "#{node.source}#{SQUISH}", current: node.source)
78
83
  end
79
84
  end
80
85
  end
@@ -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
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces the absence of explicit table name assignment.
7
+ #
8
+ # `self.table_name=` should only be used for very good reasons,
9
+ # such as not having control over the database, or working
10
+ # on a legacy project.
11
+ #
12
+ # If you need to change how your model's name is translated to
13
+ # a table name, you may want to look at Inflections:
14
+ # https://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html
15
+ #
16
+ # If you wish to add a prefix in front of your model, or wish to change
17
+ # the default prefix, `self.table_name_prefix` might better suit your needs:
18
+ # https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema.html#method-c-table_name_prefix-3D
19
+ #
20
+ # STI base classes named `Base` are ignored by this cop.
21
+ # For more information: https://api.rubyonrails.org/classes/ActiveRecord/Inheritance.html
22
+ #
23
+ # @example
24
+ # # bad
25
+ # self.table_name = 'some_table_name'
26
+ # self.table_name = :some_other_name
27
+ class TableNameAssignment < Base
28
+ include ActiveRecordHelper
29
+
30
+ MSG = 'Do not use `self.table_name =`.'
31
+
32
+ def_node_matcher :base_class?, <<~PATTERN
33
+ (class (const ... :Base) ...)
34
+ PATTERN
35
+
36
+ def on_class(class_node)
37
+ return if base_class?(class_node)
38
+
39
+ find_set_table_name(class_node).each { |node| add_offense(node) }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # Enforces that boolean columns are created with default values (`false` or `true`) and
7
+ # `NOT NULL` constraint.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # add_column :users, :active, :boolean
12
+ # t.column :active, :boolean
13
+ # t.boolean :active
14
+ #
15
+ # # good
16
+ # add_column :users, :active, :boolean, default: true, null: false
17
+ # t.column :active, :boolean, default: true, null: false
18
+ # t.boolean :active, default: true, null: false
19
+ #
20
+ class ThreeStateBooleanColumn < Base
21
+ MSG = 'Boolean columns should always have a default value and a `NOT NULL` constraint.'
22
+
23
+ RESTRICT_ON_SEND = %i[add_column column boolean].freeze
24
+
25
+ def_node_matcher :three_state_boolean?, <<~PATTERN
26
+ {
27
+ (send nil? :add_column _ $_ {(sym :boolean) (str "boolean")} $_ ?)
28
+ (send !nil? :column $_ {(sym :boolean) (str "boolean")} $_ ?)
29
+ (send !nil? :boolean $_ $_ ?)
30
+ }
31
+ PATTERN
32
+
33
+ def_node_matcher :required_options?, <<~PATTERN
34
+ (hash <(pair (sym :default) !nil?) (pair (sym :null) false) ...>)
35
+ PATTERN
36
+
37
+ def_node_search :change_column_null?, <<~PATTERN
38
+ (send nil? :change_column_null {(sym %1) (str %1)} {(sym %2) (str %2)} false)
39
+ PATTERN
40
+
41
+ def on_send(node)
42
+ three_state_boolean?(node) do |column_node, options_node|
43
+ options_node = options_node.first
44
+
45
+ return if required_options?(options_node)
46
+
47
+ def_node = node.each_ancestor(:def, :defs).first
48
+ table_node = table_node(node)
49
+ if def_node && (table_node.nil? || change_column_null?(def_node, table_node.value, column_node.value))
50
+ return
51
+ end
52
+
53
+ add_offense(node)
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def table_node(node)
60
+ case node.method_name
61
+ when :add_column
62
+ node.first_argument
63
+ when :column, :boolean
64
+ ancestor = node.each_ancestor(:block).find do |n|
65
+ n.method?(:create_table) || n.method?(:change_table)
66
+ end
67
+ ancestor&.send_node&.first_argument
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -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/
@@ -14,6 +14,9 @@ module RuboCop
14
14
  # When EnforcedStyle is 'flexible' then it's also allowed
15
15
  # to use `Time#in_time_zone`.
16
16
  #
17
+ # @safety
18
+ # This cop's autocorrection is unsafe because it may change handling time.
19
+ #
17
20
  # @example
18
21
  # # bad
19
22
  # Time.now
@@ -25,38 +28,34 @@ module RuboCop
25
28
  # Time.zone.parse('2015-03-02T19:05:37')
26
29
  # Time.zone.parse('2015-03-02T19:05:37Z') # Respect ISO 8601 format with timezone specifier.
27
30
  #
28
- # @example EnforcedStyle: strict
29
- # # `strict` means that `Time` should be used with `zone`.
30
- #
31
- # # bad
32
- # Time.at(timestamp).in_time_zone
33
- #
34
31
  # @example EnforcedStyle: flexible (default)
35
32
  # # `flexible` allows usage of `in_time_zone` instead of `zone`.
36
33
  #
37
34
  # # good
38
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
39
42
  class TimeZone < Base
40
43
  include ConfigurableEnforcedStyle
41
44
  extend AutoCorrector
42
45
 
43
- MSG = 'Do not use `%<current>s` without zone. Use `%<prefer>s` ' \
44
- 'instead.'
46
+ MSG = 'Do not use `%<current>s` without zone. Use `%<prefer>s` instead.'
45
47
 
46
- MSG_ACCEPTABLE = 'Do not use `%<current>s` without zone. ' \
47
- 'Use one of %<prefer>s instead.'
48
+ MSG_ACCEPTABLE = 'Do not use `%<current>s` without zone. Use one of %<prefer>s instead.'
48
49
 
49
- MSG_LOCALTIME = 'Do not use `Time.localtime` without ' \
50
- 'offset or zone.'
50
+ MSG_LOCALTIME = 'Do not use `Time.localtime` without offset or zone.'
51
51
 
52
52
  GOOD_METHODS = %i[zone zone_default find_zone find_zone!].freeze
53
53
 
54
54
  DANGEROUS_METHODS = %i[now local new parse at].freeze
55
55
 
56
- ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601
57
- jisx0301 rfc3339 httpdate to_i to_f].freeze
56
+ ACCEPTED_METHODS = %i[in_time_zone utc getlocal xmlschema iso8601 jisx0301 rfc3339 httpdate to_i to_f].freeze
58
57
 
59
- TIMEZONE_SPECIFIER = /[A-z]/.freeze
58
+ TIMEZONE_SPECIFIER = /([A-z]|[+-]\d{2}:?\d{2})\z/.freeze
60
59
 
61
60
  def on_const(node)
62
61
  mod, klass = *node
@@ -71,7 +70,7 @@ module RuboCop
71
70
 
72
71
  def autocorrect(corrector, node)
73
72
  # add `.zone`: `Time.at` => `Time.zone.at`
74
- corrector.insert_after(node.children[0].source_range, '.zone')
73
+ corrector.insert_after(node.children[0], '.zone')
75
74
 
76
75
  case node.method_name
77
76
  when :current
@@ -82,7 +81,7 @@ module RuboCop
82
81
  end
83
82
 
84
83
  # prefer `Time` over `DateTime` class
85
- corrector.replace(node.children.first.source_range, 'Time') if strict?
84
+ corrector.replace(node.children.first, 'Time') if strict?
86
85
  remove_redundant_in_time_zone(corrector, node)
87
86
  end
88
87
 
@@ -97,13 +96,11 @@ module RuboCop
97
96
  # remove redundant `.in_time_zone` from `Time.zone.now.in_time_zone`
98
97
  def remove_redundant_in_time_zone(corrector, node)
99
98
  time_methods_called = extract_method_chain(node)
100
- return unless time_methods_called.include?(:in_time_zone) ||
101
- time_methods_called.include?(:zone)
99
+ return unless time_methods_called.include?(:in_time_zone) || time_methods_called.include?(:zone)
102
100
 
103
101
  while node&.send_type?
104
102
  if node.children.last == :in_time_zone
105
- in_time_zone_with_dot =
106
- node.loc.selector.adjust(begin_pos: -1)
103
+ in_time_zone_with_dot = node.loc.selector.adjust(begin_pos: -1)
107
104
  corrector.remove(in_time_zone_with_dot)
108
105
  end
109
106
  node = node.parent
@@ -129,7 +126,7 @@ module RuboCop
129
126
  end
130
127
 
131
128
  def attach_timezone_specifier?(date)
132
- date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s[-1])
129
+ date.respond_to?(:value) && TIMEZONE_SPECIFIER.match?(date.value.to_s)
133
130
  end
134
131
 
135
132
  def build_message(klass, method_name, node)
@@ -141,9 +138,7 @@ module RuboCop
141
138
  )
142
139
  else
143
140
  safe_method_name = safe_method(method_name, node)
144
- format(MSG,
145
- current: "#{klass}.#{method_name}",
146
- prefer: "Time.zone.#{safe_method_name}")
141
+ format(MSG, current: "#{klass}.#{method_name}", prefer: "Time.zone.#{safe_method_name}")
147
142
  end
148
143
  end
149
144
 
@@ -224,10 +219,7 @@ module RuboCop
224
219
  end
225
220
 
226
221
  def acceptable_methods(klass, method_name, node)
227
- acceptable = [
228
- "`Time.zone.#{safe_method(method_name, node)}`",
229
- "`#{klass}.current`"
230
- ]
222
+ acceptable = ["`Time.zone.#{safe_method(method_name, node)}`", "`#{klass}.current`"]
231
223
 
232
224
  ACCEPTED_METHODS.each do |am|
233
225
  acceptable << "`#{klass}.#{method_name}.#{am}`"
@@ -236,12 +228,25 @@ module RuboCop
236
228
  acceptable
237
229
  end
238
230
 
239
- # Time.new can be called with a time zone offset
231
+ # Time.new, Time.at, and Time.now can be called with a time zone offset
240
232
  # When it is, that should be considered safe
241
233
  # Example:
242
234
  # Time.new(1988, 3, 15, 3, 0, 0, "-05:00")
243
235
  def offset_provided?(node)
244
- node.arguments.size >= 7
236
+ case node.method_name
237
+ when :new
238
+ node.arguments.size == 7 || offset_option_provided?(node)
239
+ when :at, :now
240
+ offset_option_provided?(node)
241
+ end
242
+ end
243
+
244
+ def offset_option_provided?(node)
245
+ options = node.last_argument
246
+ options&.hash_type? &&
247
+ options.each_pair.any? do |pair|
248
+ pair.key.sym_type? && pair.key.value == :in && !pair.value.nil_type?
249
+ end
245
250
  end
246
251
  end
247
252
  end