rubocop 1.11.0 → 1.12.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 +2 -2
- data/config/default.yml +16 -1
- data/lib/rubocop.rb +1 -0
- data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
- data/lib/rubocop/comment_config.rb +43 -94
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
- data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
- data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
- data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
- data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
- data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
- data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
- data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
- data/lib/rubocop/cop/layout/indentation_width.rb +19 -9
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +7 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
- data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +89 -2
- data/lib/rubocop/cop/mixin/alignment.rb +10 -3
- data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -3
- data/lib/rubocop/cop/mixin/preferred_delimiters.rb +1 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +4 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
- data/lib/rubocop/cop/registry.rb +9 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -1
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
- data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
- data/lib/rubocop/cop/style/case_like_if.rb +15 -4
- data/lib/rubocop/cop/style/class_equality_comparison.rb +2 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/documentation.rb +25 -3
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +40 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -2
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +15 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +26 -3
- data/lib/rubocop/cop/style/redundant_return.rb +4 -0
- data/lib/rubocop/cop/style/redundant_self.rb +7 -3
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +16 -0
- data/lib/rubocop/cop/style/string_chars.rb +38 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +2 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +8 -2
- data/lib/rubocop/directive_comment.rb +64 -9
- data/lib/rubocop/ext/regexp_parser.rb +3 -6
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/target_finder.rb +1 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +5 -3
@@ -21,13 +21,16 @@ module RuboCop
|
|
21
21
|
#
|
22
22
|
# The indentation of the remaining lines can be corrected with
|
23
23
|
# other cops such as `IndentationConsistency` and `EndAlignment`.
|
24
|
-
class AssignmentIndentation <
|
24
|
+
class AssignmentIndentation < Base
|
25
25
|
include CheckAssignment
|
26
26
|
include Alignment
|
27
|
+
extend AutoCorrector
|
27
28
|
|
28
29
|
MSG = 'Indent the first line of the right-hand-side of a ' \
|
29
30
|
'multi-line assignment.'
|
30
31
|
|
32
|
+
private
|
33
|
+
|
31
34
|
def check_assignment(node, rhs)
|
32
35
|
return unless rhs
|
33
36
|
return unless node.loc.operator
|
@@ -37,8 +40,8 @@ module RuboCop
|
|
37
40
|
check_alignment([rhs], base + configured_indentation_width)
|
38
41
|
end
|
39
42
|
|
40
|
-
def autocorrect(node)
|
41
|
-
AlignmentCorrector.correct(processed_source, node, column_delta)
|
43
|
+
def autocorrect(corrector, node)
|
44
|
+
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
42
45
|
end
|
43
46
|
|
44
47
|
def leftmost_multiple_assignment(node)
|
@@ -24,8 +24,9 @@ module RuboCop
|
|
24
24
|
# blah { |i|
|
25
25
|
# foo(i)
|
26
26
|
# }
|
27
|
-
class BlockEndNewline <
|
27
|
+
class BlockEndNewline < Base
|
28
28
|
include Alignment
|
29
|
+
extend AutoCorrector
|
29
30
|
|
30
31
|
MSG = 'Expression at %<line>d, %<column>d should be on its own line.'
|
31
32
|
|
@@ -35,13 +36,8 @@ module RuboCop
|
|
35
36
|
# If the end is on its own line, there is no offense
|
36
37
|
return if begins_its_line?(node.loc.end)
|
37
38
|
|
38
|
-
add_offense(node,
|
39
|
-
|
40
|
-
|
41
|
-
def autocorrect(node)
|
42
|
-
lambda do |corrector|
|
43
|
-
corrector.replace(delimiter_range(node),
|
44
|
-
"\n#{node.loc.end.source}#{offset(node)}")
|
39
|
+
add_offense(node.loc.end, message: message(node)) do |corrector|
|
40
|
+
corrector.replace(delimiter_range(node), "\n#{node.loc.end.source}#{offset(node)}")
|
45
41
|
end
|
46
42
|
end
|
47
43
|
|
@@ -68,8 +68,9 @@ module RuboCop
|
|
68
68
|
# )
|
69
69
|
#
|
70
70
|
#
|
71
|
-
class ClosingParenthesisIndentation <
|
71
|
+
class ClosingParenthesisIndentation < Base
|
72
72
|
include Alignment
|
73
|
+
extend AutoCorrector
|
73
74
|
|
74
75
|
MSG_INDENT = 'Indent `)` to column %<expected>d (not %<actual>d)'
|
75
76
|
|
@@ -89,12 +90,12 @@ module RuboCop
|
|
89
90
|
end
|
90
91
|
alias on_defs on_def
|
91
92
|
|
92
|
-
def autocorrect(node)
|
93
|
-
AlignmentCorrector.correct(processed_source, node, @column_delta)
|
94
|
-
end
|
95
|
-
|
96
93
|
private
|
97
94
|
|
95
|
+
def autocorrect(corrector, node)
|
96
|
+
AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
|
97
|
+
end
|
98
|
+
|
98
99
|
def check(node, elements)
|
99
100
|
if elements.empty?
|
100
101
|
check_for_no_elements(node)
|
@@ -115,11 +116,10 @@ module RuboCop
|
|
115
116
|
|
116
117
|
return if @column_delta.zero?
|
117
118
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
right_paren))
|
119
|
+
message = message(correct_column, left_paren, right_paren)
|
120
|
+
add_offense(right_paren, message: message) do |corrector|
|
121
|
+
autocorrect(corrector, right_paren)
|
122
|
+
end
|
123
123
|
end
|
124
124
|
|
125
125
|
def check_for_no_elements(node)
|
@@ -135,11 +135,10 @@ module RuboCop
|
|
135
135
|
# select the first one of candidates to determine a specification.
|
136
136
|
correct_column = candidates.first
|
137
137
|
@column_delta = correct_column - right_paren.column
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
right_paren))
|
138
|
+
message = message(correct_column, left_paren, right_paren)
|
139
|
+
add_offense(right_paren, message: message) do |corrector|
|
140
|
+
autocorrect(corrector, right_paren)
|
141
|
+
end
|
143
142
|
end
|
144
143
|
|
145
144
|
def expected_column(left_paren, elements)
|
@@ -32,29 +32,30 @@ module RuboCop
|
|
32
32
|
# true
|
33
33
|
# end
|
34
34
|
#
|
35
|
-
class CommentIndentation <
|
35
|
+
class CommentIndentation < Base
|
36
36
|
include Alignment
|
37
|
+
extend AutoCorrector
|
37
38
|
|
38
39
|
MSG = 'Incorrect indentation detected (column %<column>d ' \
|
39
40
|
'instead of %<correct_comment_indentation>d).'
|
40
41
|
|
41
|
-
def
|
42
|
+
def on_new_investigation
|
42
43
|
processed_source.comments.each { |comment| check(comment) }
|
43
44
|
end
|
44
45
|
|
45
|
-
def autocorrect(comment)
|
46
|
-
corrections = autocorrect_preceding_comments(comment) <<
|
47
|
-
autocorrect_one(comment)
|
48
|
-
->(corrector) { corrections.each { |corr| corr.call(corrector) } }
|
49
|
-
end
|
50
|
-
|
51
46
|
private
|
52
47
|
|
48
|
+
def autocorrect(corrector, comment)
|
49
|
+
autocorrect_preceding_comments(corrector, comment)
|
50
|
+
|
51
|
+
autocorrect_one(corrector, comment)
|
52
|
+
end
|
53
|
+
|
53
54
|
# Corrects all comment lines that occur immediately before the given
|
54
55
|
# comment and have the same indentation. This is to avoid a long chain
|
55
56
|
# of correcting, saving the file, parsing and inspecting again, and
|
56
57
|
# then correcting one more line, and so on.
|
57
|
-
def autocorrect_preceding_comments(comment)
|
58
|
+
def autocorrect_preceding_comments(corrector, comment)
|
58
59
|
comments = processed_source.comments
|
59
60
|
index = comments.index(comment)
|
60
61
|
|
@@ -62,7 +63,7 @@ module RuboCop
|
|
62
63
|
.reverse_each
|
63
64
|
.each_cons(2)
|
64
65
|
.take_while { |below, above| should_correct?(above, below) }
|
65
|
-
.map { |_, above| autocorrect_one(above) }
|
66
|
+
.map { |_, above| autocorrect_one(corrector, above) }
|
66
67
|
end
|
67
68
|
|
68
69
|
def should_correct?(preceding_comment, reference_comment)
|
@@ -71,8 +72,8 @@ module RuboCop
|
|
71
72
|
loc.line == ref_loc.line - 1 && loc.column == ref_loc.column
|
72
73
|
end
|
73
74
|
|
74
|
-
def autocorrect_one(comment)
|
75
|
-
AlignmentCorrector.correct(processed_source, comment, @column_delta)
|
75
|
+
def autocorrect_one(corrector, comment)
|
76
|
+
AlignmentCorrector.correct(corrector, processed_source, comment, @column_delta)
|
76
77
|
end
|
77
78
|
|
78
79
|
def check(comment)
|
@@ -93,10 +94,9 @@ module RuboCop
|
|
93
94
|
return if column == correct_comment_indentation
|
94
95
|
end
|
95
96
|
|
96
|
-
add_offense(
|
97
|
-
comment
|
98
|
-
|
99
|
-
)
|
97
|
+
add_offense(comment, message: message(column, correct_comment_indentation)) do |corrector|
|
98
|
+
autocorrect(corrector, comment)
|
99
|
+
end
|
100
100
|
end
|
101
101
|
|
102
102
|
def message(column, correct_comment_indentation)
|
@@ -29,10 +29,11 @@ module RuboCop
|
|
29
29
|
# else
|
30
30
|
# code
|
31
31
|
# end
|
32
|
-
class ElseAlignment <
|
32
|
+
class ElseAlignment < Base
|
33
33
|
include EndKeywordAlignment
|
34
34
|
include Alignment
|
35
35
|
include CheckAssignment
|
36
|
+
extend AutoCorrector
|
36
37
|
|
37
38
|
MSG = 'Align `%<else_range>s` with `%<base_range>s`.'
|
38
39
|
|
@@ -67,12 +68,12 @@ module RuboCop
|
|
67
68
|
)
|
68
69
|
end
|
69
70
|
|
70
|
-
def autocorrect(node)
|
71
|
-
AlignmentCorrector.correct(processed_source, node, column_delta)
|
72
|
-
end
|
73
|
-
|
74
71
|
private
|
75
72
|
|
73
|
+
def autocorrect(corrector, node)
|
74
|
+
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
75
|
+
end
|
76
|
+
|
76
77
|
def check_nested(node, base)
|
77
78
|
on_if(node, base)
|
78
79
|
ignore_node(node)
|
@@ -140,7 +141,9 @@ module RuboCop
|
|
140
141
|
else_range: else_range.source,
|
141
142
|
base_range: base_range.source[/^\S*/]
|
142
143
|
)
|
143
|
-
add_offense(else_range,
|
144
|
+
add_offense(else_range, message: message) do |corrector|
|
145
|
+
autocorrect(corrector, else_range)
|
146
|
+
end
|
144
147
|
end
|
145
148
|
|
146
149
|
def assignment_node(node)
|
@@ -144,10 +144,11 @@ module RuboCop
|
|
144
144
|
# nested_first_param),
|
145
145
|
# second_param
|
146
146
|
#
|
147
|
-
class FirstArgumentIndentation <
|
147
|
+
class FirstArgumentIndentation < Base
|
148
148
|
include Alignment
|
149
149
|
include ConfigurableEnforcedStyle
|
150
150
|
include RangeHelp
|
151
|
+
extend AutoCorrector
|
151
152
|
|
152
153
|
MSG = 'Indent the first argument one step more than %<base>s.'
|
153
154
|
|
@@ -161,12 +162,12 @@ module RuboCop
|
|
161
162
|
end
|
162
163
|
alias on_csend on_send
|
163
164
|
|
164
|
-
def autocorrect(node)
|
165
|
-
AlignmentCorrector.correct(processed_source, node, column_delta)
|
166
|
-
end
|
167
|
-
|
168
165
|
private
|
169
166
|
|
167
|
+
def autocorrect(corrector, node)
|
168
|
+
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
169
|
+
end
|
170
|
+
|
170
171
|
def bare_operator?(node)
|
171
172
|
node.operator_method? && !node.dot?
|
172
173
|
end
|
@@ -79,10 +79,11 @@ module RuboCop
|
|
79
79
|
# and_now_for_something = [
|
80
80
|
# :completely_different
|
81
81
|
# ]
|
82
|
-
class FirstArrayElementIndentation <
|
82
|
+
class FirstArrayElementIndentation < Base
|
83
83
|
include Alignment
|
84
84
|
include ConfigurableEnforcedStyle
|
85
85
|
include MultilineElementIndentation
|
86
|
+
extend AutoCorrector
|
86
87
|
|
87
88
|
MSG = 'Use %<configured_indentation_width>d spaces for indentation ' \
|
88
89
|
'in an array, relative to %<base_description>s.'
|
@@ -98,12 +99,12 @@ module RuboCop
|
|
98
99
|
end
|
99
100
|
alias on_csend on_send
|
100
101
|
|
101
|
-
def autocorrect(node)
|
102
|
-
AlignmentCorrector.correct(processed_source, node, @column_delta)
|
103
|
-
end
|
104
|
-
|
105
102
|
private
|
106
103
|
|
104
|
+
def autocorrect(corrector, node)
|
105
|
+
AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
|
106
|
+
end
|
107
|
+
|
107
108
|
def brace_alignment_style
|
108
109
|
:align_brackets
|
109
110
|
end
|
@@ -132,7 +133,9 @@ module RuboCop
|
|
132
133
|
return if @column_delta.zero?
|
133
134
|
|
134
135
|
msg = msg(left_parenthesis)
|
135
|
-
add_offense(right_bracket,
|
136
|
+
add_offense(right_bracket, message: msg) do |corrector|
|
137
|
+
autocorrect(corrector, right_bracket)
|
138
|
+
end
|
136
139
|
end
|
137
140
|
|
138
141
|
# Returns the description of what the correct indentation is based on.
|
@@ -77,10 +77,11 @@ module RuboCop
|
|
77
77
|
# and_now_for_something = {
|
78
78
|
# completely: :different
|
79
79
|
# }
|
80
|
-
class FirstHashElementIndentation <
|
80
|
+
class FirstHashElementIndentation < Base
|
81
81
|
include Alignment
|
82
82
|
include ConfigurableEnforcedStyle
|
83
83
|
include MultilineElementIndentation
|
84
|
+
extend AutoCorrector
|
84
85
|
|
85
86
|
MSG = 'Use %<configured_indentation_width>d spaces for indentation ' \
|
86
87
|
'in a hash, relative to %<base_description>s.'
|
@@ -96,12 +97,12 @@ module RuboCop
|
|
96
97
|
end
|
97
98
|
alias on_csend on_send
|
98
99
|
|
99
|
-
def autocorrect(node)
|
100
|
-
AlignmentCorrector.correct(processed_source, node, @column_delta)
|
101
|
-
end
|
102
|
-
|
103
100
|
private
|
104
101
|
|
102
|
+
def autocorrect(corrector, node)
|
103
|
+
AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
|
104
|
+
end
|
105
|
+
|
105
106
|
def brace_alignment_style
|
106
107
|
:align_braces
|
107
108
|
end
|
@@ -134,16 +135,10 @@ module RuboCop
|
|
134
135
|
@column_delta = expected_column - right_brace.column
|
135
136
|
return if @column_delta.zero?
|
136
137
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
'after the preceding left parenthesis.'
|
142
|
-
else
|
143
|
-
'Indent the right brace the same as the start of the line ' \
|
144
|
-
'where the left brace is.'
|
145
|
-
end
|
146
|
-
add_offense(right_brace, location: right_brace, message: msg)
|
138
|
+
message = message_for_right_brace(left_parenthesis)
|
139
|
+
add_offense(right_brace, message: message) do |corrector|
|
140
|
+
autocorrect(corrector, right_brace)
|
141
|
+
end
|
147
142
|
end
|
148
143
|
|
149
144
|
def separator_style?(first_pair)
|
@@ -178,6 +173,18 @@ module RuboCop
|
|
178
173
|
base_description: base_description
|
179
174
|
)
|
180
175
|
end
|
176
|
+
|
177
|
+
def message_for_right_brace(left_parenthesis)
|
178
|
+
if style == :align_braces
|
179
|
+
'Indent the right brace the same as the left brace.'
|
180
|
+
elsif style == :special_inside_parentheses && left_parenthesis
|
181
|
+
'Indent the right brace the same as the first position ' \
|
182
|
+
'after the preceding left parenthesis.'
|
183
|
+
else
|
184
|
+
'Indent the right brace the same as the start of the line ' \
|
185
|
+
'where the left brace is.'
|
186
|
+
end
|
187
|
+
end
|
181
188
|
end
|
182
189
|
end
|
183
190
|
end
|
@@ -41,10 +41,11 @@ module RuboCop
|
|
41
41
|
# second_param)
|
42
42
|
# 123
|
43
43
|
# end
|
44
|
-
class FirstParameterIndentation <
|
44
|
+
class FirstParameterIndentation < Base
|
45
45
|
include Alignment
|
46
46
|
include ConfigurableEnforcedStyle
|
47
47
|
include MultilineElementIndentation
|
48
|
+
extend AutoCorrector
|
48
49
|
|
49
50
|
MSG = 'Use %<configured_indentation_width>d spaces for indentation ' \
|
50
51
|
'in method args, relative to %<base_description>s.'
|
@@ -57,12 +58,12 @@ module RuboCop
|
|
57
58
|
end
|
58
59
|
alias on_defs on_def
|
59
60
|
|
60
|
-
def autocorrect(node)
|
61
|
-
AlignmentCorrector.correct(processed_source, node, @column_delta)
|
62
|
-
end
|
63
|
-
|
64
61
|
private
|
65
62
|
|
63
|
+
def autocorrect(corrector, node)
|
64
|
+
AlignmentCorrector.correct(corrector, processed_source, node, @column_delta)
|
65
|
+
end
|
66
|
+
|
66
67
|
def brace_alignment_style
|
67
68
|
:align_parentheses
|
68
69
|
end
|
@@ -118,9 +118,10 @@ module RuboCop
|
|
118
118
|
# def bar
|
119
119
|
# end
|
120
120
|
# end
|
121
|
-
class IndentationConsistency <
|
121
|
+
class IndentationConsistency < Base
|
122
122
|
include Alignment
|
123
123
|
include ConfigurableEnforcedStyle
|
124
|
+
extend AutoCorrector
|
124
125
|
|
125
126
|
MSG = 'Inconsistent indentation detected.'
|
126
127
|
|
@@ -132,12 +133,12 @@ module RuboCop
|
|
132
133
|
check(node)
|
133
134
|
end
|
134
135
|
|
135
|
-
def autocorrect(node)
|
136
|
-
AlignmentCorrector.correct(processed_source, node, column_delta)
|
137
|
-
end
|
138
|
-
|
139
136
|
private
|
140
137
|
|
138
|
+
def autocorrect(corrector, node)
|
139
|
+
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
140
|
+
end
|
141
|
+
|
141
142
|
# Not all nodes define `bare_access_modifier?` (for example,
|
142
143
|
# `RuboCop::AST::DefNode` does not), so we must check `send_type?` first
|
143
144
|
# to avoid a NoMethodError.
|
@@ -161,8 +162,10 @@ module RuboCop
|
|
161
162
|
# to the level of the module (see `AccessModifierIndentation` cop) we
|
162
163
|
# return nil so that `check_alignment` will derive the correct
|
163
164
|
# indentation from the first child that is not an access modifier.
|
164
|
-
module_indent = display_column(node.parent.source_range)
|
165
165
|
access_modifier_indent = display_column(first_child.source_range)
|
166
|
+
return access_modifier_indent unless node.parent
|
167
|
+
|
168
|
+
module_indent = display_column(node.parent.source_range)
|
166
169
|
access_modifier_indent if access_modifier_indent > module_indent
|
167
170
|
end
|
168
171
|
|
@@ -31,60 +31,57 @@ module RuboCop
|
|
31
31
|
# def foo
|
32
32
|
# bar
|
33
33
|
# end
|
34
|
-
class IndentationStyle <
|
34
|
+
class IndentationStyle < Base
|
35
35
|
include Alignment
|
36
36
|
include ConfigurableEnforcedStyle
|
37
37
|
include RangeHelp
|
38
|
+
extend AutoCorrector
|
38
39
|
|
39
40
|
MSG = '%<type>s detected in indentation.'
|
40
41
|
|
41
|
-
def
|
42
|
+
def on_new_investigation
|
42
43
|
str_ranges = string_literal_ranges(processed_source.ast)
|
43
44
|
|
44
45
|
processed_source.lines.each.with_index(1) do |line, lineno|
|
45
|
-
|
46
|
-
next unless match
|
47
|
-
|
48
|
-
range = source_range(processed_source.buffer,
|
49
|
-
lineno,
|
50
|
-
match.begin(0)...match.end(0))
|
46
|
+
next unless (range = find_offence(line, lineno))
|
51
47
|
next if in_string_literal?(str_ranges, range)
|
52
48
|
|
53
|
-
add_offense(range
|
49
|
+
add_offense(range) do |corrector|
|
50
|
+
autocorrect(corrector, range)
|
51
|
+
end
|
54
52
|
end
|
55
53
|
end
|
56
54
|
|
57
|
-
|
55
|
+
private
|
56
|
+
|
57
|
+
def autocorrect(corrector, range)
|
58
58
|
if range.source.include?("\t")
|
59
|
-
autocorrect_lambda_for_tabs(range)
|
59
|
+
autocorrect_lambda_for_tabs(corrector, range)
|
60
60
|
else
|
61
|
-
autocorrect_lambda_for_spaces(range)
|
61
|
+
autocorrect_lambda_for_spaces(corrector, range)
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
|
65
|
+
def find_offence(line, lineno)
|
66
|
+
match = if style == :spaces
|
67
|
+
line.match(/\A\s*\t+/)
|
68
|
+
else
|
69
|
+
line.match(/\A\s* +/)
|
70
|
+
end
|
71
|
+
return unless match
|
66
72
|
|
67
|
-
|
68
|
-
if style == :spaces
|
69
|
-
line.match(/\A\s*\t+/)
|
70
|
-
else
|
71
|
-
line.match(/\A\s* +/)
|
72
|
-
end
|
73
|
+
source_range(processed_source.buffer, lineno, match.begin(0)...match.end(0))
|
73
74
|
end
|
74
75
|
|
75
|
-
def autocorrect_lambda_for_tabs(range)
|
76
|
-
|
77
|
-
|
78
|
-
corrector.replace(range, range.source.gsub(/\t/, spaces))
|
79
|
-
end
|
76
|
+
def autocorrect_lambda_for_tabs(corrector, range)
|
77
|
+
spaces = ' ' * configured_indentation_width
|
78
|
+
corrector.replace(range, range.source.gsub(/\t/, spaces))
|
80
79
|
end
|
81
80
|
|
82
|
-
def autocorrect_lambda_for_spaces(range)
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end)
|
87
|
-
end
|
81
|
+
def autocorrect_lambda_for_spaces(corrector, range)
|
82
|
+
corrector.replace(range, range.source.gsub(/\A\s+/) do |match|
|
83
|
+
"\t" * (match.size / configured_indentation_width)
|
84
|
+
end)
|
88
85
|
end
|
89
86
|
|
90
87
|
def in_string_literal?(ranges, tabs_range)
|