rubocop-rails 2.30.1 → 2.30.3

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: b27c710b734d026be33bd2bb2f777e58aa4b1bfbedbcfb487b9a900bcb3689d6
4
- data.tar.gz: d817c2bdda07d206ef7521122ed915859f820fe45c059740c885f7478d75bbfa
3
+ metadata.gz: a45ebdeef3a184afbb85ff1e1685fa978198ee00cf3bbbc0f1cbfa04b56f9dc7
4
+ data.tar.gz: 2d06943201b3836891ed9f8c0bd9a5e2bebd90de7736351add71af9460e271b7
5
5
  SHA512:
6
- metadata.gz: bf49fe022c9885721fe7d5b0466052fbfbff90c3e421cc23d0abdb1f8f45e964e12aed5978202e7a9894c50c82cc4df3ce686a74735df80255628db05136df26
7
- data.tar.gz: 8117361c6ae1e240c8994c482810278e5d157316ed3b01416630b0c3ee868ebac02c74e2c073a8e9d3958bcd04d849d9ae1f8ea90c0cc8289d8a136f3ab5e97d
6
+ metadata.gz: ed82ce027a4bee9e6dfa16dd763fa856d7b19b96356b918d469ef1274ef0c5a4e8ea5e949c6aaed84ee6d0f290ca838e34e352929640294a6bd4d579c0145cdd
7
+ data.tar.gz: 7e11061742bc02b64742e4219ccfea80d5befb27572cb043d7ebc57c7a616f2fd2c8b829ac08dfad6b644f23d8a0f096c8b571cc5b8e9ecf34e7769f1f551fb4
data/README.md CHANGED
@@ -5,7 +5,8 @@
5
5
 
6
6
  A [RuboCop](https://github.com/rubocop/rubocop) extension focused on enforcing Rails best practices and coding conventions.
7
7
 
8
- Note: This repository manages rubocop-rails gem (>= 2.0.0). rubocop-rails gem (<= 1.5.0) has been renamed to [rubocop-rails_config](https://rubygems.org/gems/rubocop-rails_config) gem.
8
+ > [!IMPORTANT]
9
+ > This repository manages rubocop-rails gem (>= 2.0.0). rubocop-rails gem (<= 1.5.0) has been renamed to [rubocop-rails_config](https://rubygems.org/gems/rubocop-rails_config) gem.
9
10
 
10
11
  ## Installation
11
12
 
data/config/default.yml CHANGED
@@ -77,6 +77,20 @@ Lint/SafeNavigationChain:
77
77
  - try!
78
78
  - in?
79
79
 
80
+ Lint/UselessAccessModifier:
81
+ # Add methods from `ActiveSupport::Concern` and `Module::Concerning`:
82
+ # https://api.rubyonrails.org/classes/ActiveSupport/Concern.html
83
+ # https://api.rubyonrails.org/classes/Module/Concerning
84
+ inherit_mode:
85
+ merge:
86
+ - ContextCreatingMethods
87
+ ContextCreatingMethods:
88
+ - class_methods
89
+ - included
90
+ - prepended
91
+ - concern
92
+ - concerning
93
+
80
94
  Rails:
81
95
  Enabled: true
82
96
  DocumentationBaseURL: https://docs.rubocop.org/rubocop-rails
@@ -353,11 +367,13 @@ Rails/Delegate:
353
367
  Description: 'Prefer delegate method for delegations.'
354
368
  Enabled: true
355
369
  VersionAdded: '0.21'
356
- VersionChanged: '0.50'
370
+ VersionChanged: '2.30'
357
371
  # When set to true, using the target object as a prefix of the
358
372
  # method name without using the `delegate` method will be a
359
373
  # violation. When set to false, this case is legal.
360
374
  EnforceForPrefixed: true
375
+ Exclude:
376
+ - app/controllers/**/*.rb
361
377
 
362
378
  Rails/DelegateAllowBlank:
363
379
  Description: 'Do not use allow_blank as an option to delegate.'
@@ -103,7 +103,7 @@ module RuboCop
103
103
  end
104
104
 
105
105
  def in_where?(node)
106
- send_node = node.each_ancestor(:send, :csend).first
106
+ send_node = node.each_ancestor(:call).first
107
107
  return false unless send_node
108
108
 
109
109
  return true if WHERE_METHODS.include?(send_node.method_name)
@@ -22,9 +22,9 @@ module RuboCop
22
22
  ].freeze
23
23
 
24
24
  def_node_matcher :create_table_with_block?, <<~PATTERN
25
- (block
25
+ (any_block
26
26
  (send nil? :create_table ...)
27
- (args (arg _var))
27
+ { _ | (args (arg _var)) }
28
28
  _)
29
29
  PATTERN
30
30
  end
@@ -123,7 +123,7 @@ module RuboCop
123
123
  def on_if(node)
124
124
  return unless cop_config['UnlessPresent']
125
125
  return unless node.unless?
126
- return if node.else? && config.for_cop('Style/UnlessElse')['Enabled']
126
+ return if node.else? && config.cop_enabled?('Style/UnlessElse')
127
127
 
128
128
  unless_present?(node) do |method_call, receiver|
129
129
  range = unless_condition(node, method_call)
@@ -15,6 +15,9 @@ module RuboCop
15
15
  # without using the `delegate` method will be a violation.
16
16
  # When set to `false`, this case is legal.
17
17
  #
18
+ # It is disabled for controllers in order to keep controller actions
19
+ # explicitly defined.
20
+ #
18
21
  # @example
19
22
  # # bad
20
23
  # def bar
@@ -74,6 +77,7 @@ module RuboCop
74
77
  def on_def(node)
75
78
  return unless trivial_delegate?(node)
76
79
  return if private_or_protected_delegation(node)
80
+ return if module_function_declared?(node)
77
81
 
78
82
  register_offense(node)
79
83
  end
@@ -163,6 +167,12 @@ module RuboCop
163
167
  private_or_protected_inline(node) || node_visibility(node) != :public
164
168
  end
165
169
 
170
+ def module_function_declared?(node)
171
+ node.each_ancestor(:module, :begin).any? do |ancestor|
172
+ ancestor.children.any? { |child| child.send_type? && child.method?(:module_function) }
173
+ end
174
+ end
175
+
166
176
  def private_or_protected_inline(node)
167
177
  processed_source[node.first_line - 1].strip.match?(/\A(private )|(protected )/)
168
178
  end
@@ -115,6 +115,10 @@ module RuboCop
115
115
  $_)))
116
116
  PATTERN
117
117
 
118
+ def_node_matcher :delegated_methods, <<~PATTERN
119
+ (send nil? :delegate (sym $_)+ (hash <(pair (sym :to) _) ...>))
120
+ PATTERN
121
+
118
122
  def on_send(node)
119
123
  methods_node = only_or_except_filter_methods(node)
120
124
  return unless methods_node
@@ -139,7 +143,13 @@ module RuboCop
139
143
  return [] unless block
140
144
 
141
145
  defined_methods = block.each_child_node(:def).map(&:method_name)
142
- defined_methods + aliased_action_methods(block, defined_methods)
146
+ defined_methods + delegated_action_methods(block) + aliased_action_methods(block, defined_methods)
147
+ end
148
+
149
+ def delegated_action_methods(node)
150
+ node.each_child_node(:send).flat_map do |child_node|
151
+ delegated_methods(child_node) || []
152
+ end
143
153
  end
144
154
 
145
155
  def aliased_action_methods(node, defined_methods)
@@ -60,7 +60,7 @@ module RuboCop
60
60
  PATTERN
61
61
 
62
62
  def on_block(node)
63
- return if node.each_ancestor(:block, :numblock).any?
63
+ return if node.each_ancestor(:any_block).any?
64
64
 
65
65
  pluck_candidate?(node) do |argument, key|
66
66
  next if key.regexp_type? || !use_one_block_argument?(argument)
@@ -110,7 +110,7 @@ module RuboCop
110
110
  def on_if(node)
111
111
  return unless cop_config['UnlessBlank']
112
112
  return unless node.unless?
113
- return if node.else? && config.for_cop('Style/UnlessElse')['Enabled']
113
+ return if node.else? && config.cop_enabled?('Style/UnlessElse')
114
114
 
115
115
  unless_blank?(node) do |method_call, receiver|
116
116
  range = unless_condition(node, method_call)
@@ -40,7 +40,7 @@ module RuboCop
40
40
 
41
41
  def on_send(node)
42
42
  association_with_reflection(node) do |reflection_class_name|
43
- return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil?
43
+ return if reflection_class_name.value.send_type? && !reflection_class_name.value.receiver&.const_type?
44
44
  return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name)
45
45
 
46
46
  add_offense(reflection_class_name) do |corrector|
@@ -90,7 +90,7 @@ module RuboCop
90
90
  end
91
91
 
92
92
  def nested_relative_date(node, &callback)
93
- return if node.nil? || node.block_type?
93
+ return if node.nil? || node.any_block_type?
94
94
 
95
95
  node.each_child_node do |child|
96
96
  nested_relative_date(child, &callback)
@@ -218,7 +218,7 @@ module RuboCop
218
218
  return unless (last_argument = node.last_argument)
219
219
 
220
220
  drop_table_call(node) do
221
- unless node.parent.block_type? || last_argument.block_pass_type?
221
+ unless node.parent.any_block_type? || last_argument.block_pass_type?
222
222
  add_offense(node, message: format(MSG, action: 'drop_table(without block)'))
223
223
  end
224
224
  end
@@ -182,7 +182,7 @@ module RuboCop
182
182
  def right_assignment_node(assignment)
183
183
  node = assignment.node.child_nodes.first
184
184
 
185
- return node unless node&.block_type?
185
+ return node unless node&.any_block_type?
186
186
 
187
187
  node.send_node
188
188
  end
@@ -305,7 +305,7 @@ module RuboCop
305
305
 
306
306
  node = assignable_node(node)
307
307
  method, sibling_index = find_method_with_sibling_index(node.parent)
308
- return false unless method&.type?(:def, :block)
308
+ return false unless method&.type?(:def, :any_block)
309
309
 
310
310
  method.children.size == node.sibling_index + sibling_index
311
311
  end
@@ -99,7 +99,7 @@ module RuboCop
99
99
  return false unless transaction_method_name?(node.method_name)
100
100
  return false unless (parent = node.parent)
101
101
 
102
- parent.block_type? && parent.body
102
+ parent.any_block_type? && parent.body
103
103
  end
104
104
 
105
105
  def statement(statement_node)
@@ -113,7 +113,7 @@ module RuboCop
113
113
  end
114
114
 
115
115
  def nested_block?(statement_node)
116
- name = statement_node.ancestors.find(&:block_type?).children.first.method_name
116
+ name = statement_node.ancestors.find(&:any_block_type?).children.first.method_name
117
117
  !transaction_method_name?(name)
118
118
  end
119
119
 
@@ -51,51 +51,28 @@ module RuboCop
51
51
 
52
52
  MSG = 'Use `distinct` before `pluck`.'
53
53
  RESTRICT_ON_SEND = %i[uniq].freeze
54
- NEWLINE = "\n"
55
- PATTERN = '[!^block (send (send %<type>s :pluck ...) :uniq ...)]'
56
54
 
57
- def_node_matcher :conservative_node_match, format(PATTERN, type: 'const')
58
-
59
- def_node_matcher :aggressive_node_match, format(PATTERN, type: '_')
55
+ def_node_matcher :uniq_before_pluck, '[!^any_block $(send $(send _ :pluck ...) :uniq ...)]'
60
56
 
61
57
  def on_send(node)
62
- uniq = if style == :conservative
63
- conservative_node_match(node)
64
- else
65
- aggressive_node_match(node)
66
- end
67
-
68
- return unless uniq
58
+ uniq_before_pluck(node) do |uniq_node, pluck_node|
59
+ next if style == :conservative && !pluck_node.receiver&.const_type?
69
60
 
70
- add_offense(node.loc.selector) do |corrector|
71
- autocorrect(corrector, node)
61
+ add_offense(uniq_node.loc.selector) do |corrector|
62
+ autocorrect(corrector, uniq_node, pluck_node)
63
+ end
72
64
  end
73
65
  end
74
66
 
75
67
  private
76
68
 
77
- def autocorrect(corrector, node)
78
- method = node.method_name
69
+ def autocorrect(corrector, uniq_node, pluck_node)
70
+ corrector.remove(range_between(pluck_node.loc.end.end_pos, uniq_node.loc.selector.end_pos))
79
71
 
80
- corrector.remove(dot_method_with_whitespace(method, node))
81
- if (dot = node.receiver.loc.dot)
72
+ if (dot = pluck_node.loc.dot)
82
73
  corrector.insert_before(dot.begin, '.distinct')
83
74
  else
84
- corrector.insert_before(node.receiver, 'distinct.')
85
- end
86
- end
87
-
88
- def dot_method_with_whitespace(method, node)
89
- range_between(dot_method_begin_pos(method, node), node.loc.selector.end_pos)
90
- end
91
-
92
- def dot_method_begin_pos(method, node)
93
- lines = node.source.split(NEWLINE)
94
-
95
- if lines.last.strip == ".#{method}"
96
- node.source.rindex(NEWLINE)
97
- else
98
- node.loc.dot.begin_pos
75
+ corrector.insert_before(pluck_node, 'distinct.')
99
76
  end
100
77
  end
101
78
  end
@@ -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.30.1'
7
+ STRING = '2.30.3'
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.30.1
4
+ version: 2.30.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -9,7 +9,7 @@ authors:
9
9
  - Yuji Nakayama
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-02-17 00:00:00.000000000 Z
12
+ date: 2025-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport