rbhint 0.85.1.rc1 → 0.85.1.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/assets/output.html.erb +1 -1
- data/config/default.yml +13 -1
- data/lib/rbhint.rb +8 -0
- data/lib/rbhint/version.rb +1 -1
- data/lib/rubocop.rb +2 -0
- 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/assignment_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +4 -4
- data/lib/rubocop/cop/layout/condition_position.rb +1 -1
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +2 -2
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
- data/lib/rubocop/cop/layout/extra_spacing.rb +6 -6
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/leading_empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +6 -6
- data/lib/rubocop/cop/layout/space_around_operators.rb +2 -2
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
- 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/layout/space_inside_string_interpolation.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- 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 +4 -4
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
- data/lib/rubocop/cop/metrics/abc_size.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_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/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
- metadata +10 -2
@@ -186,7 +186,7 @@ module RuboCop
|
|
186
186
|
pos = range.begin_pos - 1
|
187
187
|
return false if pos.negative?
|
188
188
|
|
189
|
-
|
189
|
+
!/[\s(|{\[;,*=]/.match?(range.source_buffer.source[pos])
|
190
190
|
end
|
191
191
|
|
192
192
|
def space_after_missing?(range)
|
@@ -198,7 +198,7 @@ module RuboCop
|
|
198
198
|
return false if accept_namespace_operator?(range) &&
|
199
199
|
namespace_operator?(range, pos)
|
200
200
|
|
201
|
-
|
201
|
+
!/[\s;,#\\)}\].]/.match?(char)
|
202
202
|
end
|
203
203
|
|
204
204
|
def accepted_opening_delimiter?(range, char)
|
@@ -18,9 +18,9 @@ module RuboCop
|
|
18
18
|
# foo &.bar
|
19
19
|
# foo &. bar
|
20
20
|
# foo &. bar&. buzz
|
21
|
-
#
|
22
|
-
#
|
23
|
-
# ::
|
21
|
+
# RbHint:: Hint
|
22
|
+
# RbHint:: Hint:: Hint
|
23
|
+
# :: RbHint::Hint
|
24
24
|
#
|
25
25
|
# # good
|
26
26
|
# foo.bar
|
@@ -30,9 +30,9 @@ module RuboCop
|
|
30
30
|
# .buzz
|
31
31
|
# foo&.bar
|
32
32
|
# foo&.bar&.buzz
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# ::
|
33
|
+
# RbHint::Hint
|
34
|
+
# RbHint::Hint::Hint
|
35
|
+
# ::RbHint::Hint
|
36
36
|
#
|
37
37
|
class SpaceAroundMethodCallOperator < Cop
|
38
38
|
include SurroundingSpace
|
@@ -6,7 +6,7 @@ module RuboCop
|
|
6
6
|
# Checks that operators have space around them, except for ** which
|
7
7
|
# should or shouldn't have surrounding space depending on configuration.
|
8
8
|
#
|
9
|
-
# This
|
9
|
+
# This hint has `AllowForAlignment` option. When `true`, allows most
|
10
10
|
# uses of extra spacing if the intent is to align with an operator on
|
11
11
|
# the previous or next line, not counting empty lines or comment lines.
|
12
12
|
#
|
@@ -134,7 +134,7 @@ module RuboCop
|
|
134
134
|
|
135
135
|
def autocorrect(range)
|
136
136
|
lambda do |corrector|
|
137
|
-
if range.source
|
137
|
+
if /\*\*/.match?(range.source) && !space_around_exponent_operator?
|
138
138
|
corrector.replace(range, '**')
|
139
139
|
elsif range.source.end_with?("\n")
|
140
140
|
corrector.replace(range, " #{range.source.strip}\n")
|
@@ -27,6 +27,20 @@ module RuboCop
|
|
27
27
|
# foo.map{ |a|
|
28
28
|
# a.bar.to_s
|
29
29
|
# }
|
30
|
+
#
|
31
|
+
# @example EnforcedStyleForEmptyBraces: space (default)
|
32
|
+
# # bad
|
33
|
+
# 7.times{}
|
34
|
+
#
|
35
|
+
# # good
|
36
|
+
# 7.times {}
|
37
|
+
#
|
38
|
+
# @example EnforcedStyleForEmptyBraces: no_space
|
39
|
+
# # bad
|
40
|
+
# 7.times {}
|
41
|
+
#
|
42
|
+
# # good
|
43
|
+
# 7.times{}
|
30
44
|
class SpaceBeforeBlockBraces < Cop
|
31
45
|
include ConfigurableEnforcedStyle
|
32
46
|
include RangeHelp
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Layout
|
6
|
-
# This
|
6
|
+
# This hint checks for spaces between `->` and opening parameter
|
7
7
|
# parenthesis (`(`) in lambda literals.
|
8
8
|
#
|
9
9
|
# @example EnforcedStyle: require_no_space (default)
|
@@ -150,7 +150,7 @@ module RuboCop
|
|
150
150
|
end
|
151
151
|
|
152
152
|
def check_right_brace(inner, left_brace, right_brace, single_line)
|
153
|
-
if single_line &&
|
153
|
+
if single_line && /\S$/.match?(inner)
|
154
154
|
no_space(right_brace.begin_pos, right_brace.end_pos,
|
155
155
|
'Space missing inside }.')
|
156
156
|
else
|
@@ -182,7 +182,7 @@ module RuboCop
|
|
182
182
|
def range_of_space_to_the_right(range)
|
183
183
|
src = range.source_buffer.source
|
184
184
|
end_pos = range.end_pos
|
185
|
-
end_pos += 1 while
|
185
|
+
end_pos += 1 while /[ \t]/.match?(src[end_pos])
|
186
186
|
|
187
187
|
range_between(range.begin_pos + 1, end_pos)
|
188
188
|
end
|
@@ -190,7 +190,7 @@ module RuboCop
|
|
190
190
|
def range_of_space_to_the_left(range)
|
191
191
|
src = range.source_buffer.source
|
192
192
|
begin_pos = range.begin_pos
|
193
|
-
begin_pos -= 1 while src[begin_pos - 1]
|
193
|
+
begin_pos -= 1 while /[ \t]/.match?(src[begin_pos - 1])
|
194
194
|
|
195
195
|
range_between(begin_pos, range.end_pos - 1)
|
196
196
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Check that certain constants are fully qualified.
|
7
|
+
#
|
8
|
+
# This is not enabled by default because it would mark a lot of offenses
|
9
|
+
# unnecessarily.
|
10
|
+
#
|
11
|
+
# Generally, gems should fully qualify all constants to avoid conflicts with
|
12
|
+
# the code that uses the gem. Enable this cop without using `Only`/`Ignore`
|
13
|
+
#
|
14
|
+
# Large projects will over time end up with one or two constant names that
|
15
|
+
# are problematic because of a conflict with a library or just internally
|
16
|
+
# using the same name a namespace and a class. To avoid too many unnecessary
|
17
|
+
# offenses, Enable this cop with `Only: [The, Constant, Names, Causing, Issues]`
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# # By default checks every constant
|
21
|
+
#
|
22
|
+
# # bad
|
23
|
+
# User
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# User::Login
|
27
|
+
#
|
28
|
+
# # good
|
29
|
+
# ::User
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# ::User::Login
|
33
|
+
#
|
34
|
+
# @example Only: ['Login']
|
35
|
+
# # Restrict this cop to only being concerned about certain constants
|
36
|
+
#
|
37
|
+
# # bad
|
38
|
+
# Login
|
39
|
+
#
|
40
|
+
# # good
|
41
|
+
# ::Login
|
42
|
+
#
|
43
|
+
# # good
|
44
|
+
# User::Login
|
45
|
+
#
|
46
|
+
# @example Ignore: ['Login']
|
47
|
+
# # Restrict this cop not being concerned about certain constants
|
48
|
+
#
|
49
|
+
# # bad
|
50
|
+
# User
|
51
|
+
#
|
52
|
+
# # good
|
53
|
+
# ::User::Login
|
54
|
+
#
|
55
|
+
# # good
|
56
|
+
# Login
|
57
|
+
#
|
58
|
+
class ConstantResolution < Cop
|
59
|
+
MSG = 'Fully qualify this constant to avoid possibly ambiguous resolution.'
|
60
|
+
|
61
|
+
def_node_matcher :unqualified_const?, <<~PATTERN
|
62
|
+
(const nil? #const_name?)
|
63
|
+
PATTERN
|
64
|
+
|
65
|
+
def on_const(node)
|
66
|
+
return unless unqualified_const?(node)
|
67
|
+
|
68
|
+
add_offense(node)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def const_name?(name)
|
74
|
+
name = name.to_s
|
75
|
+
(allowed_names.empty? || allowed_names.include?(name)) &&
|
76
|
+
!ignored_names.include?(name)
|
77
|
+
end
|
78
|
+
|
79
|
+
def allowed_names
|
80
|
+
cop_config['Only']
|
81
|
+
end
|
82
|
+
|
83
|
+
def ignored_names
|
84
|
+
cop_config['Ignore']
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -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)
|
@@ -47,10 +47,10 @@ module RuboCop
|
|
47
47
|
private
|
48
48
|
|
49
49
|
def check(node)
|
50
|
-
lambda do |cbase|
|
50
|
+
lambda do |exception_class, cbase|
|
51
51
|
return if cbase.nil? && implicit_namespace?(node)
|
52
52
|
|
53
|
-
add_offense(
|
53
|
+
add_offense(exception_class)
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
@@ -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)
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Metrics
|
6
|
-
# This
|
6
|
+
# This hint checks that the ABC size of methods is not higher than the
|
7
7
|
# configured maximum. The ABC size is based on assignments, branches
|
8
8
|
# (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
|
9
9
|
# and https://en.wikipedia.org/wiki/ABC_Software_Metric.
|
@@ -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
|