rubocop 0.85.1 → 0.86.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -4
- data/config/default.yml +29 -1
- data/lib/rubocop.rb +3 -0
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +6 -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/raise_exception.rb +12 -4
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -1
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -2
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
- data/lib/rubocop/cop/style/copyright.rb +3 -3
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +103 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
- data/lib/rubocop/cop/style/redundant_self.rb +6 -9
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/utils/format_string.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/name_similarity.rb +6 -0
- data/lib/rubocop/path_util.rb +2 -2
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +12 -2
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +11 -2
@@ -65,7 +65,12 @@ module RuboCop
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def invalid_format_string?(node)
|
68
|
-
|
68
|
+
string = if sprintf?(node) || format?(node)
|
69
|
+
node.first_argument.source
|
70
|
+
else
|
71
|
+
node.receiver.source
|
72
|
+
end
|
73
|
+
!RuboCop::Cop::Utils::FormatString.new(string).valid?
|
69
74
|
end
|
70
75
|
|
71
76
|
def offending_node?(node)
|
@@ -31,12 +31,12 @@ module RuboCop
|
|
31
31
|
MSG = 'Use `StandardError` over `Exception`.'
|
32
32
|
|
33
33
|
def_node_matcher :exception?, <<~PATTERN
|
34
|
-
(send nil? {:raise :fail} (const ${cbase nil?} :Exception) ... )
|
34
|
+
(send nil? {:raise :fail} $(const ${cbase nil?} :Exception) ... )
|
35
35
|
PATTERN
|
36
36
|
|
37
37
|
def_node_matcher :exception_new_with_message?, <<~PATTERN
|
38
38
|
(send nil? {:raise :fail}
|
39
|
-
(send (const ${cbase nil?} :Exception) :new ... ))
|
39
|
+
(send $(const ${cbase nil?} :Exception) :new ... ))
|
40
40
|
PATTERN
|
41
41
|
|
42
42
|
def on_send(node)
|
@@ -44,13 +44,21 @@ module RuboCop
|
|
44
44
|
exception_new_with_message?(node, &check(node))
|
45
45
|
end
|
46
46
|
|
47
|
+
def autocorrect(node)
|
48
|
+
lambda do |corrector|
|
49
|
+
exception_class = node.children.first&.cbase_type? ? '::StandardError' : 'StandardError'
|
50
|
+
|
51
|
+
corrector.replace(node, exception_class)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
47
55
|
private
|
48
56
|
|
49
57
|
def check(node)
|
50
|
-
lambda do |cbase|
|
58
|
+
lambda do |exception_class, cbase|
|
51
59
|
return if cbase.nil? && implicit_namespace?(node)
|
52
60
|
|
53
|
-
add_offense(
|
61
|
+
add_offense(exception_class)
|
54
62
|
end
|
55
63
|
end
|
56
64
|
|
@@ -135,6 +135,7 @@ module RuboCop
|
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
|
+
# rubocop:todo Metrics/CyclomaticComplexity
|
138
139
|
def find_redundant(comment, offenses, cop, line_range, next_line_range)
|
139
140
|
if all_disabled?(comment)
|
140
141
|
# If there's a disable all comment followed by a comment
|
@@ -152,9 +153,10 @@ module RuboCop
|
|
152
153
|
cop if cop_offenses.none? { |o| line_range.cover?(o.line) }
|
153
154
|
end
|
154
155
|
end
|
156
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
155
157
|
|
156
158
|
def all_disabled?(comment)
|
157
|
-
|
159
|
+
/rubocop\s*:\s*(?:disable|todo)\s+all\b/.match?(comment.text)
|
158
160
|
end
|
159
161
|
|
160
162
|
def ignore_offense?(disabled_ranges, line_range)
|
@@ -225,7 +227,7 @@ module RuboCop
|
|
225
227
|
.drop_while { |r| !r.equal?(range) }
|
226
228
|
.each_cons(2)
|
227
229
|
.map { |range1, range2| range1.end.join(range2.begin).source }
|
228
|
-
.all? { |intervening|
|
230
|
+
.all? { |intervening| /\A\s*,\s*\Z/.match?(intervening) }
|
229
231
|
end
|
230
232
|
|
231
233
|
def describe(cop)
|
@@ -13,19 +13,51 @@ module RuboCop
|
|
13
13
|
# operator (or keyword and) can be converted to a nested if statement,
|
14
14
|
# and ||/or is shorthand for a sequence of ifs, so they also add one.
|
15
15
|
# Loops can be said to have an exit condition, so they add one.
|
16
|
+
# Blocks that are calls to builtin iteration methods
|
17
|
+
# (e.g. `ary.map{...}) also add one, others are ignored.
|
18
|
+
#
|
19
|
+
# def each_child_node(*types) # count begins: 1
|
20
|
+
# unless block_given? # unless: +1
|
21
|
+
# return to_enum(__method__, *types)
|
22
|
+
#
|
23
|
+
# children.each do |child| # each{}: +1
|
24
|
+
# next unless child.is_a?(Node) # unless: +1
|
25
|
+
#
|
26
|
+
# yield child if types.empty? || # if: +1, ||: +1
|
27
|
+
# types.include?(child.type)
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# self
|
31
|
+
# end # total: 6
|
16
32
|
class CyclomaticComplexity < Cop
|
17
33
|
include MethodComplexity
|
34
|
+
include Utils::IteratingBlock
|
18
35
|
|
19
36
|
MSG = 'Cyclomatic complexity for %<method>s is too high. ' \
|
20
37
|
'[%<complexity>d/%<max>d]'
|
21
|
-
COUNTED_NODES = %i[if while until for
|
22
|
-
rescue when and or].freeze
|
38
|
+
COUNTED_NODES = %i[if while until for csend block block_pass
|
39
|
+
rescue when and or or_asgn and_asgn].freeze
|
23
40
|
|
24
41
|
private
|
25
42
|
|
26
|
-
def complexity_score_for(
|
43
|
+
def complexity_score_for(node)
|
44
|
+
return 0 if iterating_block?(node) == false
|
45
|
+
|
27
46
|
1
|
28
47
|
end
|
48
|
+
|
49
|
+
def block_method(node)
|
50
|
+
case node.type
|
51
|
+
when :block
|
52
|
+
node.method_name
|
53
|
+
when :block_pass
|
54
|
+
node.parent.method_name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def count_block?(block)
|
59
|
+
KNOWN_ITERATING_METHODS.include? block.method_name
|
60
|
+
end
|
29
61
|
end
|
30
62
|
end
|
31
63
|
end
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# > Condition -- a logical/Boolean test, == != <= >= < > else case
|
20
20
|
# > default try catch ? and unary conditionals.
|
21
21
|
# > http://c2.com/cgi/wiki?AbcMetric
|
22
|
-
CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze
|
22
|
+
CONDITION_NODES = (CyclomaticComplexity::COUNTED_NODES - %i[block block_pass]).freeze
|
23
23
|
|
24
24
|
def self.calculate(node)
|
25
25
|
new(node).calculate
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Metrics
|
6
|
+
module Utils
|
7
|
+
# Used to identify iterating blocks like `.map{}` and `.map(&:...)`
|
8
|
+
module IteratingBlock
|
9
|
+
enumerable = %i[
|
10
|
+
all? any? chain chunk chunk_while collect collect_concat count cycle
|
11
|
+
detect drop drop_while each each_cons each_entry each_slice
|
12
|
+
each_with_index each_with_object entries filter filter_map find
|
13
|
+
find_all find_index flat_map grep grep_v group_by inject lazy map
|
14
|
+
max max_by min min_by minmax minmax_by none? one? partition reduce
|
15
|
+
reject reverse_each select slice_after slice_before slice_when sort
|
16
|
+
sort_by sum take take_while tally to_h uniq zip
|
17
|
+
]
|
18
|
+
|
19
|
+
enumerator = %i[with_index with_object]
|
20
|
+
|
21
|
+
array = %i[
|
22
|
+
bsearch bsearch_index collect! combination d_permutation delete_if
|
23
|
+
each_index keep_if map! permutation product reject! repeat
|
24
|
+
repeated_combination select! sort sort! sort_by sort_by
|
25
|
+
]
|
26
|
+
|
27
|
+
hash = %i[
|
28
|
+
each_key each_pair each_value fetch fetch_values has_key? merge
|
29
|
+
merge! transform_keys transform_keys! transform_values
|
30
|
+
transform_values!
|
31
|
+
]
|
32
|
+
|
33
|
+
KNOWN_ITERATING_METHODS = (Set.new(enumerable) + enumerator + array + hash).freeze
|
34
|
+
|
35
|
+
# Returns the name of the method called with a block
|
36
|
+
# if node is a block node, or a block-pass node.
|
37
|
+
def block_method_name(node)
|
38
|
+
case node.type
|
39
|
+
when :block
|
40
|
+
node.method_name
|
41
|
+
when :block_pass
|
42
|
+
node.parent.method_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns true iff name is a known iterating type (e.g. :each, :transform_values)
|
47
|
+
def iterating_method?(name)
|
48
|
+
KNOWN_ITERATING_METHODS.include? name
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns nil if node is neither a block node or a block-pass node.
|
52
|
+
# Otherwise returns true/false if method call is a known iterating call
|
53
|
+
def iterating_block?(node)
|
54
|
+
name = block_method_name(node)
|
55
|
+
name && iterating_method?(name)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -41,11 +41,11 @@ module RuboCop
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def interpreter_directive_comment?(comment)
|
44
|
-
|
44
|
+
/^#\s*(frozen_string_literal|encoding):/.match?(comment.text)
|
45
45
|
end
|
46
46
|
|
47
47
|
def rubocop_directive_comment?(comment)
|
48
|
-
comment.text
|
48
|
+
CommentConfig::COMMENT_DIRECTIVE_REGEXP.match?(comment.text)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
@@ -51,7 +51,7 @@ module RuboCop
|
|
51
51
|
|
52
52
|
def accept_end_kw_alignment?(end_loc)
|
53
53
|
end_loc.nil? || # Discard modifier forms of if/while/until.
|
54
|
-
processed_source.lines[end_loc.line - 1]
|
54
|
+
!/\A[ \t]*end/.match?(processed_source.lines[end_loc.line - 1])
|
55
55
|
end
|
56
56
|
|
57
57
|
def style_parameter_name
|
@@ -152,7 +152,7 @@ module RuboCop
|
|
152
152
|
expression
|
153
153
|
end
|
154
154
|
|
155
|
-
def argument_in_method_call(node, kind)
|
155
|
+
def argument_in_method_call(node, kind) # rubocop:todo Metrics/CyclomaticComplexity
|
156
156
|
node.each_ancestor(:send, :block).find do |a|
|
157
157
|
# If the node is inside a block, it makes no difference if that block
|
158
158
|
# is an argument in a method call. It doesn't count.
|
@@ -9,8 +9,7 @@ module RuboCop
|
|
9
9
|
def parens_required?(node)
|
10
10
|
range = node.source_range
|
11
11
|
source = range.source_buffer.source
|
12
|
-
source[range.begin_pos - 1]
|
13
|
-
source[range.end_pos] =~ /[a-z]/
|
12
|
+
/[a-z]/.match?(source[range.begin_pos - 1]) || /[a-z]/.match?(source[range.end_pos])
|
14
13
|
end
|
15
14
|
end
|
16
15
|
end
|
@@ -109,7 +109,7 @@ module RuboCop
|
|
109
109
|
|
110
110
|
def move_pos(src, pos, step, condition, regexp)
|
111
111
|
offset = step == -1 ? -1 : 0
|
112
|
-
pos += step while condition && src[pos + offset]
|
112
|
+
pos += step while condition && regexp.match?(src[pos + offset])
|
113
113
|
pos.negative? ? 0 : pos
|
114
114
|
end
|
115
115
|
end
|
@@ -11,6 +11,33 @@ module RuboCop
|
|
11
11
|
|
12
12
|
regopt.children.include?(:x)
|
13
13
|
end
|
14
|
+
|
15
|
+
def pattern_source(node)
|
16
|
+
freespace_mode = freespace_mode_regexp?(node)
|
17
|
+
|
18
|
+
node.children.reject(&:regopt_type?).map do |child|
|
19
|
+
source_with_comments_and_interpolations_blanked(child, freespace_mode)
|
20
|
+
end.join
|
21
|
+
end
|
22
|
+
|
23
|
+
def source_with_comments_and_interpolations_blanked(child, freespace_mode)
|
24
|
+
source = child.source
|
25
|
+
|
26
|
+
# We don't want to consider the contents of interpolations or free-space mode comments as
|
27
|
+
# part of the pattern source, but need to preserve their width, to allow offsets to
|
28
|
+
# correctly line up with the original source: spaces have no effect, and preserve width.
|
29
|
+
if child.begin_type?
|
30
|
+
replace_match_with_spaces(source, /.*/) # replace all content
|
31
|
+
elsif freespace_mode
|
32
|
+
replace_match_with_spaces(source, /(?<!\\)#.*/) # replace any comments
|
33
|
+
else
|
34
|
+
source
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def replace_match_with_spaces(source, pattern)
|
39
|
+
source.sub(pattern) { ' ' * Regexp.last_match[0].length }
|
40
|
+
end
|
14
41
|
end
|
15
42
|
end
|
16
43
|
end
|
@@ -87,15 +87,15 @@ module RuboCop
|
|
87
87
|
return false unless token
|
88
88
|
|
89
89
|
if side == :left
|
90
|
-
String(token.space_after?)
|
90
|
+
SINGLE_SPACE_REGEXP.match?(String(token.space_after?))
|
91
91
|
else
|
92
|
-
String(token.space_before?)
|
92
|
+
SINGLE_SPACE_REGEXP.match?(String(token.space_before?))
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
96
|
def reposition(src, pos, step)
|
97
97
|
offset = step == -1 ? -1 : 0
|
98
|
-
pos += step while src[pos + offset]
|
98
|
+
pos += step while SINGLE_SPACE_REGEXP.match?(src[pos + offset])
|
99
99
|
pos.negative? ? 0 : pos
|
100
100
|
end
|
101
101
|
|
@@ -31,7 +31,7 @@ module RuboCop
|
|
31
31
|
# If there is any heredoc in items, then match the comma succeeding
|
32
32
|
# any whitespace (except newlines), otherwise allow for newlines
|
33
33
|
comma_regex = any_heredoc?(items) ? /\A[^\S\n]*,/ : /\A\s*,/
|
34
|
-
range.source
|
34
|
+
comma_regex.match?(range.source) && range.source.index(',')
|
35
35
|
end
|
36
36
|
|
37
37
|
def check_comma(node, kind, comma_pos)
|
@@ -44,7 +44,7 @@ module RuboCop
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def uppercase?(name)
|
47
|
-
|
47
|
+
/[[:upper:]]/.match?(name)
|
48
48
|
end
|
49
49
|
|
50
50
|
def name_type(node)
|
@@ -62,7 +62,7 @@ module RuboCop
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def ends_with_num?(name)
|
65
|
-
name[-1]
|
65
|
+
/\d/.match?(name[-1])
|
66
66
|
end
|
67
67
|
|
68
68
|
def length_offense(node, range)
|