rubocop 0.85.0 → 0.88.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -17
  3. data/bin/rubocop-profile +31 -0
  4. data/config/default.yml +132 -11
  5. data/lib/rubocop.rb +17 -1
  6. data/lib/rubocop/cli.rb +2 -4
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +1 -1
  10. data/lib/rubocop/config.rb +1 -1
  11. data/lib/rubocop/config_loader.rb +39 -67
  12. data/lib/rubocop/config_loader_resolver.rb +1 -1
  13. data/lib/rubocop/config_obsoletion.rb +0 -1
  14. data/lib/rubocop/config_store.rb +4 -0
  15. data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
  16. data/lib/rubocop/cop/badge.rb +1 -1
  17. data/lib/rubocop/cop/base.rb +407 -0
  18. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
  19. data/lib/rubocop/cop/commissioner.rb +48 -50
  20. data/lib/rubocop/cop/cop.rb +91 -235
  21. data/lib/rubocop/cop/corrector.rb +38 -115
  22. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
  23. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +7 -2
  24. data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +1 -1
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
  28. data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
  29. data/lib/rubocop/cop/layout/class_structure.rb +2 -37
  30. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  31. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  32. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
  33. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +3 -8
  34. data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
  35. data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -1
  37. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  38. data/lib/rubocop/cop/layout/hash_alignment.rb +2 -3
  39. data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
  40. data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
  41. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
  43. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  44. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
  45. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  46. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  47. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +4 -3
  48. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  50. data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
  51. data/lib/rubocop/cop/legacy/corrector.rb +29 -0
  52. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  53. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
  54. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +8 -2
  55. data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
  56. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
  57. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  58. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  59. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
  60. data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
  61. data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
  62. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +8 -1
  63. data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
  64. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +69 -2
  65. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
  66. data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
  67. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  68. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  69. data/lib/rubocop/cop/lint/rand_one.rb +1 -1
  70. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
  71. data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
  72. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  73. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
  74. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  75. data/lib/rubocop/cop/lint/syntax.rb +11 -26
  76. data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
  77. data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
  78. data/lib/rubocop/cop/metrics/block_length.rb +22 -0
  79. data/lib/rubocop/cop/metrics/class_length.rb +25 -2
  80. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  81. data/lib/rubocop/cop/metrics/method_length.rb +23 -0
  82. data/lib/rubocop/cop/metrics/module_length.rb +25 -2
  83. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  84. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
  85. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  86. data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
  87. data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
  88. data/lib/rubocop/cop/mixin/code_length.rb +4 -0
  89. data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
  90. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  91. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  92. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  93. data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
  94. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  95. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  96. data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
  97. data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -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/statement_modifier.rb +3 -3
  103. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  104. data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
  105. data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  107. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +6 -4
  108. data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
  109. data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
  110. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
  111. data/lib/rubocop/cop/naming/file_name.rb +1 -3
  112. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  113. data/lib/rubocop/cop/naming/method_name.rb +1 -1
  114. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  115. data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
  116. data/lib/rubocop/cop/naming/variable_name.rb +1 -1
  117. data/lib/rubocop/cop/naming/variable_number.rb +1 -1
  118. data/lib/rubocop/cop/offense.rb +16 -2
  119. data/lib/rubocop/cop/registry.rb +62 -7
  120. data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
  121. data/lib/rubocop/cop/style/array_coercion.rb +63 -0
  122. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
  123. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  124. data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
  125. data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
  126. data/lib/rubocop/cop/style/case_like_if.rb +217 -0
  127. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  128. data/lib/rubocop/cop/style/class_vars.rb +21 -0
  129. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  130. data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
  131. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  132. data/lib/rubocop/cop/style/copyright.rb +3 -3
  133. data/lib/rubocop/cop/style/date_time.rb +1 -1
  134. data/lib/rubocop/cop/style/dir.rb +2 -2
  135. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  136. data/lib/rubocop/cop/style/documentation.rb +2 -2
  137. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  138. data/lib/rubocop/cop/style/empty_literal.rb +5 -5
  139. data/lib/rubocop/cop/style/encoding.rb +1 -1
  140. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  141. data/lib/rubocop/cop/style/exponential_notation.rb +8 -10
  142. data/lib/rubocop/cop/style/float_division.rb +7 -10
  143. data/lib/rubocop/cop/style/format_string_token.rb +5 -5
  144. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  145. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
  146. data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
  147. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  148. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  149. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -11
  150. data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
  151. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  152. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  153. data/lib/rubocop/cop/style/missing_else.rb +1 -11
  154. data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
  155. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  156. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  157. data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
  158. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
  159. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  160. data/lib/rubocop/cop/style/next.rb +2 -2
  161. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  162. data/lib/rubocop/cop/style/numeric_predicate.rb +3 -4
  163. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
  164. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  165. data/lib/rubocop/cop/style/proc.rb +1 -1
  166. data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
  167. data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
  168. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  169. data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
  170. data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
  171. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
  172. data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
  173. data/lib/rubocop/cop/style/redundant_parentheses.rb +8 -2
  174. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  175. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +4 -3
  176. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
  177. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  178. data/lib/rubocop/cop/style/redundant_sort.rb +3 -2
  179. data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
  180. data/lib/rubocop/cop/style/sample.rb +1 -1
  181. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  182. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  183. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
  184. data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
  185. data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
  186. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  187. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  188. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  189. data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
  190. data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
  191. data/lib/rubocop/cop/style/word_array.rb +1 -1
  192. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  193. data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
  194. data/lib/rubocop/cop/team.rb +105 -81
  195. data/lib/rubocop/cop/util.rb +2 -2
  196. data/lib/rubocop/cop/utils/format_string.rb +19 -2
  197. data/lib/rubocop/cop/variable_force/variable.rb +5 -3
  198. data/lib/rubocop/file_finder.rb +12 -12
  199. data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
  200. data/lib/rubocop/formatter/formatter_set.rb +1 -1
  201. data/lib/rubocop/name_similarity.rb +7 -3
  202. data/lib/rubocop/options.rb +15 -8
  203. data/lib/rubocop/path_util.rb +4 -19
  204. data/lib/rubocop/platform.rb +1 -1
  205. data/lib/rubocop/rake_task.rb +6 -9
  206. data/lib/rubocop/result_cache.rb +12 -8
  207. data/lib/rubocop/rspec/cop_helper.rb +4 -4
  208. data/lib/rubocop/rspec/expect_offense.rb +65 -21
  209. data/lib/rubocop/rspec/shared_contexts.rb +19 -16
  210. data/lib/rubocop/runner.rb +34 -33
  211. data/lib/rubocop/target_finder.rb +3 -3
  212. data/lib/rubocop/target_ruby.rb +2 -2
  213. data/lib/rubocop/version.rb +1 -1
  214. metadata +34 -9
  215. data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -30,7 +30,15 @@ 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
+ end
35
+
36
+ def autocorrect(node)
37
+ lambda do |corrector|
38
+ receiver = node.receiver.source
39
+
40
+ corrector.replace(node, "#{receiver} && #{receiver}.#{node.method_name}")
41
+ end
34
42
  end
35
43
  end
36
44
  end
@@ -78,7 +78,7 @@ module RuboCop
78
78
 
79
79
  def comment_between_rescue_and_end?(node)
80
80
  end_line = nil
81
- node.each_ancestor(:kwbegin, :def) do |ancestor|
81
+ node.each_ancestor(:kwbegin, :def, :defs, :block) do |ancestor|
82
82
  end_line = ancestor.loc.end.line
83
83
  break
84
84
  end
@@ -3,49 +3,34 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # This is not actually a cop. It does not inspect anything. It just
7
- # provides methods to repack Parser's diagnostics/errors
6
+ # This cop repacks Parser's diagnostics/errors
8
7
  # into RuboCop's offenses.
9
- class Syntax < Cop
10
- PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
11
- :end_pos)
12
-
13
- ERROR_SOURCE_RANGE = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
14
-
15
- def self.offenses_from_processed_source(processed_source,
16
- config, options)
17
- cop = new(config, options)
18
-
19
- cop.add_offense_from_error(processed_source.parser_error) if processed_source.parser_error
20
-
8
+ class Syntax < Base
9
+ def on_other_file
10
+ add_offense_from_error(processed_source.parser_error) if processed_source.parser_error
21
11
  processed_source.diagnostics.each do |diagnostic|
22
- cop.add_offense_from_diagnostic(diagnostic,
23
- processed_source.ruby_version)
12
+ add_offense_from_diagnostic(diagnostic,
13
+ processed_source.ruby_version)
24
14
  end
25
-
26
- cop.offenses
15
+ super
27
16
  end
28
17
 
18
+ private
19
+
29
20
  def add_offense_from_diagnostic(diagnostic, ruby_version)
30
21
  message =
31
22
  "#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
32
23
  'configure using `TargetRubyVersion` parameter, under `AllCops`)'
33
- add_offense(nil,
34
- location: diagnostic.location,
24
+ add_offense(diagnostic.location,
35
25
  message: message,
36
26
  severity: diagnostic.level)
37
27
  end
38
28
 
39
29
  def add_offense_from_error(error)
40
30
  message = beautify_message(error.message)
41
- add_offense(nil,
42
- location: ERROR_SOURCE_RANGE,
43
- message: message,
44
- severity: :fatal)
31
+ add_global_offense(message, severity: :fatal)
45
32
  end
46
33
 
47
- private
48
-
49
34
  def beautify_message(message)
50
35
  message = message.capitalize
51
36
  message << '.' unless message.end_with?('.')
@@ -62,7 +62,7 @@ module RuboCop
62
62
  include UnusedArgument
63
63
 
64
64
  def_node_matcher :not_implemented?, <<~PATTERN
65
- {(send nil? :raise (const nil? :NotImplementedError))
65
+ {(send nil? :raise (const {nil? cbase} :NotImplementedError))
66
66
  (send nil? :fail ...)}
67
67
  PATTERN
68
68
 
@@ -172,7 +172,7 @@ module RuboCop
172
172
  PATTERN
173
173
 
174
174
  def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
175
- (block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
175
+ (block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
176
176
  PATTERN
177
177
 
178
178
  def check_node(node)
@@ -7,6 +7,28 @@ module RuboCop
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
9
  # The cop can be configured to ignore blocks passed to certain methods.
10
+ #
11
+ # You can set literals you want to fold with `CountAsOne`.
12
+ # Available are: 'array', 'hash', and 'heredoc'. Each literal
13
+ # will be counted as one line regardless of its actual size.
14
+ #
15
+ # @example CountAsOne: ['array', 'heredoc']
16
+ #
17
+ # something do
18
+ # array = [ # +1
19
+ # 1,
20
+ # 2
21
+ # ]
22
+ #
23
+ # hash = { # +3
24
+ # key: 'value'
25
+ # }
26
+ #
27
+ # msg = <<~HEREDOC # +1
28
+ # Heredoc
29
+ # content.
30
+ # HEREDOC
31
+ # end # 5 points
10
32
  class BlockLength < Cop
11
33
  include TooManyLines
12
34
 
@@ -6,8 +6,31 @@ module RuboCop
6
6
  # This cop checks if the length a class exceeds some maximum value.
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
+ #
10
+ # You can set literals you want to fold with `CountAsOne`.
11
+ # Available are: 'array', 'hash', and 'heredoc'. Each literal
12
+ # will be counted as one line regardless of its actual size.
13
+ #
14
+ # @example CountAsOne: ['array', 'heredoc']
15
+ #
16
+ # class Foo
17
+ # ARRAY = [ # +1
18
+ # 1,
19
+ # 2
20
+ # ]
21
+ #
22
+ # HASH = { # +3
23
+ # key: 'value'
24
+ # }
25
+ #
26
+ # MSG = <<~HEREDOC # +1
27
+ # Heredoc
28
+ # content.
29
+ # HEREDOC
30
+ # end # 5 points
31
+ #
9
32
  class ClassLength < Cop
10
- include ClassishLength
33
+ include TooManyLines
11
34
 
12
35
  def on_class(node)
13
36
  check_code_length(node)
@@ -22,7 +45,7 @@ module RuboCop
22
45
  private
23
46
 
24
47
  def_node_matcher :class_definition?, <<~PATTERN
25
- (casgn nil? _ (block (send (const nil? :Class) :new) ...))
48
+ (casgn nil? _ (block (send (const {nil? cbase} :Class) :new) ...))
26
49
  PATTERN
27
50
 
28
51
  def message(length, max_length)
@@ -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
@@ -6,6 +6,29 @@ module RuboCop
6
6
  # This cop checks if the length of a method exceeds some maximum value.
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
+ #
10
+ # You can set literals you want to fold with `CountAsOne`.
11
+ # Available are: 'array', 'hash', and 'heredoc'. Each literal
12
+ # will be counted as one line regardless of its actual size.
13
+ #
14
+ # @example CountAsOne: ['array', 'heredoc']
15
+ #
16
+ # def m
17
+ # array = [ # +1
18
+ # 1,
19
+ # 2
20
+ # ]
21
+ #
22
+ # hash = { # +3
23
+ # key: 'value'
24
+ # }
25
+ #
26
+ # <<~HEREDOC # +1
27
+ # Heredoc
28
+ # content.
29
+ # HEREDOC
30
+ # end # 5 points
31
+ #
9
32
  class MethodLength < Cop
10
33
  include TooManyLines
11
34
 
@@ -6,8 +6,31 @@ module RuboCop
6
6
  # This cop checks if the length a module exceeds some maximum value.
7
7
  # Comment lines can optionally be ignored.
8
8
  # The maximum allowed length is configurable.
9
+ #
10
+ # You can set literals you want to fold with `CountAsOne`.
11
+ # Available are: 'array', 'hash', and 'heredoc'. Each literal
12
+ # will be counted as one line regardless of its actual size.
13
+ #
14
+ # @example CountAsOne: ['array', 'heredoc']
15
+ #
16
+ # module M
17
+ # ARRAY = [ # +1
18
+ # 1,
19
+ # 2
20
+ # ]
21
+ #
22
+ # HASH = { # +3
23
+ # key: 'value'
24
+ # }
25
+ #
26
+ # MSG = <<~HEREDOC # +1
27
+ # Heredoc
28
+ # content.
29
+ # HEREDOC
30
+ # end # 5 points
31
+ #
9
32
  class ModuleLength < Cop
10
- include ClassishLength
33
+ include TooManyLines
11
34
 
12
35
  def on_module(node)
13
36
  check_code_length(node)
@@ -22,7 +45,7 @@ module RuboCop
22
45
  private
23
46
 
24
47
  def_node_matcher :module_definition?, <<~PATTERN
25
- (casgn nil? _ (block (send (const nil? :Module) :new) ...))
48
+ (casgn nil? _ (block (send (const {nil? cbase} :Module) :new) ...))
26
49
  PATTERN
27
50
 
28
51
  def message(length, max_length)
@@ -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,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Metrics
6
+ module Utils
7
+ # Helps to calculate code length for the provided node.
8
+ class CodeLengthCalculator
9
+ extend NodePattern::Macros
10
+ include Util
11
+
12
+ FOLDABLE_TYPES = %i[array hash heredoc].freeze
13
+ CLASSISH_TYPES = %i[class module].freeze
14
+
15
+ def initialize(node, count_comments: false, foldable_types: [])
16
+ @node = node
17
+ @count_comments = count_comments
18
+ @foldable_checks = build_foldable_checks(foldable_types)
19
+ @foldable_types = normalize_foldable_types(foldable_types)
20
+ end
21
+
22
+ def calculate
23
+ length = code_length(@node)
24
+
25
+ each_top_level_descendant(@node, *@foldable_types, *CLASSISH_TYPES) do |descendant|
26
+ descendant_length = code_length(descendant)
27
+
28
+ if classlike_node?(descendant)
29
+ length -= (descendant_length + 2)
30
+ elsif foldable_node?(descendant)
31
+ length = length - descendant_length + 1
32
+ end
33
+ end
34
+
35
+ length
36
+ end
37
+
38
+ private
39
+
40
+ def_node_matcher :class_definition?, <<~PATTERN
41
+ (casgn nil? _ (block (send (const nil? :Class) :new) ...))
42
+ PATTERN
43
+
44
+ def_node_matcher :module_definition?, <<~PATTERN
45
+ (casgn nil? _ (block (send (const nil? :Module) :new) ...))
46
+ PATTERN
47
+
48
+ def build_foldable_checks(types)
49
+ types.map do |type|
50
+ case type
51
+ when :array
52
+ ->(node) { node.array_type? }
53
+ when :hash
54
+ ->(node) { node.hash_type? }
55
+ when :heredoc
56
+ ->(node) { heredoc_node?(node) }
57
+ else
58
+ raise ArgumentError, "Unknown foldable type: #{type.inspect}. "\
59
+ "Valid foldable types are: #{FOLDABLE_TYPES.join(', ')}."
60
+ end
61
+ end
62
+ end
63
+
64
+ def normalize_foldable_types(types)
65
+ types.concat(%i[str dstr]) if types.delete(:heredoc)
66
+ types
67
+ end
68
+
69
+ def code_length(node)
70
+ return heredoc_length(node) if heredoc_node?(node)
71
+
72
+ body = extract_body(node)
73
+ lines = body&.source&.lines || []
74
+ lines.count { |line| !irrelevant_line?(line) }
75
+ end
76
+
77
+ def heredoc_node?(node)
78
+ node.respond_to?(:heredoc?) && node.heredoc?
79
+ end
80
+
81
+ def heredoc_length(node)
82
+ lines = node.loc.heredoc_body.source.lines
83
+ lines.count { |line| !irrelevant_line?(line) } + 2
84
+ end
85
+
86
+ def each_top_level_descendant(node, *types, &block)
87
+ node.each_child_node do |child|
88
+ if types.include?(child.type)
89
+ yield child
90
+ else
91
+ each_top_level_descendant(child, *types, &block)
92
+ end
93
+ end
94
+ end
95
+
96
+ def classlike_node?(node)
97
+ CLASSISH_TYPES.include?(node.type) ||
98
+ (node.casgn_type? && (class_definition?(node) || module_definition?(node)))
99
+ end
100
+
101
+ def foldable_node?(node)
102
+ @foldable_checks.any? { |check| check.call(node) }
103
+ end
104
+
105
+ def extract_body(node)
106
+ case node.type
107
+ when :class, :module, :block, :def, :defs
108
+ node.body
109
+ when :casgn
110
+ _scope, _name, value = *node
111
+ extract_body(value)
112
+ else
113
+ node
114
+ end
115
+ end
116
+
117
+ # Returns true for lines that shall not be included in the count.
118
+ def irrelevant_line?(source_line)
119
+ source_line.blank? || !count_comments? && comment_line?(source_line)
120
+ end
121
+
122
+ def count_comments?
123
+ @count_comments
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -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