rubocop-rails 2.26.0 → 2.26.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c18cefb961b3100a871e2411c7a029244eda6998d793e3ece62aef1adcd4c571
4
- data.tar.gz: 24cde448ef91e689e05a2357c1aa0cbcc4d61630f1bb6e7fcaf8999ff7678105
3
+ metadata.gz: c6d480f2ef30bba709b183d1a3b71dee6c0838ea2815975b5fd25ab5933277ad
4
+ data.tar.gz: 1d36f2779e44e1fa03437154f388bf2e865c3af810e0238f5ddc6580f024fd04
5
5
  SHA512:
6
- metadata.gz: 7be18be7c11552f4a8d460e4833b57881ec3244d3abc7509ef3f9aa78533e3ff77e0ac60cd1737c608c8a724aed4b37fd1faed06ccb689aabf1e0a21970beef6
7
- data.tar.gz: fb09716c5cb563cff4eaa33e68f0657424dce2650a9c7da8e37e6876ee8ee12a942ef2b096cb5604ee5a457a7f951dbd2198d290d0f5e0486c04b36e5a2679bc
6
+ metadata.gz: 5cc2521c185293872667eb2a23aaaadc4ddee8289b1f16c9fff1e6c87b9498708226a0147697acb9bcc3be69642c3f3ecc063b749056c0df1cdb065a224e4a3d
7
+ data.tar.gz: ea268a009b987a6f61008602d67a8e89659611b7469fe6c1b9c81825b5f9227391035866b2ec593e6109e91a470d607f6f4bb48fd11155b9177d59daf2301111
data/README.md CHANGED
@@ -65,7 +65,7 @@ end
65
65
 
66
66
  ## Rails configuration tip
67
67
 
68
- If you are using Rails 6.1 or newer, add the following `config.generators.after_generate` setting to
68
+ In Rails 6.1+, add the following `config.generators.after_generate` setting to
69
69
  your `config/environments/development.rb` to apply RuboCop autocorrection to code generated by `bin/rails g`.
70
70
 
71
71
  ```ruby
@@ -84,6 +84,20 @@ It uses `rubocop -A` to apply `Style/FrozenStringLiteralComment` and other unsaf
84
84
  `rubocop -A` is unsafe autocorrection, but code generated by default is simple and less likely to
85
85
  be incompatible with `rubocop -A`. If you have problems you can replace it with `rubocop -a` instead.
86
86
 
87
+ In Rails 7.2+, it is recommended to use `config.generators.apply_rubocop_autocorrect_after_generate!` instead of the above setting:
88
+
89
+ ```diff
90
+ # config/environments/development.rb
91
+ Rails.application.configure do
92
+ (snip)
93
+ # Apply autocorrection by RuboCop to files generated by `bin/rails generate`.
94
+ - # config.generators.apply_rubocop_autocorrect_after_generate!
95
+ + config.generators.apply_rubocop_autocorrect_after_generate!
96
+ end
97
+ ```
98
+
99
+ You only need to uncomment.
100
+
87
101
  ## The Cops
88
102
 
89
103
  All cops are located under
data/config/default.yml CHANGED
@@ -212,7 +212,9 @@ Rails/ApplicationRecord:
212
212
  Enabled: true
213
213
  SafeAutoCorrect: false
214
214
  VersionAdded: '0.49'
215
- VersionChanged: '2.5'
215
+ VersionChanged: '2.26'
216
+ Exclude:
217
+ - db/**/*.rb
216
218
 
217
219
  Rails/ArelStar:
218
220
  Description: 'Enforces `Arel.star` instead of `"*"` for expanded columns.'
@@ -72,13 +72,13 @@ module RuboCop
72
72
  if (node = context.each_ancestor(:if, :rescue).first)
73
73
  return false if use_redirect_to?(context)
74
74
 
75
- context = node.rescue_type? ? node.parent : node
75
+ context = node
76
+ elsif context.right_siblings.empty?
77
+ return true
76
78
  end
79
+ context = context.right_siblings
77
80
 
78
- siblings = context.right_siblings
79
- return true if siblings.empty?
80
-
81
- siblings.compact.any? do |render_candidate|
81
+ context.compact.any? do |render_candidate|
82
82
  render?(render_candidate)
83
83
  end
84
84
  end
@@ -92,11 +92,7 @@ module RuboCop
92
92
  end
93
93
 
94
94
  def range_with_comments(node)
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
95
+ ranges = [node, *processed_source.ast_with_comments[node]].map(&:source_range)
100
96
  ranges.reduce do |result, range|
101
97
  add_range(result, range)
102
98
  end
@@ -123,11 +123,7 @@ module RuboCop
123
123
  end
124
124
 
125
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.
129
- !comment_line?(comment.loc.expression.source_line)
130
- # rubocop:enable InternalAffairs/LocationExpression
126
+ !comment_line?(comment.source_range.source_line)
131
127
  end
132
128
 
133
129
  def start_line_position(node)
@@ -5,6 +5,10 @@ module RuboCop
5
5
  module Rails
6
6
  # Checks that models subclass `ApplicationRecord` with Rails 5.0.
7
7
  #
8
+ # It is a common practice to define models inside migrations in order to retain forward
9
+ # compatibility by avoiding loading any application code. And so migration files are excluded
10
+ # by default for this cop.
11
+ #
8
12
  # @safety
9
13
  # This cop's autocorrection is unsafe because it may let the logic from `ApplicationRecord`
10
14
  # sneak into an Active Record model that is not purposed to inherit logic common among other
@@ -25,6 +25,8 @@ module RuboCop
25
25
  # collection.reject { |_k, v| v.blank? }
26
26
  # collection.select(&:present?)
27
27
  # collection.select { |_k, v| v.present? }
28
+ # collection.filter(&:present?)
29
+ # collection.filter { |_k, v| v.present? }
28
30
  #
29
31
  # # good
30
32
  # collection.compact_blank
@@ -44,7 +46,8 @@ module RuboCop
44
46
  extend TargetRailsVersion
45
47
 
46
48
  MSG = 'Use `%<preferred_method>s` instead.'
47
- RESTRICT_ON_SEND = %i[reject delete_if select keep_if].freeze
49
+ RESTRICT_ON_SEND = %i[reject delete_if select filter keep_if].freeze
50
+ DESTRUCTIVE_METHODS = %i[delete_if keep_if].freeze
48
51
 
49
52
  minimum_target_rails_version 6.1
50
53
 
@@ -64,19 +67,20 @@ module RuboCop
64
67
 
65
68
  def_node_matcher :select_with_block?, <<~PATTERN
66
69
  (block
67
- (send _ {:select :keep_if})
70
+ (send _ {:select :filter :keep_if})
68
71
  $(args ...)
69
72
  (send
70
73
  $(lvar _) :present?))
71
74
  PATTERN
72
75
 
73
76
  def_node_matcher :select_with_block_pass?, <<~PATTERN
74
- (send _ {:select :keep_if}
77
+ (send _ {:select :filter :keep_if}
75
78
  (block-pass
76
79
  (sym :present?)))
77
80
  PATTERN
78
81
 
79
82
  def on_send(node)
83
+ return if target_ruby_version < 2.6 && node.method?(:filter)
80
84
  return unless bad_method?(node)
81
85
 
82
86
  range = offense_range(node)
@@ -120,7 +124,7 @@ module RuboCop
120
124
  end
121
125
 
122
126
  def preferred_method(node)
123
- node.method?(:reject) || node.method?(:select) ? 'compact_blank' : 'compact_blank!'
127
+ DESTRUCTIVE_METHODS.include?(node.method_name) ? 'compact_blank!' : 'compact_blank'
124
128
  end
125
129
  end
126
130
  end
@@ -17,14 +17,19 @@ module RuboCop
17
17
  #
18
18
  class EnumSyntax < Base
19
19
  extend AutoCorrector
20
+ extend TargetRubyVersion
20
21
  extend TargetRailsVersion
21
22
 
23
+ minimum_target_ruby_version 3.0
22
24
  minimum_target_rails_version 7.0
23
25
 
24
26
  MSG = 'Enum defined with keyword arguments in `%<enum>s` enum declaration. Use positional arguments instead.'
25
27
  MSG_OPTIONS = 'Enum defined with deprecated options in `%<enum>s` enum declaration. Remove the `_` prefix.'
26
28
  RESTRICT_ON_SEND = %i[enum].freeze
27
- OPTION_NAMES = %w[prefix suffix scopes default].freeze
29
+
30
+ # From https://github.com/rails/rails/blob/v7.2.1/activerecord/lib/active_record/enum.rb#L231
31
+ OPTION_NAMES = %w[prefix suffix scopes default instance_methods].freeze
32
+ UNDERSCORED_OPTION_NAMES = OPTION_NAMES.map { |option| "_#{option}" }.freeze
28
33
 
29
34
  def_node_matcher :enum?, <<~PATTERN
30
35
  (send nil? :enum (hash $...))
@@ -34,14 +39,6 @@ module RuboCop
34
39
  (send nil? :enum $_ ${array hash} $_)
35
40
  PATTERN
36
41
 
37
- def_node_matcher :enum_values, <<~PATTERN
38
- (pair $_ ${array hash})
39
- PATTERN
40
-
41
- def_node_matcher :enum_options, <<~PATTERN
42
- (pair $_ $_)
43
- PATTERN
44
-
45
42
  def on_send(node)
46
43
  check_and_correct_keyword_args(node)
47
44
  check_enum_options(node)
@@ -52,10 +49,9 @@ module RuboCop
52
49
  def check_and_correct_keyword_args(node)
53
50
  enum?(node) do |pairs|
54
51
  pairs.each do |pair|
55
- key, values = enum_values(pair)
56
- next unless key
52
+ next if option_key?(pair)
57
53
 
58
- correct_keyword_args(node, key, values, pairs[1..])
54
+ correct_keyword_args(node, pair.key, pair.value, pairs[1..])
59
55
  end
60
56
  end
61
57
  end
@@ -63,9 +59,11 @@ module RuboCop
63
59
  def check_enum_options(node)
64
60
  enum_with_options?(node) do |key, _, options|
65
61
  options.children.each do |option|
66
- name, = enum_options(option)
62
+ next unless option_key?(option)
67
63
 
68
- add_offense(name, message: format(MSG_OPTIONS, enum: enum_name_value(key))) if name.source[0] == '_'
64
+ add_offense(option.key, message: format(MSG_OPTIONS, enum: enum_name_value(key))) do |corrector|
65
+ corrector.replace(option.key, option.key.source.delete_prefix('_'))
66
+ end
69
67
  end
70
68
  end
71
69
  end
@@ -107,6 +105,10 @@ module RuboCop
107
105
  end
108
106
  end
109
107
 
108
+ def option_key?(pair)
109
+ UNDERSCORED_OPTION_NAMES.include?(pair.key.source)
110
+ end
111
+
110
112
  def correct_options(options)
111
113
  corrected_options = options.map do |pair|
112
114
  name = if pair.key.source[0] == '_'
@@ -97,7 +97,7 @@ module RuboCop
97
97
  return unless node.arguments.any? { |e| rails_root_nodes?(e) }
98
98
 
99
99
  register_offense(node, require_to_s: true) do |corrector|
100
- autocorrect_file_join(corrector, node)
100
+ autocorrect_file_join(corrector, node) unless node.first_argument.array_type?
101
101
  end
102
102
  end
103
103
 
@@ -11,7 +11,7 @@ module RuboCop
11
11
  # 3.day.ago
12
12
  # 1.months.ago
13
13
  # 5.megabyte
14
- # 1.gigabyte
14
+ # 1.gigabytes
15
15
  #
16
16
  # # good
17
17
  # 3.days.ago
@@ -98,8 +98,6 @@ module RuboCop
98
98
  end
99
99
 
100
100
  def on_or(node)
101
- return unless cop_config['NilOrEmpty']
102
-
103
101
  exists_and_not_empty?(node) do |var1, var2|
104
102
  return unless var1 == var2
105
103
 
@@ -3,35 +3,6 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Rails
6
- # TODO: In the future, please support only RuboCop 1.52+ and use `RuboCop::Cop::AllowedReceivers`:
7
- # https://github.com/rubocop/rubocop/blob/v1.52.0/lib/rubocop/cop/mixin/allowed_receivers.rb
8
- # At that time, this duplicated module implementation can be removed.
9
- module AllowedReceivers
10
- def allowed_receiver?(receiver)
11
- receiver_name = receiver_name(receiver)
12
-
13
- allowed_receivers.include?(receiver_name)
14
- end
15
-
16
- def receiver_name(receiver)
17
- return receiver_name(receiver.receiver) if receiver.receiver && !receiver.receiver.const_type?
18
-
19
- if receiver.send_type?
20
- if receiver.receiver
21
- "#{receiver_name(receiver.receiver)}.#{receiver.method_name}"
22
- else
23
- receiver.method_name.to_s
24
- end
25
- else
26
- receiver.source
27
- end
28
- end
29
-
30
- def allowed_receivers
31
- cop_config.fetch('AllowedReceivers', [])
32
- end
33
- end
34
-
35
6
  # Detect redundant `all` used as a receiver for Active Record query methods.
36
7
  #
37
8
  # For the methods `delete_all` and `destroy_all`, this cop will only check cases where the receiver is a model.
@@ -40,7 +40,7 @@ module RuboCop
40
40
  def on_send(node)
41
41
  association_with_foreign_key(node) do |type, name, options, foreign_key_pair, foreign_key|
42
42
  if redundant?(node, type, name, options, foreign_key)
43
- add_offense(foreign_key_pair.source_range) do |corrector|
43
+ add_offense(foreign_key_pair) do |corrector|
44
44
  range = range_with_surrounding_space(foreign_key_pair.source_range, side: :left)
45
45
  range = range_with_surrounding_comma(range, :left)
46
46
 
@@ -78,7 +78,7 @@ module RuboCop
78
78
 
79
79
  send_nodes.each do |send_node|
80
80
  receiver = send_node.receiver
81
- add_offense(receiver.source_range) do |corrector|
81
+ add_offense(receiver) do |corrector|
82
82
  autocorrect(corrector, send_node, node)
83
83
  end
84
84
  end
@@ -43,7 +43,7 @@ module RuboCop
43
43
  return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
44
44
  return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)
45
45
 
46
- add_offense(reflection_class_name.source_range) do |corrector|
46
+ add_offense(reflection_class_name) do |corrector|
47
47
  autocorrect(corrector, reflection_class_name)
48
48
  end
49
49
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  referer?(node) do
35
35
  return unless node.method?(wrong_method_name)
36
36
 
37
- add_offense(node.source_range) do |corrector|
37
+ add_offense(node) do |corrector|
38
38
  corrector.replace(node, "request.#{style}")
39
39
  end
40
40
  end
@@ -23,6 +23,8 @@ module RuboCop
23
23
  # File.binread(Rails.root.join('db', 'schema.rb'))
24
24
  # File.write(Rails.root.join('db', 'schema.rb'), content)
25
25
  # File.binwrite(Rails.root.join('db', 'schema.rb'), content)
26
+ # Dir.glob(Rails.root.join('db', 'schema.rb'))
27
+ # Dir[Rails.root.join('db', 'schema.rb')]
26
28
  #
27
29
  # # good
28
30
  # Rails.root.join('db', 'schema.rb').open
@@ -31,12 +33,13 @@ module RuboCop
31
33
  # Rails.root.join('db', 'schema.rb').binread
32
34
  # Rails.root.join('db', 'schema.rb').write(content)
33
35
  # Rails.root.join('db', 'schema.rb').binwrite(content)
36
+ # Rails.root.glob("db/schema.rb")
34
37
  #
35
38
  class RootPathnameMethods < Base # rubocop:disable Metrics/ClassLength
36
39
  extend AutoCorrector
37
40
  include RangeHelp
38
41
 
39
- MSG = '`%<rails_root>s` is a `Pathname` so you can just append `#%<method>s`.'
42
+ MSG = '`%<rails_root>s` is a `Pathname`, so you can use `%<replacement>s`.'
40
43
 
41
44
  DIR_GLOB_METHODS = %i[[] glob].to_set.freeze
42
45
 
@@ -188,13 +191,14 @@ module RuboCop
188
191
 
189
192
  def on_send(node)
190
193
  evidence(node) do |method, path, args, rails_root|
191
- add_offense(node, message: format(MSG, method: method, rails_root: rails_root.source)) do |corrector|
192
- replacement = if dir_glob?(node)
193
- build_path_glob_replacement(path)
194
- else
195
- build_path_replacement(path, method, args)
196
- end
194
+ replacement = if dir_glob?(node)
195
+ build_path_glob_replacement(path)
196
+ else
197
+ build_path_replacement(path, method, args)
198
+ end
197
199
 
200
+ message = format(MSG, rails_root: rails_root.source, replacement: replacement)
201
+ add_offense(node, message: message) do |corrector|
198
202
  corrector.replace(node, replacement)
199
203
  end
200
204
  end
@@ -100,7 +100,8 @@ module RuboCop
100
100
  end
101
101
 
102
102
  def forbidden_methods
103
- obsolete_result = cop_config['Blacklist']
103
+ # TODO: Remove when RuboCop Rails 3 releases.
104
+ obsolete_result = cop_config['Blacklist'] # rubocop:disable InternalAffairs/UndefinedConfig
104
105
  if obsolete_result
105
106
  warn '`Blacklist` has been renamed to `ForbiddenMethods`.' unless @displayed_forbidden_warning
106
107
  @displayed_forbidden_warning = true
@@ -111,7 +112,8 @@ module RuboCop
111
112
  end
112
113
 
113
114
  def allowed_methods
114
- obsolete_result = cop_config['Whitelist']
115
+ # TODO: Remove when RuboCop Rails 3 releases.
116
+ obsolete_result = cop_config['Whitelist'] # rubocop:disable InternalAffairs/UndefinedConfig
115
117
  if obsolete_result
116
118
  warn '`Whitelist` has been renamed to `AllowedMethods`.' unless @displayed_allowed_warning
117
119
  @displayed_allowed_warning = true
@@ -74,6 +74,7 @@ module RuboCop
74
74
  range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
75
75
  end
76
76
 
77
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
77
78
  def extract_column_and_value(template_node, value_node)
78
79
  value =
79
80
  case template_node.value
@@ -90,8 +91,12 @@ module RuboCop
90
91
  return
91
92
  end
92
93
 
93
- [Regexp.last_match(1), value]
94
+ column_qualifier = Regexp.last_match(1)
95
+ return if column_qualifier.count('.') > 1
96
+
97
+ [column_qualifier, value]
94
98
  end
99
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
95
100
 
96
101
  def build_good_method(method_name, column, value)
97
102
  if column.include?('.')
@@ -68,6 +68,7 @@ module RuboCop
68
68
  range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
69
69
  end
70
70
 
71
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
71
72
  def extract_column_and_value(template_node, value_node)
72
73
  value =
73
74
  case template_node.value
@@ -84,8 +85,12 @@ module RuboCop
84
85
  return
85
86
  end
86
87
 
87
- [Regexp.last_match(1), value]
88
+ column_qualifier = Regexp.last_match(1)
89
+ return if column_qualifier.count('.') > 1
90
+
91
+ [column_qualifier, value]
88
92
  end
93
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
89
94
 
90
95
  def build_good_method(dot, column, value)
91
96
  dot ||= '.'
@@ -140,6 +140,8 @@ module RuboCop
140
140
  rhs = pair2.value
141
141
  end
142
142
  end
143
+ else
144
+ return
143
145
  end
144
146
 
145
147
  if lhs
@@ -150,7 +152,10 @@ module RuboCop
150
152
  rhs_source = parentheses_needed?(rhs) ? "(#{rhs.source})" : rhs.source
151
153
  end
152
154
 
153
- [Regexp.last_match(1), "#{lhs_source}#{operator}#{rhs_source}"] if operator
155
+ column_qualifier = Regexp.last_match(1)
156
+ return if column_qualifier.count('.') > 1
157
+
158
+ [column_qualifier, "#{lhs_source}#{operator}#{rhs_source}"] if operator
154
159
  end
155
160
  # rubocop:enable Metrics
156
161
 
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.26.0'
7
+ STRING = '2.26.2'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.26.0
4
+ version: 2.26.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2024-08-24 00:00:00.000000000 Z
13
+ date: 2024-09-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -265,7 +265,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
265
265
  - !ruby/object:Gem::Version
266
266
  version: '0'
267
267
  requirements: []
268
- rubygems_version: 3.5.11
268
+ rubygems_version: 3.5.16
269
269
  signing_key:
270
270
  specification_version: 4
271
271
  summary: Automatic Rails code style checking tool.