rubocop 0.82.0 → 0.86.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (280) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -19
  3. data/config/default.yml +111 -23
  4. data/lib/rubocop.rb +16 -59
  5. data/lib/rubocop/ast_aliases.rb +8 -0
  6. data/lib/rubocop/cli.rb +3 -3
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +2 -2
  8. data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
  9. data/lib/rubocop/cli/command/show_cops.rb +2 -6
  10. data/lib/rubocop/comment_config.rb +1 -1
  11. data/lib/rubocop/config.rb +6 -2
  12. data/lib/rubocop/config_loader.rb +19 -24
  13. data/lib/rubocop/config_loader_resolver.rb +45 -6
  14. data/lib/rubocop/config_store.rb +12 -2
  15. data/lib/rubocop/config_validator.rb +2 -1
  16. data/lib/rubocop/cop/autocorrect_logic.rb +1 -2
  17. data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
  18. data/lib/rubocop/cop/commissioner.rb +0 -21
  19. data/lib/rubocop/cop/cop.rb +36 -21
  20. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -6
  21. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
  22. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
  23. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
  24. data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +4 -3
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/ignored_node.rb +1 -3
  28. data/lib/rubocop/cop/layout/case_indentation.rb +3 -3
  29. data/lib/rubocop/cop/layout/class_structure.rb +19 -16
  30. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
  31. data/lib/rubocop/cop/layout/condition_position.rb +12 -2
  32. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  33. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
  34. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +128 -0
  35. data/lib/rubocop/cop/layout/end_of_line.rb +3 -3
  36. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -4
  37. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -3
  38. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
  39. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
  40. data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
  41. data/lib/rubocop/cop/layout/hash_alignment.rb +7 -7
  42. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +3 -7
  43. data/lib/rubocop/cop/layout/heredoc_indentation.rb +20 -103
  44. data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
  45. data/lib/rubocop/cop/layout/line_length.rb +21 -18
  46. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  47. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
  48. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
  49. data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
  50. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  51. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -3
  52. data/lib/rubocop/cop/layout/space_around_operators.rb +19 -2
  53. data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
  54. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
  55. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -4
  56. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
  57. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
  58. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -3
  59. data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
  60. data/lib/rubocop/cop/lint/ambiguous_operator.rb +41 -0
  61. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
  62. data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
  63. data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +137 -0
  64. data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
  65. data/lib/rubocop/cop/lint/empty_when.rb +29 -6
  66. data/lib/rubocop/cop/lint/ensure_return.rb +19 -2
  67. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -3
  68. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  69. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
  70. data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
  71. data/lib/rubocop/cop/lint/loop.rb +1 -1
  72. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +69 -0
  73. data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
  74. data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
  75. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +33 -11
  76. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
  77. data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
  78. data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
  79. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +5 -8
  80. data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
  81. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  82. data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
  83. data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -1
  84. data/lib/rubocop/cop/lint/suppressed_exception.rb +11 -4
  85. data/lib/rubocop/cop/lint/syntax.rb +1 -3
  86. data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
  87. data/lib/rubocop/cop/lint/useless_access_modifier.rb +13 -3
  88. data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
  89. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +6 -1
  90. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  91. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
  92. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  93. data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
  94. data/lib/rubocop/cop/migration/department_name.rb +7 -7
  95. data/lib/rubocop/cop/mixin/alignment.rb +1 -3
  96. data/lib/rubocop/cop/mixin/array_min_size.rb +1 -3
  97. data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -9
  98. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
  99. data/lib/rubocop/cop/mixin/configurable_formatting.rb +2 -4
  100. data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
  101. data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
  102. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  103. data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
  104. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +10 -1
  105. data/lib/rubocop/cop/mixin/hash_transform_method.rb +8 -1
  106. data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
  107. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -2
  108. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
  109. data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
  110. data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
  111. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  112. data/lib/rubocop/cop/mixin/range_help.rb +1 -1
  113. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +43 -0
  114. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -23
  115. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  116. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  117. data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
  118. data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
  119. data/lib/rubocop/cop/mixin/uncommunicative_name.rb +3 -3
  120. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  121. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
  122. data/lib/rubocop/cop/naming/file_name.rb +28 -17
  123. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  124. data/lib/rubocop/cop/naming/method_name.rb +1 -5
  125. data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
  126. data/lib/rubocop/cop/registry.rb +63 -10
  127. data/lib/rubocop/cop/severity.rb +1 -3
  128. data/lib/rubocop/cop/style/and_or.rb +2 -2
  129. data/lib/rubocop/cop/style/array_join.rb +1 -1
  130. data/lib/rubocop/cop/style/attr.rb +1 -3
  131. data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -2
  132. data/lib/rubocop/cop/style/block_delimiters.rb +4 -12
  133. data/lib/rubocop/cop/style/case_equality.rb +1 -1
  134. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  135. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  136. data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
  137. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -4
  138. data/lib/rubocop/cop/style/copyright.rb +5 -5
  139. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
  140. data/lib/rubocop/cop/style/documentation.rb +2 -2
  141. data/lib/rubocop/cop/style/double_negation.rb +41 -4
  142. data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
  143. data/lib/rubocop/cop/style/empty_literal.rb +1 -3
  144. data/lib/rubocop/cop/style/empty_method.rb +1 -5
  145. data/lib/rubocop/cop/style/encoding.rb +1 -1
  146. data/lib/rubocop/cop/style/exponential_notation.rb +5 -5
  147. data/lib/rubocop/cop/style/format_string_token.rb +2 -3
  148. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -6
  149. data/lib/rubocop/cop/style/guard_clause.rb +25 -2
  150. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
  151. data/lib/rubocop/cop/style/hash_syntax.rb +16 -7
  152. data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
  153. data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
  154. data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
  155. data/lib/rubocop/cop/style/inline_comment.rb +1 -1
  156. data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
  157. data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
  158. data/lib/rubocop/cop/style/lambda_call.rb +0 -20
  159. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +1 -3
  160. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -3
  161. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  162. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  163. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
  164. data/lib/rubocop/cop/style/multiline_when_then.rb +16 -1
  165. data/lib/rubocop/cop/style/negated_if.rb +3 -3
  166. data/lib/rubocop/cop/style/negated_unless.rb +3 -3
  167. data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
  168. data/lib/rubocop/cop/style/next.rb +2 -2
  169. data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
  170. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
  171. data/lib/rubocop/cop/style/one_line_conditional.rb +2 -6
  172. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  173. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  174. data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
  175. data/lib/rubocop/cop/style/redundant_fetch_block.rb +103 -0
  176. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -7
  177. data/lib/rubocop/cop/style/redundant_percent_q.rb +3 -3
  178. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +89 -0
  179. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +121 -0
  180. data/lib/rubocop/cop/style/redundant_self.rb +6 -9
  181. data/lib/rubocop/cop/style/safe_navigation.rb +2 -6
  182. data/lib/rubocop/cop/style/sample.rb +1 -1
  183. data/lib/rubocop/cop/style/semicolon.rb +1 -1
  184. data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
  185. data/lib/rubocop/cop/style/special_global_vars.rb +2 -6
  186. data/lib/rubocop/cop/style/struct_inheritance.rb +21 -0
  187. data/lib/rubocop/cop/style/symbol_array.rb +5 -5
  188. data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -4
  189. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
  190. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
  191. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
  192. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
  193. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -3
  194. data/lib/rubocop/cop/style/unless_else.rb +1 -1
  195. data/lib/rubocop/cop/style/when_then.rb +1 -1
  196. data/lib/rubocop/cop/style/word_array.rb +1 -1
  197. data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
  198. data/lib/rubocop/cop/team.rb +69 -25
  199. data/lib/rubocop/cop/util.rb +27 -3
  200. data/lib/rubocop/cop/utils/format_string.rb +18 -0
  201. data/lib/rubocop/cop/variable_force.rb +3 -9
  202. data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
  203. data/lib/rubocop/cop/variable_force/branch.rb +1 -3
  204. data/lib/rubocop/cop/variable_force/scope.rb +1 -0
  205. data/lib/rubocop/cop/variable_force/variable.rb +3 -6
  206. data/lib/rubocop/ext/processed_source.rb +18 -0
  207. data/lib/rubocop/formatter/base_formatter.rb +0 -4
  208. data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -13
  209. data/lib/rubocop/formatter/formatter_set.rb +2 -4
  210. data/lib/rubocop/formatter/junit_formatter.rb +14 -4
  211. data/lib/rubocop/magic_comment.rb +1 -1
  212. data/lib/rubocop/name_similarity.rb +18 -9
  213. data/lib/rubocop/options.rb +26 -11
  214. data/lib/rubocop/path_util.rb +2 -2
  215. data/lib/rubocop/platform.rb +1 -1
  216. data/lib/rubocop/remote_config.rb +1 -3
  217. data/lib/rubocop/result_cache.rb +5 -7
  218. data/lib/rubocop/rspec/cop_helper.rb +2 -25
  219. data/lib/rubocop/rspec/expect_offense.rb +58 -15
  220. data/lib/rubocop/rspec/shared_contexts.rb +54 -16
  221. data/lib/rubocop/runner.rb +20 -13
  222. data/lib/rubocop/target_finder.rb +8 -8
  223. data/lib/rubocop/target_ruby.rb +4 -1
  224. data/lib/rubocop/version.rb +5 -3
  225. metadata +51 -74
  226. data/lib/rubocop/ast/builder.rb +0 -85
  227. data/lib/rubocop/ast/node.rb +0 -637
  228. data/lib/rubocop/ast/node/alias_node.rb +0 -24
  229. data/lib/rubocop/ast/node/and_node.rb +0 -29
  230. data/lib/rubocop/ast/node/args_node.rb +0 -29
  231. data/lib/rubocop/ast/node/array_node.rb +0 -70
  232. data/lib/rubocop/ast/node/block_node.rb +0 -121
  233. data/lib/rubocop/ast/node/break_node.rb +0 -17
  234. data/lib/rubocop/ast/node/case_match_node.rb +0 -56
  235. data/lib/rubocop/ast/node/case_node.rb +0 -56
  236. data/lib/rubocop/ast/node/class_node.rb +0 -31
  237. data/lib/rubocop/ast/node/def_node.rb +0 -82
  238. data/lib/rubocop/ast/node/defined_node.rb +0 -17
  239. data/lib/rubocop/ast/node/ensure_node.rb +0 -17
  240. data/lib/rubocop/ast/node/float_node.rb +0 -12
  241. data/lib/rubocop/ast/node/for_node.rb +0 -53
  242. data/lib/rubocop/ast/node/forward_args_node.rb +0 -18
  243. data/lib/rubocop/ast/node/hash_node.rb +0 -109
  244. data/lib/rubocop/ast/node/if_node.rb +0 -175
  245. data/lib/rubocop/ast/node/int_node.rb +0 -12
  246. data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
  247. data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +0 -16
  248. data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -43
  249. data/lib/rubocop/ast/node/mixin/collection_node.rb +0 -15
  250. data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
  251. data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
  252. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -269
  253. data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +0 -114
  254. data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
  255. data/lib/rubocop/ast/node/mixin/numeric_node.rb +0 -21
  256. data/lib/rubocop/ast/node/mixin/parameterized_node.rb +0 -61
  257. data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
  258. data/lib/rubocop/ast/node/module_node.rb +0 -24
  259. data/lib/rubocop/ast/node/or_node.rb +0 -29
  260. data/lib/rubocop/ast/node/pair_node.rb +0 -63
  261. data/lib/rubocop/ast/node/range_node.rb +0 -18
  262. data/lib/rubocop/ast/node/regexp_node.rb +0 -33
  263. data/lib/rubocop/ast/node/resbody_node.rb +0 -24
  264. data/lib/rubocop/ast/node/retry_node.rb +0 -17
  265. data/lib/rubocop/ast/node/return_node.rb +0 -24
  266. data/lib/rubocop/ast/node/self_class_node.rb +0 -24
  267. data/lib/rubocop/ast/node/send_node.rb +0 -13
  268. data/lib/rubocop/ast/node/str_node.rb +0 -16
  269. data/lib/rubocop/ast/node/super_node.rb +0 -21
  270. data/lib/rubocop/ast/node/symbol_node.rb +0 -12
  271. data/lib/rubocop/ast/node/until_node.rb +0 -35
  272. data/lib/rubocop/ast/node/when_node.rb +0 -53
  273. data/lib/rubocop/ast/node/while_node.rb +0 -35
  274. data/lib/rubocop/ast/node/yield_node.rb +0 -21
  275. data/lib/rubocop/ast/sexp.rb +0 -16
  276. data/lib/rubocop/ast/traversal.rb +0 -202
  277. data/lib/rubocop/node_pattern.rb +0 -887
  278. data/lib/rubocop/processed_source.rb +0 -213
  279. data/lib/rubocop/string_util.rb +0 -14
  280. data/lib/rubocop/token.rb +0 -114
@@ -42,7 +42,7 @@ module RuboCop
42
42
  def contains_percent_literals?(node)
43
43
  node.each_child_node.any? do |child|
44
44
  literal = child.children.first.to_s.scrub
45
- REGEXES.any? { |regex| literal.match(regex) }
45
+ REGEXES.any? { |regex| literal.match?(regex) }
46
46
  end
47
47
  end
48
48
  end
@@ -6,13 +6,13 @@ module RuboCop
6
6
  # This cop checks for non-local exits from iterators without a return
7
7
  # value. It registers an offense under these conditions:
8
8
  #
9
- # - No value is returned,
10
- # - the block is preceded by a method chain,
11
- # - the block has arguments,
12
- # - the method which receives the block is not `define_method`
13
- # or `define_singleton_method`,
14
- # - the return is not contained in an inner scope, e.g. a lambda or a
15
- # method definition.
9
+ # * No value is returned,
10
+ # * the block is preceded by a method chain,
11
+ # * the block has arguments,
12
+ # * the method which receives the block is not `define_method`
13
+ # or `define_singleton_method`,
14
+ # * the return is not contained in an inner scope, e.g. a lambda or a
15
+ # method definition.
16
16
  #
17
17
  # @example
18
18
  #
@@ -9,36 +9,58 @@ module RuboCop
9
9
  # @example
10
10
  #
11
11
  # # bad
12
- #
13
- # puts (x + y)
14
- #
15
- # @example
12
+ # do_something (foo)
16
13
  #
17
14
  # # good
18
- #
19
- # puts(x + y)
15
+ # do_something(foo)
16
+ # do_something (2 + 3) * 4
17
+ # do_something (foo * bar).baz
20
18
  class ParenthesesAsGroupedExpression < Cop
21
19
  include RangeHelp
22
20
 
23
21
  MSG = '`(...)` interpreted as grouped expression.'
24
22
 
25
23
  def on_send(node)
26
- return unless node.arguments.one?
27
- return if node.operator_method? || node.setter_method?
28
-
29
- return unless node.first_argument.source.start_with?('(')
24
+ return if valid_context?(node)
30
25
 
31
26
  space_length = spaces_before_left_parenthesis(node)
32
27
  return unless space_length.positive?
33
28
 
34
29
  range = space_range(node.first_argument.source_range, space_length)
35
30
 
36
- add_offense(nil, location: range)
31
+ add_offense(node, location: range)
37
32
  end
38
33
  alias on_csend on_send
39
34
 
35
+ def autocorrect(node)
36
+ space_length = spaces_before_left_parenthesis(node)
37
+ range = space_range(node.first_argument.source_range, space_length)
38
+
39
+ lambda do |corrector|
40
+ corrector.remove(range)
41
+ end
42
+ end
43
+
40
44
  private
41
45
 
46
+ def valid_context?(node)
47
+ unless node.arguments.one? && first_argument_starts_with_left_parenthesis?(node)
48
+ return true
49
+ end
50
+
51
+ node.operator_method? || node.setter_method? || grouped_parentheses?(node)
52
+ end
53
+
54
+ def first_argument_starts_with_left_parenthesis?(node)
55
+ node.first_argument.source.start_with?('(')
56
+ end
57
+
58
+ def grouped_parentheses?(node)
59
+ first_argument = node.first_argument
60
+
61
+ first_argument.send_type? && first_argument.receiver&.begin_type?
62
+ end
63
+
42
64
  def spaces_before_left_parenthesis(node)
43
65
  receiver = node.receiver
44
66
  receiver_length = if receiver
@@ -48,9 +48,7 @@ module RuboCop
48
48
  match = range.source.match(TRAILING_QUOTE)
49
49
  corrector.remove_trailing(range, match[0].length) if match
50
50
 
51
- if LEADING_QUOTE.match?(range.source)
52
- corrector.remove_leading(range, 1)
53
- end
51
+ corrector.remove_leading(range, 1) if LEADING_QUOTE.match?(range.source)
54
52
  end
55
53
  end
56
54
  end
@@ -64,7 +62,7 @@ module RuboCop
64
62
  # To avoid likely false positives (e.g. a single ' or ")
65
63
  next if literal.gsub(/[^[[:alnum:]]]/, '').empty?
66
64
 
67
- QUOTES_AND_COMMAS.any? { |pat| literal =~ pat }
65
+ QUOTES_AND_COMMAS.any? { |pat| literal.match?(pat) }
68
66
  end
69
67
  end
70
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)
@@ -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(node)
61
+ add_offense(exception_class)
54
62
  end
55
63
  end
56
64
 
@@ -26,7 +26,6 @@ module RuboCop
26
26
  # # good
27
27
  # x += 1
28
28
  class RedundantCopDisableDirective < Cop
29
- include NameSimilarity
30
29
  include RangeHelp
31
30
 
32
31
  COP_NAME = 'Lint/RedundantCopDisableDirective'
@@ -136,6 +135,7 @@ module RuboCop
136
135
  end
137
136
  end
138
137
 
138
+ # rubocop:todo Metrics/CyclomaticComplexity
139
139
  def find_redundant(comment, offenses, cop, line_range, next_line_range)
140
140
  if all_disabled?(comment)
141
141
  # If there's a disable all comment followed by a comment
@@ -153,9 +153,10 @@ module RuboCop
153
153
  cop if cop_offenses.none? { |o| line_range.cover?(o.line) }
154
154
  end
155
155
  end
156
+ # rubocop:enable Metrics/CyclomaticComplexity
156
157
 
157
158
  def all_disabled?(comment)
158
- comment.text =~ /rubocop\s*:\s*(?:disable|todo)\s+all\b/
159
+ /rubocop\s*:\s*(?:disable|todo)\s+all\b/.match?(comment.text)
159
160
  end
160
161
 
161
162
  def ignore_offense?(disabled_ranges, line_range)
@@ -226,7 +227,7 @@ module RuboCop
226
227
  .drop_while { |r| !r.equal?(range) }
227
228
  .each_cons(2)
228
229
  .map { |range1, range2| range1.end.join(range2.begin).source }
229
- .all? { |intervening| intervening =~ /\A\s*,\s*\Z/ }
230
+ .all? { |intervening| /\A\s*,\s*\Z/.match?(intervening) }
230
231
  end
231
232
 
232
233
  def describe(cop)
@@ -235,7 +236,7 @@ module RuboCop
235
236
  elsif all_cop_names.include?(cop)
236
237
  "`#{cop}`"
237
238
  else
238
- similar = find_similar_name(cop, [])
239
+ similar = NameSimilarity.find_similar_name(cop, all_cop_names)
239
240
  if similar
240
241
  "`#{cop}` (did you mean `#{similar}`?)"
241
242
  else
@@ -244,10 +245,6 @@ module RuboCop
244
245
  end
245
246
  end
246
247
 
247
- def collect_variable_like_names(scope)
248
- all_cop_names.each { |name| scope << name }
249
- end
250
-
251
248
  def all_cop_names
252
249
  @all_cop_names ||= Cop.registry.names
253
250
  end
@@ -8,9 +8,9 @@ module RuboCop
8
8
  # The following features are unnecessary `require` statement because
9
9
  # they are already loaded.
10
10
  #
11
- # ruby -ve 'p $LOADED_FEATURES.reject { |feature| %r|/| =~ feature }'
12
- # ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
13
- # ["enumerator.so", "rational.so", "complex.so", "thread.rb"]
11
+ # ruby -ve 'p $LOADED_FEATURES.reject { |feature| %r|/| =~ feature }'
12
+ # ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
13
+ # ["enumerator.so", "rational.so", "complex.so", "thread.rb"]
14
14
  #
15
15
  # This cop targets Ruby 2.2 or higher containing these 4 features.
16
16
  #
@@ -23,6 +23,12 @@ module RuboCop
23
23
  def on_match_current_line(node)
24
24
  add_offense(node)
25
25
  end
26
+
27
+ def autocorrect(node)
28
+ lambda do |corrector|
29
+ corrector.replace(node, "#{node.source} =~ $_")
30
+ end
31
+ end
26
32
  end
27
33
  end
28
34
  end
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # This cop checks for *rescue* blocks targeting the Exception class.
6
+ # This cop checks for `rescue` blocks targeting the Exception class.
7
7
  #
8
8
  # @example
9
9
  #
@@ -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 Lint
6
- # This cop checks for *rescue* blocks with no body.
6
+ # This cop checks for `rescue` blocks with no body.
7
7
  #
8
8
  # @example
9
9
  #
@@ -69,15 +69,22 @@ module RuboCop
69
69
 
70
70
  def on_resbody(node)
71
71
  return if node.body
72
- return if cop_config['AllowComments'] && comment_lines?(node)
72
+ return if cop_config['AllowComments'] && comment_between_rescue_and_end?(node)
73
73
 
74
74
  add_offense(node)
75
75
  end
76
76
 
77
77
  private
78
78
 
79
- def comment_lines?(node)
80
- processed_source[line_range(node)].any? { |line| comment_line?(line) }
79
+ def comment_between_rescue_and_end?(node)
80
+ end_line = nil
81
+ node.each_ancestor(:kwbegin, :def, :defs, :block) do |ancestor|
82
+ end_line = ancestor.loc.end.line
83
+ break
84
+ end
85
+ return false unless end_line
86
+
87
+ processed_source[node.first_line...end_line].any? { |line| comment_line?(line) }
81
88
  end
82
89
  end
83
90
  end
@@ -16,9 +16,7 @@ module RuboCop
16
16
  config, options)
17
17
  cop = new(config, options)
18
18
 
19
- if processed_source.parser_error
20
- cop.add_offense_from_error(processed_source.parser_error)
21
- end
19
+ cop.add_offense_from_error(processed_source.parser_error) if processed_source.parser_error
22
20
 
23
21
  processed_source.diagnostics.each do |diagnostic|
24
22
  cop.add_offense_from_diagnostic(diagnostic,
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Lint
6
6
  # This cop checks for unreachable code.
7
7
  # The check are based on the presence of flow of control
8
- # statement in non-final position in *begin*(implicit) blocks.
8
+ # statement in non-final position in `begin` (implicit) blocks.
9
9
  #
10
10
  # @example
11
11
  #
@@ -125,6 +125,8 @@ module RuboCop
125
125
  # delegate :method_a, to: :method_b
126
126
  # end
127
127
  class UselessAccessModifier < Cop
128
+ include RangeHelp
129
+
128
130
  MSG = 'Useless `%<current>s` access modifier.'
129
131
 
130
132
  def on_class(node)
@@ -145,6 +147,16 @@ module RuboCop
145
147
  check_node(node.children[1]) # singleton class body
146
148
  end
147
149
 
150
+ def autocorrect(node)
151
+ lambda do |corrector|
152
+ range = range_by_whole_lines(
153
+ node.source_range, include_final_newline: true
154
+ )
155
+
156
+ corrector.remove(range)
157
+ end
158
+ end
159
+
148
160
  private
149
161
 
150
162
  def_node_matcher :static_method_definition?, <<~PATTERN
@@ -215,9 +227,7 @@ module RuboCop
215
227
  add_offense(node, message: format(MSG, current: cur_vis))
216
228
  else
217
229
  # was the previous modifier never applied to any defs?
218
- if unused
219
- add_offense(unused, message: format(MSG, current: cur_vis))
220
- end
230
+ add_offense(unused, message: format(MSG, current: cur_vis)) if unused
221
231
  # once we have already warned about a certain modifier, don't
222
232
  # warn again even if it is never applied to any method defs
223
233
  unused = node
@@ -31,7 +31,6 @@ module RuboCop
31
31
  # do_something(some_var)
32
32
  # end
33
33
  class UselessAssignment < Cop
34
- include NameSimilarity
35
34
  MSG = 'Useless assignment to variable - `%<variable>s`.'
36
35
 
37
36
  def join_force?(force_class)
@@ -94,7 +93,9 @@ module RuboCop
94
93
  end
95
94
 
96
95
  def similar_name_message(variable)
97
- similar_name = find_similar_name(variable.name, variable.scope)
96
+ variable_like_names = collect_variable_like_names(variable.scope)
97
+ similar_name = NameSimilarity.find_similar_name(variable.name,
98
+ variable_like_names)
98
99
  " Did you mean `#{similar_name}`?" if similar_name
99
100
  end
100
101
 
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Lint
6
6
  # This cop checks for useless `else` in `begin..end` without `rescue`.
7
7
  #
8
- # Note: This syntax is no longer valid on Ruby 2.6 or higher and
8
+ # NOTE: This syntax is no longer valid on Ruby 2.6 or higher and
9
9
  # this cop is going to be removed at some point the future.
10
10
  #
11
11
  # @example
@@ -40,6 +40,11 @@ module RuboCop
40
40
  diagnostic.reason == :useless_else
41
41
  end
42
42
 
43
+ def find_offense_node_by(diagnostic)
44
+ # TODO: When implementing auto-correction, this method should return
45
+ # an offense node passed as first argument of `add_offense` method.
46
+ end
47
+
43
48
  def alternative_message(_diagnostic)
44
49
  MSG
45
50
  end
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # This cop checks for setter call to local variable as the final
7
7
  # expression of a function definition.
8
8
  #
9
- # Note: There are edge cases in which the local variable references a
9
+ # NOTE: There are edge cases in which the local variable references a
10
10
  # value that is also accessible outside the local scope. This is not
11
11
  # detected by the cop, and it can yield a false positive.
12
12
  #
@@ -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