rubocop 1.18.3 → 1.19.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/README.md +1 -1
- data/config/default.yml +22 -5
- data/lib/rubocop.rb +4 -0
- data/lib/rubocop/cli.rb +18 -0
- data/lib/rubocop/config_loader_resolver.rb +21 -6
- data/lib/rubocop/config_validator.rb +18 -5
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- 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/layout/class_structure.rb +5 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +10 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +33 -14
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +21 -9
- data/lib/rubocop/cop/layout/space_around_operators.rb +8 -1
- 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/trailing_whitespace.rb +24 -1
- 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/duplicate_branch.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +6 -1
- data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +10 -7
- 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/inclusive_language.rb +18 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +16 -0
- 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/double_cop_disable_directive.rb +1 -7
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
- 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/method_def_parentheses.rb +10 -1
- data/lib/rubocop/cop/style/missing_else.rb +7 -0
- data/lib/rubocop/cop/style/mutable_constant.rb +6 -8
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +88 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- 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/single_line_methods.rb +14 -9
- data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
- data/lib/rubocop/cop/style/word_array.rb +20 -2
- data/lib/rubocop/cop/util.rb +7 -2
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +11 -5
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for ambiguous ranges.
|
7
|
+
#
|
8
|
+
# Ranges have quite low precedence, which leads to unexpected behaviour when
|
9
|
+
# using a range with other operators. This cop avoids that by making ranges
|
10
|
+
# explicit by requiring parenthesis around complex range boundaries (anything
|
11
|
+
# that is not a basic literal: numerics, strings, symbols, etc.).
|
12
|
+
#
|
13
|
+
# NOTE: The cop auto-corrects by wrapping the entire boundary in parentheses, which
|
14
|
+
# makes the outcome more explicit but is possible to not be the intention of the
|
15
|
+
# programmer. For this reason, this cop's auto-correct is marked as unsafe (it
|
16
|
+
# will not change the behaviour of the code, but will not necessarily match the
|
17
|
+
# intent of the program).
|
18
|
+
#
|
19
|
+
# This cop can be configured with `RequireParenthesesForMethodChains` in order to
|
20
|
+
# specify whether method chains (including `self.foo`) should be wrapped in parens
|
21
|
+
# by this cop.
|
22
|
+
#
|
23
|
+
# NOTE: Regardless of this configuration, if a method receiver is a basic literal
|
24
|
+
# value, it will be wrapped in order to prevent the ambiguity of `1..2.to_a`.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# # bad
|
28
|
+
# x || 1..2
|
29
|
+
# (x || 1..2)
|
30
|
+
# 1..2.to_a
|
31
|
+
#
|
32
|
+
# # good, unambiguous
|
33
|
+
# 1..2
|
34
|
+
# 'a'..'z'
|
35
|
+
# :bar..:baz
|
36
|
+
# MyClass::MIN..MyClass::MAX
|
37
|
+
# @min..@max
|
38
|
+
# a..b
|
39
|
+
# -a..b
|
40
|
+
#
|
41
|
+
# # good, ambiguity removed
|
42
|
+
# x || (1..2)
|
43
|
+
# (x || 1)..2
|
44
|
+
# (x || 1)..(y || 2)
|
45
|
+
# (1..2).to_a
|
46
|
+
#
|
47
|
+
# @example RequireParenthesesForMethodChains: false (default)
|
48
|
+
# # good
|
49
|
+
# a.foo..b.bar
|
50
|
+
# (a.foo)..(b.bar)
|
51
|
+
#
|
52
|
+
# @example RequireParenthesesForMethodChains: true
|
53
|
+
# # bad
|
54
|
+
# a.foo..b.bar
|
55
|
+
#
|
56
|
+
# # good
|
57
|
+
# (a.foo)..(b.bar)
|
58
|
+
#
|
59
|
+
class AmbiguousRange < Base
|
60
|
+
extend AutoCorrector
|
61
|
+
|
62
|
+
MSG = 'Wrap complex range boundaries with parentheses to avoid ambiguity.'
|
63
|
+
|
64
|
+
def on_irange(node)
|
65
|
+
each_boundary(node) do |boundary|
|
66
|
+
next if acceptable?(boundary)
|
67
|
+
|
68
|
+
add_offense(boundary) do |corrector|
|
69
|
+
corrector.wrap(boundary, '(', ')')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
alias on_erange on_irange
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def each_boundary(range)
|
78
|
+
yield range.begin if range.begin
|
79
|
+
yield range.end if range.end
|
80
|
+
end
|
81
|
+
|
82
|
+
def acceptable?(node)
|
83
|
+
node.begin_type? ||
|
84
|
+
node.basic_literal? ||
|
85
|
+
node.variable? || node.const_type? ||
|
86
|
+
node.call_type? && acceptable_call?(node)
|
87
|
+
end
|
88
|
+
|
89
|
+
def acceptable_call?(node)
|
90
|
+
return true if node.unary_operation?
|
91
|
+
|
92
|
+
# Require parentheses when making a method call on a literal
|
93
|
+
# to avoid the ambiguity of `1..2.to_a`.
|
94
|
+
return false if node.receiver&.basic_literal?
|
95
|
+
|
96
|
+
require_parentheses_for_method_chain? || node.receiver.nil?
|
97
|
+
end
|
98
|
+
|
99
|
+
def require_parentheses_for_method_chain?
|
100
|
+
!cop_config['RequireParenthesesForMethodChains']
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -46,12 +46,11 @@ module RuboCop
|
|
46
46
|
node = processed_source.ast.each_node(:regexp).find do |regexp_node|
|
47
47
|
regexp_node.source_range.begin_pos == diagnostic.location.begin_pos
|
48
48
|
end
|
49
|
-
|
50
49
|
find_offense_node(node.parent, node)
|
51
50
|
end
|
52
51
|
|
53
52
|
def find_offense_node(node, regexp_receiver)
|
54
|
-
return node
|
53
|
+
return node if first_argument_is_regexp?(node) || !node.parent
|
55
54
|
|
56
55
|
if (node.parent.send_type? && node.receiver) ||
|
57
56
|
method_chain_to_regexp_receiver?(node, regexp_receiver)
|
@@ -61,6 +60,10 @@ module RuboCop
|
|
61
60
|
node
|
62
61
|
end
|
63
62
|
|
63
|
+
def first_argument_is_regexp?(node)
|
64
|
+
node.send_type? && node.first_argument&.regexp_type?
|
65
|
+
end
|
66
|
+
|
64
67
|
def method_chain_to_regexp_receiver?(node, regexp_receiver)
|
65
68
|
return false unless (parent = node.parent)
|
66
69
|
return false unless (parent_receiver = parent.receiver)
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
6
|
# This cop checks that there are no repeated bodies
|
7
|
-
# within `if/unless`, `case-when` and `rescue` constructs.
|
7
|
+
# within `if/unless`, `case-when`, `case-in` and `rescue` constructs.
|
8
8
|
#
|
9
9
|
# With `IgnoreLiteralBranches: true`, branches are not registered
|
10
10
|
# as offenses if they return a basic literal value (string, symbol,
|
@@ -97,6 +97,7 @@ module RuboCop
|
|
97
97
|
end
|
98
98
|
alias on_if on_branching_statement
|
99
99
|
alias on_case on_branching_statement
|
100
|
+
alias on_case_match on_branching_statement
|
100
101
|
alias on_rescue on_branching_statement
|
101
102
|
|
102
103
|
private
|
@@ -135,11 +135,14 @@ module RuboCop
|
|
135
135
|
def found_instance_method(node, name)
|
136
136
|
return unless (scope = node.parent_module_name)
|
137
137
|
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
138
|
+
# Humanize the scope
|
139
|
+
scope = scope.sub(
|
140
|
+
/(?:(?<name>.*)::)#<Class:\k<name>>|#<Class:(?<name>.*)>(?:::)?/,
|
141
|
+
'\k<name>.'
|
142
|
+
)
|
143
|
+
scope << '#' unless scope.end_with?('.')
|
144
|
+
|
145
|
+
found_method(node, "#{scope}#{name}")
|
143
146
|
end
|
144
147
|
|
145
148
|
def found_method(node, method_name)
|
@@ -97,9 +97,9 @@ module RuboCop
|
|
97
97
|
# If a send node contains a heredoc argument, splitting cannot happen
|
98
98
|
# after the heredoc or else it will cause a syntax error.
|
99
99
|
def shift_elements_for_heredoc_arg(node, elements, index)
|
100
|
-
return index unless node.send_type?
|
100
|
+
return index unless node.send_type? || node.array_type?
|
101
101
|
|
102
|
-
heredoc_index = elements.index { |arg|
|
102
|
+
heredoc_index = elements.index { |arg| arg.respond_to?(:heredoc?) && arg.heredoc? }
|
103
103
|
return index unless heredoc_index
|
104
104
|
return nil if heredoc_index.zero?
|
105
105
|
|
@@ -175,7 +175,12 @@ module RuboCop
|
|
175
175
|
end
|
176
176
|
|
177
177
|
def set_new_body_expression(transforming_body_expr, corrector)
|
178
|
-
|
178
|
+
body_source = transforming_body_expr.loc.expression.source
|
179
|
+
if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
|
180
|
+
body_source = "{ #{body_source} }"
|
181
|
+
end
|
182
|
+
|
183
|
+
corrector.replace(block_node.body, body_source)
|
179
184
|
end
|
180
185
|
end
|
181
186
|
end
|
@@ -20,6 +20,13 @@ module RuboCop
|
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
|
+
def indent_level(str)
|
24
|
+
indentations = str.lines
|
25
|
+
.map { |line| line[/^\s*/] }
|
26
|
+
.reject { |line| line.end_with?("\n") }
|
27
|
+
indentations.empty? ? 0 : indentations.min_by(&:size).size
|
28
|
+
end
|
29
|
+
|
23
30
|
def delimiter_string(node)
|
24
31
|
node.source.match(OPENING_DELIMITER).captures[1]
|
25
32
|
end
|
@@ -18,15 +18,16 @@ module RuboCop
|
|
18
18
|
!parent.parenthesized? && parent&.block_literal?
|
19
19
|
end
|
20
20
|
|
21
|
+
# Override to determine values that are invalid in a percent array
|
22
|
+
def invalid_percent_array_contents?(_node)
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
21
26
|
def allowed_bracket_array?(node)
|
22
27
|
comments_in_array?(node) || below_array_length?(node) ||
|
23
28
|
invalid_percent_array_context?(node)
|
24
29
|
end
|
25
30
|
|
26
|
-
def message(_node)
|
27
|
-
style == :percent ? self.class::PERCENT_MSG : self.class::ARRAY_MSG
|
28
|
-
end
|
29
|
-
|
30
31
|
def comments_in_array?(node)
|
31
32
|
line_span = node.source_range.first_line...node.source_range.last_line
|
32
33
|
processed_source.each_comment_in_lines(line_span).any?
|
@@ -35,9 +36,11 @@ module RuboCop
|
|
35
36
|
def check_percent_array(node)
|
36
37
|
array_style_detected(:percent, node.values.size)
|
37
38
|
|
38
|
-
return unless style == :brackets
|
39
|
+
return unless style == :brackets || invalid_percent_array_contents?(node)
|
39
40
|
|
40
|
-
add_offense(node)
|
41
|
+
add_offense(node, message: self.class::ARRAY_MSG) do |corrector|
|
42
|
+
correct_bracketed(corrector, node)
|
43
|
+
end
|
41
44
|
end
|
42
45
|
|
43
46
|
def check_bracketed_array(node, literal_prefix)
|
@@ -47,7 +50,7 @@ module RuboCop
|
|
47
50
|
|
48
51
|
return unless style == :percent
|
49
52
|
|
50
|
-
add_offense(node) do |corrector|
|
53
|
+
add_offense(node, message: self.class::PERCENT_MSG) do |corrector|
|
51
54
|
percent_literal_corrector = PercentLiteralCorrector.new(@config, @preferred_delimiters)
|
52
55
|
percent_literal_corrector.correct(corrector, node, literal_prefix)
|
53
56
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Ensure a require statement is present for a standard library determined
|
6
|
+
# by variable library_name
|
7
|
+
module RequireLibrary
|
8
|
+
extend NodePattern::Macros
|
9
|
+
|
10
|
+
def ensure_required(corrector, node, library_name)
|
11
|
+
node = node.parent while node.parent&.parent?
|
12
|
+
|
13
|
+
if node.parent&.begin_type?
|
14
|
+
return if @required_libs.include?(library_name)
|
15
|
+
|
16
|
+
remove_subsequent_requires(corrector, node, library_name)
|
17
|
+
end
|
18
|
+
|
19
|
+
RequireLibraryCorrector.correct(corrector, node, library_name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def remove_subsequent_requires(corrector, node, library_name)
|
23
|
+
node.right_siblings.each do |sibling|
|
24
|
+
next unless require_library_name?(sibling, library_name)
|
25
|
+
|
26
|
+
range = range_by_whole_lines(sibling.source_range, include_final_newline: true)
|
27
|
+
corrector.remove(range)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def on_send(node)
|
32
|
+
return if node.parent&.parent?
|
33
|
+
|
34
|
+
name = require_any_library?(node)
|
35
|
+
return if name.nil?
|
36
|
+
|
37
|
+
@required_libs.add(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def on_new_investigation
|
43
|
+
# Holds the required files at top-level
|
44
|
+
@required_libs = Set.new
|
45
|
+
super
|
46
|
+
end
|
47
|
+
|
48
|
+
# @!method require_any_library?(node)
|
49
|
+
def_node_matcher :require_any_library?, <<~PATTERN
|
50
|
+
(send {(const {nil? cbase} :Kernel) nil?} :require (str $_))
|
51
|
+
PATTERN
|
52
|
+
|
53
|
+
# @!method require_library_name?(node, library_name)
|
54
|
+
def_node_matcher :require_library_name?, <<~PATTERN
|
55
|
+
(send {(const {nil? cbase} :Kernel) nil?} :require (str %1))
|
56
|
+
PATTERN
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
MSG = 'Space found before %<token>s.'
|
11
11
|
|
12
12
|
def on_new_investigation
|
13
|
-
each_missing_space(processed_source.
|
13
|
+
each_missing_space(processed_source.sorted_tokens) do |token, pos_before|
|
14
14
|
add_offense(pos_before, message: format(MSG, token: kind(token))) do |corrector|
|
15
15
|
PunctuationCorrector.remove_space(corrector, pos_before)
|
16
16
|
end
|
@@ -19,6 +19,8 @@ module RuboCop
|
|
19
19
|
# Regex can be specified to identify offenses. Suggestions for replacing a flagged term can
|
20
20
|
# be configured and will be displayed as part of the offense message.
|
21
21
|
# An AllowedRegex can be specified for a flagged term to exempt allowed uses of the term.
|
22
|
+
# `WholeWord: true` can be set on a flagged term to indicate the cop should only match when
|
23
|
+
# a term matches the whole word (partial matches will not be offenses).
|
22
24
|
#
|
23
25
|
# @example FlaggedTerms: { whitelist: { Suggestions: ['allowlist'] } }
|
24
26
|
# # Suggest replacing identifier whitelist with allowlist
|
@@ -56,6 +58,14 @@ module RuboCop
|
|
56
58
|
# # good
|
57
59
|
# # They had a master's degree
|
58
60
|
#
|
61
|
+
# @example FlaggedTerms: { slave: { WholeWord: true } }
|
62
|
+
# # Specify that only terms that are full matches will be flagged.
|
63
|
+
#
|
64
|
+
# # bad
|
65
|
+
# Slave
|
66
|
+
#
|
67
|
+
# # good (won't be flagged despite containing `slave`)
|
68
|
+
# TeslaVehicle
|
59
69
|
class InclusiveLanguage < Base
|
60
70
|
include RangeHelp
|
61
71
|
|
@@ -123,7 +133,7 @@ module RuboCop
|
|
123
133
|
next if term_definition.nil?
|
124
134
|
|
125
135
|
allowed_strings.concat(process_allowed_regex(term_definition['AllowedRegex']))
|
126
|
-
regex_string = ensure_regex_string(term_definition
|
136
|
+
regex_string = ensure_regex_string(extract_regexp(term, term_definition))
|
127
137
|
flagged_term_strings << regex_string
|
128
138
|
|
129
139
|
add_to_flagged_term_hash(regex_string, term, term_definition)
|
@@ -132,6 +142,13 @@ module RuboCop
|
|
132
142
|
set_regexes(flagged_term_strings, allowed_strings)
|
133
143
|
end
|
134
144
|
|
145
|
+
def extract_regexp(term, term_definition)
|
146
|
+
return term_definition['Regex'] if term_definition['Regex']
|
147
|
+
return /(?:\b|(?<=[\W_]))#{term}(?:\b|(?=[\W_]))/ if term_definition['WholeWord']
|
148
|
+
|
149
|
+
term
|
150
|
+
end
|
151
|
+
|
135
152
|
def add_to_flagged_term_hash(regex_string, term, term_definition)
|
136
153
|
@flagged_term_hash[Regexp.new(regex_string, Regexp::IGNORECASE)] =
|
137
154
|
term_definition.merge('Term' => term,
|
@@ -6,6 +6,11 @@ module RuboCop
|
|
6
6
|
# Check for uses of braces or do/end around single line or
|
7
7
|
# multi-line blocks.
|
8
8
|
#
|
9
|
+
# Methods that can be either procedural or functional and cannot be
|
10
|
+
# categorised from their usage alone is ignored.
|
11
|
+
# `lambda`, `proc`, and `it` are their defaults.
|
12
|
+
# Additional methods can be added to the `IgnoredMethods`.
|
13
|
+
#
|
9
14
|
# @example EnforcedStyle: line_count_based (default)
|
10
15
|
# # bad - single line block
|
11
16
|
# items.each do |item| item / 5 end
|
@@ -132,6 +137,17 @@ module RuboCop
|
|
132
137
|
# puts foo
|
133
138
|
# end
|
134
139
|
#
|
140
|
+
# @example IgnoredMethods: ['lambda', 'proc', 'it' ] (default)
|
141
|
+
#
|
142
|
+
# # good
|
143
|
+
# foo = lambda do |x|
|
144
|
+
# puts "Hello, #{x}"
|
145
|
+
# end
|
146
|
+
#
|
147
|
+
# foo = lambda do |x|
|
148
|
+
# x * 100
|
149
|
+
# end
|
150
|
+
#
|
135
151
|
class BlockDelimiters < Base
|
136
152
|
include ConfigurableEnforcedStyle
|
137
153
|
include IgnoredMethods
|
@@ -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
|