rubocop-rails 2.26.0 → 2.26.1

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: f5544e111596dd242dd77d44afe7e6999d1c339d3d6f6a03187a148495c836d1
4
+ data.tar.gz: 7da9bdcc53ee453774708dadfd1df0d1913736f78288781fa50ebfd465cc4042
5
5
  SHA512:
6
- metadata.gz: 7be18be7c11552f4a8d460e4833b57881ec3244d3abc7509ef3f9aa78533e3ff77e0ac60cd1737c608c8a724aed4b37fd1faed06ccb689aabf1e0a21970beef6
7
- data.tar.gz: fb09716c5cb563cff4eaa33e68f0657424dce2650a9c7da8e37e6876ee8ee12a942ef2b096cb5604ee5a457a7f951dbd2198d290d0f5e0486c04b36e5a2679bc
6
+ metadata.gz: b9677c9549dc593121b1832432afdc18cc1b6f47435d4b4a39f4bc2e73ebb660252cd747dd0deaa0fe293d9e4c165467aad80115b1306e007d33d1c2c151ecbb
7
+ data.tar.gz: 1508e3913f537195067ff8129845729251043ad7bedea2c19879c24882036f2d15b31c0af6178d91dd64d21f5d7468460e9e3fbacb2ce4e499be76a03efb59dd
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.'
@@ -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
@@ -24,7 +24,10 @@ module RuboCop
24
24
  MSG = 'Enum defined with keyword arguments in `%<enum>s` enum declaration. Use positional arguments instead.'
25
25
  MSG_OPTIONS = 'Enum defined with deprecated options in `%<enum>s` enum declaration. Remove the `_` prefix.'
26
26
  RESTRICT_ON_SEND = %i[enum].freeze
27
- OPTION_NAMES = %w[prefix suffix scopes default].freeze
27
+
28
+ # From https://github.com/rails/rails/blob/v7.2.1/activerecord/lib/active_record/enum.rb#L231
29
+ OPTION_NAMES = %w[prefix suffix scopes default instance_methods].freeze
30
+ UNDERSCORED_OPTION_NAMES = OPTION_NAMES.map { |option| "_#{option}" }.freeze
28
31
 
29
32
  def_node_matcher :enum?, <<~PATTERN
30
33
  (send nil? :enum (hash $...))
@@ -34,14 +37,6 @@ module RuboCop
34
37
  (send nil? :enum $_ ${array hash} $_)
35
38
  PATTERN
36
39
 
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
40
  def on_send(node)
46
41
  check_and_correct_keyword_args(node)
47
42
  check_enum_options(node)
@@ -52,10 +47,9 @@ module RuboCop
52
47
  def check_and_correct_keyword_args(node)
53
48
  enum?(node) do |pairs|
54
49
  pairs.each do |pair|
55
- key, values = enum_values(pair)
56
- next unless key
50
+ next if option_key?(pair)
57
51
 
58
- correct_keyword_args(node, key, values, pairs[1..])
52
+ correct_keyword_args(node, pair.key, pair.value, pairs[1..])
59
53
  end
60
54
  end
61
55
  end
@@ -63,9 +57,11 @@ module RuboCop
63
57
  def check_enum_options(node)
64
58
  enum_with_options?(node) do |key, _, options|
65
59
  options.children.each do |option|
66
- name, = enum_options(option)
60
+ next unless option_key?(option)
67
61
 
68
- add_offense(name, message: format(MSG_OPTIONS, enum: enum_name_value(key))) if name.source[0] == '_'
62
+ add_offense(option.key, message: format(MSG_OPTIONS, enum: enum_name_value(key))) do |corrector|
63
+ corrector.replace(option.key, option.key.source.delete_prefix('_'))
64
+ end
69
65
  end
70
66
  end
71
67
  end
@@ -107,6 +103,10 @@ module RuboCop
107
103
  end
108
104
  end
109
105
 
106
+ def option_key?(pair)
107
+ UNDERSCORED_OPTION_NAMES.include?(pair.key.source)
108
+ end
109
+
110
110
  def correct_options(options)
111
111
  corrected_options = options.map do |pair|
112
112
  name = if pair.key.source[0] == '_'
@@ -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.
@@ -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.1'
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.1
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-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport