rubocop 0.89.1 → 0.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +79 -11
  4. data/lib/rubocop.rb +13 -0
  5. data/lib/rubocop/cached_data.rb +1 -0
  6. data/lib/rubocop/cli/command.rb +1 -0
  7. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -0
  8. data/lib/rubocop/cli/command/base.rb +1 -0
  9. data/lib/rubocop/cli/command/execute_runner.rb +1 -0
  10. data/lib/rubocop/cli/command/init_dotfile.rb +1 -0
  11. data/lib/rubocop/cli/command/show_cops.rb +1 -0
  12. data/lib/rubocop/cli/command/version.rb +1 -0
  13. data/lib/rubocop/cli/environment.rb +1 -0
  14. data/lib/rubocop/comment_config.rb +5 -0
  15. data/lib/rubocop/config_loader.rb +17 -6
  16. data/lib/rubocop/config_loader_resolver.rb +1 -0
  17. data/lib/rubocop/config_obsoletion.rb +1 -0
  18. data/lib/rubocop/config_validator.rb +3 -0
  19. data/lib/rubocop/cop/base.rb +23 -0
  20. data/lib/rubocop/cop/bundler/gem_comment.rb +7 -3
  21. data/lib/rubocop/cop/commissioner.rb +47 -7
  22. data/lib/rubocop/cop/correctors/alignment_corrector.rb +4 -4
  23. data/lib/rubocop/cop/correctors/condition_corrector.rb +3 -5
  24. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +9 -10
  25. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  26. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +8 -3
  27. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +5 -8
  28. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +4 -9
  29. data/lib/rubocop/cop/correctors/punctuation_corrector.rb +8 -10
  30. data/lib/rubocop/cop/documentation.rb +22 -0
  31. data/lib/rubocop/cop/generator.rb +1 -0
  32. data/lib/rubocop/cop/layout/block_alignment.rb +23 -19
  33. data/lib/rubocop/cop/layout/class_structure.rb +10 -9
  34. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +4 -6
  35. data/lib/rubocop/cop/layout/condition_position.rb +13 -15
  36. data/lib/rubocop/cop/layout/def_end_alignment.rb +7 -4
  37. data/lib/rubocop/cop/layout/dot_position.rb +21 -17
  38. data/lib/rubocop/cop/layout/empty_comment.rb +30 -23
  39. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +19 -16
  40. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +13 -13
  41. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +144 -0
  42. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +21 -23
  43. data/lib/rubocop/cop/layout/empty_lines.rb +6 -7
  44. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +4 -6
  45. data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +7 -8
  46. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +3 -6
  47. data/lib/rubocop/cop/layout/empty_lines_around_begin_body.rb +2 -5
  48. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -5
  49. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -5
  50. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -5
  51. data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +2 -5
  52. data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +2 -5
  53. data/lib/rubocop/cop/layout/end_alignment.rb +6 -7
  54. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +2 -5
  55. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +2 -5
  56. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +4 -8
  57. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +2 -5
  58. data/lib/rubocop/cop/layout/hash_alignment.rb +17 -20
  59. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +15 -14
  60. data/lib/rubocop/cop/layout/heredoc_indentation.rb +14 -11
  61. data/lib/rubocop/cop/layout/initial_indentation.rb +6 -7
  62. data/lib/rubocop/cop/layout/leading_comment_space.rb +11 -9
  63. data/lib/rubocop/cop/layout/leading_empty_lines.rb +6 -11
  64. data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +2 -5
  65. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +2 -5
  66. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +10 -14
  67. data/lib/rubocop/cop/layout/multiline_block_layout.rb +21 -19
  68. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +2 -5
  69. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +2 -5
  70. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +5 -9
  71. data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +2 -5
  72. data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +2 -5
  73. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +18 -21
  74. data/lib/rubocop/cop/layout/space_after_colon.rb +11 -7
  75. data/lib/rubocop/cop/layout/space_after_comma.rb +2 -5
  76. data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -6
  77. data/lib/rubocop/cop/layout/space_after_not.rb +9 -11
  78. data/lib/rubocop/cop/layout/space_after_semicolon.rb +2 -5
  79. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +20 -15
  80. data/lib/rubocop/cop/layout/space_around_keyword.rb +17 -18
  81. data/lib/rubocop/cop/layout/space_around_operators.rb +17 -16
  82. data/lib/rubocop/cop/layout/space_before_block_braces.rb +23 -22
  83. data/lib/rubocop/cop/layout/space_before_comma.rb +3 -5
  84. data/lib/rubocop/cop/layout/space_before_comment.rb +10 -7
  85. data/lib/rubocop/cop/layout/space_before_first_arg.rb +7 -7
  86. data/lib/rubocop/cop/layout/space_before_semicolon.rb +2 -5
  87. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -17
  88. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +13 -16
  89. data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +3 -8
  90. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +17 -16
  91. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +16 -19
  92. data/lib/rubocop/cop/layout/space_inside_parens.rb +9 -14
  93. data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +5 -10
  94. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +8 -17
  95. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +13 -16
  96. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +10 -11
  97. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +10 -15
  98. data/lib/rubocop/cop/layout/trailing_whitespace.rb +11 -11
  99. data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +1 -1
  100. data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
  101. data/lib/rubocop/cop/lint/duplicate_require.rb +41 -0
  102. data/lib/rubocop/cop/lint/duplicate_rescue_exception.rb +0 -11
  103. data/lib/rubocop/cop/lint/empty_file.rb +53 -0
  104. data/lib/rubocop/cop/lint/missing_super.rb +2 -2
  105. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +2 -35
  106. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +9 -19
  107. data/lib/rubocop/cop/lint/percent_string_array.rb +8 -12
  108. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +4 -4
  109. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
  110. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +57 -0
  111. data/lib/rubocop/cop/lint/useless_method_definition.rb +77 -0
  112. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +2 -0
  113. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -0
  114. data/lib/rubocop/cop/mixin/alignment.rb +3 -0
  115. data/lib/rubocop/cop/mixin/allowed_methods.rb +2 -0
  116. data/lib/rubocop/cop/mixin/annotation_comment.rb +5 -0
  117. data/lib/rubocop/cop/mixin/check_line_breakable.rb +16 -7
  118. data/lib/rubocop/cop/mixin/comments_help.rb +54 -0
  119. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +8 -7
  120. data/lib/rubocop/cop/mixin/empty_parameter.rb +3 -1
  121. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +3 -1
  122. data/lib/rubocop/cop/mixin/first_element_line_break.rb +3 -1
  123. data/lib/rubocop/cop/mixin/hash_transform_method.rb +17 -0
  124. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +3 -1
  125. data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +12 -10
  126. data/lib/rubocop/cop/mixin/negative_conditional.rb +2 -2
  127. data/lib/rubocop/cop/mixin/percent_array.rb +14 -3
  128. data/lib/rubocop/cop/mixin/regexp_literal_help.rb +1 -1
  129. data/lib/rubocop/cop/mixin/rescue_node.rb +10 -1
  130. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +4 -3
  131. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +4 -3
  132. data/lib/rubocop/cop/mixin/surrounding_space.rb +8 -4
  133. data/lib/rubocop/cop/mixin/trailing_comma.rb +7 -7
  134. data/lib/rubocop/cop/offense.rb +1 -0
  135. data/lib/rubocop/cop/severity.rb +0 -8
  136. data/lib/rubocop/cop/style/case_equality.rb +8 -3
  137. data/lib/rubocop/cop/style/case_like_if.rb +20 -4
  138. data/lib/rubocop/cop/style/class_methods_definitions.rb +131 -0
  139. data/lib/rubocop/cop/style/combinable_loops.rb +89 -0
  140. data/lib/rubocop/cop/style/empty_block_parameter.rb +9 -10
  141. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +9 -10
  142. data/lib/rubocop/cop/style/guard_clause.rb +1 -0
  143. data/lib/rubocop/cop/style/hash_syntax.rb +6 -5
  144. data/lib/rubocop/cop/style/hash_transform_keys.rb +14 -1
  145. data/lib/rubocop/cop/style/hash_transform_values.rb +14 -1
  146. data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
  147. data/lib/rubocop/cop/style/keyword_parameters_order.rb +58 -0
  148. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +10 -1
  149. data/lib/rubocop/cop/style/multiline_when_then.rb +2 -2
  150. data/lib/rubocop/cop/style/negated_if.rb +6 -6
  151. data/lib/rubocop/cop/style/negated_unless.rb +6 -6
  152. data/lib/rubocop/cop/style/negated_while.rb +7 -15
  153. data/lib/rubocop/cop/style/nested_modifier.rb +10 -13
  154. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +11 -11
  155. data/lib/rubocop/cop/style/nested_ternary_operator.rb +14 -16
  156. data/lib/rubocop/cop/style/next.rb +10 -14
  157. data/lib/rubocop/cop/style/nil_comparison.rb +11 -11
  158. data/lib/rubocop/cop/style/non_nil_check.rb +32 -26
  159. data/lib/rubocop/cop/style/not.rb +19 -26
  160. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +4 -9
  161. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -11
  162. data/lib/rubocop/cop/style/one_line_conditional.rb +71 -23
  163. data/lib/rubocop/cop/style/option_hash.rb +1 -1
  164. data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
  165. data/lib/rubocop/cop/style/or_assignment.rb +13 -10
  166. data/lib/rubocop/cop/style/parallel_assignment.rb +14 -14
  167. data/lib/rubocop/cop/style/parentheses_around_condition.rb +6 -6
  168. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +13 -19
  169. data/lib/rubocop/cop/style/percent_q_literals.rb +8 -10
  170. data/lib/rubocop/cop/style/perl_backrefs.rb +8 -10
  171. data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -14
  172. data/lib/rubocop/cop/style/proc.rb +6 -6
  173. data/lib/rubocop/cop/style/raise_args.rb +13 -24
  174. data/lib/rubocop/cop/style/random_with_offset.rb +15 -16
  175. data/lib/rubocop/cop/style/redundant_assignment.rb +8 -10
  176. data/lib/rubocop/cop/style/redundant_begin.rb +7 -9
  177. data/lib/rubocop/cop/style/redundant_capital_w.rb +6 -9
  178. data/lib/rubocop/cop/style/redundant_condition.rb +5 -6
  179. data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -12
  180. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +8 -8
  181. data/lib/rubocop/cop/style/redundant_freeze.rb +3 -6
  182. data/lib/rubocop/cop/style/redundant_interpolation.rb +25 -24
  183. data/lib/rubocop/cop/style/redundant_parentheses.rb +7 -9
  184. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +6 -13
  185. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +5 -14
  186. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  187. data/lib/rubocop/cop/style/redundant_self_assignment.rb +116 -0
  188. data/lib/rubocop/cop/style/regexp_literal.rb +10 -21
  189. data/lib/rubocop/cop/style/rescue_modifier.rb +29 -9
  190. data/lib/rubocop/cop/style/return_nil.rb +5 -5
  191. data/lib/rubocop/cop/style/safe_navigation.rb +13 -12
  192. data/lib/rubocop/cop/style/sample.rb +10 -13
  193. data/lib/rubocop/cop/style/self_assignment.rb +26 -22
  194. data/lib/rubocop/cop/style/semicolon.rb +6 -9
  195. data/lib/rubocop/cop/style/send.rb +2 -2
  196. data/lib/rubocop/cop/style/signal_exception.rb +21 -19
  197. data/lib/rubocop/cop/style/single_line_block_params.rb +4 -2
  198. data/lib/rubocop/cop/style/single_line_methods.rb +17 -16
  199. data/lib/rubocop/cop/style/slicing_with_range.rb +4 -7
  200. data/lib/rubocop/cop/style/sole_nested_conditional.rb +66 -0
  201. data/lib/rubocop/cop/style/special_global_vars.rb +10 -15
  202. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -21
  203. data/lib/rubocop/cop/style/stderr_puts.rb +4 -6
  204. data/lib/rubocop/cop/style/string_hash_keys.rb +6 -7
  205. data/lib/rubocop/cop/style/string_methods.rb +7 -17
  206. data/lib/rubocop/cop/style/strip.rb +8 -14
  207. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -6
  208. data/lib/rubocop/cop/style/symbol_array.rb +5 -16
  209. data/lib/rubocop/cop/style/symbol_literal.rb +4 -6
  210. data/lib/rubocop/cop/style/symbol_proc.rb +14 -18
  211. data/lib/rubocop/cop/style/ternary_parentheses.rb +21 -20
  212. data/lib/rubocop/cop/style/trailing_body_on_class.rb +3 -6
  213. data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +4 -7
  214. data/lib/rubocop/cop/style/trailing_body_on_module.rb +3 -6
  215. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +2 -5
  216. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +2 -5
  217. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +7 -6
  218. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +2 -5
  219. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +8 -17
  220. data/lib/rubocop/cop/style/trivial_accessors.rb +26 -30
  221. data/lib/rubocop/cop/style/unless_else.rb +5 -8
  222. data/lib/rubocop/cop/style/unpack_first.rb +4 -8
  223. data/lib/rubocop/cop/style/variable_interpolation.rb +7 -10
  224. data/lib/rubocop/cop/style/when_then.rb +4 -6
  225. data/lib/rubocop/cop/style/while_until_do.rb +6 -16
  226. data/lib/rubocop/cop/style/while_until_modifier.rb +6 -20
  227. data/lib/rubocop/cop/style/word_array.rb +5 -23
  228. data/lib/rubocop/cop/style/yoda_condition.rb +4 -15
  229. data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -13
  230. data/lib/rubocop/cop/team.rb +1 -0
  231. data/lib/rubocop/cop/util.rb +1 -1
  232. data/lib/rubocop/cop/utils/format_string.rb +3 -5
  233. data/lib/rubocop/cop/variable_force.rb +2 -0
  234. data/lib/rubocop/cops_documentation_generator.rb +4 -2
  235. data/lib/rubocop/core_ext/string.rb +1 -1
  236. data/lib/rubocop/ext/regexp_node.rb +46 -0
  237. data/lib/rubocop/file_finder.rb +1 -0
  238. data/lib/rubocop/formatter/auto_gen_config_formatter.rb +2 -1
  239. data/lib/rubocop/formatter/html_formatter.rb +2 -0
  240. data/lib/rubocop/formatter/progress_formatter.rb +2 -1
  241. data/lib/rubocop/formatter/quiet_formatter.rb +1 -1
  242. data/lib/rubocop/formatter/simple_text_formatter.rb +36 -6
  243. data/lib/rubocop/name_similarity.rb +1 -0
  244. data/lib/rubocop/options.rb +3 -0
  245. data/lib/rubocop/remote_config.rb +1 -0
  246. data/lib/rubocop/result_cache.rb +1 -0
  247. data/lib/rubocop/rspec/cop_helper.rb +4 -1
  248. data/lib/rubocop/rspec/expect_offense.rb +10 -5
  249. data/lib/rubocop/rspec/shared_contexts.rb +12 -0
  250. data/lib/rubocop/runner.rb +1 -0
  251. data/lib/rubocop/string_interpreter.rb +3 -0
  252. data/lib/rubocop/target_finder.rb +1 -0
  253. data/lib/rubocop/target_ruby.rb +6 -0
  254. data/lib/rubocop/version.rb +2 -1
  255. data/lib/rubocop/yaml_duplication_checker.rb +1 -0
  256. metadata +15 -2
@@ -19,7 +19,8 @@ module RuboCop
19
19
  private
20
20
 
21
21
  def_node_matcher :constant_definition?, '{class module}'
22
- def_node_matcher :empty_line_required?, '{def defs class module}'
22
+ def_node_matcher :empty_line_required?,
23
+ '{def defs class module (send nil? {:private :protected :public})}'
23
24
 
24
25
  def check(node, body, adjusted_first_line: nil)
25
26
  return if valid_body_style?(body)
@@ -100,7 +101,9 @@ module RuboCop
100
101
 
101
102
  offset = style == :empty_lines && msg.include?('end.') ? 2 : 1
102
103
  range = source_range(processed_source.buffer, line + offset, 0)
103
- add_offense([style, range], location: range, message: msg)
104
+ add_offense(range, message: msg) do |corrector|
105
+ EmptyLineCorrector.correct(corrector, [style, range])
106
+ end
104
107
  end
105
108
 
106
109
  def check_deferred_empty_line(body)
@@ -112,11 +115,9 @@ module RuboCop
112
115
 
113
116
  range = source_range(processed_source.buffer, line + 2, 0)
114
117
 
115
- add_offense(
116
- [:empty_lines, range],
117
- location: range,
118
- message: deferred_message(node)
119
- )
118
+ add_offense(range, message: deferred_message(node)) do |corrector|
119
+ EmptyLineCorrector.correct(corrector, [:empty_lines, range])
120
+ end
120
121
  end
121
122
 
122
123
  def namespace?(body, with_one_child: false)
@@ -16,7 +16,9 @@ module RuboCop
16
16
  empty_arguments?(node) do |args|
17
17
  return if args.empty_and_without_delimiters?
18
18
 
19
- add_offense(args)
19
+ add_offense(args) do |corrector|
20
+ autocorrect(corrector, args)
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -46,7 +46,9 @@ module RuboCop
46
46
  source: align_with.source,
47
47
  align_line: align_with.line,
48
48
  align_col: align_with.column)
49
- add_offense(node, location: end_loc, message: msg)
49
+ add_offense(end_loc, message: msg) do |corrector|
50
+ autocorrect(corrector, node)
51
+ end
50
52
  end
51
53
 
52
54
  def accept_end_kw_alignment?(end_loc)
@@ -31,7 +31,9 @@ module RuboCop
31
31
  max = last_by_line(children)
32
32
  return if line == max.last_line
33
33
 
34
- add_offense(min)
34
+ add_offense(min) do |corrector|
35
+ EmptyLineCorrector.insert_before(corrector, min)
36
+ end
35
37
  end
36
38
 
37
39
  def first_by_line(nodes)
@@ -9,6 +9,12 @@ module RuboCop
9
9
  on_bad_each_with_object(node) do |*match|
10
10
  handle_possible_offense(node, match, 'each_with_object')
11
11
  end
12
+
13
+ return if target_ruby_version < 2.6
14
+
15
+ on_bad_to_h(node) do |*match|
16
+ handle_possible_offense(node, match, 'to_h {...}')
17
+ end
12
18
  end
13
19
 
14
20
  def on_send(node)
@@ -43,6 +49,11 @@ module RuboCop
43
49
  raise NotImplementedError
44
50
  end
45
51
 
52
+ # @abstract Implemented with `def_node_matcher`
53
+ def on_bad_to_h(_node)
54
+ raise NotImplementedError
55
+ end
56
+
46
57
  def handle_possible_offense(node, match, match_desc)
47
58
  captures = extract_captures(match)
48
59
 
@@ -82,6 +93,8 @@ module RuboCop
82
93
  Autocorrection.from_hash_brackets_map(node, match)
83
94
  elsif (match = on_bad_map_to_h(node))
84
95
  Autocorrection.from_map_to_h(node, match)
96
+ elsif (match = on_bad_to_h(node))
97
+ Autocorrection.from_to_h(node, match)
85
98
  else
86
99
  raise 'unreachable'
87
100
  end
@@ -137,6 +150,10 @@ module RuboCop
137
150
  new(match, node.children.first, 0, strip_trailing_chars)
138
151
  end
139
152
 
153
+ def self.from_to_h(node, match)
154
+ new(match, node, 0, 0)
155
+ end
156
+
140
157
  def strip_prefix_and_suffix(node, corrector)
141
158
  expression = node.loc.expression
142
159
  corrector.remove_leading(expression, leading)
@@ -16,7 +16,9 @@ module RuboCop
16
16
  last_seen_line = -1
17
17
  children.each do |child|
18
18
  if last_seen_line >= child.first_line
19
- add_offense(child)
19
+ add_offense(child) do |corrector|
20
+ EmptyLineCorrector.insert_before(corrector, child)
21
+ end
20
22
  else
21
23
  last_seen_line = child.last_line
22
24
  end
@@ -43,30 +43,32 @@ module RuboCop
43
43
  def check_new_line(node)
44
44
  return unless closing_brace_on_same_line?(node)
45
45
 
46
- add_offense(node,
47
- location: :end,
48
- message: self.class::ALWAYS_NEW_LINE_MESSAGE)
46
+ add_offense(node.loc.end, message: self.class::ALWAYS_NEW_LINE_MESSAGE) do |corrector|
47
+ MultilineLiteralBraceCorrector.correct(corrector, node, processed_source)
48
+ end
49
49
  end
50
50
 
51
51
  def check_same_line(node)
52
52
  return if closing_brace_on_same_line?(node)
53
53
 
54
- add_offense(node,
55
- location: :end,
56
- message: self.class::ALWAYS_SAME_LINE_MESSAGE)
54
+ add_offense(node.loc.end, message: self.class::ALWAYS_SAME_LINE_MESSAGE) do |corrector|
55
+ MultilineLiteralBraceCorrector.correct(corrector, node, processed_source)
56
+ end
57
57
  end
58
58
 
59
59
  def check_symmetrical(node)
60
60
  if opening_brace_on_same_line?(node)
61
61
  return if closing_brace_on_same_line?(node)
62
62
 
63
- add_offense(node, location: :end,
64
- message: self.class::SAME_LINE_MESSAGE)
63
+ add_offense(node.loc.end, message: self.class::SAME_LINE_MESSAGE) do |corrector|
64
+ MultilineLiteralBraceCorrector.correct(corrector, node, processed_source)
65
+ end
65
66
  else
66
67
  return unless closing_brace_on_same_line?(node)
67
68
 
68
- add_offense(node, location: :end,
69
- message: self.class::NEW_LINE_MESSAGE)
69
+ add_offense(node.loc.end, message: self.class::NEW_LINE_MESSAGE) do |corrector|
70
+ MultilineLiteralBraceCorrector.correct(corrector, node, processed_source)
71
+ end
70
72
  end
71
73
  end
72
74
 
@@ -15,7 +15,7 @@ module RuboCop
15
15
  def_node_matcher :single_negative?, '(send !(send _ :!) :!)'
16
16
  def_node_matcher :empty_condition?, '(begin)'
17
17
 
18
- def check_negative_conditional(node)
18
+ def check_negative_conditional(node, message:, &block)
19
19
  condition = node.condition
20
20
 
21
21
  return if empty_condition?(condition)
@@ -25,7 +25,7 @@ module RuboCop
25
25
  return unless single_negative?(condition)
26
26
  return if node.if_type? && node.else?
27
27
 
28
- add_offense(node)
28
+ add_offense(node, message: message, &block)
29
29
  end
30
30
  end
31
31
  end
@@ -34,14 +34,25 @@ module RuboCop
34
34
 
35
35
  def check_percent_array(node)
36
36
  array_style_detected(:percent, node.values.size)
37
- add_offense(node) if style == :brackets
37
+
38
+ return unless style == :brackets
39
+
40
+ add_offense(node) do |corrector|
41
+ correct_bracketed(corrector, node)
42
+ end
38
43
  end
39
44
 
40
- def check_bracketed_array(node)
45
+ def check_bracketed_array(node, literal_prefix)
41
46
  return if allowed_bracket_array?(node)
42
47
 
43
48
  array_style_detected(:brackets, node.values.size)
44
- add_offense(node) if style == :percent
49
+
50
+ return unless style == :percent
51
+
52
+ add_offense(node) do |corrector|
53
+ percent_literal_corrector = PercentLiteralCorrector.new(@config, @preferred_delimiters)
54
+ percent_literal_corrector.correct(corrector, node, literal_prefix)
55
+ end
45
56
  end
46
57
  end
47
58
  end
@@ -27,7 +27,7 @@ module RuboCop
27
27
  # part of the pattern source, but need to preserve their width, to allow offsets to
28
28
  # correctly line up with the original source: spaces have no effect, and preserve width.
29
29
  if child.begin_type?
30
- replace_match_with_spaces(source, /.*/) # replace all content
30
+ replace_match_with_spaces(source, /.*/m) # replace all content
31
31
  elsif freespace_mode
32
32
  replace_match_with_spaces(source, /(?<!\\)#.*/) # replace any comments
33
33
  else
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for checking `rescue` nodes.
6
6
  module RescueNode
7
- def investigate(processed_source)
7
+ def on_new_investigation
8
8
  @modifier_locations = processed_source
9
9
  .tokens
10
10
  .select(&:rescue_modifier?)
@@ -17,6 +17,15 @@ module RuboCop
17
17
  node&.resbody_type? &&
18
18
  @modifier_locations.include?(node.loc.keyword)
19
19
  end
20
+
21
+ def rescued_exceptions(resbody)
22
+ rescue_group, = *resbody
23
+ if rescue_group
24
+ rescue_group.values
25
+ else
26
+ []
27
+ end
28
+ end
20
29
  end
21
30
  end
22
31
  end
@@ -7,10 +7,11 @@ module RuboCop
7
7
  module SpaceAfterPunctuation
8
8
  MSG = 'Space missing after %<token>s.'
9
9
 
10
- def investigate(processed_source)
10
+ def on_new_investigation
11
11
  each_missing_space(processed_source.tokens) do |token|
12
- add_offense(token, location: token.pos,
13
- message: format(MSG, token: kind(token)))
12
+ add_offense(token.pos, message: format(MSG, token: kind(token))) do |corrector|
13
+ PunctuationCorrector.add_space(corrector, token)
14
+ end
14
15
  end
15
16
  end
16
17
 
@@ -9,10 +9,11 @@ module RuboCop
9
9
 
10
10
  MSG = 'Space found before %<token>s.'
11
11
 
12
- def investigate(processed_source)
12
+ def on_new_investigation
13
13
  each_missing_space(processed_source.tokens) do |token, pos_before|
14
- add_offense(pos_before, location: pos_before,
15
- message: format(MSG, token: kind(token)))
14
+ add_offense(pos_before, message: format(MSG, token: kind(token))) do |corrector|
15
+ PunctuationCorrector.remove_space(corrector, pos_before)
16
+ end
16
17
  end
17
18
  end
18
19
 
@@ -81,8 +81,11 @@ module RuboCop
81
81
 
82
82
  def space_offense(node, token, side, message, command)
83
83
  range = side_space_range(range: token.pos, side: side)
84
- add_offense(node, location: range,
85
- message: format(message, command: command))
84
+ add_offense(range, message: format(message, command: command)) do |corrector|
85
+ autocorrect(corrector, node) unless ignored_node?(node)
86
+
87
+ ignore_node(node)
88
+ end
86
89
  end
87
90
 
88
91
  def empty_offenses(node, left, right, message)
@@ -96,8 +99,9 @@ module RuboCop
96
99
  end
97
100
 
98
101
  def empty_offense(node, range, message, command)
99
- add_offense(node, location: range,
100
- message: format(message, command: command))
102
+ add_offense(range, message: format(message, command: command)) do |corrector|
103
+ autocorrect(corrector, node)
104
+ end
101
105
  end
102
106
 
103
107
  def empty_brackets?(left_bracket_token, right_bracket_token)
@@ -140,7 +140,9 @@ module RuboCop
140
140
  unit: format(kind, article: article) + extra_info.to_s
141
141
  )
142
142
 
143
- add_offense(range, location: range, message: msg)
143
+ add_offense(range, message: msg) do |corrector|
144
+ PunctuationCorrector.swap_comma(corrector, range)
145
+ end
144
146
  end
145
147
 
146
148
  def put_comma(items, kind)
@@ -148,13 +150,11 @@ module RuboCop
148
150
  return if last_item.block_pass_type?
149
151
 
150
152
  range = autocorrect_range(last_item)
151
- msg = format(
152
- MSG,
153
- command: 'Put a',
154
- unit: format(kind, article: 'a multiline')
155
- )
153
+ msg = format(MSG, command: 'Put a', unit: format(kind, article: 'a multiline'))
156
154
 
157
- add_offense(range, location: range, message: msg)
155
+ add_offense(range, message: msg) do |corrector|
156
+ PunctuationCorrector.swap_comma(corrector, range)
157
+ end
158
158
  end
159
159
 
160
160
  def autocorrect_range(item)
@@ -64,6 +64,7 @@ module RuboCop
64
64
 
65
65
  PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
66
66
  :end_pos)
67
+ private_constant :PseudoSourceRange
67
68
 
68
69
  NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
69
70
 
@@ -6,7 +6,6 @@ module RuboCop
6
6
  class Severity
7
7
  include Comparable
8
8
 
9
- # @api private
10
9
  NAMES = %i[refactor convention warning error fatal].freeze
11
10
 
12
11
  # @api private
@@ -22,7 +21,6 @@ module RuboCop
22
21
  # any of `:refactor`, `:convention`, `:warning`, `:error` or `:fatal`.
23
22
  attr_reader :name
24
23
 
25
- # @api private
26
24
  def self.name_from_code(code)
27
25
  name = code.to_sym
28
26
  CODE_TABLE[name] || name
@@ -37,22 +35,18 @@ module RuboCop
37
35
  freeze
38
36
  end
39
37
 
40
- # @api private
41
38
  def to_s
42
39
  @name.to_s
43
40
  end
44
41
 
45
- # @api private
46
42
  def code
47
43
  @name.to_s[0].upcase
48
44
  end
49
45
 
50
- # @api private
51
46
  def level
52
47
  NAMES.index(name) + 1
53
48
  end
54
49
 
55
- # @api private
56
50
  def ==(other)
57
51
  @name == if other.is_a?(Symbol)
58
52
  other
@@ -61,12 +55,10 @@ module RuboCop
61
55
  end
62
56
  end
63
57
 
64
- # @api private
65
58
  def hash
66
59
  @name.hash
67
60
  end
68
61
 
69
- # @api private
70
62
  def <=>(other)
71
63
  level <=> other.level
72
64
  end
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # # good
15
15
  # something.is_a?(Array)
16
16
  # (1..100).include?(7)
17
- # some_string =~ /something/
17
+ # /something/.match?(some_string)
18
18
  #
19
19
  # @example AllowOnConstant
20
20
  # # Style/CaseEquality:
@@ -27,7 +27,7 @@ module RuboCop
27
27
  # # good
28
28
  # Array === something
29
29
  # (1..100).include?(7)
30
- # some_string =~ /something/
30
+ # /something/.match?(some_string)
31
31
  #
32
32
  class CaseEquality < Base
33
33
  extend AutoCorrector
@@ -58,7 +58,12 @@ module RuboCop
58
58
  def replacement(lhs, rhs)
59
59
  case lhs.type
60
60
  when :regexp
61
- "#{rhs.source} =~ #{lhs.source}"
61
+ # The automatic correction from `a === b` to `a.match?(b)` needs to
62
+ # consider `Regexp.last_match?`, `$~`, `$1`, and etc.
63
+ # This correction is expected to be supported by `Performance/Regexp` cop.
64
+ # See: https://github.com/rubocop-hq/rubocop-performance/issues/152
65
+ #
66
+ # So here is noop.
62
67
  when :begin
63
68
  child = lhs.children.first
64
69
  "#{lhs.source}.include?(#{rhs.source})" if child&.range_type?
@@ -115,7 +115,7 @@ module RuboCop
115
115
  def find_target_in_equality_node(node)
116
116
  argument = node.arguments.first
117
117
  receiver = node.receiver
118
- return unless receiver
118
+ return unless argument && receiver
119
119
 
120
120
  if argument.literal? || const_reference?(argument)
121
121
  receiver
@@ -159,9 +159,10 @@ module RuboCop
159
159
  case node.method_name
160
160
  when :is_a?
161
161
  node.arguments.first if node.receiver == target
162
- when :==, :eql?, :equal?, :=~, :match, :match?
163
- lhs, _method, rhs = *node
164
- condition_from_binary_op(lhs, rhs, target)
162
+ when :==, :eql?, :equal?
163
+ condition_from_equality_node(node, target)
164
+ when :=~, :match, :match?
165
+ condition_from_match_node(node, target)
165
166
  when :===
166
167
  lhs, _method, rhs = *node
167
168
  lhs if rhs == target
@@ -172,6 +173,17 @@ module RuboCop
172
173
  end
173
174
  # rubocop:enable Metrics/CyclomaticComplexity
174
175
 
176
+ def condition_from_equality_node(node, target)
177
+ lhs, _method, rhs = *node
178
+ condition = condition_from_binary_op(lhs, rhs, target)
179
+ condition if condition && !class_reference?(condition)
180
+ end
181
+
182
+ def condition_from_match_node(node, target)
183
+ lhs, _method, rhs = *node
184
+ condition_from_binary_op(lhs, rhs, target)
185
+ end
186
+
175
187
  def condition_from_binary_op(lhs, rhs, target)
176
188
  lhs = deparenthesize(lhs)
177
189
  rhs = deparenthesize(rhs)
@@ -202,6 +214,10 @@ module RuboCop
202
214
  name == name.upcase
203
215
  end
204
216
 
217
+ def class_reference?(node)
218
+ node.const_type? && node.children[1].match?(/[[:lower:]]/)
219
+ end
220
+
205
221
  def deparenthesize(node)
206
222
  node = node.children.last while node.begin_type?
207
223
  node