rubocop 1.36.0 → 1.39.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 +31 -3
- data/lib/rubocop/arguments_env.rb +17 -0
- data/lib/rubocop/arguments_file.rb +17 -0
- data/lib/rubocop/cli/command/execute_runner.rb +7 -7
- data/lib/rubocop/cli/command/suggest_extensions.rb +8 -1
- data/lib/rubocop/comment_config.rb +36 -1
- data/lib/rubocop/cop/commissioner.rb +3 -1
- data/lib/rubocop/cop/generator.rb +1 -2
- data/lib/rubocop/cop/internal_affairs/create_empty_file.rb +37 -0
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +111 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +29 -8
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +13 -9
- data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +3 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +30 -3
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +34 -0
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +6 -2
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_magic_comment.rb +73 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +28 -9
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +25 -6
- data/lib/rubocop/cop/lint/empty_class.rb +3 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +20 -8
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +18 -3
- data/lib/rubocop/cop/lint/nested_method_definition.rb +50 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +4 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +23 -1
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +7 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +38 -10
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -2
- data/lib/rubocop/cop/lint/shadowed_exception.rb +0 -10
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +3 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +4 -0
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +12 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +6 -3
- data/lib/rubocop/cop/mixin/range_help.rb +23 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +3 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -8
- data/lib/rubocop/cop/mixin/visibility_help.rb +40 -5
- data/lib/rubocop/cop/naming/inclusive_language.rb +1 -1
- data/lib/rubocop/cop/registry.rb +10 -4
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +5 -7
- data/lib/rubocop/cop/style/accessor_grouping.rb +7 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -2
- data/lib/rubocop/cop/style/character_literal.rb +1 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +8 -6
- data/lib/rubocop/cop/style/collection_compact.rb +12 -3
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/endless_method.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +4 -0
- data/lib/rubocop/cop/style/format_string_token.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +62 -21
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +32 -10
- data/lib/rubocop/cop/style/hash_except.rb +4 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +25 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +13 -2
- data/lib/rubocop/cop/style/module_function.rb +28 -6
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +7 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/object_then.rb +3 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +53 -0
- data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +5 -2
- data/lib/rubocop/cop/style/redundant_each.rb +116 -0
- data/lib/rubocop/cop/style/redundant_initialize.rb +3 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +8 -1
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +12 -3
- data/lib/rubocop/cop/style/redundant_string_escape.rb +181 -0
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/static_class.rb +32 -1
- data/lib/rubocop/cop/style/symbol_array.rb +2 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +2 -0
- data/lib/rubocop/cop/team.rb +3 -4
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +1 -1
- data/lib/rubocop/cops_documentation_generator.rb +2 -1
- data/lib/rubocop/ext/processed_source.rb +2 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +8 -2
- data/lib/rubocop/formatter/offense_count_formatter.rb +8 -5
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +6 -3
- data/lib/rubocop/options.rb +19 -15
- data/lib/rubocop/rspec/cop_helper.rb +21 -1
- data/lib/rubocop/rspec/shared_contexts.rb +14 -1
- data/lib/rubocop/runner.rb +15 -11
- data/lib/rubocop/server/cache.rb +5 -1
- data/lib/rubocop/server/cli.rb +9 -2
- data/lib/rubocop/server/client_command/exec.rb +5 -0
- data/lib/rubocop/server/core.rb +18 -1
- data/lib/rubocop/server/socket_reader.rb +5 -1
- data/lib/rubocop/server.rb +1 -1
- data/lib/rubocop/version.rb +8 -3
- data/lib/rubocop.rb +4 -0
- metadata +13 -5
@@ -74,7 +74,7 @@ module RuboCop
|
|
74
74
|
|
75
75
|
non_redundant =
|
76
76
|
whitespace_in_free_space_mode?(node, class_elem) ||
|
77
|
-
backslash_b?(class_elem) ||
|
77
|
+
backslash_b?(class_elem) || backslash_zero?(class_elem) ||
|
78
78
|
requires_escape_outside_char_class?(class_elem)
|
79
79
|
|
80
80
|
!non_redundant
|
@@ -104,6 +104,13 @@ module RuboCop
|
|
104
104
|
elem == '\b'
|
105
105
|
end
|
106
106
|
|
107
|
+
def backslash_zero?(elem)
|
108
|
+
# See https://github.com/rubocop/rubocop/issues/11067 for details - in short "\0" != "0" -
|
109
|
+
# the former means an Unicode code point `"\u0000"`, the latter a number character `"0"`.
|
110
|
+
# Similarly "\032" means "\u001A". Other numbers starting with "\0" can also be mentioned.
|
111
|
+
elem == '\0'
|
112
|
+
end
|
113
|
+
|
107
114
|
def requires_escape_outside_char_class?(elem)
|
108
115
|
REQUIRES_ESCAPE_OUTSIDE_CHAR_CLASS_CHARS.include?(elem)
|
109
116
|
end
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
|
45
45
|
def on_regexp(node)
|
46
46
|
each_escape(node) do |char, index, within_character_class|
|
47
|
-
next if allowed_escape?(node, char, within_character_class)
|
47
|
+
next if allowed_escape?(node, char, index, within_character_class)
|
48
48
|
|
49
49
|
location = escape_range_at_index(node, index)
|
50
50
|
|
@@ -56,7 +56,7 @@ module RuboCop
|
|
56
56
|
|
57
57
|
private
|
58
58
|
|
59
|
-
def allowed_escape?(node, char, within_character_class)
|
59
|
+
def allowed_escape?(node, char, index, within_character_class)
|
60
60
|
# Strictly speaking a few single-letter metachars are currently
|
61
61
|
# unnecessary to "escape", e.g. i, E, F, but enumerating them is
|
62
62
|
# rather difficult, and their behavior could change over time with
|
@@ -65,12 +65,21 @@ module RuboCop
|
|
65
65
|
return true if ALLOWED_ALWAYS_ESCAPES.include?(char) || delimiter?(node, char)
|
66
66
|
|
67
67
|
if within_character_class
|
68
|
-
ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
|
68
|
+
ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES.include?(char) &&
|
69
|
+
!char_class_begins_or_ends_with_escaped_hyphen?(node, index)
|
69
70
|
else
|
70
71
|
ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES.include?(char)
|
71
72
|
end
|
72
73
|
end
|
73
74
|
|
75
|
+
def char_class_begins_or_ends_with_escaped_hyphen?(node, index)
|
76
|
+
# The hyphen character is allowed to be escaped within a character class
|
77
|
+
# but it's not necessry to escape hyphen if it's the first or last character
|
78
|
+
# within the character class. This method checks if that's the case.
|
79
|
+
# e.g. "[0-9\\-]" or "[\\-0-9]" would return true
|
80
|
+
node.source[index] == '[' || node.source[index + 3] == ']'
|
81
|
+
end
|
82
|
+
|
74
83
|
def delimiter?(node, char)
|
75
84
|
delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
|
76
85
|
|
@@ -0,0 +1,181 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# Checks for redundant escapes in string literals.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad - no need to escape # without following {/$/@
|
10
|
+
# "\#foo"
|
11
|
+
#
|
12
|
+
# # bad - no need to escape single quotes inside double quoted string
|
13
|
+
# "\'foo\'"
|
14
|
+
#
|
15
|
+
# # bad - heredocs are also checked for unnecessary escapes
|
16
|
+
# <<~STR
|
17
|
+
# \#foo \"foo\"
|
18
|
+
# STR
|
19
|
+
#
|
20
|
+
# # good
|
21
|
+
# "#foo"
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# "\#{no_interpolation}"
|
25
|
+
#
|
26
|
+
# # good
|
27
|
+
# "'foo'"
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# "foo\
|
31
|
+
# bar"
|
32
|
+
#
|
33
|
+
# # good
|
34
|
+
# <<~STR
|
35
|
+
# #foo "foo"
|
36
|
+
# STR
|
37
|
+
class RedundantStringEscape < Base
|
38
|
+
include MatchRange
|
39
|
+
include RangeHelp
|
40
|
+
extend AutoCorrector
|
41
|
+
|
42
|
+
MSG = 'Redundant escape of %<char>s inside string literal.'
|
43
|
+
|
44
|
+
def on_str(node)
|
45
|
+
return if node.parent&.regexp_type? || node.parent&.xstr_type? || node.character_literal?
|
46
|
+
|
47
|
+
str_contents_range = str_contents_range(node)
|
48
|
+
|
49
|
+
each_match_range(str_contents_range, /(\\.)/) do |range|
|
50
|
+
next if allowed_escape?(node, range.resize(3))
|
51
|
+
|
52
|
+
add_offense(range) do |corrector|
|
53
|
+
corrector.remove_leading(range, 1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def message(range)
|
61
|
+
format(MSG, char: range.source.chars.last)
|
62
|
+
end
|
63
|
+
|
64
|
+
def str_contents_range(node)
|
65
|
+
if heredoc?(node)
|
66
|
+
node.loc.heredoc_body
|
67
|
+
elsif begin_loc_present?(node)
|
68
|
+
contents_range(node)
|
69
|
+
else
|
70
|
+
node.loc.expression
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def begin_loc_present?(node)
|
75
|
+
# e.g. a __FILE__ literal has no begin loc so we can't query if it's nil
|
76
|
+
node.loc.to_hash.key?(:begin) && !node.loc.begin.nil?
|
77
|
+
end
|
78
|
+
|
79
|
+
def allowed_escape?(node, range)
|
80
|
+
escaped = range.source[(1..-1)]
|
81
|
+
|
82
|
+
# Inside a single-quoted string, escapes (except \\ and \') do not have special meaning,
|
83
|
+
# and so are not redundant, as they are a literal backslash.
|
84
|
+
return true if interpolation_not_enabled?(node)
|
85
|
+
|
86
|
+
# Strictly speaking a few single-letter chars are currently unnecessary to "escape", e.g.
|
87
|
+
# d, but enumerating them is rather difficult, and their behavior could change over time
|
88
|
+
# with different versions of Ruby so that e.g. /\d/ != /d/
|
89
|
+
return true if /[\n\\[[:alnum:]]]/.match?(escaped[0])
|
90
|
+
|
91
|
+
return true if escaped[0] == ' ' && percent_array_literal?(node)
|
92
|
+
|
93
|
+
return true if disabling_interpolation?(range)
|
94
|
+
return true if delimiter?(node, escaped[0])
|
95
|
+
|
96
|
+
false
|
97
|
+
end
|
98
|
+
|
99
|
+
def interpolation_not_enabled?(node)
|
100
|
+
single_quoted?(node) ||
|
101
|
+
percent_w_literal?(node) ||
|
102
|
+
percent_q_literal?(node) ||
|
103
|
+
heredoc_with_disabled_interpolation?(node)
|
104
|
+
end
|
105
|
+
|
106
|
+
def single_quoted?(node)
|
107
|
+
delimiter?(node, "'")
|
108
|
+
end
|
109
|
+
|
110
|
+
def percent_q_literal?(node)
|
111
|
+
if literal_in_interpolated_or_multiline_string?(node)
|
112
|
+
percent_q_literal?(node.parent)
|
113
|
+
else
|
114
|
+
node.source.start_with?('%q')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def array_literal?(node, prefix)
|
119
|
+
if literal_in_interpolated_or_multiline_string?(node)
|
120
|
+
array_literal?(node.parent, prefix)
|
121
|
+
else
|
122
|
+
node.parent&.array_type? && node.parent.source.start_with?(prefix)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def percent_w_literal?(node)
|
127
|
+
array_literal?(node, '%w')
|
128
|
+
end
|
129
|
+
|
130
|
+
def percent_w_upper_literal?(node)
|
131
|
+
array_literal?(node, '%W')
|
132
|
+
end
|
133
|
+
|
134
|
+
def percent_array_literal?(node)
|
135
|
+
(percent_w_literal?(node) || percent_w_upper_literal?(node))
|
136
|
+
end
|
137
|
+
|
138
|
+
def heredoc_with_disabled_interpolation?(node)
|
139
|
+
if heredoc?(node)
|
140
|
+
node.loc.expression.source.end_with?("'")
|
141
|
+
elsif node.parent&.dstr_type?
|
142
|
+
heredoc_with_disabled_interpolation?(node.parent)
|
143
|
+
else
|
144
|
+
false
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def heredoc?(node)
|
149
|
+
(node.str_type? || node.dstr_type?) && node.heredoc?
|
150
|
+
end
|
151
|
+
|
152
|
+
def delimiter?(node, char)
|
153
|
+
return false if heredoc?(node)
|
154
|
+
|
155
|
+
if literal_in_interpolated_or_multiline_string?(node) || percent_array_literal?(node)
|
156
|
+
return delimiter?(node.parent, char)
|
157
|
+
end
|
158
|
+
|
159
|
+
delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
|
160
|
+
|
161
|
+
delimiters.include?(char)
|
162
|
+
end
|
163
|
+
|
164
|
+
def literal_in_interpolated_or_multiline_string?(node)
|
165
|
+
node.str_type? && !begin_loc_present?(node) && node.parent&.dstr_type?
|
166
|
+
end
|
167
|
+
|
168
|
+
def disabling_interpolation?(range)
|
169
|
+
# Allow \#{foo}, \#$foo, \#@foo, and \#@@foo
|
170
|
+
# for escaping local, global, instance and class variable interpolations
|
171
|
+
return true if range.source.match?(/\A\\#[{$@]/)
|
172
|
+
|
173
|
+
# Also allow #\{foo}, #\$foo, #\@foo and #\@@foo
|
174
|
+
return true if range.adjust(begin_pos: -2).source.match?(/\A[^\\]#\\[{$@]/)
|
175
|
+
|
176
|
+
false
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# reasons:
|
10
10
|
#
|
11
11
|
# * The syntax of modifier form `rescue` can be misleading because it
|
12
|
-
# might
|
12
|
+
# might lead us to believe that `rescue` handles the given exception
|
13
13
|
# but it actually rescue all exceptions to return the given rescue
|
14
14
|
# block. In this case, value returned by handle_error or
|
15
15
|
# SomeException.
|
@@ -44,18 +44,49 @@ module RuboCop
|
|
44
44
|
# end
|
45
45
|
#
|
46
46
|
class StaticClass < Base
|
47
|
+
include RangeHelp
|
47
48
|
include VisibilityHelp
|
49
|
+
extend AutoCorrector
|
48
50
|
|
49
51
|
MSG = 'Prefer modules to classes with only class methods.'
|
50
52
|
|
51
53
|
def on_class(class_node)
|
52
54
|
return if class_node.parent_class
|
55
|
+
return unless class_convertible_to_module?(class_node)
|
53
56
|
|
54
|
-
add_offense(class_node)
|
57
|
+
add_offense(class_node) do |corrector|
|
58
|
+
autocorrect(corrector, class_node)
|
59
|
+
end
|
55
60
|
end
|
56
61
|
|
57
62
|
private
|
58
63
|
|
64
|
+
def autocorrect(corrector, class_node)
|
65
|
+
corrector.replace(class_node.loc.keyword, 'module')
|
66
|
+
corrector.insert_after(class_node.loc.name, "\nmodule_function\n")
|
67
|
+
|
68
|
+
class_elements(class_node).each do |node|
|
69
|
+
if node.defs_type?
|
70
|
+
autocorrect_def(corrector, node)
|
71
|
+
elsif node.sclass_type?
|
72
|
+
autocorrect_sclass(corrector, node)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def autocorrect_def(corrector, node)
|
78
|
+
corrector.remove(
|
79
|
+
range_between(node.receiver.source_range.begin_pos, node.loc.name.begin_pos)
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
def autocorrect_sclass(corrector, node)
|
84
|
+
corrector.remove(
|
85
|
+
range_between(node.loc.keyword.begin_pos, node.identifier.source_range.end_pos)
|
86
|
+
)
|
87
|
+
corrector.remove(node.loc.end)
|
88
|
+
end
|
89
|
+
|
59
90
|
def class_convertible_to_module?(class_node)
|
60
91
|
nodes = class_elements(class_node)
|
61
92
|
return false if nodes.empty?
|
data/lib/rubocop/cop/team.rb
CHANGED
@@ -40,10 +40,9 @@ module RuboCop
|
|
40
40
|
|
41
41
|
# @return [Array<Cop::Cop>]
|
42
42
|
def self.mobilize_cops(cop_classes, config, options = {})
|
43
|
-
cop_classes = Registry.new(cop_classes.to_a) unless cop_classes.is_a?(Registry)
|
44
|
-
|
45
|
-
|
46
|
-
cop_classes.enabled(config, only, only_safe: safe).map do |cop_class|
|
43
|
+
cop_classes = Registry.new(cop_classes.to_a, options) unless cop_classes.is_a?(Registry)
|
44
|
+
|
45
|
+
cop_classes.enabled(config).map do |cop_class|
|
47
46
|
cop_class.new(config, options)
|
48
47
|
end
|
49
48
|
end
|
data/lib/rubocop/cop/util.rb
CHANGED
@@ -109,7 +109,7 @@ module RuboCop
|
|
109
109
|
end
|
110
110
|
|
111
111
|
def accessible_variables
|
112
|
-
scope_stack.reverse_each.
|
112
|
+
scope_stack.reverse_each.with_object([]) do |scope, variables|
|
113
113
|
variables.concat(scope.variables.values)
|
114
114
|
break variables unless scope.node.block_type?
|
115
115
|
end
|
@@ -185,7 +185,8 @@ class CopsDocumentationGenerator # rubocop:disable Metrics/ClassLength
|
|
185
185
|
def to_table(header, content)
|
186
186
|
table = ['|===', "| #{header.join(' | ')}\n\n"].join("\n")
|
187
187
|
marked_contents = content.map do |plain_content|
|
188
|
-
|
188
|
+
# Escape `|` with backslash to prevent the regexp `|` is not used as a table separator.
|
189
|
+
plain_content.map { |c| "| #{c.gsub(/\|/, '\|')}" }.join("\n")
|
189
190
|
end
|
190
191
|
table << marked_contents.join("\n\n")
|
191
192
|
table << "\n|===\n"
|
@@ -70,7 +70,9 @@ module RuboCop
|
|
70
70
|
|
71
71
|
command += ' --auto-gen-only-exclude' if @options[:auto_gen_only_exclude]
|
72
72
|
|
73
|
-
if
|
73
|
+
if no_exclude_limit?
|
74
|
+
command += ' --no-exclude-limit'
|
75
|
+
elsif @exclude_limit_option
|
74
76
|
command += format(' --exclude-limit %<limit>d', limit: Integer(@exclude_limit_option))
|
75
77
|
end
|
76
78
|
command += ' --no-offense-counts' unless show_offense_counts?
|
@@ -187,7 +189,7 @@ module RuboCop
|
|
187
189
|
return unless cfg.empty?
|
188
190
|
|
189
191
|
offending_files = @files_with_offenses[cop_name].sort
|
190
|
-
if offending_files.count > @exclude_limit
|
192
|
+
if !no_exclude_limit? && offending_files.count > @exclude_limit
|
191
193
|
output_buffer.puts ' Enabled: false'
|
192
194
|
else
|
193
195
|
output_exclude_list(output_buffer, offending_files, cop_name)
|
@@ -245,6 +247,10 @@ module RuboCop
|
|
245
247
|
def safe_autocorrect?(config)
|
246
248
|
config.fetch('Safe', true) && config.fetch('SafeAutoCorrect', true)
|
247
249
|
end
|
250
|
+
|
251
|
+
def no_exclude_limit?
|
252
|
+
@options[:no_exclude_limit] == false
|
253
|
+
end
|
248
254
|
end
|
249
255
|
end
|
250
256
|
end
|
@@ -12,13 +12,14 @@ module RuboCop
|
|
12
12
|
# 26 LineLength
|
13
13
|
# 3 OneLineConditional
|
14
14
|
# --
|
15
|
-
# 29 Total
|
15
|
+
# 29 Total in 5 files
|
16
16
|
class OffenseCountFormatter < BaseFormatter
|
17
17
|
attr_reader :offense_counts
|
18
18
|
|
19
19
|
def started(target_files)
|
20
20
|
super
|
21
21
|
@offense_counts = Hash.new(0)
|
22
|
+
@offending_files_count = 0
|
22
23
|
@style_guide_links = {}
|
23
24
|
|
24
25
|
return unless output.tty?
|
@@ -43,26 +44,28 @@ module RuboCop
|
|
43
44
|
if options[:display_style_guide]
|
44
45
|
offenses.each { |o| @style_guide_links[o.cop_name] ||= o.message[/ \(http\S+\)\Z/] }
|
45
46
|
end
|
47
|
+
@offending_files_count += 1 unless offenses.empty?
|
46
48
|
@progressbar.increment if instance_variable_defined?(:@progressbar)
|
47
49
|
end
|
48
50
|
|
49
51
|
def finished(_inspected_files)
|
50
|
-
report_summary(@offense_counts)
|
52
|
+
report_summary(@offense_counts, @offending_files_count)
|
51
53
|
end
|
52
54
|
|
53
55
|
# rubocop:disable Metrics/AbcSize
|
54
|
-
def report_summary(offense_counts)
|
56
|
+
def report_summary(offense_counts, offending_files_count)
|
55
57
|
per_cop_counts = ordered_offense_counts(offense_counts)
|
56
58
|
total_count = total_offense_count(offense_counts)
|
57
59
|
|
58
60
|
output.puts
|
59
61
|
|
62
|
+
column_width = total_count.to_s.length + 2
|
60
63
|
per_cop_counts.each do |cop_name, count|
|
61
|
-
output.puts "#{count.to_s.ljust(
|
64
|
+
output.puts "#{count.to_s.ljust(column_width)}#{cop_name}" \
|
62
65
|
"#{@style_guide_links[cop_name]}\n"
|
63
66
|
end
|
64
67
|
output.puts '--'
|
65
|
-
output.puts "#{total_count} Total"
|
68
|
+
output.puts "#{total_count} Total in #{offending_files_count} files"
|
66
69
|
|
67
70
|
output.puts
|
68
71
|
end
|
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
# 26 this/file/is/really/bad.rb
|
13
13
|
# 3 just/ok.rb
|
14
14
|
# --
|
15
|
-
# 29 Total
|
15
|
+
# 29 Total in 2 files
|
16
16
|
class WorstOffendersFormatter < BaseFormatter
|
17
17
|
attr_reader :offense_counts
|
18
18
|
|
@@ -36,14 +36,17 @@ module RuboCop
|
|
36
36
|
def report_summary(offense_counts)
|
37
37
|
per_file_counts = ordered_offense_counts(offense_counts)
|
38
38
|
total_count = total_offense_count(offense_counts)
|
39
|
+
file_count = per_file_counts.size
|
39
40
|
|
40
41
|
output.puts
|
41
42
|
|
43
|
+
column_width = total_count.to_s.length + 2
|
42
44
|
per_file_counts.each do |file_name, count|
|
43
|
-
output.puts "#{count.to_s.ljust(
|
45
|
+
output.puts "#{count.to_s.ljust(column_width)}#{file_name}\n"
|
44
46
|
end
|
47
|
+
|
45
48
|
output.puts '--'
|
46
|
-
output.puts "#{total_count} Total"
|
49
|
+
output.puts "#{total_count} Total in #{file_count} files"
|
47
50
|
|
48
51
|
output.puts
|
49
52
|
end
|
data/lib/rubocop/options.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'optparse'
|
4
|
-
|
4
|
+
require_relative 'arguments_env'
|
5
|
+
require_relative 'arguments_file'
|
5
6
|
|
6
7
|
module RuboCop
|
7
8
|
class IncorrectCopNameError < StandardError; end
|
@@ -24,7 +25,10 @@ module RuboCop
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def parse(command_line_args)
|
28
|
+
args_from_file = ArgumentsFile.read_as_arguments
|
29
|
+
args_from_env = ArgumentsEnv.read_as_arguments
|
27
30
|
args = args_from_file.concat(args_from_env).concat(command_line_args)
|
31
|
+
|
28
32
|
define_options.parse!(args)
|
29
33
|
|
30
34
|
@validator.validate_compatibility
|
@@ -45,18 +49,6 @@ module RuboCop
|
|
45
49
|
|
46
50
|
private
|
47
51
|
|
48
|
-
def args_from_file
|
49
|
-
if File.exist?('.rubocop') && !File.directory?('.rubocop')
|
50
|
-
File.read('.rubocop').shellsplit
|
51
|
-
else
|
52
|
-
[]
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def args_from_env
|
57
|
-
Shellwords.split(ENV.fetch('RUBOCOP_OPTS', ''))
|
58
|
-
end
|
59
|
-
|
60
52
|
def define_options
|
61
53
|
OptionParser.new do |opts|
|
62
54
|
opts.banner = rainbow.wrap('Usage: rubocop [options] [file1, file2, ...]').bright
|
@@ -73,7 +65,7 @@ module RuboCop
|
|
73
65
|
end
|
74
66
|
|
75
67
|
def add_check_options(opts) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
76
|
-
section(opts, 'Basic Options') do
|
68
|
+
section(opts, 'Basic Options') do # rubocop:disable Metrics/BlockLength
|
77
69
|
option(opts, '-l', '--lint') do
|
78
70
|
@options[:only] ||= []
|
79
71
|
@options[:only] << 'Lint'
|
@@ -98,6 +90,7 @@ module RuboCop
|
|
98
90
|
option(opts, '--force-default-config')
|
99
91
|
option(opts, '-s', '--stdin FILE')
|
100
92
|
option(opts, '-P', '--[no-]parallel')
|
93
|
+
option(opts, '--raise-cop-error')
|
101
94
|
add_severity_option(opts)
|
102
95
|
end
|
103
96
|
end
|
@@ -169,6 +162,7 @@ module RuboCop
|
|
169
162
|
end
|
170
163
|
|
171
164
|
option(opts, '--exclude-limit COUNT') { @validator.validate_exclude_limit_option }
|
165
|
+
option(opts, '--no-exclude-limit')
|
172
166
|
|
173
167
|
option(opts, '--[no-]offense-counts')
|
174
168
|
option(opts, '--[no-]auto-gen-only-exclude')
|
@@ -400,6 +394,12 @@ module RuboCop
|
|
400
394
|
end
|
401
395
|
|
402
396
|
def validate_autocorrect
|
397
|
+
if @options.key?(:safe_autocorrect) && @options.key?(:autocorrect_all)
|
398
|
+
message = Rainbow(<<~MESSAGE).red
|
399
|
+
Error: Both safe and unsafe autocorrect options are specified, use only one.
|
400
|
+
MESSAGE
|
401
|
+
raise OptionArgumentError, message
|
402
|
+
end
|
403
403
|
return if @options.key?(:autocorrect)
|
404
404
|
return unless @options.key?(:disable_uncorrectable)
|
405
405
|
|
@@ -497,6 +497,7 @@ module RuboCop
|
|
497
497
|
disable_uncorrectable: ['Used with --autocorrect to annotate any',
|
498
498
|
'offenses that do not support autocorrect',
|
499
499
|
'with `rubocop:todo` comments.'],
|
500
|
+
no_exclude_limit: ['Do not set the limit for how many files to exclude.'],
|
500
501
|
force_exclusion: ['Any files excluded by `Exclude` in configuration',
|
501
502
|
'files will be excluded, even if given explicitly',
|
502
503
|
'as arguments.'],
|
@@ -589,7 +590,10 @@ module RuboCop
|
|
589
590
|
restart_server: 'Restart server process.',
|
590
591
|
start_server: 'Start server process.',
|
591
592
|
stop_server: 'Stop server process.',
|
592
|
-
server_status: 'Show server status.'
|
593
|
+
server_status: 'Show server status.',
|
594
|
+
raise_cop_error: ['Raise cop-related errors with cause and location.',
|
595
|
+
'This is used to prevent cops from failing silently.',
|
596
|
+
'Default is false.']
|
593
597
|
}.freeze
|
594
598
|
end
|
595
599
|
end
|
@@ -28,7 +28,27 @@ module CopHelper
|
|
28
28
|
file = file.path
|
29
29
|
end
|
30
30
|
|
31
|
-
RuboCop::ProcessedSource.new(source, ruby_version, file)
|
31
|
+
processed_source = RuboCop::ProcessedSource.new(source, ruby_version, file)
|
32
|
+
processed_source.config = configuration
|
33
|
+
processed_source.registry = registry
|
34
|
+
processed_source
|
35
|
+
end
|
36
|
+
|
37
|
+
def configuration
|
38
|
+
@configuration ||= if defined?(config)
|
39
|
+
config
|
40
|
+
else
|
41
|
+
RuboCop::Config.new({}, "#{Dir.pwd}/.rubocop.yml")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def registry
|
46
|
+
@registry ||= begin
|
47
|
+
cops = configuration.keys.map { |cop| RuboCop::Cop::Registry.global.find_by_cop_name(cop) }
|
48
|
+
cops << cop_class if defined?(cop_class) && !cops.include?(cop_class)
|
49
|
+
cops.compact!
|
50
|
+
RuboCop::Cop::Registry.new(cops)
|
51
|
+
end
|
32
52
|
end
|
33
53
|
|
34
54
|
def autocorrect_source_file(source)
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'tmpdir'
|
4
4
|
|
5
|
-
RSpec.shared_context 'isolated environment' do
|
5
|
+
RSpec.shared_context 'isolated environment' do # rubocop:disable Metrics/BlockLength
|
6
6
|
around do |example|
|
7
7
|
Dir.mktmpdir do |tmpdir|
|
8
8
|
original_home = Dir.home
|
@@ -36,6 +36,19 @@ RSpec.shared_context 'isolated environment' do
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
if RuboCop.const_defined?(:Server)
|
41
|
+
around do |example|
|
42
|
+
RuboCop::Server::Cache.cache_root_path = nil
|
43
|
+
RuboCop::Server::Cache.instance_variable_set(:@project_dir_cache_key, nil)
|
44
|
+
begin
|
45
|
+
example.run
|
46
|
+
ensure
|
47
|
+
RuboCop::Server::Cache.cache_root_path = nil
|
48
|
+
RuboCop::Server::Cache.instance_variable_set(:@project_dir_cache_key, nil)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
39
52
|
end
|
40
53
|
|
41
54
|
RSpec.shared_context 'maintain registry' do
|