rubocop 1.14.0 → 1.18.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 +3 -1
- data/config/default.yml +120 -34
- data/lib/rubocop.rb +8 -0
- data/lib/rubocop/cli/command/suggest_extensions.rb +3 -3
- data/lib/rubocop/config_loader.rb +1 -1
- data/lib/rubocop/config_validator.rb +5 -5
- data/lib/rubocop/cop/base.rb +2 -2
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
- data/lib/rubocop/cop/bundler/gem_version.rb +38 -4
- data/lib/rubocop/cop/corrector.rb +4 -4
- data/lib/rubocop/cop/generator.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
- data/lib/rubocop/cop/layout/argument_alignment.rb +30 -12
- data/lib/rubocop/cop/layout/array_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +57 -9
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +7 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +13 -15
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +14 -2
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +40 -14
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +13 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +122 -0
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +2 -2
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/parameter_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/redundant_line_break.rb +11 -9
- data/lib/rubocop/cop/layout/space_around_keyword.rb +28 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +7 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -0
- data/lib/rubocop/cop/lint/empty_block.rb +18 -2
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +62 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +13 -1
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +32 -17
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +105 -74
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +5 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -13
- data/lib/rubocop/cop/lint/unused_block_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/migration/department_name.rb +3 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +28 -3
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +6 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +14 -3
- data/lib/rubocop/cop/mixin/string_literals_help.rb +2 -4
- data/lib/rubocop/cop/mixin/symbol_help.rb +13 -0
- data/lib/rubocop/cop/naming/inclusive_language.rb +249 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +2 -2
- data/lib/rubocop/cop/style/class_and_module_children.rb +28 -2
- data/lib/rubocop/cop/style/empty_literal.rb +8 -1
- data/lib/rubocop/cop/style/hash_each_methods.rb +18 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +58 -8
- data/lib/rubocop/cop/style/in_pattern_then.rb +56 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +62 -0
- data/lib/rubocop/cop/style/multiline_when_then.rb +2 -11
- data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +29 -12
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/quoted_symbols.rb +110 -0
- data/lib/rubocop/cop/style/raise_args.rb +2 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self.rb +24 -2
- data/lib/rubocop/cop/style/regexp_literal.rb +10 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/string_concatenation.rb +32 -5
- data/lib/rubocop/cop/style/string_literals.rb +3 -2
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -0
- data/lib/rubocop/cop/style/swap_values.rb +1 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +83 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +65 -0
- data/lib/rubocop/cop/style/unpack_first.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +6 -2
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
- data/lib/rubocop/directive_comment.rb +58 -6
- data/lib/rubocop/formatter/junit_formatter.rb +21 -6
- data/lib/rubocop/options.rb +18 -24
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/remote_config.rb +10 -2
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +13 -5
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
|
20
20
|
puts
|
21
21
|
puts 'Tip: Based on detected gems, the following '\
|
22
|
-
|
22
|
+
'RuboCop extension libraries might be helpful:'
|
23
23
|
|
24
24
|
extensions.sort.each do |extension|
|
25
25
|
puts " * #{extension} (https://github.com/rubocop/#{extension})"
|
@@ -27,8 +27,8 @@ module RuboCop
|
|
27
27
|
|
28
28
|
puts
|
29
29
|
puts 'You can opt out of this message by adding the following to your config '\
|
30
|
-
|
31
|
-
|
30
|
+
'(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions '\
|
31
|
+
'for more options):'
|
32
32
|
puts ' AllCops:'
|
33
33
|
puts ' SuggestExtensions: false'
|
34
34
|
|
@@ -229,7 +229,7 @@ module RuboCop
|
|
229
229
|
line1 = key1.start_line + 1
|
230
230
|
line2 = key2.start_line + 1
|
231
231
|
"#{smart_path}:#{line1}: " \
|
232
|
-
|
232
|
+
"`#{value}` is concealed by line #{line2}"
|
233
233
|
else
|
234
234
|
"#{smart_path}: `#{value}` is concealed by duplicate"
|
235
235
|
end
|
@@ -81,11 +81,11 @@ module RuboCop
|
|
81
81
|
|
82
82
|
msg = if last_version
|
83
83
|
"RuboCop found unsupported Ruby version #{target_ruby_version} " \
|
84
|
-
|
85
|
-
|
84
|
+
"in #{source}. #{target_ruby_version}-compatible " \
|
85
|
+
"analysis was dropped after version #{last_version}."
|
86
86
|
else
|
87
87
|
'RuboCop found unknown Ruby version ' \
|
88
|
-
|
88
|
+
"#{target_ruby_version.inspect} in #{source}."
|
89
89
|
end
|
90
90
|
|
91
91
|
msg += "\nSupported versions: #{TargetRuby.supported_versions.join(', ')}"
|
@@ -179,8 +179,8 @@ module RuboCop
|
|
179
179
|
next if validate_support_and_has_list(name, style, valid)
|
180
180
|
|
181
181
|
msg = "invalid #{style_name} '#{style}' for #{name} found in " \
|
182
|
-
|
183
|
-
|
182
|
+
"#{smart_loaded_path}\n" \
|
183
|
+
"Valid choices are: #{valid.join(', ')}"
|
184
184
|
raise ValidationError, msg
|
185
185
|
end
|
186
186
|
end
|
data/lib/rubocop/cop/base.rb
CHANGED
@@ -261,7 +261,7 @@ module RuboCop
|
|
261
261
|
# @deprecated Make potential errors with previous API more obvious
|
262
262
|
def offenses
|
263
263
|
raise 'The offenses are not directly available; ' \
|
264
|
-
|
264
|
+
'they are returned as the result of the investigation'
|
265
265
|
end
|
266
266
|
|
267
267
|
### Reserved for Commissioner
|
@@ -441,7 +441,7 @@ module RuboCop
|
|
441
441
|
severity.to_sym
|
442
442
|
else
|
443
443
|
message = "Warning: Invalid severity '#{severity}'. " \
|
444
|
-
|
444
|
+
"Valid severities are #{Severity::NAMES.join(', ')}."
|
445
445
|
warn(Rainbow(message).red)
|
446
446
|
end
|
447
447
|
end
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
39
39
|
include RangeHelp
|
40
40
|
|
41
41
|
MSG = 'Gem `%<gem_name>s` requirements already given on line '\
|
42
|
-
|
42
|
+
'%<line_of_first_occurrence>d of the Gemfile.'
|
43
43
|
|
44
44
|
def on_new_investigation
|
45
45
|
return if processed_source.blank?
|
@@ -3,8 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Bundler
|
6
|
-
# Enforce that Gem version specifications
|
7
|
-
# or forbidden.
|
6
|
+
# Enforce that Gem version specifications or a commit reference (branch,
|
7
|
+
# ref, or tag) are either required or forbidden.
|
8
8
|
#
|
9
9
|
# @example EnforcedStyle: required (default)
|
10
10
|
# # bad
|
@@ -19,6 +19,15 @@ module RuboCop
|
|
19
19
|
# # good
|
20
20
|
# gem 'rubocop', '>= 1.5.0', '< 1.10.0'
|
21
21
|
#
|
22
|
+
# # good
|
23
|
+
# gem 'rubocop', branch: 'feature-branch'
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
|
27
|
+
#
|
28
|
+
# # good
|
29
|
+
# gem 'rubocop', tag: 'v1.17.0'
|
30
|
+
#
|
22
31
|
# @example EnforcedStyle: forbidden
|
23
32
|
# # good
|
24
33
|
# gem 'rubocop'
|
@@ -32,6 +41,15 @@ module RuboCop
|
|
32
41
|
# # bad
|
33
42
|
# gem 'rubocop', '>= 1.5.0', '< 1.10.0'
|
34
43
|
#
|
44
|
+
# # bad
|
45
|
+
# gem 'rubocop', branch: 'feature-branch'
|
46
|
+
#
|
47
|
+
# # bad
|
48
|
+
# gem 'rubocop', ref: '74b5bfbb2c4b6fd6cdbbc7254bd7084b36e0c85b'
|
49
|
+
#
|
50
|
+
# # bad
|
51
|
+
# gem 'rubocop', tag: 'v1.17.0'
|
52
|
+
#
|
35
53
|
class GemVersion < Base
|
36
54
|
include ConfigurableEnforcedStyle
|
37
55
|
include GemDeclaration
|
@@ -45,6 +63,11 @@ module RuboCop
|
|
45
63
|
(send nil? :gem <(str #version_specification?) ...>)
|
46
64
|
PATTERN
|
47
65
|
|
66
|
+
# @!method includes_commit_reference?(node)
|
67
|
+
def_node_matcher :includes_commit_reference?, <<~PATTERN
|
68
|
+
(send nil? :gem <(hash <(pair (sym {:branch :ref :tag}) (str _)) ...>) ...>)
|
69
|
+
PATTERN
|
70
|
+
|
48
71
|
def on_send(node)
|
49
72
|
return unless gem_declaration?(node)
|
50
73
|
return if allowed_gem?(node)
|
@@ -78,8 +101,19 @@ module RuboCop
|
|
78
101
|
end
|
79
102
|
|
80
103
|
def offense?(node)
|
81
|
-
(
|
82
|
-
|
104
|
+
required_offense?(node) || forbidden_offense?(node)
|
105
|
+
end
|
106
|
+
|
107
|
+
def required_offense?(node)
|
108
|
+
return unless required_style?
|
109
|
+
|
110
|
+
!includes_version_specification?(node) && !includes_commit_reference?(node)
|
111
|
+
end
|
112
|
+
|
113
|
+
def forbidden_offense?(node)
|
114
|
+
return unless forbidden_style?
|
115
|
+
|
116
|
+
includes_version_specification?(node) || includes_commit_reference?(node)
|
83
117
|
end
|
84
118
|
|
85
119
|
def forbidden_style?
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
|
33
33
|
# Removes `size` characters prior to the source range.
|
34
34
|
#
|
35
|
-
# @param [Parser::Source::Range,
|
35
|
+
# @param [Parser::Source::Range, RuboCop::AST::Node] range or node
|
36
36
|
# @param [Integer] size
|
37
37
|
def remove_preceding(node_or_range, size)
|
38
38
|
range = to_range(node_or_range)
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
# If `size` is greater than the size of `range`, the removed region can
|
45
45
|
# overrun the end of `range`.
|
46
46
|
#
|
47
|
-
# @param [Parser::Source::Range,
|
47
|
+
# @param [Parser::Source::Range, RuboCop::AST::Node] range or node
|
48
48
|
# @param [Integer] size
|
49
49
|
def remove_leading(node_or_range, size)
|
50
50
|
range = to_range(node_or_range)
|
@@ -56,7 +56,7 @@ module RuboCop
|
|
56
56
|
# If `size` is greater than the size of `range`, the removed region can
|
57
57
|
# overrun the beginning of `range`.
|
58
58
|
#
|
59
|
-
# @param [Parser::Source::Range,
|
59
|
+
# @param [Parser::Source::Range, RuboCop::AST::Node] range or node
|
60
60
|
# @param [Integer] size
|
61
61
|
def remove_trailing(node_or_range, size)
|
62
62
|
range = to_range(node_or_range)
|
@@ -90,7 +90,7 @@ module RuboCop
|
|
90
90
|
else
|
91
91
|
raise TypeError,
|
92
92
|
'Expected a Parser::Source::Range, Comment or ' \
|
93
|
-
"
|
93
|
+
"RuboCop::AST::Node, got #{node_or_range.class}"
|
94
94
|
end
|
95
95
|
validate_buffer(range.source_buffer)
|
96
96
|
range
|
@@ -104,7 +104,7 @@ module RuboCop
|
|
104
104
|
|
105
105
|
CONFIGURATION_ADDED_MESSAGE =
|
106
106
|
'[modify] A configuration for the cop is added into ' \
|
107
|
-
|
107
|
+
'%<configuration_file_path>s.'
|
108
108
|
|
109
109
|
def initialize(name, github_user, output: $stdout)
|
110
110
|
@badge = Badge.parse(name)
|
@@ -25,7 +25,7 @@ module RuboCop
|
|
25
25
|
|
26
26
|
MSG = 'Preceed `%<method>s` with a `@!method` YARD directive.'
|
27
27
|
MSG_WRONG_NAME = '`@!method` YARD directive has invalid method name, ' \
|
28
|
-
|
28
|
+
'use `%<expected>s` instead of `%<actual>s`.'
|
29
29
|
MSG_TOO_MANY = 'Multiple `@!method` YARD directives found for this matcher.'
|
30
30
|
|
31
31
|
RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].to_set.freeze
|
@@ -10,33 +10,39 @@ module RuboCop
|
|
10
10
|
# # good
|
11
11
|
#
|
12
12
|
# foo :bar,
|
13
|
-
# :baz
|
13
|
+
# :baz,
|
14
|
+
# key: value
|
14
15
|
#
|
15
16
|
# foo(
|
16
17
|
# :bar,
|
17
|
-
# :baz
|
18
|
+
# :baz,
|
19
|
+
# key: value
|
18
20
|
# )
|
19
21
|
#
|
20
22
|
# # bad
|
21
23
|
#
|
22
24
|
# foo :bar,
|
23
|
-
# :baz
|
25
|
+
# :baz,
|
26
|
+
# key: value
|
24
27
|
#
|
25
28
|
# foo(
|
26
29
|
# :bar,
|
27
|
-
# :baz
|
30
|
+
# :baz,
|
31
|
+
# key: value
|
28
32
|
# )
|
29
33
|
#
|
30
34
|
# @example EnforcedStyle: with_fixed_indentation
|
31
35
|
# # good
|
32
36
|
#
|
33
37
|
# foo :bar,
|
34
|
-
# :baz
|
38
|
+
# :baz,
|
39
|
+
# key: value
|
35
40
|
#
|
36
41
|
# # bad
|
37
42
|
#
|
38
43
|
# foo :bar,
|
39
|
-
# :baz
|
44
|
+
# :baz,
|
45
|
+
# key: value
|
40
46
|
class ArgumentAlignment < Base
|
41
47
|
include Alignment
|
42
48
|
extend AutoCorrector
|
@@ -44,17 +50,29 @@ module RuboCop
|
|
44
50
|
ALIGN_PARAMS_MSG = 'Align the arguments of a method call if they span more than one line.'
|
45
51
|
|
46
52
|
FIXED_INDENT_MSG = 'Use one level of indentation for arguments ' \
|
47
|
-
|
53
|
+
'following the first line of a multi-line method call.'
|
48
54
|
|
49
55
|
def on_send(node)
|
50
|
-
|
56
|
+
first_arg = node.first_argument
|
57
|
+
return if !multiple_arguments?(node, first_arg) || node.send_type? && node.method?(:[]=)
|
51
58
|
|
52
|
-
|
59
|
+
if first_arg.hash_type? && !first_arg.braces?
|
60
|
+
pairs = first_arg.pairs
|
61
|
+
check_alignment(pairs, base_column(node, pairs.first))
|
62
|
+
else
|
63
|
+
check_alignment(node.arguments, base_column(node, first_arg))
|
64
|
+
end
|
53
65
|
end
|
54
66
|
alias on_csend on_send
|
55
67
|
|
56
68
|
private
|
57
69
|
|
70
|
+
def multiple_arguments?(node, first_argument)
|
71
|
+
return true if node.arguments.size >= 2
|
72
|
+
|
73
|
+
first_argument&.hash_type? && first_argument.pairs.count >= 2
|
74
|
+
end
|
75
|
+
|
58
76
|
def autocorrect(corrector, node)
|
59
77
|
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
60
78
|
end
|
@@ -67,14 +85,14 @@ module RuboCop
|
|
67
85
|
cop_config['EnforcedStyle'] == 'with_fixed_indentation'
|
68
86
|
end
|
69
87
|
|
70
|
-
def base_column(node,
|
71
|
-
if fixed_indentation?
|
88
|
+
def base_column(node, first_argument)
|
89
|
+
if fixed_indentation? || first_argument.nil?
|
72
90
|
lineno = target_method_lineno(node)
|
73
91
|
line = node.source_range.source_buffer.source_line(lineno)
|
74
92
|
indentation_of_line = /\S.*/.match(line).begin(0)
|
75
93
|
indentation_of_line + configured_indentation_width
|
76
94
|
else
|
77
|
-
display_column(
|
95
|
+
display_column(first_argument.source_range)
|
78
96
|
end
|
79
97
|
end
|
80
98
|
|
@@ -38,10 +38,10 @@ module RuboCop
|
|
38
38
|
extend AutoCorrector
|
39
39
|
|
40
40
|
ALIGN_ELEMENTS_MSG = 'Align the elements of an array literal ' \
|
41
|
-
|
41
|
+
'if they span more than one line.'
|
42
42
|
|
43
43
|
FIXED_INDENT_MSG = 'Use one level of indentation for elements ' \
|
44
|
-
|
44
|
+
'following the first line of a multi-line array.'
|
45
45
|
|
46
46
|
def on_array(node)
|
47
47
|
return if node.children.size < 2
|
@@ -210,7 +210,7 @@ module RuboCop
|
|
210
210
|
|
211
211
|
def format_source_line_column(source_line_column)
|
212
212
|
"`#{source_line_column[:source]}` at #{source_line_column[:line]}, " \
|
213
|
-
|
213
|
+
"#{source_line_column[:column]}"
|
214
214
|
end
|
215
215
|
|
216
216
|
def compute_start_col(ancestor_node, node)
|
@@ -3,10 +3,10 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Layout
|
6
|
-
# This cop checks how the
|
6
|
+
# This cop checks how the `when` and `in`s of a `case` expression
|
7
7
|
# are indented in relation to its `case` or `end` keyword.
|
8
8
|
#
|
9
|
-
# It will register a separate offense for each misaligned `when`.
|
9
|
+
# It will register a separate offense for each misaligned `when` and `in`.
|
10
10
|
#
|
11
11
|
# @example
|
12
12
|
# # If Layout/EndAlignment is set to keyword style (default)
|
@@ -22,6 +22,13 @@ module RuboCop
|
|
22
22
|
# y / 3
|
23
23
|
# end
|
24
24
|
#
|
25
|
+
# case n
|
26
|
+
# in pattern
|
27
|
+
# x * 2
|
28
|
+
# else
|
29
|
+
# y / 3
|
30
|
+
# end
|
31
|
+
#
|
25
32
|
# # good for all styles
|
26
33
|
# case n
|
27
34
|
# when 0
|
@@ -30,6 +37,13 @@ module RuboCop
|
|
30
37
|
# y / 3
|
31
38
|
# end
|
32
39
|
#
|
40
|
+
# case n
|
41
|
+
# in pattern
|
42
|
+
# x * 2
|
43
|
+
# else
|
44
|
+
# y / 3
|
45
|
+
# end
|
46
|
+
#
|
33
47
|
# @example EnforcedStyle: case (default)
|
34
48
|
# # if EndAlignment is set to other style such as
|
35
49
|
# # start_of_line (as shown below), then *when* alignment
|
@@ -43,6 +57,13 @@ module RuboCop
|
|
43
57
|
# y / 3
|
44
58
|
# end
|
45
59
|
#
|
60
|
+
# a = case n
|
61
|
+
# in pattern
|
62
|
+
# x * 2
|
63
|
+
# else
|
64
|
+
# y / 3
|
65
|
+
# end
|
66
|
+
#
|
46
67
|
# # good
|
47
68
|
# a = case n
|
48
69
|
# when 0
|
@@ -51,6 +72,13 @@ module RuboCop
|
|
51
72
|
# y / 3
|
52
73
|
# end
|
53
74
|
#
|
75
|
+
# a = case n
|
76
|
+
# in pattern
|
77
|
+
# x * 2
|
78
|
+
# else
|
79
|
+
# y / 3
|
80
|
+
# end
|
81
|
+
#
|
54
82
|
# @example EnforcedStyle: end
|
55
83
|
# # bad
|
56
84
|
# a = case n
|
@@ -60,6 +88,13 @@ module RuboCop
|
|
60
88
|
# y / 3
|
61
89
|
# end
|
62
90
|
#
|
91
|
+
# a = case n
|
92
|
+
# in pattern
|
93
|
+
# x * 2
|
94
|
+
# else
|
95
|
+
# y / 3
|
96
|
+
# end
|
97
|
+
#
|
63
98
|
# # good
|
64
99
|
# a = case n
|
65
100
|
# when 0
|
@@ -67,30 +102,43 @@ module RuboCop
|
|
67
102
|
# else
|
68
103
|
# y / 3
|
69
104
|
# end
|
105
|
+
#
|
106
|
+
# a = case n
|
107
|
+
# in pattern
|
108
|
+
# x * 2
|
109
|
+
# else
|
110
|
+
# y / 3
|
111
|
+
# end
|
70
112
|
class CaseIndentation < Base
|
71
113
|
include Alignment
|
72
114
|
include ConfigurableEnforcedStyle
|
73
115
|
include RangeHelp
|
74
116
|
extend AutoCorrector
|
75
117
|
|
76
|
-
MSG = 'Indent `
|
118
|
+
MSG = 'Indent `%<branch_type>s` %<depth>s `%<base>s`.'
|
77
119
|
|
78
120
|
def on_case(case_node)
|
79
121
|
return if case_node.single_line?
|
80
122
|
|
81
|
-
case_node.each_when { |when_node| check_when(when_node) }
|
123
|
+
case_node.each_when { |when_node| check_when(when_node, 'when') }
|
124
|
+
end
|
125
|
+
|
126
|
+
def on_case_match(case_match_node)
|
127
|
+
return if case_match_node.single_line?
|
128
|
+
|
129
|
+
case_match_node.each_in_pattern { |in_pattern_node| check_when(in_pattern_node, 'in') }
|
82
130
|
end
|
83
131
|
|
84
132
|
private
|
85
133
|
|
86
|
-
def check_when(when_node)
|
134
|
+
def check_when(when_node, branch_type)
|
87
135
|
when_column = when_node.loc.keyword.column
|
88
136
|
base_column = base_column(when_node.parent, style)
|
89
137
|
|
90
138
|
if when_column == base_column + indentation_width
|
91
139
|
correct_style_detected
|
92
140
|
else
|
93
|
-
incorrect_style(when_node)
|
141
|
+
incorrect_style(when_node, branch_type)
|
94
142
|
end
|
95
143
|
end
|
96
144
|
|
@@ -102,9 +150,9 @@ module RuboCop
|
|
102
150
|
indent_one_step? ? configured_indentation_width : 0
|
103
151
|
end
|
104
152
|
|
105
|
-
def incorrect_style(when_node)
|
153
|
+
def incorrect_style(when_node, branch_type)
|
106
154
|
depth = indent_one_step? ? 'one step more than' : 'as deep as'
|
107
|
-
message = format(MSG, depth: depth, base: style)
|
155
|
+
message = format(MSG, branch_type: branch_type, depth: depth, base: style)
|
108
156
|
|
109
157
|
add_offense(when_node.loc.keyword, message: message) do |corrector|
|
110
158
|
detect_incorrect_style(when_node)
|
@@ -141,7 +189,7 @@ module RuboCop
|
|
141
189
|
end
|
142
190
|
|
143
191
|
def replacement(node)
|
144
|
-
case_node = node.each_ancestor(:case).first
|
192
|
+
case_node = node.each_ancestor(:case, :case_match).first
|
145
193
|
base_type = cop_config[style_parameter_name] == 'end' ? :end : :case
|
146
194
|
|
147
195
|
column = base_column(case_node, base_type)
|