rubocop-rails 2.32.0 → 2.33.0
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 +4 -4
- data/config/default.yml +24 -2
- data/lib/rubocop/cop/mixin/active_record_helper.rb +1 -1
- data/lib/rubocop/cop/mixin/index_method.rb +4 -0
- data/lib/rubocop/cop/rails/delegate.rb +4 -4
- data/lib/rubocop/cop/rails/duplicate_association.rb +1 -1
- data/lib/rubocop/cop/rails/env_local.rb +50 -26
- data/lib/rubocop/cop/rails/file_path.rb +2 -2
- data/lib/rubocop/cop/rails/find_by_or_assignment_memoization.rb +65 -0
- data/lib/rubocop/cop/rails/index_with.rb +5 -0
- data/lib/rubocop/cop/rails/order_arguments.rb +79 -0
- data/lib/rubocop/cop/rails/output.rb +3 -0
- data/lib/rubocop/cop/rails/pluck.rb +6 -3
- data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +3 -3
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -1
- data/lib/rubocop/cop/rails/save_bang.rb +2 -2
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +4 -2
- data/lib/rubocop/cop/rails/where_exists.rb +2 -2
- data/lib/rubocop/cop/rails_cops.rb +2 -0
- data/lib/rubocop/rails/version.rb +1 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a61be89f227c5abed6543c4fb84957391ac66fdbadc7f7891a1876b7be0c193
|
4
|
+
data.tar.gz: f2e30c0c3e5a5cac32bae9ec0900a0e1fc382dda89f764f8a844b085ad2abffb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8610629e8893c71c21d591cc68897f993699f20edab9cda08a18ce65d9bf67ac153830dffa144e18b2d9e4e204027684c18cbb02e822d01c9c40e35312709083
|
7
|
+
data.tar.gz: f7dc418f1cb2f4a38968b9a79ae17028368e10f2d529cba2de15954a2edc5c3f8ebf7bb35617b670cf7e8dab758ccb138c0387a0fd6caa31051ee5e803925c35
|
data/config/default.yml
CHANGED
@@ -91,6 +91,12 @@ Lint/UselessAccessModifier:
|
|
91
91
|
- concern
|
92
92
|
- concerning
|
93
93
|
|
94
|
+
Lint/UselessMethodDefinition:
|
95
|
+
# Avoids conflict with `Rails/LexicallyScopedActionFilter` cop.
|
96
|
+
Exclude:
|
97
|
+
- '**/app/controllers/**/*.rb'
|
98
|
+
- '**/app/mailers/**/*.rb'
|
99
|
+
|
94
100
|
Rails:
|
95
101
|
Enabled: true
|
96
102
|
DocumentationBaseURL: https://docs.rubocop.org/rubocop-rails
|
@@ -453,6 +459,7 @@ Rails/EnumSyntax:
|
|
453
459
|
VersionAdded: '2.26'
|
454
460
|
Include:
|
455
461
|
- '**/app/models/**/*.rb'
|
462
|
+
- '**/lib/**/*.rb'
|
456
463
|
|
457
464
|
Rails/EnumUniqueness:
|
458
465
|
Description: 'Avoid duplicate integers in hash-syntax `enum` declaration.'
|
@@ -532,6 +539,13 @@ Rails/FindById:
|
|
532
539
|
Enabled: 'pending'
|
533
540
|
VersionAdded: '2.7'
|
534
541
|
|
542
|
+
Rails/FindByOrAssignmentMemoization:
|
543
|
+
Description: 'Avoid memoizing `find_by` results with `||=`.'
|
544
|
+
StyleGuide: 'https://rails.rubystyle.guide/#find-by-memoization'
|
545
|
+
Enabled: pending
|
546
|
+
Safe: false
|
547
|
+
VersionAdded: '2.33'
|
548
|
+
|
535
549
|
Rails/FindEach:
|
536
550
|
Description: 'Prefer all.find_each over all.each.'
|
537
551
|
StyleGuide: 'https://rails.rubystyle.guide#find-each'
|
@@ -647,8 +661,9 @@ Rails/IndexBy:
|
|
647
661
|
Rails/IndexWith:
|
648
662
|
Description: 'Prefer `index_with` over `each_with_object`, `to_h`, or `map`.'
|
649
663
|
Enabled: true
|
664
|
+
SafeAutoCorrect: false
|
650
665
|
VersionAdded: '2.5'
|
651
|
-
VersionChanged: '2.
|
666
|
+
VersionChanged: '2.33'
|
652
667
|
|
653
668
|
Rails/Inquiry:
|
654
669
|
Description: "Prefer Ruby's comparison operators over Active Support's `Array#inquiry` and `String#inquiry`."
|
@@ -742,6 +757,13 @@ Rails/NotNullColumn:
|
|
742
757
|
Include:
|
743
758
|
- db/**/*.rb
|
744
759
|
|
760
|
+
Rails/OrderArguments:
|
761
|
+
Description: 'Prefer symbol arguments over strings in `order` method.'
|
762
|
+
StyleGuide: 'https://rails.rubystyle.guide/#order-arguments'
|
763
|
+
Enabled: pending
|
764
|
+
VersionAdded: '2.33'
|
765
|
+
Safe: false
|
766
|
+
|
745
767
|
Rails/OrderById:
|
746
768
|
Description: >-
|
747
769
|
Do not use the `id` column for ordering.
|
@@ -1126,7 +1148,7 @@ Rails/ThreeStateBooleanColumn:
|
|
1126
1148
|
Rails/TimeZone:
|
1127
1149
|
Description: 'Checks the correct usage of time zone aware methods.'
|
1128
1150
|
StyleGuide: 'https://rails.rubystyle.guide#time'
|
1129
|
-
Reference: '
|
1151
|
+
Reference: 'https://danilenko.org/2012/7/6/rails_timezones'
|
1130
1152
|
Enabled: true
|
1131
1153
|
SafeAutoCorrect: false
|
1132
1154
|
VersionAdded: '0.30'
|
@@ -106,7 +106,7 @@ module RuboCop
|
|
106
106
|
send_node = node.each_ancestor(:call).first
|
107
107
|
return false unless send_node
|
108
108
|
|
109
|
-
return true if WHERE_METHODS.include?(send_node.method_name)
|
109
|
+
return true if WHERE_METHODS.include?(send_node.method_name) && send_node.receiver != node
|
110
110
|
|
111
111
|
receiver = send_node.receiver
|
112
112
|
return false unless receiver&.send_type?
|
@@ -132,9 +132,13 @@ module RuboCop
|
|
132
132
|
add_offense(
|
133
133
|
node, message: "Prefer `#{new_method_name}` over `#{match_desc}`."
|
134
134
|
) do |corrector|
|
135
|
+
next if part_of_ignored_node?(node)
|
136
|
+
|
135
137
|
correction = prepare_correction(node)
|
136
138
|
execute_correction(corrector, node, correction)
|
137
139
|
end
|
140
|
+
|
141
|
+
ignore_node(node)
|
138
142
|
end
|
139
143
|
|
140
144
|
def extract_captures(match)
|
@@ -76,7 +76,7 @@ module RuboCop
|
|
76
76
|
|
77
77
|
def on_def(node)
|
78
78
|
return unless trivial_delegate?(node)
|
79
|
-
return if private_or_protected_delegation(node)
|
79
|
+
return if private_or_protected_delegation?(node)
|
80
80
|
return if module_function_declared?(node)
|
81
81
|
|
82
82
|
register_offense(node)
|
@@ -163,8 +163,8 @@ module RuboCop
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
166
|
-
def private_or_protected_delegation(node)
|
167
|
-
private_or_protected_inline(node) || node_visibility(node) != :public
|
166
|
+
def private_or_protected_delegation?(node)
|
167
|
+
private_or_protected_inline?(node) || node_visibility(node) != :public
|
168
168
|
end
|
169
169
|
|
170
170
|
def module_function_declared?(node)
|
@@ -173,7 +173,7 @@ module RuboCop
|
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
176
|
-
def private_or_protected_inline(node)
|
176
|
+
def private_or_protected_inline?(node)
|
177
177
|
processed_source[node.first_line - 1].strip.match?(/\A(private )|(protected )/)
|
178
178
|
end
|
179
179
|
end
|
@@ -94,7 +94,7 @@ module RuboCop
|
|
94
94
|
filtered_nodes = association_nodes.reject { |node| node.method?(:belongs_to) }
|
95
95
|
grouped_associations = filtered_nodes.group_by do |node|
|
96
96
|
arguments = association(node).last
|
97
|
-
next unless arguments.
|
97
|
+
next unless arguments.one?
|
98
98
|
|
99
99
|
if (class_name = class_name(arguments.first))
|
100
100
|
class_name.source
|
@@ -24,45 +24,69 @@ module RuboCop
|
|
24
24
|
|
25
25
|
minimum_target_rails_version 7.1
|
26
26
|
|
27
|
-
# @!method
|
28
|
-
def_node_matcher :
|
29
|
-
(
|
30
|
-
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
31
|
-
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
32
|
-
)
|
27
|
+
# @!method rails_env_local?(node)
|
28
|
+
def_node_matcher :rails_env_local?, <<~PATTERN
|
29
|
+
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
33
30
|
PATTERN
|
34
31
|
|
35
|
-
# @!method
|
36
|
-
def_node_matcher :
|
37
|
-
(
|
38
|
-
(send
|
39
|
-
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
40
|
-
:!)
|
41
|
-
(send
|
42
|
-
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
|
43
|
-
:!)
|
44
|
-
)
|
32
|
+
# @!method not_rails_env_local?(node)
|
33
|
+
def_node_matcher :not_rails_env_local?, <<~PATTERN
|
34
|
+
(send #rails_env_local? :!)
|
45
35
|
PATTERN
|
46
36
|
|
47
37
|
def on_or(node)
|
48
|
-
|
49
|
-
|
38
|
+
lhs, rhs = *node.children
|
39
|
+
return unless rails_env_local?(rhs)
|
50
40
|
|
51
|
-
|
52
|
-
|
53
|
-
|
41
|
+
nodes = [rhs]
|
42
|
+
|
43
|
+
if rails_env_local?(lhs)
|
44
|
+
nodes << lhs
|
45
|
+
elsif lhs.or_type? && rails_env_local?(lhs.rhs)
|
46
|
+
nodes << lhs.rhs
|
47
|
+
end
|
48
|
+
|
49
|
+
return unless environments(nodes).to_set == LOCAL_ENVIRONMENTS
|
50
|
+
|
51
|
+
range = offense_range(nodes)
|
52
|
+
add_offense(range) do |corrector|
|
53
|
+
corrector.replace(range, 'Rails.env.local?')
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
57
|
def on_and(node)
|
58
|
-
|
59
|
-
|
58
|
+
lhs, rhs = *node.children
|
59
|
+
return unless not_rails_env_local?(rhs)
|
60
|
+
|
61
|
+
nodes = [rhs]
|
62
|
+
|
63
|
+
if not_rails_env_local?(lhs)
|
64
|
+
nodes << lhs
|
65
|
+
elsif lhs.operator_keyword? && not_rails_env_local?(lhs.rhs)
|
66
|
+
nodes << lhs.rhs
|
67
|
+
end
|
60
68
|
|
61
|
-
|
62
|
-
|
63
|
-
|
69
|
+
return unless environments(nodes).to_set == LOCAL_ENVIRONMENTS
|
70
|
+
|
71
|
+
range = offense_range(nodes)
|
72
|
+
add_offense(range, message: MSG_NEGATED) do |corrector|
|
73
|
+
corrector.replace(range, '!Rails.env.local?')
|
64
74
|
end
|
65
75
|
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def environments(nodes)
|
80
|
+
if nodes[0].method?(:!)
|
81
|
+
nodes.map { |node| node.receiver.method_name }
|
82
|
+
else
|
83
|
+
nodes.map(&:method_name)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def offense_range(nodes)
|
88
|
+
nodes[1].source_range.begin.join(nodes[0].source_range.end)
|
89
|
+
end
|
66
90
|
end
|
67
91
|
end
|
68
92
|
end
|
@@ -190,7 +190,7 @@ module RuboCop
|
|
190
190
|
else
|
191
191
|
replace_with_rails_root_join(corrector, rails_root_node, argument_source)
|
192
192
|
end
|
193
|
-
node.children[rails_root_index + 1..].each { |child| corrector.remove(child) }
|
193
|
+
node.children[(rails_root_index + 1)..].each { |child| corrector.remove(child) }
|
194
194
|
end
|
195
195
|
|
196
196
|
def autocorrect_extension_after_rails_root_join_in_dstr(corrector, node, rails_root_index, extension_node)
|
@@ -281,7 +281,7 @@ module RuboCop
|
|
281
281
|
end
|
282
282
|
|
283
283
|
def extract_rails_root_join_argument_source(node, rails_root_index)
|
284
|
-
node.children[rails_root_index + 1..].map(&:source).join.delete_prefix(File::SEPARATOR)
|
284
|
+
node.children[(rails_root_index + 1)..].map(&:source).join.delete_prefix(File::SEPARATOR)
|
285
285
|
end
|
286
286
|
|
287
287
|
def extension_node?(node)
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Avoid memoizing `find_by` results with `||=`.
|
7
|
+
#
|
8
|
+
# It is common to see code that attempts to memoize `find_by` result by `||=`,
|
9
|
+
# but `find_by` may return `nil`, in which case it is not memoized as intended.
|
10
|
+
#
|
11
|
+
# @safety
|
12
|
+
# This cop is unsafe because detected `find_by` may not be Active Record's method,
|
13
|
+
# or the code may have a different purpose than memoization.
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
# # bad
|
17
|
+
# def current_user
|
18
|
+
# @current_user ||= User.find_by(id: session[:user_id])
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# # good
|
22
|
+
# def current_user
|
23
|
+
# if instance_variable_defined?(:@current_user)
|
24
|
+
# @current_user
|
25
|
+
# else
|
26
|
+
# @current_user = User.find_by(id: session[:user_id])
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
class FindByOrAssignmentMemoization < Base
|
30
|
+
extend AutoCorrector
|
31
|
+
|
32
|
+
MSG = 'Avoid memoizing `find_by` results with `||=`.'
|
33
|
+
|
34
|
+
RESTRICT_ON_SEND = %i[find_by].freeze
|
35
|
+
|
36
|
+
def_node_matcher :find_by_or_assignment_memoization, <<~PATTERN
|
37
|
+
(or_asgn
|
38
|
+
(ivasgn $_)
|
39
|
+
$(send _ :find_by ...)
|
40
|
+
)
|
41
|
+
PATTERN
|
42
|
+
|
43
|
+
def on_send(node)
|
44
|
+
assignment_node = node.parent
|
45
|
+
find_by_or_assignment_memoization(assignment_node) do |varible_name, find_by|
|
46
|
+
next if assignment_node.each_ancestor(:if).any?
|
47
|
+
|
48
|
+
add_offense(assignment_node) do |corrector|
|
49
|
+
corrector.replace(
|
50
|
+
assignment_node,
|
51
|
+
<<~RUBY.rstrip
|
52
|
+
if instance_variable_defined?(:#{varible_name})
|
53
|
+
#{varible_name}
|
54
|
+
else
|
55
|
+
#{varible_name} = #{find_by.source}
|
56
|
+
end
|
57
|
+
RUBY
|
58
|
+
)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -8,6 +8,11 @@ module RuboCop
|
|
8
8
|
# an enumerable into a hash where the keys are the original elements.
|
9
9
|
# Rails provides the `index_with` method for this purpose.
|
10
10
|
#
|
11
|
+
# @safety
|
12
|
+
# This cop is marked as unsafe autocorrection, because `nil.to_h` returns {}
|
13
|
+
# but `nil.with_index` throws `NoMethodError`. Therefore, autocorrection is not
|
14
|
+
# compatible if the receiver is nil.
|
15
|
+
#
|
11
16
|
# @example
|
12
17
|
# # bad
|
13
18
|
# [1, 2, 3].each_with_object({}) { |el, h| h[el] = foo(el) }
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Prefer symbol arguments over strings in `order` method.
|
7
|
+
#
|
8
|
+
# @safety
|
9
|
+
# Cop is unsafe because the receiver might not be an Active Record query.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# # bad
|
13
|
+
# User.order('name')
|
14
|
+
# User.order('name DESC')
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# User.order(:name)
|
18
|
+
# User.order(name: :desc)
|
19
|
+
#
|
20
|
+
class OrderArguments < Base
|
21
|
+
extend AutoCorrector
|
22
|
+
|
23
|
+
MSG = 'Prefer `%<prefer>s` instead.'
|
24
|
+
|
25
|
+
RESTRICT_ON_SEND = %i[order].freeze
|
26
|
+
|
27
|
+
def_node_matcher :string_order, <<~PATTERN
|
28
|
+
(call _ :order (str $_value)+)
|
29
|
+
PATTERN
|
30
|
+
|
31
|
+
ORDER_EXPRESSION_REGEX = /\A(\w+) ?(asc|desc)?\z/i.freeze
|
32
|
+
|
33
|
+
def on_send(node)
|
34
|
+
return unless (current_expressions = string_order(node))
|
35
|
+
return unless (preferred_expressions = replacement(current_expressions))
|
36
|
+
|
37
|
+
offense_range = find_offense_range(node)
|
38
|
+
add_offense(offense_range, message: format(MSG, prefer: preferred_expressions)) do |corrector|
|
39
|
+
corrector.replace(offense_range, preferred_expressions)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
alias on_csend on_send
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def find_offense_range(node)
|
47
|
+
node.first_argument.source_range.join(node.last_argument.source_range)
|
48
|
+
end
|
49
|
+
|
50
|
+
def replacement(order_expressions)
|
51
|
+
order_arguments = order_expressions.flat_map { |expr| expr.split(',') }
|
52
|
+
order_arguments.map! { |arg| extract_column_and_direction(arg.strip) }
|
53
|
+
|
54
|
+
return if order_arguments.any?(&:nil?)
|
55
|
+
|
56
|
+
convert_to_preferred_arguments(order_arguments).join(', ')
|
57
|
+
end
|
58
|
+
|
59
|
+
def convert_to_preferred_arguments(order_expressions)
|
60
|
+
use_hash = false
|
61
|
+
order_expressions.map do |column, direction|
|
62
|
+
if direction == :asc && !use_hash
|
63
|
+
":#{column}"
|
64
|
+
else
|
65
|
+
use_hash = true
|
66
|
+
"#{column}: :#{direction}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def extract_column_and_direction(order_expression)
|
72
|
+
return unless (column, direction = ORDER_EXPRESSION_REGEX.match(order_expression)&.captures)
|
73
|
+
|
74
|
+
[column.downcase, direction&.downcase&.to_sym || :asc]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -38,9 +38,11 @@ module RuboCop
|
|
38
38
|
...)
|
39
39
|
PATTERN
|
40
40
|
|
41
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
41
42
|
def on_send(node)
|
42
43
|
return if node.parent&.call_type? || node.block_node
|
43
44
|
return if !output?(node) && !io_output?(node)
|
45
|
+
return if node.arguments.any? { |arg| arg.type?(:hash, :block_pass) }
|
44
46
|
|
45
47
|
range = offense_range(node)
|
46
48
|
|
@@ -48,6 +50,7 @@ module RuboCop
|
|
48
50
|
corrector.replace(range, 'Rails.logger.debug')
|
49
51
|
end
|
50
52
|
end
|
53
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
51
54
|
|
52
55
|
private
|
53
56
|
|
@@ -27,6 +27,9 @@ module RuboCop
|
|
27
27
|
# end
|
28
28
|
# ----
|
29
29
|
#
|
30
|
+
# If a method call has no receiver, like `do_something { users.map { |user| user[:foo] }`,
|
31
|
+
# it is not considered part of an iteration and will be detected.
|
32
|
+
#
|
30
33
|
# @safety
|
31
34
|
# This cop is unsafe because model can use column aliases.
|
32
35
|
#
|
@@ -59,9 +62,9 @@ module RuboCop
|
|
59
62
|
(any_block (call _ {:map :collect}) $_argument (send lvar :[] $_key))
|
60
63
|
PATTERN
|
61
64
|
|
62
|
-
# rubocop:disable Metrics/AbcSize
|
65
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
63
66
|
def on_block(node)
|
64
|
-
return if node.each_ancestor(:any_block).
|
67
|
+
return if node.each_ancestor(:any_block).first&.receiver
|
65
68
|
|
66
69
|
pluck_candidate?(node) do |argument, key|
|
67
70
|
next if key.regexp_type? || !use_one_block_argument?(argument)
|
@@ -79,7 +82,7 @@ module RuboCop
|
|
79
82
|
register_offense(node, key)
|
80
83
|
end
|
81
84
|
end
|
82
|
-
# rubocop:enable Metrics/AbcSize
|
85
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
83
86
|
alias on_numblock on_block
|
84
87
|
alias on_itblock on_block
|
85
88
|
|
@@ -74,7 +74,7 @@ module RuboCop
|
|
74
74
|
$[
|
75
75
|
(hash <$(pair (sym :presence) true) ...>) # presence: true
|
76
76
|
!(hash <$(pair (sym :strict) {true const}) ...>) # strict: true
|
77
|
-
!(hash <$(pair (sym {:if
|
77
|
+
!(hash <$(pair (sym {:if}) _) ...>) # if: some_condition or unless: some_condition
|
78
78
|
]
|
79
79
|
)
|
80
80
|
PATTERN
|
@@ -211,12 +211,12 @@ module RuboCop
|
|
211
211
|
|
212
212
|
def non_optional_belongs_to(node, keys)
|
213
213
|
keys.select do |key|
|
214
|
-
belongs_to = belongs_to_for(node, key)
|
214
|
+
belongs_to = belongs_to_for?(node, key)
|
215
215
|
belongs_to && !optional?(belongs_to)
|
216
216
|
end
|
217
217
|
end
|
218
218
|
|
219
|
-
def belongs_to_for(model_class_node, key)
|
219
|
+
def belongs_to_for?(model_class_node, key)
|
220
220
|
if key.to_s.end_with?('_id')
|
221
221
|
normalized_key = key.to_s.delete_suffix('_id').to_sym
|
222
222
|
belongs_to?(model_class_node, key: normalized_key, fk: key)
|
@@ -71,7 +71,7 @@ module RuboCop
|
|
71
71
|
def on_block(node)
|
72
72
|
return unless node.method?(:with_options)
|
73
73
|
return unless (body = node.body)
|
74
|
-
return unless all_block_nodes_in(body).
|
74
|
+
return unless all_block_nodes_in(body).none?
|
75
75
|
|
76
76
|
send_nodes = all_send_nodes_in(body)
|
77
77
|
return unless redundant_receiver?(send_nodes, node)
|
@@ -156,7 +156,7 @@ module RuboCop
|
|
156
156
|
return unless persist_method?(node)
|
157
157
|
return if return_value_assigned?(node)
|
158
158
|
return if implicit_return?(node)
|
159
|
-
return if check_used_in_condition_or_compound_boolean(node)
|
159
|
+
return if check_used_in_condition_or_compound_boolean?(node)
|
160
160
|
return if argument?(node)
|
161
161
|
return if explicit_return?(node)
|
162
162
|
return if checked_immediately?(node)
|
@@ -227,7 +227,7 @@ module RuboCop
|
|
227
227
|
array
|
228
228
|
end
|
229
229
|
|
230
|
-
def check_used_in_condition_or_compound_boolean(node)
|
230
|
+
def check_used_in_condition_or_compound_boolean?(node)
|
231
231
|
return false unless in_condition_or_compound_boolean?(node)
|
232
232
|
|
233
233
|
register_offense(node, CREATE_CONDITIONAL_MSG) unless MODIFY_PERSIST_METHODS.include?(node.method_name)
|
@@ -97,9 +97,11 @@ module RuboCop
|
|
97
97
|
|
98
98
|
def in_transaction_block?(node)
|
99
99
|
return false unless transaction_method_name?(node.method_name)
|
100
|
-
return false unless
|
100
|
+
return false unless node.parent&.body
|
101
101
|
|
102
|
-
|
102
|
+
node.right_siblings.none? do |sibling|
|
103
|
+
sibling.respond_to?(:loop_keyword?) && sibling.loop_keyword?
|
104
|
+
end
|
103
105
|
end
|
104
106
|
|
105
107
|
def statement(statement_node)
|
@@ -63,7 +63,7 @@ module RuboCop
|
|
63
63
|
PATTERN
|
64
64
|
|
65
65
|
def on_send(node)
|
66
|
-
find_offenses(node) do |args|
|
66
|
+
find_offenses?(node) do |args|
|
67
67
|
return unless convertable_args?(args)
|
68
68
|
|
69
69
|
range = correction_range(node)
|
@@ -87,7 +87,7 @@ module RuboCop
|
|
87
87
|
style == :exists
|
88
88
|
end
|
89
89
|
|
90
|
-
def find_offenses(node, &block)
|
90
|
+
def find_offenses?(node, &block)
|
91
91
|
if exists_style?
|
92
92
|
where_exists_call?(node, &block)
|
93
93
|
elsif where_style?
|
@@ -58,6 +58,7 @@ require_relative 'rails/expanded_date_range'
|
|
58
58
|
require_relative 'rails/file_path'
|
59
59
|
require_relative 'rails/find_by'
|
60
60
|
require_relative 'rails/find_by_id'
|
61
|
+
require_relative 'rails/find_by_or_assignment_memoization'
|
61
62
|
require_relative 'rails/find_each'
|
62
63
|
require_relative 'rails/freeze_time'
|
63
64
|
require_relative 'rails/has_and_belongs_to_many'
|
@@ -82,6 +83,7 @@ require_relative 'rails/migration_class_name'
|
|
82
83
|
require_relative 'rails/multiple_route_paths'
|
83
84
|
require_relative 'rails/negate_include'
|
84
85
|
require_relative 'rails/not_null_column'
|
86
|
+
require_relative 'rails/order_arguments'
|
85
87
|
require_relative 'rails/order_by_id'
|
86
88
|
require_relative 'rails/output'
|
87
89
|
require_relative 'rails/output_safety'
|
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.
|
4
|
+
version: 2.33.0
|
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:
|
12
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- lib/rubocop/cop/rails/file_path.rb
|
165
165
|
- lib/rubocop/cop/rails/find_by.rb
|
166
166
|
- lib/rubocop/cop/rails/find_by_id.rb
|
167
|
+
- lib/rubocop/cop/rails/find_by_or_assignment_memoization.rb
|
167
168
|
- lib/rubocop/cop/rails/find_each.rb
|
168
169
|
- lib/rubocop/cop/rails/freeze_time.rb
|
169
170
|
- lib/rubocop/cop/rails/has_and_belongs_to_many.rb
|
@@ -188,6 +189,7 @@ files:
|
|
188
189
|
- lib/rubocop/cop/rails/multiple_route_paths.rb
|
189
190
|
- lib/rubocop/cop/rails/negate_include.rb
|
190
191
|
- lib/rubocop/cop/rails/not_null_column.rb
|
192
|
+
- lib/rubocop/cop/rails/order_arguments.rb
|
191
193
|
- lib/rubocop/cop/rails/order_by_id.rb
|
192
194
|
- lib/rubocop/cop/rails/output.rb
|
193
195
|
- lib/rubocop/cop/rails/output_safety.rb
|
@@ -264,7 +266,7 @@ metadata:
|
|
264
266
|
homepage_uri: https://docs.rubocop.org/rubocop-rails/
|
265
267
|
changelog_uri: https://github.com/rubocop/rubocop-rails/blob/master/CHANGELOG.md
|
266
268
|
source_code_uri: https://github.com/rubocop/rubocop-rails/
|
267
|
-
documentation_uri: https://docs.rubocop.org/rubocop-rails/2.
|
269
|
+
documentation_uri: https://docs.rubocop.org/rubocop-rails/2.33/
|
268
270
|
bug_tracker_uri: https://github.com/rubocop/rubocop-rails/issues
|
269
271
|
rubygems_mfa_required: 'true'
|
270
272
|
default_lint_roller_plugin: RuboCop::Rails::Plugin
|
@@ -282,7 +284,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
282
284
|
- !ruby/object:Gem::Version
|
283
285
|
version: '0'
|
284
286
|
requirements: []
|
285
|
-
rubygems_version: 3.6.
|
287
|
+
rubygems_version: 3.6.9
|
286
288
|
specification_version: 4
|
287
289
|
summary: Automatic Rails code style checking tool.
|
288
290
|
test_files: []
|