rubocop-rails 2.17.4 → 2.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +22 -2
  4. data/config/default.yml +54 -29
  5. data/lib/rubocop/cop/mixin/enforce_superclass.rb +1 -1
  6. data/lib/rubocop/cop/mixin/index_method.rb +3 -3
  7. data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +10 -2
  8. data/lib/rubocop/cop/rails/action_controller_test_case.rb +1 -1
  9. data/lib/rubocop/cop/rails/action_order.rb +5 -6
  10. data/lib/rubocop/cop/rails/active_record_aliases.rb +2 -0
  11. data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +6 -3
  12. data/lib/rubocop/cop/rails/add_column_index.rb +2 -2
  13. data/lib/rubocop/cop/rails/application_job.rb +1 -1
  14. data/lib/rubocop/cop/rails/arel_star.rb +1 -1
  15. data/lib/rubocop/cop/rails/assert_not.rb +1 -1
  16. data/lib/rubocop/cop/rails/belongs_to.rb +1 -4
  17. data/lib/rubocop/cop/rails/blank.rb +5 -5
  18. data/lib/rubocop/cop/rails/bulk_change_table.rb +1 -4
  19. data/lib/rubocop/cop/rails/compact_blank.rb +2 -2
  20. data/lib/rubocop/cop/rails/content_tag.rb +1 -1
  21. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +16 -3
  22. data/lib/rubocop/cop/rails/delegate.rb +17 -3
  23. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
  24. data/lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb +1 -1
  25. data/lib/rubocop/cop/rails/duration_arithmetic.rb +1 -1
  26. data/lib/rubocop/cop/rails/dynamic_find_by.rb +1 -1
  27. data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +1 -1
  28. data/lib/rubocop/cop/rails/enum_hash.rb +1 -1
  29. data/lib/rubocop/cop/rails/environment_comparison.rb +1 -1
  30. data/lib/rubocop/cop/rails/file_path.rb +31 -15
  31. data/lib/rubocop/cop/rails/find_by_id.rb +2 -2
  32. data/lib/rubocop/cop/rails/find_each.rb +6 -2
  33. data/lib/rubocop/cop/rails/freeze_time.rb +3 -0
  34. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +2 -2
  35. data/lib/rubocop/cop/rails/http_positional_arguments.rb +18 -2
  36. data/lib/rubocop/cop/rails/http_status.rb +1 -1
  37. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +0 -2
  38. data/lib/rubocop/cop/rails/inverse_of.rb +0 -3
  39. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +17 -7
  40. data/lib/rubocop/cop/rails/link_to_blank.rb +1 -1
  41. data/lib/rubocop/cop/rails/mailer_name.rb +1 -1
  42. data/lib/rubocop/cop/rails/negate_include.rb +1 -1
  43. data/lib/rubocop/cop/rails/not_null_column.rb +9 -6
  44. data/lib/rubocop/cop/rails/output.rb +3 -2
  45. data/lib/rubocop/cop/rails/output_safety.rb +5 -1
  46. data/lib/rubocop/cop/rails/pluck.rb +13 -1
  47. data/lib/rubocop/cop/rails/pluck_id.rb +1 -1
  48. data/lib/rubocop/cop/rails/presence.rb +3 -3
  49. data/lib/rubocop/cop/rails/present.rb +5 -5
  50. data/lib/rubocop/cop/rails/rake_environment.rb +1 -1
  51. data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -1
  52. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +3 -3
  53. data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
  54. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -1
  55. data/lib/rubocop/cop/rails/reflection_class_name.rb +17 -1
  56. data/lib/rubocop/cop/rails/relative_date_constant.rb +2 -2
  57. data/lib/rubocop/cop/rails/response_parsed_body.rb +57 -0
  58. data/lib/rubocop/cop/rails/reversible_migration.rb +4 -29
  59. data/lib/rubocop/cop/rails/root_join_chain.rb +1 -1
  60. data/lib/rubocop/cop/rails/root_pathname_methods.rb +1 -1
  61. data/lib/rubocop/cop/rails/safe_navigation.rb +1 -1
  62. data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +8 -1
  63. data/lib/rubocop/cop/rails/three_state_boolean_column.rb +71 -0
  64. data/lib/rubocop/cop/rails/time_zone.rb +2 -2
  65. data/lib/rubocop/cop/rails/transaction_exit_statement.rb +0 -2
  66. data/lib/rubocop/cop/rails/unique_validation_without_index.rb +1 -1
  67. data/lib/rubocop/cop/rails/unused_ignored_columns.rb +6 -1
  68. data/lib/rubocop/cop/rails/validation.rb +1 -1
  69. data/lib/rubocop/cop/rails/where_equals.rb +1 -1
  70. data/lib/rubocop/cop/rails/where_exists.rb +1 -1
  71. data/lib/rubocop/cop/rails/where_missing.rb +3 -3
  72. data/lib/rubocop/cop/rails/where_not.rb +1 -1
  73. data/lib/rubocop/cop/rails/where_not_with_multiple_conditions.rb +2 -2
  74. data/lib/rubocop/cop/rails_cops.rb +2 -0
  75. data/lib/rubocop/rails/version.rb +1 -1
  76. data/lib/rubocop/rails.rb +1 -1
  77. data/lib/rubocop-rails.rb +1 -0
  78. metadata +6 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a9beeaefa16cbe4afb5d598878444a835536ccda5d9b46c3e44e111513f1d8e
4
- data.tar.gz: 669a9681ce5ab1fca038229a831f7e5acae6bbdcc3a58ecfc88d65fd54ef7190
3
+ metadata.gz: 327dd4bed957b6be1d52e47a9087f43abf7f7361ddaf8b0c3b13dbb0bf7bbb52
4
+ data.tar.gz: cf2c5747f8abab6e65ff8ffc9d31693c42c0515e8751a1d5b0651c12c7ceb93a
5
5
  SHA512:
6
- metadata.gz: 72ca38ec9a9136bfa5bd635db9290314a4d1a4c8e3942f15aa62a823885ff1a1783f884598dce6f52765db75daccf8e4054d3faecdf973da6f2c53b10dc2c153
7
- data.tar.gz: 536c7d8f5dfec22444d2d0fc4b297eac18f7b87cc6c871ce250bec58a2a6d380e814749534e5aedc47c0a66c7ae80ac8c61ffec330093c575cd1cc8dd76e4de9
6
+ metadata.gz: 4114a576ae595944cd0ec1b673e8f706fa975a643fb9d56e25b5498b7964f5141e2724aeb2dc24c6b63f6d501c7e1a85469a32333be69862e054fdb10874f09c
7
+ data.tar.gz: c87e88b3de8ea166a68a0be6a08e70e387790b3d705648145a8d769978f1c92e03ea18fe510b1ce5543c6f1fefe59be1f0fd583ba392dd2274b1dde58a21ca61
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012-22 Bozhidar Batsov
1
+ Copyright (c) 2012-23 Bozhidar Batsov
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -12,7 +12,7 @@ Note: This repository manages rubocop-rails gem (>= 2.0.0). rubocop-rails gem (<
12
12
  Just install the `rubocop-rails` gem
13
13
 
14
14
  ```sh
15
- gem install rubocop-rails
15
+ $ gem install rubocop-rails
16
16
  ```
17
17
 
18
18
  or if you use bundler put this in your `Gemfile`
@@ -48,7 +48,7 @@ cops together with the standard cops.
48
48
  ### Command line
49
49
 
50
50
  ```sh
51
- rubocop --require rubocop-rails
51
+ $ rubocop --require rubocop-rails
52
52
  ```
53
53
 
54
54
  Note: `--rails` option is required while `rubocop` command supports `--rails` option.
@@ -61,6 +61,26 @@ RuboCop::RakeTask.new do |task|
61
61
  end
62
62
  ```
63
63
 
64
+ ## Rails configuration tip
65
+
66
+ If you are using Rails 6.1 or newer, add the following `config.generators.after_generate` setting to
67
+ your config/application.rb to apply RuboCop autocorrection to code generated by `bin/rails g`.
68
+
69
+ ```ruby
70
+ # config/application.rb
71
+ module YourCoolApp
72
+ class Application < Rails::Application
73
+ config.generators.after_generate do |files|
74
+ system("bundle exec rubocop -A --fail-level=E #{files.shelljoin}", exception: true)
75
+ end
76
+ end
77
+ end
78
+ ```
79
+
80
+ It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsafe autocorretion cops.
81
+ `rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to
82
+ be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead.
83
+
64
84
  ## The Cops
65
85
 
66
86
  All cops are located under
data/config/default.yml CHANGED
@@ -46,23 +46,6 @@ Lint/NumberConversion:
46
46
  - fortnights
47
47
  - in_milliseconds
48
48
  AllowedPatterns: []
49
- # Deprecated.
50
- IgnoredMethods:
51
- - ago
52
- - from_now
53
- - second
54
- - seconds
55
- - minute
56
- - minutes
57
- - hour
58
- - hours
59
- - day
60
- - days
61
- - week
62
- - weeks
63
- - fortnight
64
- - fortnights
65
- - in_milliseconds
66
49
 
67
50
  Rails:
68
51
  Enabled: true
@@ -70,8 +53,6 @@ Rails:
70
53
 
71
54
  Rails/ActionControllerFlashBeforeRender:
72
55
  Description: 'Use `flash.now` instead of `flash` before `render`.'
73
- StyleGuide: 'https://rails.rubystyle.guide/#flash-before-render'
74
- Reference: 'https://api.rubyonrails.org/classes/ActionController/FlashBeforeRender.html'
75
56
  Enabled: 'pending'
76
57
  SafeAutoCorrect: false
77
58
  VersionAdded: '2.16'
@@ -135,7 +116,9 @@ Rails/ActiveRecordOverride:
135
116
  Check for overriding Active Record methods instead of using
136
117
  callbacks.
137
118
  Enabled: true
119
+ Severity: warning
138
120
  VersionAdded: '0.67'
121
+ VersionChanged: '2.18'
139
122
  Include:
140
123
  - app/models/**/*.rb
141
124
 
@@ -226,6 +209,9 @@ Rails/BelongsTo:
226
209
  Description: >-
227
210
  Use `optional: true` instead of `required: false` for
228
211
  `belongs_to` relations.
212
+ Reference:
213
+ - https://guides.rubyonrails.org/5_0_release_notes.html
214
+ - https://github.com/rails/rails/pull/18937
229
215
  Enabled: true
230
216
  VersionAdded: '0.62'
231
217
 
@@ -244,6 +230,9 @@ Rails/Blank:
244
230
 
245
231
  Rails/BulkChangeTable:
246
232
  Description: 'Check whether alter queries are combinable.'
233
+ Reference:
234
+ - https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_table
235
+ - https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html
247
236
  Enabled: true
248
237
  VersionAdded: '0.57'
249
238
  Database: null
@@ -330,9 +319,10 @@ Rails/DelegateAllowBlank:
330
319
  Rails/DeprecatedActiveModelErrorsMethods:
331
320
  Description: 'Avoid manipulating ActiveModel errors hash directly.'
332
321
  Enabled: pending
322
+ Severity: warning
333
323
  Safe: false
334
324
  VersionAdded: '2.14'
335
- VersionChanged: '2.15'
325
+ VersionChanged: '2.18'
336
326
 
337
327
  Rails/DotSeparatedKeys:
338
328
  Description: 'Enforces the use of dot-separated keys instead of `:scope` options in `I18n` translation methods.'
@@ -343,12 +333,16 @@ Rails/DotSeparatedKeys:
343
333
  Rails/DuplicateAssociation:
344
334
  Description: "Don't repeat associations in a model."
345
335
  Enabled: pending
336
+ Severity: warning
346
337
  VersionAdded: '2.14'
338
+ VersionChanged: '2.18'
347
339
 
348
340
  Rails/DuplicateScope:
349
341
  Description: 'Multiple scopes share this same where clause.'
350
342
  Enabled: pending
343
+ Severity: warning
351
344
  VersionAdded: '2.14'
345
+ VersionChanged: '2.18'
352
346
 
353
347
  Rails/DurationArithmetic:
354
348
  Description: 'Do not use duration as arithmetic operand with `Time.current`.'
@@ -463,11 +457,12 @@ Rails/FindById:
463
457
  VersionAdded: '2.7'
464
458
 
465
459
  Rails/FindEach:
466
- Description: 'Prefer all.find_each over all.find.'
460
+ Description: 'Prefer all.find_each over all.each.'
467
461
  StyleGuide: 'https://rails.rubystyle.guide#find-each'
468
462
  Enabled: true
463
+ Safe: false
469
464
  VersionAdded: '0.30'
470
- VersionChanged: '2.9'
465
+ VersionChanged: '2.19'
471
466
  Include:
472
467
  - app/models/**/*.rb
473
468
  AllowedMethods:
@@ -477,13 +472,6 @@ Rails/FindEach:
477
472
  - select
478
473
  - lock
479
474
  AllowedPatterns: []
480
- # Deprecated.
481
- IgnoredMethods:
482
- # Methods that don't work well with `find_each`.
483
- - order
484
- - limit
485
- - select
486
- - lock
487
475
 
488
476
  Rails/FreezeTime:
489
477
  Description: 'Prefer `freeze_time` over `travel_to` with an argument of the current time.'
@@ -592,6 +580,9 @@ Rails/Inquiry:
592
580
 
593
581
  Rails/InverseOf:
594
582
  Description: 'Checks for associations where the inverse cannot be determined automatically.'
583
+ Reference:
584
+ - https://guides.rubyonrails.org/association_basics.html#bi-directional-associations
585
+ - https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Setting+Inverses
595
586
  Enabled: true
596
587
  VersionAdded: '0.52'
597
588
  IgnoreScopes: false
@@ -695,7 +686,9 @@ Rails/Pluck:
695
686
  Description: 'Prefer `pluck` over `map { ... }`.'
696
687
  StyleGuide: 'https://rails.rubystyle.guide#pluck'
697
688
  Enabled: 'pending'
689
+ Safe: false
698
690
  VersionAdded: '2.7'
691
+ VersionChanged: '2.18'
699
692
 
700
693
  Rails/PluckId:
701
694
  Description: 'Use `ids` instead of `pluck(:id)` or `pluck(primary_key)`.'
@@ -847,6 +840,18 @@ Rails/RequireDependency:
847
840
  Enabled: false
848
841
  VersionAdded: '2.10'
849
842
 
843
+ Rails/ResponseParsedBody:
844
+ Description: Prefer `response.parsed_body` to `JSON.parse(response.body)`.
845
+ Enabled: pending
846
+ Safe: false
847
+ VersionAdded: '2.18'
848
+ VersionChanged: '2.19'
849
+ Include:
850
+ - spec/controllers/**/*.rb
851
+ - spec/requests/**/*.rb
852
+ - test/controllers/**/*.rb
853
+ - test/integration/**/*.rb
854
+
850
855
  Rails/ReversibleMigration:
851
856
  Description: 'Checks whether the change method of the migration file is reversible.'
852
857
  StyleGuide: 'https://rails.rubystyle.guide#reversible-migration'
@@ -992,6 +997,14 @@ Rails/TableNameAssignment:
992
997
  Include:
993
998
  - app/models/**/*.rb
994
999
 
1000
+ Rails/ThreeStateBooleanColumn:
1001
+ Description: 'Add a default value and a `NOT NULL` constraint to boolean columns.'
1002
+ StyleGuide: 'https://rails.rubystyle.guide/#three-state-boolean'
1003
+ Enabled: pending
1004
+ VersionAdded: '2.19'
1005
+ Include:
1006
+ - db/**/*.rb
1007
+
995
1008
  Rails/TimeZone:
996
1009
  Description: 'Checks the correct usage of time zone aware methods.'
997
1010
  StyleGuide: 'https://rails.rubystyle.guide#time'
@@ -1038,10 +1051,14 @@ Rails/TopLevelHashWithIndifferentAccess:
1038
1051
  Description: 'Identifies top-level `HashWithIndifferentAccess`.'
1039
1052
  Reference: 'https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#top-level-hashwithindifferentaccess-is-soft-deprecated'
1040
1053
  Enabled: pending
1054
+ Severity: warning
1041
1055
  VersionAdded: '2.16'
1056
+ VersionChanged: '2.18'
1042
1057
 
1043
1058
  Rails/TransactionExitStatement:
1044
1059
  Description: 'Avoid the usage of `return`, `break` and `throw` in transaction blocks.'
1060
+ Reference:
1061
+ - https://github.com/rails/rails/commit/15aa4200e083
1045
1062
  Enabled: pending
1046
1063
  VersionAdded: '2.14'
1047
1064
 
@@ -1066,7 +1083,9 @@ Rails/UniqueValidationWithoutIndex:
1066
1083
  Rails/UnknownEnv:
1067
1084
  Description: 'Use correct environment name.'
1068
1085
  Enabled: true
1086
+ Severity: warning
1069
1087
  VersionAdded: '0.51'
1088
+ VersionChanged: '2.18'
1070
1089
  Environments:
1071
1090
  - development
1072
1091
  - test
@@ -1121,12 +1140,18 @@ Rails/WhereNot:
1121
1140
  Rails/WhereNotWithMultipleConditions:
1122
1141
  Description: 'Do not use `where.not(...)` with multiple conditions.'
1123
1142
  Enabled: 'pending'
1143
+ Severity: warning
1124
1144
  VersionAdded: '2.17'
1145
+ VersionChanged: '2.18'
1125
1146
 
1126
1147
  # Accept `redirect_to(...) and return` and similar cases.
1127
1148
  Style/AndOr:
1128
1149
  EnforcedStyle: conditionals
1129
1150
 
1151
+ Style/FormatStringToken:
1152
+ AllowedMethods:
1153
+ - redirect
1154
+
1130
1155
  Style/SymbolProc:
1131
1156
  AllowedMethods:
1132
1157
  - define_method
@@ -32,7 +32,7 @@ module RuboCop
32
32
 
33
33
  def register_offense(offense_node)
34
34
  add_offense(offense_node) do |corrector|
35
- corrector.replace(offense_node.source_range, self.class::SUPERCLASS)
35
+ corrector.replace(offense_node, self.class::SUPERCLASS)
36
36
  end
37
37
  end
38
38
  end
@@ -138,7 +138,7 @@ module RuboCop
138
138
  end
139
139
 
140
140
  def strip_prefix_and_suffix(node, corrector)
141
- expression = node.loc.expression
141
+ expression = node.source_range
142
142
  corrector.remove_leading(expression, leading)
143
143
  corrector.remove_trailing(expression, trailing)
144
144
  end
@@ -153,11 +153,11 @@ module RuboCop
153
153
  end
154
154
 
155
155
  def set_new_arg_name(transformed_argname, corrector)
156
- corrector.replace(block_node.arguments.loc.expression, "|#{transformed_argname}|")
156
+ corrector.replace(block_node.arguments, "|#{transformed_argname}|")
157
157
  end
158
158
 
159
159
  def set_new_body_expression(transforming_body_expr, corrector)
160
- corrector.replace(block_node.body.loc.expression, transforming_body_expr.loc.expression.source)
160
+ corrector.replace(block_node.body, transforming_body_expr.source)
161
161
  end
162
162
  end
163
163
  end
@@ -67,9 +67,11 @@ module RuboCop
67
67
  private
68
68
 
69
69
  def followed_by_render?(flash_node)
70
- flash_assigment_node = find_ancestor(flash_node, type: :send)
71
- context = flash_assigment_node
70
+ flash_assignment_node = find_ancestor(flash_node, type: :send)
71
+ context = flash_assignment_node
72
72
  if (node = context.each_ancestor(:if, :rescue).first)
73
+ return false if use_redirect_to?(context)
74
+
73
75
  context = node
74
76
  elsif context.right_siblings.empty?
75
77
  return true
@@ -95,6 +97,12 @@ module RuboCop
95
97
  def_node || block_node
96
98
  end
97
99
 
100
+ def use_redirect_to?(context)
101
+ context.right_siblings.compact.any? do |sibling|
102
+ sibling.send_type? && sibling.method?(:redirect_to)
103
+ end
104
+ end
105
+
98
106
  def find_ancestor(node, type:)
99
107
  node.each_ancestor(type).first
100
108
  end
@@ -30,7 +30,7 @@ module RuboCop
30
30
 
31
31
  def_node_matcher :action_controller_test_case?, <<~PATTERN
32
32
  (class
33
- (const {nil? cbase} _)
33
+ (const _ _)
34
34
  (const (const {nil? cbase} :ActionController) :TestCase) _)
35
35
  PATTERN
36
36
 
@@ -92,12 +92,11 @@ module RuboCop
92
92
  end
93
93
 
94
94
  def range_with_comments(node)
95
- ranges = [
96
- node,
97
- *processed_source.ast_with_comments[node]
98
- ].map do |element|
99
- element.location.expression
100
- end
95
+ # rubocop:todo InternalAffairs/LocationExpression
96
+ # Using `RuboCop::Ext::Comment#source_range` requires RuboCop > 1.46,
97
+ # which introduces https://github.com/rubocop/rubocop/pull/11630.
98
+ ranges = [node, *processed_source.ast_with_comments[node]].map { |comment| comment.loc.expression }
99
+ # rubocop:enable InternalAffairs/LocationExpression
101
100
  ranges.reduce do |result, range|
102
101
  add_range(result, range)
103
102
  end
@@ -26,6 +26,8 @@ module RuboCop
26
26
  RESTRICT_ON_SEND = ALIASES.keys.freeze
27
27
 
28
28
  def on_send(node)
29
+ return if node.arguments.empty?
30
+
29
31
  method_name = node.method_name
30
32
  alias_method = ALIASES[method_name]
31
33
 
@@ -104,7 +104,7 @@ module RuboCop
104
104
  end
105
105
 
106
106
  def end_position_for(node)
107
- end_line = buffer.line_for_position(node.loc.expression.end_pos)
107
+ end_line = buffer.line_for_position(node.source_range.end_pos)
108
108
  buffer.line_range(end_line).end_pos
109
109
  end
110
110
 
@@ -112,8 +112,7 @@ module RuboCop
112
112
  annotation_line = node.first_line - 1
113
113
  first_comment = nil
114
114
 
115
- processed_source.comments_before_line(annotation_line)
116
- .reverse_each do |comment|
115
+ processed_source.each_comment_in_lines(0..annotation_line).reverse_each do |comment|
117
116
  if comment.location.line == annotation_line && !inline_comment?(comment)
118
117
  first_comment = comment
119
118
  annotation_line -= 1
@@ -124,7 +123,11 @@ module RuboCop
124
123
  end
125
124
 
126
125
  def inline_comment?(comment)
126
+ # rubocop:todo InternalAffairs/LocationExpression
127
+ # Using `RuboCop::Ext::Comment#source_range` requires RuboCop > 1.46,
128
+ # which introduces https://github.com/rubocop/rubocop/pull/11630.
127
129
  !comment_line?(comment.loc.expression.source_line)
130
+ # rubocop:enable InternalAffairs/LocationExpression
128
131
  end
129
132
 
130
133
  def start_line_position(node)
@@ -42,7 +42,7 @@ module RuboCop
42
42
  add_index_opts = ''
43
43
 
44
44
  if value.hash_type?
45
- hash = value.loc.expression.adjust(begin_pos: 1, end_pos: -1).source.strip
45
+ hash = value.source_range.adjust(begin_pos: 1, end_pos: -1).source.strip
46
46
  add_index_opts = ", #{hash}"
47
47
  end
48
48
 
@@ -53,7 +53,7 @@ module RuboCop
53
53
  private
54
54
 
55
55
  def index_range(pair_node)
56
- range_with_surrounding_comma(range_with_surrounding_space(pair_node.loc.expression, side: :left), :left)
56
+ range_with_surrounding_comma(range_with_surrounding_space(pair_node.source_range, side: :left), :left)
57
57
  end
58
58
  end
59
59
  end
@@ -36,7 +36,7 @@ module RuboCop
36
36
 
37
37
  def autocorrect(node)
38
38
  lambda do |corrector|
39
- corrector.replace(node.source_range, self.class::SUPERCLASS)
39
+ corrector.replace(node, self.class::SUPERCLASS)
40
40
  end
41
41
  end
42
42
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  return unless (star = star_bracket?(node))
39
39
 
40
40
  add_offense(star) do |corrector|
41
- corrector.replace(star.loc.expression, 'Arel.star')
41
+ corrector.replace(star, 'Arel.star')
42
42
  end
43
43
  end
44
44
  end
@@ -25,7 +25,7 @@ module RuboCop
25
25
  return unless offensive?(node)
26
26
 
27
27
  add_offense(node) do |corrector|
28
- expression = node.loc.expression
28
+ expression = node.source_range
29
29
 
30
30
  corrector.replace(expression, corrected_source(expression.source))
31
31
  end
@@ -47,9 +47,6 @@ module RuboCop
47
47
  # class Post < ApplicationRecord
48
48
  # belongs_to :blog, optional: false
49
49
  # end
50
- #
51
- # @see https://guides.rubyonrails.org/5_0_release_notes.html
52
- # @see https://github.com/rails/rails/pull/18937
53
50
  class BelongsTo < Base
54
51
  extend AutoCorrector
55
52
  extend TargetRailsVersion
@@ -83,7 +80,7 @@ module RuboCop
83
80
  end
84
81
 
85
82
  add_offense(node.loc.selector, message: message) do |corrector|
86
- corrector.replace(option_node.loc.expression, replacement)
83
+ corrector.replace(option_node, replacement)
87
84
  end
88
85
  end
89
86
  end
@@ -8,7 +8,7 @@ module RuboCop
8
8
  #
9
9
  # Interaction with `Style/UnlessElse`:
10
10
  # The configuration of `NotPresent` will not produce an offense in the
11
- # context of `unless else` if `Style/UnlessElse` is inabled. This is
11
+ # context of `unless else` if `Style/UnlessElse` is enabled. This is
12
12
  # to prevent interference between the autocorrection of the two cops.
13
13
  #
14
14
  # @safety
@@ -142,10 +142,10 @@ module RuboCop
142
142
 
143
143
  if method_call
144
144
  corrector.replace(node.loc.keyword, 'if')
145
- range = method_call.loc.expression
145
+ range = method_call.source_range
146
146
  else
147
147
  variable1, _variable2 = nil_or_empty?(node) || not_present?(node)
148
- range = node.loc.expression
148
+ range = node.source_range
149
149
  end
150
150
 
151
151
  corrector.replace(range, replacement(variable1))
@@ -153,9 +153,9 @@ module RuboCop
153
153
 
154
154
  def unless_condition(node, method_call)
155
155
  if node.modifier_form?
156
- node.loc.keyword.join(node.loc.expression.end)
156
+ node.loc.keyword.join(node.source_range.end)
157
157
  else
158
- node.loc.expression.begin.join(method_call.loc.expression)
158
+ node.source_range.begin.join(method_call.source_range)
159
159
  end
160
160
  end
161
161
 
@@ -62,9 +62,6 @@ module RuboCop
62
62
  # t.string :nickname
63
63
  # end
64
64
  # end
65
- #
66
- # @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_table
67
- # @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html
68
65
  class BulkChangeTable < Base
69
66
  MSG_FOR_CHANGE_TABLE = <<~MSG.chomp
70
67
  You can combine alter queries using `bulk: true` options.
@@ -216,7 +213,7 @@ module RuboCop
216
213
  true
217
214
  when POSTGRESQL
218
215
  # Add bulk alter support for PostgreSQL in 5.2.0
219
- # @see https://github.com/rails/rails/pull/31331
216
+ # See: https://github.com/rails/rails/pull/31331
220
217
  target_rails_version >= 5.2
221
218
  else
222
219
  false
@@ -94,9 +94,9 @@ module RuboCop
94
94
 
95
95
  def offense_range(node)
96
96
  end_pos = if node.parent&.block_type? && node.parent&.send_node == node
97
- node.parent.loc.expression.end_pos
97
+ node.parent.source_range.end_pos
98
98
  else
99
- node.loc.expression.end_pos
99
+ node.source_range.end_pos
100
100
  end
101
101
 
102
102
  range_between(node.loc.selector.begin_pos, end_pos)
@@ -85,7 +85,7 @@ module RuboCop
85
85
  end
86
86
 
87
87
  def correction_range(node)
88
- range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
88
+ range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
89
89
  end
90
90
  end
91
91
  end
@@ -3,10 +3,12 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # Checks the migration for which timestamps are not included
7
- # when creating a new table.
6
+ # Checks the migration for which timestamps are not included when creating a new table.
8
7
  # In many cases, timestamps are useful information and should be added.
9
8
  #
9
+ # NOTE: Allow `timestamps` not written when `id: false` because this emphasizes respecting
10
+ # user's editing intentions.
11
+ #
10
12
  # @example
11
13
  # # bad
12
14
  # create_table :users
@@ -40,12 +42,23 @@ module RuboCop
40
42
  #
41
43
  # t.datetime :updated_at, default: -> { 'CURRENT_TIMESTAMP' }
42
44
  # end
45
+ #
46
+ # # good
47
+ # create_table :users, articles, id: false do |t|
48
+ # t.integer :user_id
49
+ # t.integer :article_id
50
+ # end
51
+ #
43
52
  class CreateTableWithTimestamps < Base
44
53
  include ActiveRecordMigrationsHelper
45
54
 
46
55
  MSG = 'Add timestamps when creating a new table.'
47
56
  RESTRICT_ON_SEND = %i[create_table].freeze
48
57
 
58
+ def_node_search :use_id_false_option?, <<~PATTERN
59
+ (pair (sym :id) (false))
60
+ PATTERN
61
+
49
62
  def_node_matcher :create_table_with_timestamps_proc?, <<~PATTERN
50
63
  (send nil? :create_table (sym _) ... (block-pass (sym :timestamps)))
51
64
  PATTERN
@@ -61,7 +74,7 @@ module RuboCop
61
74
  PATTERN
62
75
 
63
76
  def on_send(node)
64
- return unless node.command?(:create_table)
77
+ return if !node.command?(:create_table) || use_id_false_option?(node)
65
78
 
66
79
  parent = node.parent
67
80
 
@@ -24,6 +24,14 @@ module RuboCop
24
24
  # # good
25
25
  # delegate :bar, to: :foo
26
26
  #
27
+ # # bad
28
+ # def bar
29
+ # self.bar
30
+ # end
31
+ #
32
+ # # good
33
+ # delegate :bar, to: :self
34
+ #
27
35
  # # good
28
36
  # def bar
29
37
  # foo&.bar
@@ -60,7 +68,7 @@ module RuboCop
60
68
 
61
69
  def_node_matcher :delegate?, <<~PATTERN
62
70
  (def _method_name _args
63
- (send (send nil? _) _ ...))
71
+ (send {(send nil? _) (self)} _ ...))
64
72
  PATTERN
65
73
 
66
74
  def on_def(node)
@@ -74,10 +82,14 @@ module RuboCop
74
82
 
75
83
  def register_offense(node)
76
84
  add_offense(node.loc.keyword) do |corrector|
77
- delegation = ["delegate :#{node.body.method_name}", "to: :#{node.body.receiver.method_name}"]
85
+ body = node.body
86
+
87
+ receiver = body.receiver.self_type? ? 'self' : ":#{body.receiver.method_name}"
88
+
89
+ delegation = ["delegate :#{body.method_name}", "to: #{receiver}"]
78
90
  delegation << ['prefix: true'] if node.method?(prefixed_method_name(node.body))
79
91
 
80
- corrector.replace(node.source_range, delegation.join(', '))
92
+ corrector.replace(node, delegation.join(', '))
81
93
  end
82
94
  end
83
95
 
@@ -106,6 +118,8 @@ module RuboCop
106
118
  end
107
119
 
108
120
  def prefixed_method_name(body)
121
+ return '' if body.receiver.self_type?
122
+
109
123
  [body.receiver.method_name, body.method_name].join('_').to_sym
110
124
  end
111
125
 
@@ -27,7 +27,7 @@ module RuboCop
27
27
  return unless (offending_node = allow_blank_option(node))
28
28
 
29
29
  add_offense(offending_node) do |corrector|
30
- corrector.replace(offending_node.key.source_range, 'allow_nil')
30
+ corrector.replace(offending_node.key, 'allow_nil')
31
31
  end
32
32
  end
33
33
  end
@@ -160,7 +160,7 @@ module RuboCop
160
160
  end
161
161
 
162
162
  def model_file?
163
- processed_source.buffer.name.include?('/models/')
163
+ processed_source.file_path.include?('/models/')
164
164
  end
165
165
  end
166
166
  end