rubocop 0.75.1 → 0.76.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 +52 -53
- data/lib/rubocop.rb +10 -9
- data/lib/rubocop/ast/builder.rb +1 -0
- data/lib/rubocop/ast/node.rb +4 -0
- data/lib/rubocop/ast/node/return_node.rb +24 -0
- data/lib/rubocop/cli.rb +7 -4
- data/lib/rubocop/comment_config.rb +2 -2
- data/lib/rubocop/config.rb +7 -0
- data/lib/rubocop/config_loader.rb +1 -1
- data/lib/rubocop/config_loader_resolver.rb +2 -1
- data/lib/rubocop/config_obsoletion.rb +9 -0
- data/lib/rubocop/config_validator.rb +24 -15
- data/lib/rubocop/cop/commissioner.rb +15 -7
- data/lib/rubocop/cop/cop.rb +10 -6
- data/lib/rubocop/cop/corrector.rb +8 -7
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/align_hash.rb +6 -2
- data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
- data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
- data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
- data/lib/rubocop/cop/layout/indent_first_argument.rb +9 -7
- data/lib/rubocop/cop/layout/indent_first_hash_element.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
- data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
- data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +23 -23
- data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +6 -8
- data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
- data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +5 -5
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
- data/lib/rubocop/cop/lint/void.rb +4 -4
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/line_length.rb +1 -4
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
- data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +5 -2
- data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -6
- data/lib/rubocop/cop/naming/file_name.rb +12 -5
- data/lib/rubocop/cop/registry.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +2 -2
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
- data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
- data/lib/rubocop/cop/style/copyright.rb +11 -7
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +2 -2
- data/lib/rubocop/cop/style/empty_method.rb +5 -5
- data/lib/rubocop/cop/style/format_string.rb +10 -7
- data/lib/rubocop/cop/style/format_string_token.rb +2 -0
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +9 -2
- data/lib/rubocop/cop/style/infinite_loop.rb +4 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
- data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
- data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
- data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
- data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
- data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
- data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
- data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
- data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +25 -21
- data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +4 -4
- data/lib/rubocop/cop/style/safe_navigation.rb +13 -10
- data/lib/rubocop/cop/style/semicolon.rb +2 -2
- data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +10 -18
- data/lib/rubocop/cop/variable_force.rb +7 -5
- data/lib/rubocop/node_pattern.rb +3 -1
- data/lib/rubocop/options.rb +12 -6
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/runner.rb +32 -27
- data/lib/rubocop/target_finder.rb +12 -6
- data/lib/rubocop/version.rb +1 -1
- metadata +12 -11
@@ -5,27 +5,39 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for non-nil checks, which are usually redundant.
|
7
7
|
#
|
8
|
-
#
|
8
|
+
# With `IncludeSemanticChanges` set to `false` by default, this cop
|
9
|
+
# does not report offenses for `!x.nil?` and does no changes that might
|
10
|
+
# change behavior.
|
11
|
+
#
|
12
|
+
# With `IncludeSemanticChanges` set to `true`, this cop reports offenses
|
13
|
+
# for `!x.nil?` and autocorrects that and `x != nil` to solely `x`, which
|
14
|
+
# is **usually** OK, but might change behavior.
|
9
15
|
#
|
16
|
+
# @example
|
10
17
|
# # bad
|
11
18
|
# if x != nil
|
12
19
|
# end
|
13
20
|
#
|
14
|
-
# # good
|
15
|
-
# # bad (when allowing semantic changes)
|
16
|
-
# if !x.nil?
|
17
|
-
# end
|
18
|
-
#
|
19
|
-
# # good (when allowing semantic changes)
|
21
|
+
# # good
|
20
22
|
# if x
|
21
23
|
# end
|
22
24
|
#
|
23
|
-
# Non-nil checks are allowed if they are the final nodes of predicate.
|
24
|
-
#
|
25
|
+
# # Non-nil checks are allowed if they are the final nodes of predicate.
|
25
26
|
# # good
|
26
27
|
# def signed_in?
|
27
28
|
# !current_user.nil?
|
28
29
|
# end
|
30
|
+
#
|
31
|
+
# @example IncludeSemanticChanges: false (default)
|
32
|
+
# # good
|
33
|
+
# if !x.nil?
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# @example IncludeSemanticChanges: true
|
37
|
+
# # bad
|
38
|
+
# if !x.nil?
|
39
|
+
# end
|
40
|
+
#
|
29
41
|
class NonNilCheck < Cop
|
30
42
|
def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
|
31
43
|
def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
|
@@ -30,11 +30,11 @@ module RuboCop
|
|
30
30
|
# c
|
31
31
|
# end
|
32
32
|
#
|
33
|
-
class
|
33
|
+
class RedundantCondition < Cop
|
34
34
|
include RangeHelp
|
35
35
|
|
36
36
|
MSG = 'Use double pipes `||` instead.'
|
37
|
-
|
37
|
+
REDUNDANT_CONDITION = 'This condition is not needed.'
|
38
38
|
|
39
39
|
def on_if(node)
|
40
40
|
return if node.elsif_conditional?
|
@@ -61,7 +61,7 @@ module RuboCop
|
|
61
61
|
|
62
62
|
def message(node)
|
63
63
|
if node.modifier_form? || !node.else_branch
|
64
|
-
|
64
|
+
REDUNDANT_CONDITION
|
65
65
|
else
|
66
66
|
MSG
|
67
67
|
end
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
# time = "8 o'clock"
|
18
18
|
# question = '"What did you say?"'
|
19
19
|
#
|
20
|
-
class
|
20
|
+
class RedundantPercentQ < Cop
|
21
21
|
MSG = 'Use `%<q_type>s` only for strings that contain both ' \
|
22
22
|
'single quotes and double quotes%<extra>s.'
|
23
23
|
DYNAMIC_MSG = ', or for dynamic strings that contain ' \
|
@@ -60,27 +60,38 @@ module RuboCop
|
|
60
60
|
end
|
61
61
|
alias on_defs on_def
|
62
62
|
|
63
|
-
def autocorrect(node)
|
63
|
+
def autocorrect(node)
|
64
64
|
lambda do |corrector|
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
if node.arguments?
|
66
|
+
correct_with_arguments(node, corrector)
|
67
|
+
else
|
68
|
+
correct_without_arguments(node, corrector)
|
68
69
|
end
|
69
|
-
|
70
|
-
return_value, = *node
|
71
|
-
if node.children.size > 1
|
72
|
-
add_brackets(corrector, node)
|
73
|
-
elsif return_value.hash_type?
|
74
|
-
add_braces(corrector, return_value) unless return_value.braces?
|
75
|
-
end
|
76
|
-
return_kw = range_with_surrounding_space(range: node.loc.keyword,
|
77
|
-
side: :right)
|
78
|
-
corrector.remove(return_kw)
|
79
70
|
end
|
80
71
|
end
|
81
72
|
|
82
73
|
private
|
83
74
|
|
75
|
+
def correct_without_arguments(return_node, corrector)
|
76
|
+
corrector.replace(return_node.source_range, 'nil')
|
77
|
+
end
|
78
|
+
|
79
|
+
def correct_with_arguments(return_node, corrector)
|
80
|
+
if return_node.arguments.size > 1
|
81
|
+
add_brackets(corrector, return_node)
|
82
|
+
elsif hash_without_braces?(return_node.first_argument)
|
83
|
+
add_braces(corrector, return_node.first_argument)
|
84
|
+
end
|
85
|
+
|
86
|
+
keyword = range_with_surrounding_space(range: return_node.loc.keyword,
|
87
|
+
side: :right)
|
88
|
+
corrector.remove(keyword)
|
89
|
+
end
|
90
|
+
|
91
|
+
def hash_without_braces?(node)
|
92
|
+
node.hash_type? && !node.braces?
|
93
|
+
end
|
94
|
+
|
84
95
|
def add_brackets(corrector, node)
|
85
96
|
kids = node.children.map(&:source_range)
|
86
97
|
corrector.insert_before(kids.first, '[')
|
@@ -93,13 +104,6 @@ module RuboCop
|
|
93
104
|
corrector.insert_after(kids.last, '}')
|
94
105
|
end
|
95
106
|
|
96
|
-
def arguments?(args)
|
97
|
-
return false if args.empty?
|
98
|
-
return true if args.size > 1
|
99
|
-
|
100
|
-
!args.first.begin_type? || !args.first.children.empty?
|
101
|
-
end
|
102
|
-
|
103
107
|
# rubocop:disable Metrics/CyclomaticComplexity
|
104
108
|
def check_branch(node)
|
105
109
|
return unless node
|
@@ -49,13 +49,13 @@ module RuboCop
|
|
49
49
|
# # good
|
50
50
|
# arr.max_by(&:foo)
|
51
51
|
#
|
52
|
-
class
|
52
|
+
class RedundantSort < Cop
|
53
53
|
include RangeHelp
|
54
54
|
|
55
55
|
MSG = 'Use `%<suggestion>s` instead of '\
|
56
56
|
'`%<sorter>s...%<accessor_source>s`.'
|
57
57
|
|
58
|
-
def_node_matcher :
|
58
|
+
def_node_matcher :redundant_sort?, <<~MATCHER
|
59
59
|
{
|
60
60
|
(send $(send _ $:sort ...) ${:last :first})
|
61
61
|
(send $(send _ $:sort ...) ${:[] :at :slice} {(int 0) (int -1)})
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
MATCHER
|
73
73
|
|
74
74
|
def on_send(node)
|
75
|
-
|
75
|
+
redundant_sort?(node) do |sort_node, sorter, accessor|
|
76
76
|
range = range_between(
|
77
77
|
sort_node.loc.selector.begin_pos,
|
78
78
|
node.loc.expression.end_pos
|
@@ -87,7 +87,7 @@ module RuboCop
|
|
87
87
|
end
|
88
88
|
|
89
89
|
def autocorrect(node)
|
90
|
-
sort_node, sorter, accessor =
|
90
|
+
sort_node, sorter, accessor = redundant_sort?(node)
|
91
91
|
|
92
92
|
lambda do |corrector|
|
93
93
|
# Remove accessor, e.g. `first` or `[-1]`.
|
@@ -112,9 +112,8 @@ module RuboCop
|
|
112
112
|
end
|
113
113
|
|
114
114
|
def autocorrect(node)
|
115
|
-
|
116
|
-
|
117
|
-
method_call, = matching_receiver.parent
|
115
|
+
body = node.node_parts[1]
|
116
|
+
method_call = method_call(node)
|
118
117
|
|
119
118
|
lambda do |corrector|
|
120
119
|
corrector.remove(begin_range(node, body))
|
@@ -129,25 +128,29 @@ module RuboCop
|
|
129
128
|
private
|
130
129
|
|
131
130
|
def handle_comments(corrector, node, method_call)
|
132
|
-
|
131
|
+
comments = comments(node)
|
132
|
+
return if comments.empty?
|
133
133
|
|
134
134
|
corrector.insert_before(method_call.loc.expression,
|
135
|
-
"#{comments(
|
135
|
+
"#{comments.map(&:text).join("\n")}\n")
|
136
136
|
end
|
137
137
|
|
138
138
|
def comments(node)
|
139
|
-
|
140
|
-
comment.loc.first_line
|
141
|
-
comment.loc.last_line
|
139
|
+
processed_source.comments.select do |comment|
|
140
|
+
comment.loc.first_line > node.loc.first_line &&
|
141
|
+
comment.loc.last_line < node.loc.last_line
|
142
142
|
end
|
143
|
-
|
144
|
-
comments.map(&:text)
|
145
143
|
end
|
146
144
|
|
147
145
|
def allowed_if_condition?(node)
|
148
146
|
node.else? || node.elsif? || node.ternary?
|
149
147
|
end
|
150
148
|
|
149
|
+
def method_call(node)
|
150
|
+
_checked_variable, matching_receiver, = extract_parts(node)
|
151
|
+
matching_receiver.parent
|
152
|
+
end
|
153
|
+
|
151
154
|
def extract_parts(node)
|
152
155
|
case node.type
|
153
156
|
when :if
|
@@ -47,8 +47,8 @@ module RuboCop
|
|
47
47
|
return if exprs.size < 2
|
48
48
|
|
49
49
|
# create a map matching lines to the number of expressions on them
|
50
|
-
exprs_lines = exprs.map
|
51
|
-
lines = exprs_lines.group_by
|
50
|
+
exprs_lines = exprs.map(&:first_line)
|
51
|
+
lines = exprs_lines.group_by(&:itself)
|
52
52
|
|
53
53
|
lines.each do |line, expr_on_line|
|
54
54
|
# Every line with more than one expression on it is a
|
@@ -160,17 +160,15 @@ module RuboCop
|
|
160
160
|
end
|
161
161
|
|
162
162
|
def format_message(english, regular, global)
|
163
|
-
if
|
163
|
+
if regular.empty?
|
164
|
+
format(MSG_ENGLISH, prefer: format_list(english), global: global)
|
165
|
+
elsif english.empty?
|
166
|
+
format(MSG_REGULAR, prefer: format_list(regular), global: global)
|
167
|
+
else
|
164
168
|
format(MSG_BOTH,
|
165
169
|
prefer: format_list(english),
|
166
170
|
regular: format_list(regular),
|
167
171
|
global: global)
|
168
|
-
elsif !regular.empty?
|
169
|
-
format(MSG_REGULAR, prefer: format_list(regular), global: global)
|
170
|
-
elsif !english.empty?
|
171
|
-
format(MSG_ENGLISH, prefer: format_list(english), global: global)
|
172
|
-
else
|
173
|
-
raise 'Bug in SpecialGlobalVars - global var w/o preferred vars!'
|
174
172
|
end
|
175
173
|
end
|
176
174
|
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# Match literal regex characters, not including anchors, character
|
10
10
|
# classes, alternatives, groups, repetitions, references, etc
|
11
11
|
LITERAL_REGEX =
|
12
|
-
|
12
|
+
%r{[\w\s\-,"'!#%&<>=;:`~/]|\\[^AbBdDgGhHkpPRwWXsSzZ0-9]}.freeze
|
13
13
|
|
14
14
|
module_function
|
15
15
|
|
@@ -44,15 +44,15 @@ module RuboCop
|
|
44
44
|
attr_reader :begin_pos, :end_pos
|
45
45
|
attr_reader :flags, :width, :precision, :name, :type
|
46
46
|
|
47
|
-
def initialize(
|
48
|
-
@source =
|
49
|
-
@begin_pos =
|
50
|
-
@end_pos =
|
51
|
-
@flags =
|
52
|
-
@width =
|
53
|
-
@precision =
|
54
|
-
@name =
|
55
|
-
@type =
|
47
|
+
def initialize(match)
|
48
|
+
@source = match[0]
|
49
|
+
@begin_pos = match.begin(0)
|
50
|
+
@end_pos = match.end(0)
|
51
|
+
@flags = match[:flags].to_s + match[:more_flags].to_s
|
52
|
+
@width = match[:width]
|
53
|
+
@precision = match[:precision]
|
54
|
+
@name = match[:name]
|
55
|
+
@type = match[:type]
|
56
56
|
end
|
57
57
|
|
58
58
|
def percent?
|
@@ -109,16 +109,8 @@ module RuboCop
|
|
109
109
|
|
110
110
|
def parse
|
111
111
|
@source.to_enum(:scan, SEQUENCE).map do
|
112
|
-
match = Regexp.last_match
|
113
112
|
FormatSequence.new(
|
114
|
-
|
115
|
-
begin_pos: match.begin(0),
|
116
|
-
end_pos: match.end(0),
|
117
|
-
flags: match[:flags].to_s + match[:more_flags].to_s,
|
118
|
-
width: match[:width],
|
119
|
-
precision: match[:precision],
|
120
|
-
name: match[:name],
|
121
|
-
type: match[:type]
|
113
|
+
Regexp.last_match
|
122
114
|
)
|
123
115
|
end
|
124
116
|
end
|
@@ -284,11 +284,7 @@ module RuboCop
|
|
284
284
|
def process_scope(node)
|
285
285
|
if TWISTED_SCOPE_TYPES.include?(node.type)
|
286
286
|
# See the comment at the end of file for this behavior.
|
287
|
-
twisted_nodes
|
288
|
-
twisted_nodes << node.children[1] if node.class_type?
|
289
|
-
twisted_nodes.compact!
|
290
|
-
|
291
|
-
twisted_nodes.each do |twisted_node|
|
287
|
+
twisted_nodes(node).each do |twisted_node|
|
292
288
|
process_node(twisted_node)
|
293
289
|
scanned_nodes << twisted_node
|
294
290
|
end
|
@@ -298,6 +294,12 @@ module RuboCop
|
|
298
294
|
skip_children!
|
299
295
|
end
|
300
296
|
|
297
|
+
def twisted_nodes(node)
|
298
|
+
twisted_nodes = [node.children[0]]
|
299
|
+
twisted_nodes << node.children[1] if node.class_type?
|
300
|
+
twisted_nodes.compact
|
301
|
+
end
|
302
|
+
|
301
303
|
def process_send(node)
|
302
304
|
_receiver, method_name, args = *node
|
303
305
|
return unless method_name == :binding
|
data/lib/rubocop/node_pattern.rb
CHANGED
@@ -429,12 +429,13 @@ module RuboCop
|
|
429
429
|
[0..Float::INFINITY, 'true']
|
430
430
|
end
|
431
431
|
|
432
|
+
# rubocop:disable Metrics/AbcSize
|
432
433
|
# rubocop:disable Metrics/MethodLength
|
433
434
|
def compile_any_order(capture_all = nil)
|
434
435
|
rest = capture_rest = nil
|
435
436
|
patterns = []
|
436
437
|
with_temp_variables do |child, matched|
|
437
|
-
tokens_until('>', 'any child')
|
438
|
+
tokens_until('>', 'any child') do
|
438
439
|
fail_due_to 'ellipsis must be at the end of <>' if rest
|
439
440
|
token = tokens.shift
|
440
441
|
case token
|
@@ -448,6 +449,7 @@ module RuboCop
|
|
448
449
|
end
|
449
450
|
end
|
450
451
|
# rubocop:enable Metrics/MethodLength
|
452
|
+
# rubocop:enable Metrics/AbcSize
|
451
453
|
|
452
454
|
def insure_same_captures(enum, what)
|
453
455
|
return to_enum __method__, enum, what unless block_given?
|
data/lib/rubocop/options.rb
CHANGED
@@ -255,11 +255,17 @@ module RuboCop
|
|
255
255
|
@options = options
|
256
256
|
end
|
257
257
|
|
258
|
+
def validate_cop_options
|
259
|
+
%i[only except].each do |opt|
|
260
|
+
OptionsValidator.validate_cop_list(@options[opt])
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
258
264
|
# rubocop:disable Metrics/AbcSize
|
259
265
|
def validate_compatibility # rubocop:disable Metrics/MethodLength
|
260
|
-
if
|
261
|
-
raise OptionArgumentError, 'Lint/
|
262
|
-
'be used with --only.'
|
266
|
+
if only_includes_redundant_disable?
|
267
|
+
raise OptionArgumentError, 'Lint/RedundantCopDisableDirective can ' \
|
268
|
+
'not be used with --only.'
|
263
269
|
end
|
264
270
|
if except_syntax?
|
265
271
|
raise OptionArgumentError, 'Syntax checking can not be turned off.'
|
@@ -332,10 +338,10 @@ module RuboCop
|
|
332
338
|
end
|
333
339
|
end
|
334
340
|
|
335
|
-
def
|
341
|
+
def only_includes_redundant_disable?
|
336
342
|
@options.key?(:only) &&
|
337
|
-
(@options[:only] & %w[Lint/
|
338
|
-
|
343
|
+
(@options[:only] & %w[Lint/RedundantCopDisableDirective
|
344
|
+
RedundantCopDisableDirective]).any?
|
339
345
|
end
|
340
346
|
|
341
347
|
def display_only_fail_level_offenses_with_autocorrect?
|
data/lib/rubocop/result_cache.rb
CHANGED
data/lib/rubocop/runner.rb
CHANGED
@@ -104,25 +104,20 @@ module RuboCop
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def process_file(file)
|
107
|
-
puts "Scanning #{file}" if @options[:debug]
|
108
107
|
file_started(file)
|
109
|
-
|
110
108
|
offenses = file_offenses(file)
|
111
|
-
if @options[:display_only_fail_level_offenses]
|
112
|
-
offenses = offenses.select { |o| considered_failure?(o) }
|
113
|
-
end
|
114
|
-
formatter_set.file_finished(file, offenses)
|
115
|
-
offenses
|
116
109
|
rescue InfiniteCorrectionLoop => e
|
117
|
-
|
110
|
+
offenses = e.offenses.compact.sort.freeze
|
118
111
|
raise
|
112
|
+
ensure
|
113
|
+
file_finished(file, offenses || [])
|
119
114
|
end
|
120
115
|
|
121
116
|
def file_offenses(file)
|
122
117
|
file_offense_cache(file) do
|
123
118
|
source = get_processed_source(file)
|
124
119
|
source, offenses = do_inspection_loop(file, source)
|
125
|
-
|
120
|
+
add_redundant_disables(file, offenses.compact.sort, source)
|
126
121
|
end
|
127
122
|
end
|
128
123
|
|
@@ -146,33 +141,37 @@ module RuboCop
|
|
146
141
|
offenses
|
147
142
|
end
|
148
143
|
|
149
|
-
def
|
150
|
-
if
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
cop.check(offenses, source.disabled_line_ranges, source.comments)
|
157
|
-
offenses += cop.offenses
|
158
|
-
offenses += autocorrect_unneeded_disables(file, source, cop,
|
159
|
-
offenses)
|
160
|
-
end
|
144
|
+
def add_redundant_disables(file, offenses, source)
|
145
|
+
if check_for_redundant_disables?(source)
|
146
|
+
redundant_cop_disable_directive(file) do |cop|
|
147
|
+
cop.check(offenses, source.disabled_line_ranges, source.comments)
|
148
|
+
offenses += cop.offenses
|
149
|
+
offenses += autocorrect_redundant_disables(file, source, cop,
|
150
|
+
offenses)
|
161
151
|
end
|
162
152
|
end
|
163
153
|
|
164
154
|
offenses.sort.reject(&:disabled?).freeze
|
165
155
|
end
|
166
156
|
|
167
|
-
def
|
157
|
+
def check_for_redundant_disables?(source)
|
168
158
|
!source.disabled_line_ranges.empty? && !filtered_run?
|
169
159
|
end
|
170
160
|
|
161
|
+
def redundant_cop_disable_directive(file)
|
162
|
+
config = @config_store.for(file)
|
163
|
+
if config.for_cop(Cop::Lint::RedundantCopDisableDirective)
|
164
|
+
.fetch('Enabled')
|
165
|
+
cop = Cop::Lint::RedundantCopDisableDirective.new(config, @options)
|
166
|
+
yield cop if cop.relevant_file?(file)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
171
170
|
def filtered_run?
|
172
171
|
@options[:except] || @options[:only]
|
173
172
|
end
|
174
173
|
|
175
|
-
def
|
174
|
+
def autocorrect_redundant_disables(file, source, cop, offenses)
|
176
175
|
cop.processed_source = source
|
177
176
|
|
178
177
|
team = Cop::Team.new(RuboCop::Cop::Registry.new, nil, @options)
|
@@ -180,7 +179,7 @@ module RuboCop
|
|
180
179
|
|
181
180
|
return [] unless team.updated_source_file?
|
182
181
|
|
183
|
-
# Do one extra inspection loop if any
|
182
|
+
# Do one extra inspection loop if any redundant disables were
|
184
183
|
# removed. This is done in order to find rubocop:enable directives that
|
185
184
|
# have now become useless.
|
186
185
|
_source, new_offenses = do_inspection_loop(file,
|
@@ -189,11 +188,19 @@ module RuboCop
|
|
189
188
|
end
|
190
189
|
|
191
190
|
def file_started(file)
|
191
|
+
puts "Scanning #{file}" if @options[:debug]
|
192
192
|
formatter_set.file_started(file,
|
193
193
|
cli_options: @options,
|
194
194
|
config_store: @config_store)
|
195
195
|
end
|
196
196
|
|
197
|
+
def file_finished(file, offenses)
|
198
|
+
if @options[:display_only_fail_level_offenses]
|
199
|
+
offenses = offenses.select { |o| considered_failure?(o) }
|
200
|
+
end
|
201
|
+
formatter_set.file_finished(file, offenses)
|
202
|
+
end
|
203
|
+
|
197
204
|
def cached_run?
|
198
205
|
@cached_run ||=
|
199
206
|
(@options[:cache] == 'true' ||
|
@@ -291,9 +298,7 @@ module RuboCop
|
|
291
298
|
@mobilized_cop_classes[config.object_id] ||= begin
|
292
299
|
cop_classes = Cop::Cop.all
|
293
300
|
|
294
|
-
|
295
|
-
OptionsValidator.validate_cop_list(@options[opt])
|
296
|
-
end
|
301
|
+
OptionsValidator.new(@options).validate_cop_options
|
297
302
|
|
298
303
|
if @options[:only]
|
299
304
|
cop_classes.select! { |c| c.match?(@options[:only]) }
|