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.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -2
  3. data/assets/output.html.erb +1 -1
  4. data/config/default.yml +13 -1
  5. data/lib/rbhint.rb +8 -0
  6. data/lib/rbhint/version.rb +1 -1
  7. data/lib/rubocop.rb +2 -0
  8. data/lib/rubocop/config.rb +1 -1
  9. data/lib/rubocop/config_loader_resolver.rb +1 -1
  10. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  11. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  12. data/lib/rubocop/cop/layout/assignment_indentation.rb +2 -2
  13. data/lib/rubocop/cop/layout/block_alignment.rb +1 -1
  14. data/lib/rubocop/cop/layout/block_end_newline.rb +1 -1
  15. data/lib/rubocop/cop/layout/case_indentation.rb +2 -2
  16. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
  17. data/lib/rubocop/cop/layout/comment_indentation.rb +4 -4
  18. data/lib/rubocop/cop/layout/condition_position.rb +1 -1
  19. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  20. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  21. data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
  22. data/lib/rubocop/cop/layout/empty_comment.rb +2 -2
  23. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
  24. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
  25. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  26. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +1 -1
  27. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +2 -0
  28. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +1 -1
  29. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -1
  30. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
  31. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -2
  32. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +1 -1
  34. data/lib/rubocop/cop/layout/end_alignment.rb +1 -1
  35. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  36. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -6
  37. data/lib/rubocop/cop/layout/first_argument_indentation.rb +3 -3
  38. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  39. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +2 -2
  40. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
  41. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +1 -1
  42. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -1
  43. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +1 -1
  44. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  45. data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
  46. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
  47. data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -3
  48. data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -1
  49. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  50. data/lib/rubocop/cop/layout/indentation_width.rb +2 -2
  51. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  52. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  53. data/lib/rubocop/cop/layout/leading_empty_lines.rb +1 -1
  54. data/lib/rubocop/cop/layout/line_length.rb +6 -6
  55. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +1 -1
  56. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +1 -1
  57. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  58. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +1 -1
  60. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +1 -1
  61. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  62. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
  63. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  64. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
  65. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
  66. data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
  67. data/lib/rubocop/cop/layout/space_after_colon.rb +2 -2
  68. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  69. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  70. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +6 -6
  71. data/lib/rubocop/cop/layout/space_around_operators.rb +2 -2
  72. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  73. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  74. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  75. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -1
  76. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  77. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  78. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -1
  79. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
  80. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  81. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  82. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  83. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +6 -1
  84. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  85. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  86. data/lib/rubocop/cop/lint/raise_exception.rb +4 -4
  87. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +4 -2
  88. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  89. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  90. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  91. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  92. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  93. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  94. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  95. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  96. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  97. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  98. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  99. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  100. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  101. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
  102. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  103. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  104. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  105. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +2 -2
  106. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  107. data/lib/rubocop/cop/naming/file_name.rb +1 -3
  108. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  109. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  110. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  111. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  112. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  113. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  114. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
  115. data/lib/rubocop/cop/style/copyright.rb +3 -3
  116. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  117. data/lib/rubocop/cop/style/documentation.rb +2 -2
  118. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  119. data/lib/rubocop/cop/style/encoding.rb +1 -1
  120. data/lib/rubocop/cop/style/exponential_notation.rb +2 -2
  121. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  122. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  123. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  124. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  125. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  126. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  127. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  128. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  129. data/lib/rubocop/cop/style/next.rb +2 -2
  130. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  131. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  132. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  133. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  134. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  135. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
  136. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  137. data/lib/rubocop/cop/style/sample.rb +1 -1
  138. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  139. data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
  140. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  141. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  142. data/lib/rubocop/cop/style/word_array.rb +1 -1
  143. data/lib/rubocop/cop/util.rb +2 -2
  144. data/lib/rubocop/cop/utils/format_string.rb +1 -1
  145. data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
  146. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  147. data/lib/rubocop/name_similarity.rb +6 -0
  148. data/lib/rubocop/path_util.rb +2 -2
  149. data/lib/rubocop/platform.rb +1 -1
  150. data/lib/rubocop/rspec/expect_offense.rb +12 -2
  151. data/lib/rubocop/target_finder.rb +1 -1
  152. data/lib/rubocop/target_ruby.rb +1 -1
  153. 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
- range.source_buffer.source[pos] !~ /[\s(|{\[;,*=]/
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
- char !~ /[\s;,#\\)}\].]/
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
- # RuboCop:: Cop
22
- # RuboCop:: Cop:: Cop
23
- # :: RuboCop::Cop
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
- # RuboCop::Cop
34
- # RuboCop::Cop::Cop
35
- # ::RuboCop::Cop
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 cop has `AllowForAlignment` option. When `true`, allows most
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 =~ /\*\*/ && !space_around_exponent_operator?
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 cop checks for missing space between a token and a comment on the
6
+ # This hint checks for missing space between a token and a comment on the
7
7
  # same line.
8
8
  #
9
9
  # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # This cop checks for spaces between `->` and opening parameter
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)
@@ -130,7 +130,7 @@ module RuboCop
130
130
  line, col = line_and_column_for(token)
131
131
  return true if col == -1
132
132
 
133
- processed_source.lines[line][0..col] !~ /\S/
133
+ !/\S/.match?(processed_source.lines[line][0..col])
134
134
  end
135
135
 
136
136
  def index_for(node, token)
@@ -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 && inner =~ /\S$/
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 src[end_pos] =~ /[ \t]/
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] =~ /[ \t]/
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
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # This cop checks for whitespace within string interpolations.
6
+ # This hint checks for whitespace within string interpolations.
7
7
  #
8
8
  # @example EnforcedStyle: no_space (default)
9
9
  # # bad
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # This cop looks for trailing blank lines and a final newline in the
6
+ # This hint looks for trailing blank lines and a final newline in the
7
7
  # source code.
8
8
  #
9
9
  # @example EnforcedStyle: final_blank_line
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- # This cop looks for trailing whitespace in the source code.
6
+ # This hint looks for trailing whitespace in the source code.
7
7
  #
8
8
  # @example
9
9
  # # The line in this example contains spaces after the 0.
@@ -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
@@ -25,7 +25,7 @@ module RuboCop
25
25
  value, = *node
26
26
 
27
27
  return unless value.infinite? ||
28
- value.zero? && node.source =~ /[1-9]/
28
+ value.zero? && /[1-9]/.match?(node.source)
29
29
 
30
30
  add_offense(node)
31
31
  end
@@ -65,7 +65,12 @@ module RuboCop
65
65
  end
66
66
 
67
67
  def invalid_format_string?(node)
68
- !RuboCop::Cop::Utils::FormatString.new(node.source).valid?
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)
@@ -62,7 +62,7 @@ module RuboCop
62
62
  # To avoid likely false positives (e.g. a single ' or ")
63
63
  next if literal.gsub(/[^[[:alnum:]]]/, '').empty?
64
64
 
65
- QUOTES_AND_COMMAS.any? { |pat| literal =~ pat }
65
+ QUOTES_AND_COMMAS.any? { |pat| literal.match?(pat) }
66
66
  end
67
67
  end
68
68
  end
@@ -61,7 +61,7 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def non_alphanumeric_literal?(literal)
64
- literal !~ /[[:alnum:]]/
64
+ !/[[:alnum:]]/.match?(literal)
65
65
  end
66
66
  end
67
67
  end
@@ -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(node)
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
- comment.text =~ /rubocop\s*:\s*(?:disable|todo)\s+all\b/
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| intervening =~ /\A\s*,\s*\Z/ }
230
+ .all? { |intervening| /\A\s*,\s*\Z/.match?(intervening) }
229
231
  end
230
232
 
231
233
  def describe(cop)
@@ -30,7 +30,7 @@ module RuboCop
30
30
  def on_if(node)
31
31
  return unless safe_navigation_empty_in_conditional?(node)
32
32
 
33
- add_offense(node)
33
+ add_offense(node.condition)
34
34
  end
35
35
  end
36
36
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Metrics
6
- # This cop checks that the ABC size of methods is not higher than the
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(_node)
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