rubocop 1.53.1 → 1.57.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 +4 -4
- data/README.md +3 -1
- data/config/default.yml +34 -8
- data/config/obsoletion.yml +5 -0
- data/lib/rubocop/cli/command/auto_generate_config.rb +10 -5
- data/lib/rubocop/cli.rb +1 -1
- data/lib/rubocop/config_finder.rb +2 -2
- data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
- data/lib/rubocop/cop/base.rb +1 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
- data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
- data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/layout/dot_position.rb +1 -5
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +26 -3
- data/lib/rubocop/cop/layout/end_alignment.rb +7 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
- data/lib/rubocop/cop/layout/redundant_line_break.rb +13 -3
- data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
- data/lib/rubocop/cop/lint/debugger.rb +17 -4
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_case_range.rb +3 -1
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +10 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
- data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +5 -3
- data/lib/rubocop/cop/lint/useless_assignment.rb +38 -12
- data/lib/rubocop/cop/lint/void.rb +32 -20
- data/lib/rubocop/cop/metrics/block_length.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +8 -3
- data/lib/rubocop/cop/metrics/method_length.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +32 -4
- data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -11
- data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +5 -7
- data/lib/rubocop/cop/mixin/string_help.rb +4 -2
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
- data/lib/rubocop/cop/style/alias.rb +9 -8
- data/lib/rubocop/cop/style/arguments_forwarding.rb +280 -63
- data/lib/rubocop/cop/style/array_intersect.rb +13 -5
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +7 -0
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +4 -2
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
- data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
- data/lib/rubocop/cop/style/for.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +24 -3
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -1
- data/lib/rubocop/cop/style/guard_clause.rb +26 -0
- data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +25 -3
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
- data/lib/rubocop/cop/style/lambda.rb +3 -3
- data/lib/rubocop/cop/style/lambda_call.rb +5 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +8 -1
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
- data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +6 -0
- data/lib/rubocop/cop/style/redundant_argument.rb +6 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +9 -1
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -9
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +93 -5
- data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +22 -5
- data/lib/rubocop/cop/style/redundant_parentheses.rb +41 -15
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -1
- data/lib/rubocop/cop/style/redundant_return.rb +7 -2
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +5 -0
- data/lib/rubocop/cop/style/return_nil.rb +6 -2
- data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +23 -9
- data/lib/rubocop/cop/style/semicolon.rb +0 -3
- data/lib/rubocop/cop/style/single_argument_dig.rb +2 -1
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +67 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +3 -1
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
- data/lib/rubocop/cop/style/symbol_array.rb +35 -15
- data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
- data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
- data/lib/rubocop/cop/utils/regexp_ranges.rb +26 -13
- data/lib/rubocop/cop/variable_force/assignment.rb +14 -5
- data/lib/rubocop/file_finder.rb +4 -7
- data/lib/rubocop/formatter/html_formatter.rb +4 -2
- data/lib/rubocop/formatter/junit_formatter.rb +1 -1
- data/lib/rubocop/lsp/routes.rb +41 -18
- data/lib/rubocop/lsp/runtime.rb +22 -2
- data/lib/rubocop/lsp/server.rb +10 -4
- data/lib/rubocop/magic_comment.rb +12 -10
- data/lib/rubocop/result_cache.rb +4 -0
- data/lib/rubocop/rspec/shared_contexts.rb +2 -3
- data/lib/rubocop/runner.rb +5 -3
- data/lib/rubocop/server/cache.rb +1 -0
- data/lib/rubocop/server/client_command/exec.rb +1 -1
- data/lib/rubocop/string_interpreter.rb +3 -3
- data/lib/rubocop/target_finder.rb +7 -3
- data/lib/rubocop/target_ruby.rb +9 -5
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +2 -0
- metadata +16 -14
@@ -6,6 +6,12 @@ module RuboCop
|
|
6
6
|
# Identifies usages of `any?`, `empty?` or `none?` predicate methods
|
7
7
|
# chained to `select`/`filter`/`find_all` and change them to use predicate method instead.
|
8
8
|
#
|
9
|
+
# @safety
|
10
|
+
# This cop's autocorrection is unsafe because `array.select.any?` evaluates all elements
|
11
|
+
# through the `select` method, while `array.any?` uses short-circuit evaluation.
|
12
|
+
# In other words, `array.select.any?` guarantees the evaluation of every element,
|
13
|
+
# but `array.any?` does not necessarily evaluate all of them.
|
14
|
+
#
|
9
15
|
# @example
|
10
16
|
# # bad
|
11
17
|
# arr.select { |x| x > 1 }.any?
|
@@ -28,6 +34,9 @@ module RuboCop
|
|
28
34
|
# # good
|
29
35
|
# arr.select { |x| x > 1 }.many?
|
30
36
|
#
|
37
|
+
# # good
|
38
|
+
# arr.select { |x| x > 1 }.present?
|
39
|
+
#
|
31
40
|
# @example AllCops:ActiveSupportExtensionsEnabled: true
|
32
41
|
# # bad
|
33
42
|
# arr.select { |x| x > 1 }.many?
|
@@ -35,20 +44,26 @@ module RuboCop
|
|
35
44
|
# # good
|
36
45
|
# arr.many? { |x| x > 1 }
|
37
46
|
#
|
47
|
+
# # bad
|
48
|
+
# arr.select { |x| x > 1 }.present?
|
49
|
+
#
|
50
|
+
# # good
|
51
|
+
# arr.any? { |x| x > 1 }
|
52
|
+
#
|
38
53
|
class RedundantFilterChain < Base
|
39
54
|
extend AutoCorrector
|
40
55
|
|
41
56
|
MSG = 'Use `%<prefer>s` instead of `%<first_method>s.%<second_method>s`.'
|
42
57
|
|
43
|
-
RAILS_METHODS = %i[many?].freeze
|
58
|
+
RAILS_METHODS = %i[many? present?].freeze
|
44
59
|
RESTRICT_ON_SEND = (%i[any? empty? none? one?] + RAILS_METHODS).freeze
|
45
60
|
|
46
61
|
# @!method select_predicate?(node)
|
47
62
|
def_node_matcher :select_predicate?, <<~PATTERN
|
48
|
-
(
|
63
|
+
(call
|
49
64
|
{
|
50
|
-
(block $(
|
51
|
-
$(
|
65
|
+
(block $(call _ {:select :filter :find_all}) ...)
|
66
|
+
$(call _ {:select :filter :find_all} block_pass_type?)
|
52
67
|
}
|
53
68
|
${:#{RESTRICT_ON_SEND.join(' :')}})
|
54
69
|
PATTERN
|
@@ -58,7 +73,8 @@ module RuboCop
|
|
58
73
|
empty?: :none?,
|
59
74
|
none?: :none?,
|
60
75
|
one?: :one?,
|
61
|
-
many?: :many
|
76
|
+
many?: :many?,
|
77
|
+
present?: :any?
|
62
78
|
}.freeze
|
63
79
|
private_constant :REPLACEMENT_METHODS
|
64
80
|
|
@@ -71,6 +87,7 @@ module RuboCop
|
|
71
87
|
register_offense(select_node, node)
|
72
88
|
end
|
73
89
|
end
|
90
|
+
alias on_csend on_send
|
74
91
|
|
75
92
|
private
|
76
93
|
|
@@ -98,7 +98,9 @@ module RuboCop
|
|
98
98
|
end
|
99
99
|
|
100
100
|
def like_method_argument_parentheses?(node)
|
101
|
-
node.send_type? && node.
|
101
|
+
return false if !node.send_type? && !node.super_type? && !node.yield_type?
|
102
|
+
|
103
|
+
node.arguments.one? && !node.parenthesized? &&
|
102
104
|
!node.arithmetic_operation? && node.first_argument.begin_type?
|
103
105
|
end
|
104
106
|
|
@@ -109,31 +111,49 @@ module RuboCop
|
|
109
111
|
|
110
112
|
def first_arg_begins_with_hash_literal?(node)
|
111
113
|
# Don't flag `method ({key: value})` or `method ({key: value}.method)`
|
112
|
-
method_chain_begins_with_hash_literal
|
113
|
-
|
114
|
-
|
114
|
+
hash_literal = method_chain_begins_with_hash_literal(node.children.first)
|
115
|
+
if (root_method = node.each_ancestor(:send).to_a.last)
|
116
|
+
parenthesized = root_method.parenthesized_call?
|
117
|
+
end
|
118
|
+
hash_literal && first_argument?(node) && !parentheses?(hash_literal) && !parenthesized
|
115
119
|
end
|
116
120
|
|
117
|
-
def method_chain_begins_with_hash_literal
|
118
|
-
return
|
119
|
-
return
|
120
|
-
return
|
121
|
+
def method_chain_begins_with_hash_literal(node)
|
122
|
+
return if node.nil?
|
123
|
+
return node if node.hash_type?
|
124
|
+
return unless node.send_type?
|
121
125
|
|
122
|
-
method_chain_begins_with_hash_literal
|
126
|
+
method_chain_begins_with_hash_literal(node.children.first)
|
123
127
|
end
|
124
128
|
|
125
129
|
def check(begin_node)
|
126
130
|
node = begin_node.children.first
|
127
|
-
return offense(begin_node, 'a keyword') if keyword_with_redundant_parentheses?(node)
|
128
|
-
return offense(begin_node, 'a literal') if disallowed_literal?(begin_node, node)
|
129
|
-
return offense(begin_node, 'a variable') if node.variable?
|
130
|
-
return offense(begin_node, 'a constant') if node.const_type?
|
131
131
|
|
132
|
-
|
132
|
+
if (message = find_offense_message(begin_node, node))
|
133
|
+
return offense(begin_node, message)
|
134
|
+
end
|
133
135
|
|
134
136
|
check_send(begin_node, node) if node.call_type?
|
135
137
|
end
|
136
138
|
|
139
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
140
|
+
def find_offense_message(begin_node, node)
|
141
|
+
return 'a keyword' if keyword_with_redundant_parentheses?(node)
|
142
|
+
return 'a literal' if disallowed_literal?(begin_node, node)
|
143
|
+
return 'a variable' if node.variable?
|
144
|
+
return 'a constant' if node.const_type?
|
145
|
+
return 'an interpolated expression' if interpolation?(begin_node)
|
146
|
+
|
147
|
+
return if begin_node.chained? || !begin_node.parent.nil?
|
148
|
+
|
149
|
+
if node.and_type? || node.or_type?
|
150
|
+
'a logical expression'
|
151
|
+
elsif node.respond_to?(:comparison_method?) && node.comparison_method?
|
152
|
+
'a comparison expression'
|
153
|
+
end
|
154
|
+
end
|
155
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
156
|
+
|
137
157
|
# @!method interpolation?(node)
|
138
158
|
def_node_matcher :interpolation?, '[^begin ^^dstr]'
|
139
159
|
|
@@ -213,7 +233,13 @@ module RuboCop
|
|
213
233
|
end
|
214
234
|
|
215
235
|
def first_argument?(node)
|
216
|
-
first_send_argument?(node) ||
|
236
|
+
if first_send_argument?(node) ||
|
237
|
+
first_super_argument?(node) ||
|
238
|
+
first_yield_argument?(node)
|
239
|
+
return true
|
240
|
+
end
|
241
|
+
|
242
|
+
node.each_ancestor.any? { |ancestor| first_argument?(ancestor) }
|
217
243
|
end
|
218
244
|
|
219
245
|
# @!method first_send_argument?(node)
|
@@ -40,7 +40,9 @@ module RuboCop
|
|
40
40
|
byteindex byterindex gsub gsub! partition rpartition scan split start_with? sub sub!
|
41
41
|
].freeze
|
42
42
|
DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/.freeze
|
43
|
-
STR_SPECIAL_CHARS = %w[
|
43
|
+
STR_SPECIAL_CHARS = %w[
|
44
|
+
\a \c \C \e \f \M \n \" \' \\\\ \t \b \f \r \u \v \x \0 \1 \2 \3 \4 \5 \6 \7
|
45
|
+
].freeze
|
44
46
|
|
45
47
|
def on_send(node)
|
46
48
|
return unless (regexp_node = node.first_argument)
|
@@ -55,6 +57,7 @@ module RuboCop
|
|
55
57
|
corrector.replace(regexp_node, prefer)
|
56
58
|
end
|
57
59
|
end
|
60
|
+
alias on_csend on_send
|
58
61
|
|
59
62
|
private
|
60
63
|
|
@@ -22,13 +22,18 @@ module RuboCop
|
|
22
22
|
# return something
|
23
23
|
# end
|
24
24
|
#
|
25
|
-
# #
|
25
|
+
# # bad
|
26
26
|
# def test
|
27
27
|
# return something if something_else
|
28
28
|
# end
|
29
29
|
#
|
30
30
|
# # good
|
31
31
|
# def test
|
32
|
+
# something if something_else
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# # good
|
36
|
+
# def test
|
32
37
|
# if x
|
33
38
|
# elsif y
|
34
39
|
# else
|
@@ -136,7 +141,7 @@ module RuboCop
|
|
136
141
|
end
|
137
142
|
|
138
143
|
def check_if_node(node)
|
139
|
-
return if node.
|
144
|
+
return if node.ternary?
|
140
145
|
|
141
146
|
check_branch(node.if_branch)
|
142
147
|
check_branch(node.else_branch)
|
@@ -75,6 +75,11 @@ module RuboCop
|
|
75
75
|
add_offense(offense_branch) do |corrector|
|
76
76
|
assignment_value = opposite_branch ? opposite_branch.source : 'nil'
|
77
77
|
replacement = "#{assignment_value} #{keyword} #{if_node.condition.source}"
|
78
|
+
if opposite_branch.respond_to?(:heredoc?) && opposite_branch.heredoc?
|
79
|
+
replacement += opposite_branch.loc.heredoc_end.with(
|
80
|
+
begin_pos: opposite_branch.source_range.end_pos
|
81
|
+
).source
|
82
|
+
end
|
78
83
|
|
79
84
|
corrector.replace(if_node, replacement)
|
80
85
|
end
|
@@ -3,9 +3,13 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Enforces consistency between
|
6
|
+
# Enforces consistency between `return nil` and `return`.
|
7
7
|
#
|
8
|
-
#
|
8
|
+
# This cop is disabled by default. Because there seems to be a perceived semantic difference
|
9
|
+
# between `return` and `return nil`. The former can be seen as just halting evaluation,
|
10
|
+
# while the latter might be used when the return value is of specific concern.
|
11
|
+
#
|
12
|
+
# Supported styles are `return` and `return_nil`.
|
9
13
|
#
|
10
14
|
# @example EnforcedStyle: return (default)
|
11
15
|
# # bad
|
@@ -31,7 +31,7 @@ module RuboCop
|
|
31
31
|
# do_something?
|
32
32
|
# end
|
33
33
|
#
|
34
|
-
# @example
|
34
|
+
# @example AllowedMethods: ['foo?']
|
35
35
|
# # good
|
36
36
|
# def foo?
|
37
37
|
# return if condition
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
39
39
|
# do_something?
|
40
40
|
# end
|
41
41
|
#
|
42
|
-
# @example
|
42
|
+
# @example AllowedPatterns: [/foo/]
|
43
43
|
# # good
|
44
44
|
# def foo?
|
45
45
|
# return if condition
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
52
52
|
include AllowedMethods
|
53
53
|
include AllowedPattern
|
54
54
|
|
55
|
-
MSG = '
|
55
|
+
MSG = 'Return `false` instead of `nil` in predicate methods.'
|
56
56
|
|
57
57
|
# @!method return_nil?(node)
|
58
58
|
def_node_matcher :return_nil?, <<~PATTERN
|
@@ -65,16 +65,30 @@ module RuboCop
|
|
65
65
|
return unless (body = node.body)
|
66
66
|
|
67
67
|
body.each_descendant(:return) do |return_node|
|
68
|
-
|
68
|
+
register_offense(return_node, 'return false') if return_nil?(return_node)
|
69
|
+
end
|
69
70
|
|
70
|
-
|
71
|
+
return unless (nil_node = nil_node_at_the_end_of_method_body(body))
|
71
72
|
|
72
|
-
|
73
|
-
corrector.replace(return_node, 'return false')
|
74
|
-
end
|
75
|
-
end
|
73
|
+
register_offense(nil_node, 'false')
|
76
74
|
end
|
77
75
|
alias on_defs on_def
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def nil_node_at_the_end_of_method_body(body)
|
80
|
+
return body if body.nil_type?
|
81
|
+
return unless body.begin_type?
|
82
|
+
return unless (last_child = body.children.last)
|
83
|
+
|
84
|
+
last_child if last_child.is_a?(AST::Node) && last_child.nil_type?
|
85
|
+
end
|
86
|
+
|
87
|
+
def register_offense(offense_node, replacement)
|
88
|
+
add_offense(offense_node) do |corrector|
|
89
|
+
corrector.replace(offense_node, replacement)
|
90
|
+
end
|
91
|
+
end
|
78
92
|
end
|
79
93
|
end
|
80
94
|
end
|
@@ -62,9 +62,6 @@ module RuboCop
|
|
62
62
|
private
|
63
63
|
|
64
64
|
def check_for_line_terminator_or_opener
|
65
|
-
# Make the obvious check first
|
66
|
-
return unless processed_source.raw_source.include?(';')
|
67
|
-
|
68
65
|
each_semicolon do |line, column, token_before_semicolon|
|
69
66
|
register_semicolon(line, column, false, token_before_semicolon)
|
70
67
|
end
|
@@ -33,6 +33,7 @@ module RuboCop
|
|
33
33
|
|
34
34
|
MSG = 'Use `%<receiver>s[%<argument>s]` instead of `%<original>s`.'
|
35
35
|
RESTRICT_ON_SEND = %i[dig].freeze
|
36
|
+
IGNORED_ARGUMENT_TYPES = %i[block_pass forwarded_restarg forwarded_args hash].freeze
|
36
37
|
|
37
38
|
# @!method single_argument_dig?(node)
|
38
39
|
def_node_matcher :single_argument_dig?, <<~PATTERN
|
@@ -44,7 +45,7 @@ module RuboCop
|
|
44
45
|
|
45
46
|
expression = single_argument_dig?(node)
|
46
47
|
return unless expression
|
47
|
-
return if expression.
|
48
|
+
return if IGNORED_ARGUMENT_TYPES.include?(expression.type)
|
48
49
|
|
49
50
|
receiver = node.receiver.source
|
50
51
|
argument = expression.source
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for single-line `do`...`end` block.
|
7
|
+
#
|
8
|
+
# In practice a single line `do`...`end` is autocorrected when `EnforcedStyle: semantic`
|
9
|
+
# in `Style/BlockDelimiters`. The autocorrection maintains the `do` ... `end` syntax to
|
10
|
+
# preserve semantics and does not change it to `{`...`}` block.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
#
|
14
|
+
# # bad
|
15
|
+
# foo do |arg| bar(arg) end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# foo do |arg|
|
19
|
+
# bar(arg)
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# # bad
|
23
|
+
# ->(arg) do bar(arg) end
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# ->(arg) { bar(arg) }
|
27
|
+
#
|
28
|
+
class SingleLineDoEndBlock < Base
|
29
|
+
extend AutoCorrector
|
30
|
+
|
31
|
+
MSG = 'Prefer multiline `do`...`end` block.'
|
32
|
+
|
33
|
+
# rubocop:disable Metrics/AbcSize
|
34
|
+
def on_block(node)
|
35
|
+
return if !node.single_line? || node.braces?
|
36
|
+
|
37
|
+
add_offense(node) do |corrector|
|
38
|
+
corrector.insert_after(do_line(node), "\n")
|
39
|
+
|
40
|
+
node_body = node.body
|
41
|
+
|
42
|
+
if node_body.respond_to?(:heredoc?) && node_body.heredoc?
|
43
|
+
corrector.remove(node.loc.end)
|
44
|
+
corrector.insert_after(node_body.loc.heredoc_end, "\nend")
|
45
|
+
else
|
46
|
+
corrector.insert_before(node.loc.end, "\n")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
# rubocop:enable Metrics/AbcSize
|
51
|
+
alias on_numblock on_block
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def do_line(node)
|
56
|
+
if node.numblock_type? || node.arguments.children.empty? || node.send_node.lambda_literal?
|
57
|
+
node.loc.begin
|
58
|
+
else
|
59
|
+
node.arguments
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def x(corrector, node); end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -173,7 +173,9 @@ module RuboCop
|
|
173
173
|
end
|
174
174
|
|
175
175
|
def correct_for_comment(corrector, node, if_branch)
|
176
|
-
comments = processed_source.ast_with_comments[if_branch]
|
176
|
+
comments = processed_source.ast_with_comments[if_branch].select do |comment|
|
177
|
+
comment.loc.line < if_branch.condition.first_line
|
178
|
+
end
|
177
179
|
comment_text = comments.map(&:text).join("\n") << "\n"
|
178
180
|
|
179
181
|
corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
|
@@ -3,22 +3,42 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks that quotes inside
|
6
|
+
# Checks that quotes inside string, symbol, and regexp interpolations
|
7
7
|
# match the configured preference.
|
8
8
|
#
|
9
9
|
# @example EnforcedStyle: single_quotes (default)
|
10
10
|
# # bad
|
11
|
-
#
|
11
|
+
# string = "Tests #{success ? "PASS" : "FAIL"}"
|
12
|
+
# symbol = :"Tests #{success ? "PASS" : "FAIL"}"
|
13
|
+
# heredoc = <<~TEXT
|
14
|
+
# Tests #{success ? "PASS" : "FAIL"}
|
15
|
+
# TEXT
|
16
|
+
# regexp = /Tests #{success ? "PASS" : "FAIL"}/
|
12
17
|
#
|
13
18
|
# # good
|
14
|
-
#
|
19
|
+
# string = "Tests #{success ? 'PASS' : 'FAIL'}"
|
20
|
+
# symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
|
21
|
+
# heredoc = <<~TEXT
|
22
|
+
# Tests #{success ? 'PASS' : 'FAIL'}
|
23
|
+
# TEXT
|
24
|
+
# regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
|
15
25
|
#
|
16
26
|
# @example EnforcedStyle: double_quotes
|
17
27
|
# # bad
|
18
|
-
#
|
28
|
+
# string = "Tests #{success ? 'PASS' : 'FAIL'}"
|
29
|
+
# symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
|
30
|
+
# heredoc = <<~TEXT
|
31
|
+
# Tests #{success ? 'PASS' : 'FAIL'}
|
32
|
+
# TEXT
|
33
|
+
# regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
|
19
34
|
#
|
20
35
|
# # good
|
21
|
-
#
|
36
|
+
# string = "Tests #{success ? "PASS" : "FAIL"}"
|
37
|
+
# symbol = :"Tests #{success ? "PASS" : "FAIL"}"
|
38
|
+
# heredoc = <<~TEXT
|
39
|
+
# Tests #{success ? "PASS" : "FAIL"}
|
40
|
+
# TEXT
|
41
|
+
# regexp = /Tests #{success ? "PASS" : "FAIL"}/
|
22
42
|
class StringLiteralsInInterpolation < Base
|
23
43
|
include ConfigurableEnforcedStyle
|
24
44
|
include StringLiteralsHelp
|
@@ -29,6 +49,11 @@ module RuboCop
|
|
29
49
|
StringLiteralCorrector.correct(corrector, node, style)
|
30
50
|
end
|
31
51
|
|
52
|
+
# Cop classes that include the StringHelp module usually ignore regexp
|
53
|
+
# nodes. Not so for this cop, which is why we override the on_regexp
|
54
|
+
# definition with an empty one.
|
55
|
+
def on_regexp(node); end
|
56
|
+
|
32
57
|
private
|
33
58
|
|
34
59
|
def message(_node)
|
@@ -22,6 +22,15 @@ module RuboCop
|
|
22
22
|
# # bad
|
23
23
|
# [:foo, :bar, :baz]
|
24
24
|
#
|
25
|
+
# # bad (contains spaces)
|
26
|
+
# %i[foo\ bar baz\ quux]
|
27
|
+
#
|
28
|
+
# # bad (contains [] with spaces)
|
29
|
+
# %i[foo \[ \]]
|
30
|
+
#
|
31
|
+
# # bad (contains () with spaces)
|
32
|
+
# %i(foo \( \))
|
33
|
+
#
|
25
34
|
# @example EnforcedStyle: brackets
|
26
35
|
# # good
|
27
36
|
# [:foo, :bar, :baz]
|
@@ -40,6 +49,15 @@ module RuboCop
|
|
40
49
|
|
41
50
|
PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
|
42
51
|
ARRAY_MSG = 'Use %<prefer>s for an array of symbols.'
|
52
|
+
DELIMITERS = ['[', ']', '(', ')'].freeze
|
53
|
+
SPECIAL_GVARS = %w[
|
54
|
+
$! $" $$ $& $' $* $+ $, $/ $; $: $. $< $= $> $? $@ $\\ $_ $` $~ $0
|
55
|
+
$-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w
|
56
|
+
].freeze
|
57
|
+
REDEFINABLE_OPERATORS = %w(
|
58
|
+
| ^ & <=> == === =~ > >= < <= << >>
|
59
|
+
+ - * / % ** ~ +@ -@ [] []= ` ! != !~
|
60
|
+
).freeze
|
43
61
|
|
44
62
|
class << self
|
45
63
|
attr_accessor :largest_brackets
|
@@ -47,7 +65,7 @@ module RuboCop
|
|
47
65
|
|
48
66
|
def on_array(node)
|
49
67
|
if bracketed_array_of?(:sym, node)
|
50
|
-
return if
|
68
|
+
return if complex_content?(node)
|
51
69
|
|
52
70
|
check_bracketed_array(node, 'i')
|
53
71
|
elsif node.percent_literal?(:symbol)
|
@@ -57,13 +75,24 @@ module RuboCop
|
|
57
75
|
|
58
76
|
private
|
59
77
|
|
60
|
-
def
|
78
|
+
def complex_content?(node)
|
61
79
|
node.children.any? do |sym|
|
62
|
-
|
63
|
-
|
80
|
+
return false if DELIMITERS.include?(sym.source)
|
81
|
+
|
82
|
+
content = *sym
|
83
|
+
content = content.map { |c| c.is_a?(AST::Node) ? c.source : c }.join
|
84
|
+
content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s\(\)]*\))/, '')
|
85
|
+
|
86
|
+
content.include?(' ') || DELIMITERS.any? do |delimiter|
|
87
|
+
content_without_delimiter_pairs.include?(delimiter)
|
88
|
+
end
|
64
89
|
end
|
65
90
|
end
|
66
91
|
|
92
|
+
def invalid_percent_array_contents?(node)
|
93
|
+
complex_content?(node)
|
94
|
+
end
|
95
|
+
|
67
96
|
def build_bracketed_array(node)
|
68
97
|
return '[]' if node.children.empty?
|
69
98
|
|
@@ -88,15 +117,6 @@ module RuboCop
|
|
88
117
|
end
|
89
118
|
|
90
119
|
def symbol_without_quote?(string)
|
91
|
-
special_gvars = %w[
|
92
|
-
$! $" $$ $& $' $* $+ $, $/ $; $: $. $< $= $> $? $@ $\\ $_ $` $~ $0
|
93
|
-
$-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w
|
94
|
-
]
|
95
|
-
redefinable_operators = %w(
|
96
|
-
| ^ & <=> == === =~ > >= < <= << >>
|
97
|
-
+ - * / % ** ~ +@ -@ [] []= ` ! != !~
|
98
|
-
)
|
99
|
-
|
100
120
|
# method name
|
101
121
|
/\A[a-zA-Z_]\w*[!?]?\z/.match?(string) ||
|
102
122
|
# instance / class variable
|
@@ -104,8 +124,8 @@ module RuboCop
|
|
104
124
|
# global variable
|
105
125
|
/\A\$[1-9]\d*\z/.match?(string) ||
|
106
126
|
/\A\$[a-zA-Z_]\w*\z/.match?(string) ||
|
107
|
-
|
108
|
-
|
127
|
+
SPECIAL_GVARS.include?(string) ||
|
128
|
+
REDEFINABLE_OPERATORS.include?(string)
|
109
129
|
end
|
110
130
|
end
|
111
131
|
end
|
@@ -118,16 +118,18 @@ module RuboCop
|
|
118
118
|
node.comparison_method? && !noncommutative_operator?(node)
|
119
119
|
end
|
120
120
|
|
121
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
121
122
|
def valid_yoda?(node)
|
122
|
-
|
123
|
-
rhs = node.first_argument
|
123
|
+
return true unless (rhs = node.first_argument)
|
124
124
|
|
125
|
+
lhs = node.receiver
|
125
126
|
return true if (constant_portion?(lhs) && constant_portion?(rhs)) ||
|
126
127
|
(!constant_portion?(lhs) && !constant_portion?(rhs)) ||
|
127
128
|
interpolation?(lhs)
|
128
129
|
|
129
130
|
enforce_yoda? ? constant_portion?(lhs) : constant_portion?(rhs)
|
130
131
|
end
|
132
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
131
133
|
|
132
134
|
def message(node)
|
133
135
|
format(MSG, source: node.source)
|
@@ -19,22 +19,23 @@ module RuboCop
|
|
19
19
|
# differently on different classes, and are not guaranteed to
|
20
20
|
# have the same result if reversed.
|
21
21
|
#
|
22
|
-
# @example SupportedOperators: ['*', '+', '&'']
|
22
|
+
# @example SupportedOperators: ['*', '+', '&', '|', '^'] (default)
|
23
23
|
# # bad
|
24
|
-
# 1 + x
|
25
24
|
# 10 * y
|
25
|
+
# 1 + x
|
26
26
|
# 1 & z
|
27
|
+
# 1 | x
|
28
|
+
# 1 ^ x
|
27
29
|
# 1 + CONST
|
28
30
|
#
|
29
31
|
# # good
|
30
|
-
# 60 * 24
|
31
|
-
# x + 1
|
32
32
|
# y * 10
|
33
|
+
# x + 1
|
33
34
|
# z & 1
|
35
|
+
# x | 1
|
36
|
+
# x ^ 1
|
34
37
|
# CONST + 1
|
35
|
-
#
|
36
|
-
# # good
|
37
|
-
# 1 | x
|
38
|
+
# 60 * 24
|
38
39
|
#
|
39
40
|
class YodaExpression < Base
|
40
41
|
extend AutoCorrector
|