rubocop 1.18.4 → 1.21.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +65 -13
- data/lib/rubocop/cli.rb +18 -0
- data/lib/rubocop/config_loader.rb +2 -2
- data/lib/rubocop/config_loader_resolver.rb +21 -6
- data/lib/rubocop/cop/base.rb +2 -2
- data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +12 -11
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
- data/lib/rubocop/cop/documentation.rb +1 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/class_structure.rb +2 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +7 -3
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
- data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +33 -14
- data/lib/rubocop/cop/layout/line_length.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +22 -9
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +15 -4
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -0
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +8 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +107 -0
- data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
- data/lib/rubocop/cop/lint/debugger.rb +2 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +67 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +7 -1
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -3
- data/lib/rubocop/cop/lint/useless_times.rb +1 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
- data/lib/rubocop/cop/mixin/code_length.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +19 -1
- data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/percent_array.rb +13 -7
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +9 -1
- data/lib/rubocop/cop/mixin/require_library.rb +59 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +0 -3
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +27 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +2 -2
- data/lib/rubocop/cop/style/and_or.rb +4 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +0 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +39 -6
- data/lib/rubocop/cop/style/case_equality.rb +6 -9
- data/lib/rubocop/cop/style/collection_methods.rb +2 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +25 -39
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +23 -8
- data/lib/rubocop/cop/style/double_negation.rb +12 -1
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/encoding.rb +26 -15
- data/lib/rubocop/cop/style/explicit_block_argument.rb +46 -11
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
- data/lib/rubocop/cop/style/hash_except.rb +4 -3
- data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +3 -2
- data/lib/rubocop/cop/style/lambda_call.rb +1 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -6
- data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
- data/lib/rubocop/cop/style/missing_else.rb +7 -0
- data/lib/rubocop/cop/style/mutable_constant.rb +68 -6
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_unless.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
- data/lib/rubocop/cop/style/not.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
- data/lib/rubocop/cop/style/redundant_freeze.rb +4 -3
- data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +19 -4
- data/lib/rubocop/cop/style/regexp_literal.rb +3 -3
- data/lib/rubocop/cop/style/return_nil.rb +2 -1
- data/lib/rubocop/cop/style/semicolon.rb +32 -24
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
- data/lib/rubocop/cop/style/static_class.rb +1 -2
- data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
- data/lib/rubocop/cop/style/symbol_array.rb +3 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +23 -5
- data/lib/rubocop/cop/style/yoda_condition.rb +4 -7
- data/lib/rubocop/cop/util.rb +7 -2
- data/lib/rubocop/magic_comment.rb +44 -15
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/runner.rb +1 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +9 -2
- metadata +14 -5
@@ -12,6 +12,7 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# Auto-correction removes comments from `end` keyword and keeps comments
|
14
14
|
# for `class`, `module`, `def` and `begin` above the keyword.
|
15
|
+
# It is marked as unsafe auto-correction as it may remove meaningful comments.
|
15
16
|
#
|
16
17
|
# @example
|
17
18
|
# # bad
|
@@ -50,7 +51,7 @@ module RuboCop
|
|
50
51
|
|
51
52
|
def on_new_investigation
|
52
53
|
processed_source.comments.each do |comment|
|
53
|
-
next unless (match = line(comment).match(/(?<keyword>\S+).*#/))
|
54
|
+
next unless offensive?(comment) && (match = line(comment).match(/(?<keyword>\S+).*#/))
|
54
55
|
|
55
56
|
register_offense(comment, match[:keyword])
|
56
57
|
end
|
@@ -26,7 +26,7 @@ module RuboCop
|
|
26
26
|
# `when` nodes contain the entire branch including the condition.
|
27
27
|
# We only need the contents of the branch, not the condition.
|
28
28
|
def expand_when_branches(when_branches)
|
29
|
-
when_branches.map
|
29
|
+
when_branches.map(&:body)
|
30
30
|
end
|
31
31
|
|
32
32
|
def tail(branch)
|
@@ -272,6 +272,16 @@ module RuboCop
|
|
272
272
|
check_node(node, branches)
|
273
273
|
end
|
274
274
|
|
275
|
+
def on_case_match(node)
|
276
|
+
return unless style == :assign_to_condition
|
277
|
+
return unless node.else_branch
|
278
|
+
|
279
|
+
in_pattern_branches = expand_when_branches(node.in_pattern_branches)
|
280
|
+
branches = [*in_pattern_branches, node.else_branch]
|
281
|
+
|
282
|
+
check_node(node, branches)
|
283
|
+
end
|
284
|
+
|
275
285
|
private
|
276
286
|
|
277
287
|
def check_assignment_to_condition(node)
|
@@ -297,7 +307,7 @@ module RuboCop
|
|
297
307
|
end
|
298
308
|
|
299
309
|
# @!method candidate_condition?(node)
|
300
|
-
def_node_matcher :candidate_condition?, '[{if case} !#allowed_ternary?]'
|
310
|
+
def_node_matcher :candidate_condition?, '[{if case case_match} !#allowed_ternary?]'
|
301
311
|
|
302
312
|
def allowed_ternary?(assignment)
|
303
313
|
assignment.if_type? && assignment.ternary? && !include_ternary?
|
@@ -319,7 +329,7 @@ module RuboCop
|
|
319
329
|
end
|
320
330
|
|
321
331
|
def move_assignment_outside_condition(corrector, node)
|
322
|
-
if node.case_type?
|
332
|
+
if node.case_type? || node.case_match_type?
|
323
333
|
CaseCorrector.correct(corrector, self, node)
|
324
334
|
elsif node.ternary?
|
325
335
|
TernaryCorrector.correct(corrector, node)
|
@@ -333,7 +343,7 @@ module RuboCop
|
|
333
343
|
|
334
344
|
if ternary_condition?(condition)
|
335
345
|
TernaryCorrector.move_assignment_inside_condition(corrector, node)
|
336
|
-
elsif condition.case_type?
|
346
|
+
elsif condition.case_type? || condition.case_match_type?
|
337
347
|
CaseCorrector.move_assignment_inside_condition(corrector, node)
|
338
348
|
elsif condition.if_type?
|
339
349
|
IfCorrector.move_assignment_inside_condition(corrector, node)
|
@@ -631,7 +641,11 @@ module RuboCop
|
|
631
641
|
end
|
632
642
|
|
633
643
|
def extract_branches(case_node)
|
634
|
-
when_branches =
|
644
|
+
when_branches = if case_node.case_type?
|
645
|
+
expand_when_branches(case_node.when_branches)
|
646
|
+
else
|
647
|
+
expand_when_branches(case_node.in_pattern_branches)
|
648
|
+
end
|
635
649
|
|
636
650
|
[when_branches, case_node.else_branch]
|
637
651
|
end
|
@@ -86,7 +86,7 @@ module RuboCop
|
|
86
86
|
|
87
87
|
return unless arg_node&.dstr_type? && interpolated?(arg_node)
|
88
88
|
return if inline_comment_docs?(arg_node) ||
|
89
|
-
arg_node.heredoc? && comment_block_docs?(arg_node)
|
89
|
+
(arg_node.heredoc? && comment_block_docs?(arg_node))
|
90
90
|
|
91
91
|
add_offense(node.loc.selector)
|
92
92
|
end
|
@@ -71,8 +71,9 @@ module RuboCop
|
|
71
71
|
#
|
72
72
|
class Documentation < Base
|
73
73
|
include DocumentationComment
|
74
|
+
include RangeHelp
|
74
75
|
|
75
|
-
MSG = 'Missing top-level
|
76
|
+
MSG = 'Missing top-level documentation comment for `%<type>s %<identifier>s`.'
|
76
77
|
|
77
78
|
# @!method constant_definition?(node)
|
78
79
|
def_node_matcher :constant_definition?, '{class module casgn}'
|
@@ -88,33 +89,35 @@ module RuboCop
|
|
88
89
|
def on_class(node)
|
89
90
|
return unless node.body
|
90
91
|
|
91
|
-
check(node, node.body
|
92
|
+
check(node, node.body)
|
92
93
|
end
|
93
94
|
|
94
95
|
def on_module(node)
|
95
|
-
check(node, node.body
|
96
|
+
check(node, node.body)
|
96
97
|
end
|
97
98
|
|
98
99
|
private
|
99
100
|
|
100
|
-
def check(node, body
|
101
|
+
def check(node, body)
|
101
102
|
return if namespace?(body)
|
102
103
|
return if documentation_comment?(node)
|
103
104
|
return if constant_allowed?(node)
|
104
105
|
return if nodoc_self_or_outer_module?(node)
|
105
106
|
return if macro_only?(body)
|
106
107
|
|
107
|
-
|
108
|
+
range = range_between(node.loc.expression.begin_pos, node.loc.name.end_pos)
|
109
|
+
message = format(MSG, type: node.type, identifier: identifier(node))
|
110
|
+
add_offense(range, message: message)
|
108
111
|
end
|
109
112
|
|
110
113
|
def nodoc_self_or_outer_module?(node)
|
111
114
|
nodoc_comment?(node) ||
|
112
|
-
compact_namespace?(node) && nodoc_comment?(outer_module(node).first)
|
115
|
+
(compact_namespace?(node) && nodoc_comment?(outer_module(node).first))
|
113
116
|
end
|
114
117
|
|
115
118
|
def macro_only?(body)
|
116
|
-
body.respond_to?(:macro?) && body.macro? ||
|
117
|
-
body.respond_to?(:children) && body.children&.all? { |child| macro_only?(child) }
|
119
|
+
(body.respond_to?(:macro?) && body.macro?) ||
|
120
|
+
(body.respond_to?(:children) && body.children&.all? { |child| macro_only?(child) })
|
118
121
|
end
|
119
122
|
|
120
123
|
def namespace?(node)
|
@@ -165,6 +168,18 @@ module RuboCop
|
|
165
168
|
def allowed_constants
|
166
169
|
@allowed_constants ||= cop_config.fetch('AllowedConstants', []).map(&:intern)
|
167
170
|
end
|
171
|
+
|
172
|
+
def identifier(node)
|
173
|
+
# Get the fully qualified identifier for a class/module
|
174
|
+
nodes = [node, *node.each_ancestor(:class, :module)]
|
175
|
+
nodes.reverse_each.flat_map { |n| qualify_const(n.identifier) }.join('::')
|
176
|
+
end
|
177
|
+
|
178
|
+
def qualify_const(node)
|
179
|
+
return if node.nil?
|
180
|
+
|
181
|
+
[qualify_const(node.namespace), node.short_name].compact
|
182
|
+
end
|
168
183
|
end
|
169
184
|
end
|
170
185
|
end
|
@@ -62,7 +62,7 @@ module RuboCop
|
|
62
62
|
def end_of_method_definition?(node)
|
63
63
|
return false unless (def_node = find_def_node_from_ascendant(node))
|
64
64
|
|
65
|
-
last_child = def_node.
|
65
|
+
last_child = find_last_child(def_node.body)
|
66
66
|
|
67
67
|
last_child.last_line == node.last_line
|
68
68
|
end
|
@@ -73,6 +73,17 @@ module RuboCop
|
|
73
73
|
|
74
74
|
find_def_node_from_ascendant(node.parent)
|
75
75
|
end
|
76
|
+
|
77
|
+
def find_last_child(node)
|
78
|
+
case node.type
|
79
|
+
when :rescue
|
80
|
+
find_last_child(node.body)
|
81
|
+
when :ensure
|
82
|
+
find_last_child(node.child_nodes.first)
|
83
|
+
else
|
84
|
+
node.child_nodes.last
|
85
|
+
end
|
86
|
+
end
|
76
87
|
end
|
77
88
|
end
|
78
89
|
end
|
@@ -13,38 +13,49 @@ module RuboCop
|
|
13
13
|
include RangeHelp
|
14
14
|
extend AutoCorrector
|
15
15
|
|
16
|
-
|
16
|
+
MSG = 'Unnecessary utf-8 encoding comment.'
|
17
17
|
ENCODING_PATTERN = /#.*coding\s?[:=]\s?(?:UTF|utf)-8/.freeze
|
18
18
|
SHEBANG = '#!'
|
19
19
|
|
20
20
|
def on_new_investigation
|
21
21
|
return if processed_source.buffer.source.empty?
|
22
22
|
|
23
|
-
line_number
|
24
|
-
|
23
|
+
comments.each do |line_number, comment|
|
24
|
+
next unless offense?(comment)
|
25
25
|
|
26
|
-
|
27
|
-
add_offense(range, message: @message) do |corrector|
|
28
|
-
corrector.remove(range_with_surrounding_space(range: range, side: :right))
|
26
|
+
register_offense(line_number, comment)
|
29
27
|
end
|
30
28
|
end
|
31
29
|
|
32
30
|
private
|
33
31
|
|
34
|
-
def
|
35
|
-
line
|
32
|
+
def comments
|
33
|
+
processed_source.lines.each.with_index.with_object({}) do |(line, line_number), comments|
|
34
|
+
next if line.start_with?(SHEBANG)
|
36
35
|
|
37
|
-
|
36
|
+
comment = MagicComment.parse(line)
|
37
|
+
return comments unless comment.valid?
|
38
|
+
|
39
|
+
comments[line_number + 1] = comment
|
40
|
+
end
|
38
41
|
end
|
39
42
|
|
40
|
-
def
|
41
|
-
|
43
|
+
def offense?(comment)
|
44
|
+
comment.encoding_specified? && comment.encoding.casecmp('utf-8').zero?
|
42
45
|
end
|
43
46
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
def register_offense(line_number, comment)
|
48
|
+
range = processed_source.buffer.line_range(line_number)
|
49
|
+
|
50
|
+
add_offense(range) do |corrector|
|
51
|
+
text = comment.without(:encoding)
|
52
|
+
|
53
|
+
if text.blank?
|
54
|
+
corrector.remove(range_with_surrounding_space(range: range, side: :right))
|
55
|
+
else
|
56
|
+
corrector.replace(range, text)
|
57
|
+
end
|
58
|
+
end
|
48
59
|
end
|
49
60
|
end
|
50
61
|
end
|
@@ -67,17 +67,27 @@ module RuboCop
|
|
67
67
|
# so this can cause crashes in haml_lint
|
68
68
|
return unless def_node
|
69
69
|
|
70
|
+
block_name = extract_block_name(def_node)
|
71
|
+
|
70
72
|
add_offense(block_node) do |corrector|
|
71
73
|
corrector.remove(block_body_range(block_node, send_node))
|
72
74
|
|
73
|
-
add_block_argument(send_node, corrector)
|
74
|
-
add_block_argument(def_node, corrector) if @def_nodes.add?(def_node)
|
75
|
+
add_block_argument(send_node, corrector, block_name)
|
76
|
+
add_block_argument(def_node, corrector, block_name) if @def_nodes.add?(def_node)
|
75
77
|
end
|
76
78
|
end
|
77
79
|
end
|
78
80
|
|
79
81
|
private
|
80
82
|
|
83
|
+
def extract_block_name(def_node)
|
84
|
+
if def_node.block_argument?
|
85
|
+
def_node.arguments.last.name
|
86
|
+
else
|
87
|
+
'block'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
81
91
|
def yielding_arguments?(block_args, yield_args)
|
82
92
|
yield_args = yield_args.dup.fill(
|
83
93
|
nil,
|
@@ -91,20 +101,45 @@ module RuboCop
|
|
91
101
|
end
|
92
102
|
end
|
93
103
|
|
94
|
-
def add_block_argument(node, corrector)
|
104
|
+
def add_block_argument(node, corrector, block_name)
|
95
105
|
if node.arguments?
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
elsif node.call_type? || node.zsuper_type?
|
102
|
-
corrector.insert_after(node, '(&block)')
|
106
|
+
insert_argument(node, corrector, block_name)
|
107
|
+
elsif empty_arguments?(node)
|
108
|
+
corrector.replace(node.arguments, "(&#{block_name})")
|
109
|
+
elsif call_like?(node)
|
110
|
+
correct_call_node(node, corrector, block_name)
|
103
111
|
else
|
104
|
-
corrector.insert_after(node.loc.name,
|
112
|
+
corrector.insert_after(node.loc.name, "(&#{block_name})")
|
105
113
|
end
|
106
114
|
end
|
107
115
|
|
116
|
+
def empty_arguments?(node)
|
117
|
+
# Is there an arguments node with only parentheses?
|
118
|
+
node.arguments.is_a?(RuboCop::AST::Node) && node.arguments.loc.begin
|
119
|
+
end
|
120
|
+
|
121
|
+
def call_like?(node)
|
122
|
+
node.call_type? || node.zsuper_type? || node.super_type?
|
123
|
+
end
|
124
|
+
|
125
|
+
def insert_argument(node, corrector, block_name)
|
126
|
+
last_arg = node.arguments.last
|
127
|
+
arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
|
128
|
+
replacement = " &#{block_name}"
|
129
|
+
replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
|
130
|
+
corrector.insert_after(arg_range, replacement) unless last_arg.blockarg_type?
|
131
|
+
end
|
132
|
+
|
133
|
+
def correct_call_node(node, corrector, block_name)
|
134
|
+
corrector.insert_after(node, "(&#{block_name})")
|
135
|
+
return unless node.parenthesized?
|
136
|
+
|
137
|
+
args_begin = Util.args_begin(node)
|
138
|
+
args_end = Util.args_end(node)
|
139
|
+
range = range_between(args_begin.begin_pos, args_end.end_pos)
|
140
|
+
corrector.remove(range)
|
141
|
+
end
|
142
|
+
|
108
143
|
def block_body_range(block_node, send_node)
|
109
144
|
range_between(send_node.loc.expression.end_pos, block_node.loc.end.end_pos)
|
110
145
|
end
|
@@ -141,7 +141,7 @@ module RuboCop
|
|
141
141
|
|
142
142
|
def frozen_string_literal_comment(processed_source)
|
143
143
|
processed_source.find_token do |token|
|
144
|
-
token.text.start_with?(
|
144
|
+
token.text.start_with?(FROZEN_STRING_LITERAL)
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
@@ -29,6 +29,7 @@ module RuboCop
|
|
29
29
|
# # good
|
30
30
|
# [{ one: 1 }, { two: 2 }]
|
31
31
|
class HashAsLastArrayItem < Base
|
32
|
+
include RangeHelp
|
32
33
|
include ConfigurableEnforcedStyle
|
33
34
|
extend AutoCorrector
|
34
35
|
|
@@ -74,6 +75,7 @@ module RuboCop
|
|
74
75
|
return if node.children.empty? # Empty hash cannot be "unbraced"
|
75
76
|
|
76
77
|
add_offense(node, message: 'Omit the braces around the hash.') do |corrector|
|
78
|
+
remove_last_element_trailing_comma(corrector, node.parent)
|
77
79
|
corrector.remove(node.loc.begin)
|
78
80
|
corrector.remove(node.loc.end)
|
79
81
|
end
|
@@ -82,6 +84,15 @@ module RuboCop
|
|
82
84
|
def braces_style?
|
83
85
|
style == :braces
|
84
86
|
end
|
87
|
+
|
88
|
+
def remove_last_element_trailing_comma(corrector, node)
|
89
|
+
range = range_with_surrounding_space(
|
90
|
+
range: node.children.last.source_range,
|
91
|
+
side: :right
|
92
|
+
).end.resize(1)
|
93
|
+
|
94
|
+
corrector.remove(range) if range.source == ','
|
95
|
+
end
|
85
96
|
end
|
86
97
|
end
|
87
98
|
end
|
@@ -49,7 +49,7 @@ module RuboCop
|
|
49
49
|
return unless bad_method?(block) && semantically_except_method?(node, block)
|
50
50
|
|
51
51
|
except_key = except_key(block)
|
52
|
-
return
|
52
|
+
return if except_key.nil? || !safe_to_register_offense?(block, except_key)
|
53
53
|
|
54
54
|
range = offense_range(node)
|
55
55
|
preferred_method = "except(#{except_key.source})"
|
@@ -81,10 +81,11 @@ module RuboCop
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def except_key(node)
|
84
|
-
key_argument = node.argument_list.first
|
84
|
+
key_argument = node.argument_list.first.source
|
85
85
|
lhs, _method_name, rhs = *node.body
|
86
|
+
return if [lhs, rhs].map(&:source).none?(key_argument)
|
86
87
|
|
87
|
-
[lhs, rhs].find { |operand| operand.source != key_argument
|
88
|
+
[lhs, rhs].find { |operand| operand.source != key_argument }
|
88
89
|
end
|
89
90
|
|
90
91
|
def offense_range(node)
|
@@ -27,11 +27,8 @@ module RuboCop
|
|
27
27
|
# {a: 1, b: 2}.transform_keys { |k| k.to_s }
|
28
28
|
class HashTransformKeys < Base
|
29
29
|
include HashTransformMethod
|
30
|
-
extend TargetRubyVersion
|
31
30
|
extend AutoCorrector
|
32
31
|
|
33
|
-
minimum_target_ruby_version 2.5
|
34
|
-
|
35
32
|
# @!method on_bad_each_with_object(node)
|
36
33
|
def_node_matcher :on_bad_each_with_object, <<~PATTERN
|
37
34
|
(block
|
@@ -7,6 +7,22 @@ module RuboCop
|
|
7
7
|
# each branch of a conditional expression. Such expressions should normally
|
8
8
|
# be placed outside the conditional expression - before or after it.
|
9
9
|
#
|
10
|
+
# This cop is marked unsafe auto-correction as the order of method invocations
|
11
|
+
# must be guaranteed in the following case:
|
12
|
+
#
|
13
|
+
# [source,ruby]
|
14
|
+
# ----
|
15
|
+
# if method_that_modifies_global_state # 1
|
16
|
+
# method_that_relies_on_global_state # 2
|
17
|
+
# foo # 3
|
18
|
+
# else
|
19
|
+
# method_that_relies_on_global_state # 2
|
20
|
+
# bar # 3
|
21
|
+
# end
|
22
|
+
# ----
|
23
|
+
#
|
24
|
+
# In such a case, auto-correction may change the invocation order.
|
25
|
+
#
|
10
26
|
# NOTE: The cop is poorly named and some people might think that it actually
|
11
27
|
# checks for duplicated conditional branches. The name will probably be changed
|
12
28
|
# in a future major RuboCop release.
|
@@ -124,21 +140,30 @@ module RuboCop
|
|
124
140
|
return if branches.any?(&:nil?)
|
125
141
|
|
126
142
|
tails = branches.map { |branch| tail(branch) }
|
127
|
-
check_expressions(node, tails, :after_condition) if duplicated_expressions?(tails)
|
143
|
+
check_expressions(node, tails, :after_condition) if duplicated_expressions?(node, tails)
|
128
144
|
|
129
145
|
heads = branches.map { |branch| head(branch) }
|
130
|
-
check_expressions(node, heads, :before_condition) if duplicated_expressions?(heads)
|
146
|
+
check_expressions(node, heads, :before_condition) if duplicated_expressions?(node, heads)
|
131
147
|
end
|
132
148
|
|
133
|
-
def duplicated_expressions?(expressions)
|
134
|
-
|
149
|
+
def duplicated_expressions?(node, expressions)
|
150
|
+
unique_expressions = expressions.uniq
|
151
|
+
return false unless expressions.size >= 1 && unique_expressions.one?
|
152
|
+
|
153
|
+
unique_expression = unique_expressions.first
|
154
|
+
return true unless unique_expression.assignment?
|
155
|
+
|
156
|
+
lhs = unique_expression.child_nodes.first
|
157
|
+
node.condition.child_nodes.none? { |n| n.source == lhs.source if n.variable? }
|
135
158
|
end
|
136
159
|
|
137
|
-
def check_expressions(node, expressions, insert_position)
|
160
|
+
def check_expressions(node, expressions, insert_position) # rubocop:disable Metrics/MethodLength
|
138
161
|
inserted_expression = false
|
139
162
|
|
140
163
|
expressions.each do |expression|
|
141
164
|
add_offense(expression) do |corrector|
|
165
|
+
next if node.if_type? && node.ternary?
|
166
|
+
|
142
167
|
range = range_by_whole_lines(expression.source_range, include_final_newline: true)
|
143
168
|
corrector.remove(range)
|
144
169
|
next if inserted_expression
|