rubocop 1.50.2 → 1.57.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -3
  3. data/config/default.yml +110 -14
  4. data/config/obsoletion.yml +5 -0
  5. data/lib/rubocop/cli/command/lsp.rb +19 -0
  6. data/lib/rubocop/cli.rb +4 -1
  7. data/lib/rubocop/config.rb +4 -0
  8. data/lib/rubocop/config_finder.rb +2 -2
  9. data/lib/rubocop/config_loader_resolver.rb +4 -3
  10. data/lib/rubocop/config_obsoletion/parameter_rule.rb +9 -1
  11. data/lib/rubocop/config_obsoletion.rb +2 -2
  12. data/lib/rubocop/cop/autocorrect_logic.rb +3 -1
  13. data/lib/rubocop/cop/base.rb +6 -2
  14. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -0
  15. data/lib/rubocop/cop/bundler/duplicated_group.rb +127 -0
  16. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  17. data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
  18. data/lib/rubocop/cop/bundler/ordered_gems.rb +9 -1
  19. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
  20. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +7 -4
  21. data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
  22. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  23. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +9 -1
  24. data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/cop_description.rb +32 -8
  26. data/lib/rubocop/cop/internal_affairs/example_description.rb +42 -21
  27. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
  28. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +5 -5
  29. data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +11 -2
  30. data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
  31. data/lib/rubocop/cop/layout/class_structure.rb +7 -0
  32. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
  33. data/lib/rubocop/cop/layout/dot_position.rb +1 -5
  34. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +42 -9
  35. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
  36. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
  37. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  38. data/lib/rubocop/cop/layout/heredoc_indentation.rb +3 -0
  39. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  40. data/lib/rubocop/cop/layout/indentation_width.rb +3 -3
  41. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  42. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +17 -9
  43. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
  44. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
  45. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -3
  46. data/lib/rubocop/cop/layout/redundant_line_break.rb +14 -4
  47. data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
  48. data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
  49. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +2 -2
  50. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
  51. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
  52. data/lib/rubocop/cop/layout/space_inside_parens.rb +1 -1
  53. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  54. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +5 -0
  55. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
  56. data/lib/rubocop/cop/lint/debugger.rb +18 -5
  57. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
  58. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
  59. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  60. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
  61. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
  62. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  63. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
  64. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  65. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  66. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  67. data/lib/rubocop/cop/lint/missing_super.rb +34 -5
  68. data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
  69. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -7
  70. data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
  71. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
  72. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  73. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
  74. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
  75. data/lib/rubocop/cop/lint/redundant_require_statement.rb +12 -3
  76. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +20 -4
  77. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  78. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +11 -4
  79. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  80. data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
  81. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +7 -1
  82. data/lib/rubocop/cop/lint/struct_new_override.rb +12 -12
  83. data/lib/rubocop/cop/lint/suppressed_exception.rb +2 -2
  84. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  85. data/lib/rubocop/cop/lint/to_enum_arguments.rb +5 -3
  86. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
  87. data/lib/rubocop/cop/lint/useless_assignment.rb +94 -10
  88. data/lib/rubocop/cop/lint/void.rb +57 -7
  89. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  90. data/lib/rubocop/cop/metrics/class_length.rb +2 -2
  91. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  92. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  93. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +32 -4
  94. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  95. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  96. data/lib/rubocop/cop/mixin/comments_help.rb +7 -3
  97. data/lib/rubocop/cop/mixin/def_node.rb +1 -1
  98. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  99. data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
  100. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -2
  101. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  102. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +5 -7
  103. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  104. data/lib/rubocop/cop/mixin/string_help.rb +4 -2
  105. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  106. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  107. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  108. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +3 -1
  110. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +25 -10
  111. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
  112. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  113. data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
  114. data/lib/rubocop/cop/style/alias.rb +9 -8
  115. data/lib/rubocop/cop/style/arguments_forwarding.rb +280 -63
  116. data/lib/rubocop/cop/style/array_intersect.rb +13 -5
  117. data/lib/rubocop/cop/style/attr.rb +11 -1
  118. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  119. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  120. data/lib/rubocop/cop/style/block_delimiters.rb +5 -4
  121. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  122. data/lib/rubocop/cop/style/class_equality_comparison.rb +24 -39
  123. data/lib/rubocop/cop/style/collection_compact.rb +16 -6
  124. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  125. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  126. data/lib/rubocop/cop/style/combinable_loops.rb +30 -8
  127. data/lib/rubocop/cop/style/concat_array_literals.rb +1 -1
  128. data/lib/rubocop/cop/style/conditional_assignment.rb +5 -3
  129. data/lib/rubocop/cop/style/copyright.rb +5 -2
  130. data/lib/rubocop/cop/style/dir.rb +1 -1
  131. data/lib/rubocop/cop/style/dir_empty.rb +8 -14
  132. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  133. data/lib/rubocop/cop/style/documentation.rb +1 -1
  134. data/lib/rubocop/cop/style/empty_case_condition.rb +6 -1
  135. data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
  136. data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
  137. data/lib/rubocop/cop/style/file_read.rb +2 -2
  138. data/lib/rubocop/cop/style/for.rb +1 -1
  139. data/lib/rubocop/cop/style/format_string.rb +24 -3
  140. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -1
  141. data/lib/rubocop/cop/style/guard_clause.rb +28 -0
  142. data/lib/rubocop/cop/style/hash_conversion.rb +10 -0
  143. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
  144. data/lib/rubocop/cop/style/hash_except.rb +19 -8
  145. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  146. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  147. data/lib/rubocop/cop/style/identical_conditional_branches.rb +23 -5
  148. data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
  149. data/lib/rubocop/cop/style/if_unless_modifier.rb +3 -0
  150. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  151. data/lib/rubocop/cop/style/invertible_unless_condition.rb +10 -6
  152. data/lib/rubocop/cop/style/lambda.rb +3 -3
  153. data/lib/rubocop/cop/style/lambda_call.rb +5 -0
  154. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -5
  155. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  156. data/lib/rubocop/cop/style/multiline_block_chain.rb +1 -1
  157. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  158. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  159. data/lib/rubocop/cop/style/nested_ternary_operator.rb +3 -11
  160. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  161. data/lib/rubocop/cop/style/open_struct_use.rb +1 -1
  162. data/lib/rubocop/cop/style/operator_method_call.rb +6 -0
  163. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  164. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  165. data/lib/rubocop/cop/style/redundant_argument.rb +6 -1
  166. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  167. data/lib/rubocop/cop/style/redundant_begin.rb +10 -2
  168. data/lib/rubocop/cop/style/redundant_conditional.rb +2 -10
  169. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
  170. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +86 -5
  171. data/lib/rubocop/cop/style/redundant_exception.rb +32 -12
  172. data/lib/rubocop/cop/style/redundant_filter_chain.rb +117 -0
  173. data/lib/rubocop/cop/style/redundant_line_continuation.rb +7 -3
  174. data/lib/rubocop/cop/style/redundant_parentheses.rb +25 -7
  175. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
  176. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  177. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  178. data/lib/rubocop/cop/style/redundant_return.rb +7 -2
  179. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +8 -1
  180. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  181. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -0
  182. data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
  183. data/lib/rubocop/cop/style/require_order.rb +11 -5
  184. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  185. data/lib/rubocop/cop/style/return_nil.rb +6 -2
  186. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +95 -0
  187. data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
  188. data/lib/rubocop/cop/style/semicolon.rb +12 -4
  189. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  190. data/lib/rubocop/cop/style/single_line_do_end_block.rb +65 -0
  191. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  192. data/lib/rubocop/cop/style/sole_nested_conditional.rb +6 -2
  193. data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
  194. data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +30 -5
  195. data/lib/rubocop/cop/style/symbol_array.rb +35 -15
  196. data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
  197. data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
  198. data/lib/rubocop/cop/style/yoda_expression.rb +8 -7
  199. data/lib/rubocop/cop/team.rb +1 -1
  200. data/lib/rubocop/cop/util.rb +1 -1
  201. data/lib/rubocop/cop/utils/regexp_ranges.rb +113 -0
  202. data/lib/rubocop/cop/variable_force/assignment.rb +45 -4
  203. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  204. data/lib/rubocop/cop/variable_force.rb +1 -0
  205. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  206. data/lib/rubocop/ext/regexp_parser.rb +4 -1
  207. data/lib/rubocop/file_finder.rb +4 -7
  208. data/lib/rubocop/formatter/junit_formatter.rb +1 -1
  209. data/lib/rubocop/lsp/logger.rb +22 -0
  210. data/lib/rubocop/lsp/routes.rb +246 -0
  211. data/lib/rubocop/lsp/runtime.rb +99 -0
  212. data/lib/rubocop/lsp/server.rb +68 -0
  213. data/lib/rubocop/lsp/severity.rb +27 -0
  214. data/lib/rubocop/magic_comment.rb +12 -10
  215. data/lib/rubocop/options.rb +11 -1
  216. data/lib/rubocop/result_cache.rb +5 -1
  217. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  218. data/lib/rubocop/rspec/shared_contexts.rb +2 -3
  219. data/lib/rubocop/runner.rb +5 -3
  220. data/lib/rubocop/server/cache.rb +1 -0
  221. data/lib/rubocop/server/client_command/exec.rb +3 -2
  222. data/lib/rubocop/string_interpreter.rb +3 -3
  223. data/lib/rubocop/target_finder.rb +7 -3
  224. data/lib/rubocop/target_ruby.rb +12 -7
  225. data/lib/rubocop/version.rb +10 -6
  226. data/lib/rubocop.rb +15 -0
  227. metadata +63 -15
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for uses a redundant current directory in path.
7
+ #
8
+ # @example
9
+ #
10
+ # # bad
11
+ # require_relative './path/to/feature'
12
+ #
13
+ # # good
14
+ # require_relative 'path/to/feature'
15
+ #
16
+ class RedundantCurrentDirectoryInPath < Base
17
+ include RangeHelp
18
+ extend AutoCorrector
19
+
20
+ MSG = 'Remove the redundant current directory path.'
21
+ CURRENT_DIRECTORY_PATH = './'
22
+
23
+ def on_send(node)
24
+ return unless node.method?(:require_relative)
25
+ return unless node.first_argument.str_content&.start_with?(CURRENT_DIRECTORY_PATH)
26
+ return unless (index = node.first_argument.source.index(CURRENT_DIRECTORY_PATH))
27
+
28
+ begin_pos = node.first_argument.source_range.begin.begin_pos + index
29
+ range = range_between(begin_pos, begin_pos + 2)
30
+
31
+ add_offense(range) do |corrector|
32
+ corrector.remove(range)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -13,25 +13,68 @@ module RuboCop
13
13
  # # good
14
14
  # do_something(foo: bar, baz: qux)
15
15
  #
16
+ # # bad
17
+ # do_something(**{foo: bar, baz: qux}.merge(options))
18
+ #
19
+ # # good
20
+ # do_something(foo: bar, baz: qux, **options)
21
+ #
16
22
  class RedundantDoubleSplatHashBraces < Base
17
23
  extend AutoCorrector
18
24
 
19
25
  MSG = 'Remove the redundant double splat and braces, use keyword arguments directly.'
26
+ MERGE_METHODS = %i[merge merge!].freeze
20
27
 
28
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
21
29
  def on_hash(node)
22
30
  return if node.pairs.empty? || node.pairs.any?(&:hash_rocket?)
23
31
  return unless (parent = node.parent)
24
- return unless parent.kwsplat_type?
32
+ return if parent.call_type? && !merge_method?(parent)
33
+ return unless (kwsplat = node.each_ancestor(:kwsplat).first)
34
+ return if allowed_double_splat_receiver?(kwsplat)
25
35
 
26
- add_offense(parent) do |corrector|
27
- corrector.remove(parent.loc.operator)
28
- corrector.remove(opening_brace(node))
29
- corrector.remove(closing_brace(node))
36
+ add_offense(kwsplat) do |corrector|
37
+ autocorrect(corrector, node, kwsplat)
30
38
  end
31
39
  end
40
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
32
41
 
33
42
  private
34
43
 
44
+ def allowed_double_splat_receiver?(kwsplat)
45
+ return false unless kwsplat.children.first.call_type?
46
+
47
+ root_receiver = root_receiver(kwsplat.children.first)
48
+
49
+ !root_receiver&.hash_type?
50
+ end
51
+
52
+ def autocorrect(corrector, node, kwsplat)
53
+ corrector.remove(kwsplat.loc.operator)
54
+ corrector.remove(opening_brace(node))
55
+ corrector.remove(closing_brace(node))
56
+
57
+ merge_methods = select_merge_method_nodes(kwsplat)
58
+ return if merge_methods.empty?
59
+
60
+ autocorrect_merge_methods(corrector, merge_methods, kwsplat)
61
+ end
62
+
63
+ def root_receiver(node)
64
+ receiver = node.receiver
65
+ if receiver&.receiver
66
+ root_receiver(receiver)
67
+ else
68
+ receiver
69
+ end
70
+ end
71
+
72
+ def select_merge_method_nodes(kwsplat)
73
+ extract_send_methods(kwsplat).select do |node|
74
+ merge_method?(node)
75
+ end
76
+ end
77
+
35
78
  def opening_brace(node)
36
79
  node.loc.begin.join(node.children.first.source_range.begin)
37
80
  end
@@ -39,6 +82,44 @@ module RuboCop
39
82
  def closing_brace(node)
40
83
  node.children.last.source_range.end.join(node.loc.end)
41
84
  end
85
+
86
+ def autocorrect_merge_methods(corrector, merge_methods, kwsplat)
87
+ range = range_of_merge_methods(merge_methods)
88
+
89
+ new_kwsplat_arguments = extract_send_methods(kwsplat).map do |descendant|
90
+ convert_to_new_arguments(descendant)
91
+ end
92
+ new_source = new_kwsplat_arguments.compact.reverse.unshift('').join(', ')
93
+
94
+ corrector.replace(range, new_source)
95
+ end
96
+
97
+ def range_of_merge_methods(merge_methods)
98
+ begin_merge_method = merge_methods.last
99
+ end_merge_method = merge_methods.first
100
+
101
+ begin_merge_method.loc.dot.begin.join(end_merge_method.source_range.end)
102
+ end
103
+
104
+ def extract_send_methods(kwsplat)
105
+ kwsplat.each_descendant(:send, :csend)
106
+ end
107
+
108
+ def convert_to_new_arguments(node)
109
+ return unless merge_method?(node)
110
+
111
+ node.arguments.map do |arg|
112
+ if arg.hash_type?
113
+ arg.source
114
+ else
115
+ "**#{arg.source}"
116
+ end
117
+ end
118
+ end
119
+
120
+ def merge_method?(node)
121
+ MERGE_METHODS.include?(node.method_name)
122
+ end
42
123
  end
43
124
  end
44
125
  end
@@ -5,17 +5,21 @@ module RuboCop
5
5
  module Style
6
6
  # Checks for RuntimeError as the argument of raise/fail.
7
7
  #
8
- # It checks for code like this:
9
- #
10
8
  # @example
11
- # # Bad
9
+ # # bad
12
10
  # raise RuntimeError, 'message'
13
- #
14
- # # Bad
15
11
  # raise RuntimeError.new('message')
16
12
  #
17
- # # Good
13
+ # # good
18
14
  # raise 'message'
15
+ #
16
+ # # bad - message is not a string
17
+ # raise RuntimeError, Object.new
18
+ # raise RuntimeError.new(Object.new)
19
+ #
20
+ # # good
21
+ # raise Object.new.to_s
22
+ #
19
23
  class RedundantException < Base
20
24
  extend AutoCorrector
21
25
 
@@ -30,26 +34,42 @@ module RuboCop
30
34
  fix_exploded(node) || fix_compact(node)
31
35
  end
32
36
 
37
+ private
38
+
33
39
  def fix_exploded(node)
34
40
  exploded?(node) do |command, message|
35
41
  add_offense(node, message: MSG_1) do |corrector|
36
- if node.parenthesized?
37
- corrector.replace(node, "#{command}(#{message.source})")
38
- else
39
- corrector.replace(node, "#{command} #{message.source}")
40
- end
42
+ corrector.replace(node, replaced_exploded(node, command, message))
41
43
  end
42
44
  end
43
45
  end
44
46
 
47
+ def replaced_exploded(node, command, message)
48
+ arg = string_message?(message) ? message.source : "#{message.source}.to_s"
49
+ arg = node.parenthesized? ? "(#{arg})" : " #{arg}"
50
+ "#{command}#{arg}"
51
+ end
52
+
53
+ def string_message?(message)
54
+ message.str_type? || message.dstr_type? || message.xstr_type?
55
+ end
56
+
45
57
  def fix_compact(node)
46
58
  compact?(node) do |new_call, message|
47
59
  add_offense(node, message: MSG_2) do |corrector|
48
- corrector.replace(new_call, message.source)
60
+ corrector.replace(new_call, replaced_compact(message))
49
61
  end
50
62
  end
51
63
  end
52
64
 
65
+ def replaced_compact(message)
66
+ if string_message?(message)
67
+ message.source
68
+ else
69
+ "#{message.source}.to_s"
70
+ end
71
+ end
72
+
53
73
  # @!method exploded?(node)
54
74
  def_node_matcher :exploded?, <<~PATTERN
55
75
  (send nil? ${:raise :fail} (const {nil? cbase} :RuntimeError) $_)
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Identifies usages of `any?`, `empty?` or `none?` predicate methods
7
+ # chained to `select`/`filter`/`find_all` and change them to use predicate method instead.
8
+ #
9
+ # @safety
10
+ # This cop's autocorrection is unsafe because `array.select.any?` evaluates all elements
11
+ # through the `select` method, while `array.any?` uses short-circuit evaluation.
12
+ # In other words, `array.select.any?` guarantees the evaluation of every element,
13
+ # but `array.any?` does not necessarily evaluate all of them.
14
+ #
15
+ # @example
16
+ # # bad
17
+ # arr.select { |x| x > 1 }.any?
18
+ #
19
+ # # good
20
+ # arr.any? { |x| x > 1 }
21
+ #
22
+ # # bad
23
+ # arr.select { |x| x > 1 }.empty?
24
+ # arr.select { |x| x > 1 }.none?
25
+ #
26
+ # # good
27
+ # arr.none? { |x| x > 1 }
28
+ #
29
+ # # good
30
+ # relation.select(:name).any?
31
+ # arr.select { |x| x > 1 }.any?(&:odd?)
32
+ #
33
+ # @example AllCops:ActiveSupportExtensionsEnabled: false (default)
34
+ # # good
35
+ # arr.select { |x| x > 1 }.many?
36
+ #
37
+ # # good
38
+ # arr.select { |x| x > 1 }.present?
39
+ #
40
+ # @example AllCops:ActiveSupportExtensionsEnabled: true
41
+ # # bad
42
+ # arr.select { |x| x > 1 }.many?
43
+ #
44
+ # # good
45
+ # arr.many? { |x| x > 1 }
46
+ #
47
+ # # bad
48
+ # arr.select { |x| x > 1 }.present?
49
+ #
50
+ # # good
51
+ # arr.any? { |x| x > 1 }
52
+ #
53
+ class RedundantFilterChain < Base
54
+ extend AutoCorrector
55
+
56
+ MSG = 'Use `%<prefer>s` instead of `%<first_method>s.%<second_method>s`.'
57
+
58
+ RAILS_METHODS = %i[many? present?].freeze
59
+ RESTRICT_ON_SEND = (%i[any? empty? none? one?] + RAILS_METHODS).freeze
60
+
61
+ # @!method select_predicate?(node)
62
+ def_node_matcher :select_predicate?, <<~PATTERN
63
+ (send
64
+ {
65
+ (block $(send _ {:select :filter :find_all}) ...)
66
+ $(send _ {:select :filter :find_all} block_pass_type?)
67
+ }
68
+ ${:#{RESTRICT_ON_SEND.join(' :')}})
69
+ PATTERN
70
+
71
+ REPLACEMENT_METHODS = {
72
+ any?: :any?,
73
+ empty?: :none?,
74
+ none?: :none?,
75
+ one?: :one?,
76
+ many?: :many?,
77
+ present?: :any?
78
+ }.freeze
79
+ private_constant :REPLACEMENT_METHODS
80
+
81
+ def on_send(node)
82
+ return if node.arguments? || node.block_node
83
+
84
+ select_predicate?(node) do |select_node, filter_method|
85
+ return if RAILS_METHODS.include?(filter_method) && !active_support_extensions_enabled?
86
+
87
+ register_offense(select_node, node)
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def register_offense(select_node, predicate_node)
94
+ replacement = REPLACEMENT_METHODS[predicate_node.method_name]
95
+ message = format(MSG, prefer: replacement,
96
+ first_method: select_node.method_name,
97
+ second_method: predicate_node.method_name)
98
+
99
+ offense_range = offense_range(select_node, predicate_node)
100
+
101
+ add_offense(offense_range, message: message) do |corrector|
102
+ corrector.remove(predicate_range(predicate_node))
103
+ corrector.replace(select_node.loc.selector, replacement)
104
+ end
105
+ end
106
+
107
+ def offense_range(select_node, predicate_node)
108
+ select_node.loc.selector.join(predicate_node.loc.selector)
109
+ end
110
+
111
+ def predicate_range(predicate_node)
112
+ predicate_node.receiver.source_range.end.join(predicate_node.loc.selector)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -70,6 +70,10 @@ module RuboCop
70
70
 
71
71
  MSG = 'Redundant line continuation.'
72
72
  ALLOWED_STRING_TOKENS = %i[tSTRING tSTRING_CONTENT].freeze
73
+ ARGUMENT_TYPES = %i[
74
+ kFALSE kNIL kSELF kTRUE tCONSTANT tCVAR tFLOAT tGVAR tIDENTIFIER tINTEGER tIVAR
75
+ tLBRACK tLCURLY tLPAREN_ARG tSTRING tSTRING_BEG tSYMBOL tXSTRING_BEG
76
+ ].freeze
73
77
 
74
78
  def on_new_investigation
75
79
  return unless processed_source.ast
@@ -112,7 +116,7 @@ module RuboCop
112
116
  return false if argument_newline?(node)
113
117
 
114
118
  source = node.parent ? node.parent.source : node.source
115
- parse(source.gsub(/\\\n/, "\n")).valid_syntax?
119
+ parse(source.gsub("\\\n", "\n")).valid_syntax?
116
120
  end
117
121
 
118
122
  def inside_string_literal?(range, token)
@@ -124,7 +128,7 @@ module RuboCop
124
128
  # do_something \
125
129
  # argument
126
130
  def method_with_argument?(current_token, next_token)
127
- current_token.type == :tIDENTIFIER && next_token.type == :tIDENTIFIER
131
+ current_token.type == :tIDENTIFIER && ARGUMENT_TYPES.include?(next_token.type)
128
132
  end
129
133
 
130
134
  def argument_newline?(node)
@@ -146,7 +150,7 @@ module RuboCop
146
150
  end
147
151
 
148
152
  def same_line?(node, line)
149
- return unless (source_range = node.source_range)
153
+ return false unless (source_range = node.source_range)
150
154
 
151
155
  if node.is_a?(AST::StrNode)
152
156
  if node.heredoc?
@@ -85,7 +85,7 @@ module RuboCop
85
85
  end
86
86
 
87
87
  def allowed_ternary?(node)
88
- return unless node&.parent&.if_type?
88
+ return false unless node&.parent&.if_type?
89
89
 
90
90
  node.parent.ternary? && ternary_parentheses_required?
91
91
  end
@@ -98,7 +98,9 @@ module RuboCop
98
98
  end
99
99
 
100
100
  def like_method_argument_parentheses?(node)
101
- node.send_type? && node.arguments.one? && !node.parenthesized? &&
101
+ return false if !node.send_type? && !node.super_type? && !node.yield_type?
102
+
103
+ node.arguments.one? && !node.parenthesized? &&
102
104
  !node.arithmetic_operation? && node.first_argument.begin_type?
103
105
  end
104
106
 
@@ -124,16 +126,32 @@ module RuboCop
124
126
 
125
127
  def check(begin_node)
126
128
  node = begin_node.children.first
127
- return offense(begin_node, 'a keyword') if keyword_with_redundant_parentheses?(node)
128
- return offense(begin_node, 'a literal') if disallowed_literal?(begin_node, node)
129
- return offense(begin_node, 'a variable') if node.variable?
130
- return offense(begin_node, 'a constant') if node.const_type?
131
129
 
132
- return offense(begin_node, 'an interpolated expression') if interpolation?(begin_node)
130
+ if (message = find_offense_message(begin_node, node))
131
+ return offense(begin_node, message)
132
+ end
133
133
 
134
134
  check_send(begin_node, node) if node.call_type?
135
135
  end
136
136
 
137
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
138
+ def find_offense_message(begin_node, node)
139
+ return 'a keyword' if keyword_with_redundant_parentheses?(node)
140
+ return 'a literal' if disallowed_literal?(begin_node, node)
141
+ return 'a variable' if node.variable?
142
+ return 'a constant' if node.const_type?
143
+ return 'an interpolated expression' if interpolation?(begin_node)
144
+
145
+ return if begin_node.chained? || !begin_node.parent.nil?
146
+
147
+ if node.and_type? || node.or_type?
148
+ 'a logical expression'
149
+ elsif node.respond_to?(:comparison_method?) && node.comparison_method?
150
+ 'a comparison expression'
151
+ end
152
+ end
153
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
154
+
137
155
  # @!method interpolation?(node)
138
156
  def_node_matcher :interpolation?, '[^begin ^^dstr]'
139
157
 
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Identifies places where argument can be replaced from
7
+ # a deterministic regexp to a string.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # 'foo'.byteindex(/f/)
12
+ # 'foo'.byterindex(/f/)
13
+ # 'foo'.gsub(/f/, 'x')
14
+ # 'foo'.gsub!(/f/, 'x')
15
+ # 'foo'.partition(/f/)
16
+ # 'foo'.rpartition(/f/)
17
+ # 'foo'.scan(/f/)
18
+ # 'foo'.split(/f/)
19
+ # 'foo'.start_with?(/f/)
20
+ # 'foo'.sub(/f/, 'x')
21
+ # 'foo'.sub!(/f/, 'x')
22
+ #
23
+ # # good
24
+ # 'foo'.byteindex('f')
25
+ # 'foo'.byterindex('f')
26
+ # 'foo'.gsub('f', 'x')
27
+ # 'foo'.gsub!('f', 'x')
28
+ # 'foo'.partition('f')
29
+ # 'foo'.rpartition('f')
30
+ # 'foo'.scan('f')
31
+ # 'foo'.split('f')
32
+ # 'foo'.start_with?('f')
33
+ # 'foo'.sub('f', 'x')
34
+ # 'foo'.sub!('f', 'x')
35
+ class RedundantRegexpArgument < Base
36
+ extend AutoCorrector
37
+
38
+ MSG = 'Use string `%<prefer>s` as argument instead of regexp `%<current>s`.'
39
+ RESTRICT_ON_SEND = %i[
40
+ byteindex byterindex gsub gsub! partition rpartition scan split start_with? sub sub!
41
+ ].freeze
42
+ DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/.freeze
43
+ STR_SPECIAL_CHARS = %w[
44
+ \a \c \C \e \f \M \n \" \' \\\\ \t \b \f \r \u \v \x \0 \1 \2 \3 \4 \5 \6 \7
45
+ ].freeze
46
+
47
+ def on_send(node)
48
+ return unless (regexp_node = node.first_argument)
49
+ return unless regexp_node.regexp_type?
50
+ return if !regexp_node.regopt.children.empty? || regexp_node.content == ' '
51
+ return unless determinist_regexp?(regexp_node)
52
+
53
+ prefer = preferred_argument(regexp_node)
54
+ message = format(MSG, prefer: prefer, current: regexp_node.source)
55
+
56
+ add_offense(regexp_node, message: message) do |corrector|
57
+ corrector.replace(regexp_node, prefer)
58
+ end
59
+ end
60
+ alias on_csend on_send
61
+
62
+ private
63
+
64
+ def determinist_regexp?(regexp_node)
65
+ DETERMINISTIC_REGEX.match?(regexp_node.source)
66
+ end
67
+
68
+ def preferred_argument(regexp_node)
69
+ new_argument = replacement(regexp_node)
70
+
71
+ if new_argument.include?('"')
72
+ new_argument.gsub!("'", "\\\\'")
73
+ quote = "'"
74
+ else
75
+ quote = '"'
76
+ end
77
+
78
+ "#{quote}#{new_argument}#{quote}"
79
+ end
80
+
81
+ def replacement(regexp_node)
82
+ regexp_content = regexp_node.content
83
+ stack = []
84
+ chars = regexp_content.chars.each_with_object([]) do |char, strings|
85
+ if stack.empty? && char == '\\'
86
+ stack.push(char)
87
+ else
88
+ strings << "#{stack.pop}#{char}"
89
+ end
90
+ end
91
+ chars.map do |char|
92
+ char = char.dup
93
+ char.delete!('\\') unless STR_SPECIAL_CHARS.include?(char)
94
+ char
95
+ end.join
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for the instantiation of regexp using redundant `Regexp.new` or `Regexp.compile`.
7
+ # Autocorrect replaces to regexp literal which is the simplest and fastest.
8
+ #
9
+ # @example
10
+ #
11
+ # # bad
12
+ # Regexp.new(/regexp/)
13
+ # Regexp.compile(/regexp/)
14
+ #
15
+ # # good
16
+ # /regexp/
17
+ # Regexp.new('regexp')
18
+ # Regexp.compile('regexp')
19
+ #
20
+ class RedundantRegexpConstructor < Base
21
+ extend AutoCorrector
22
+
23
+ MSG = 'Remove the redundant `Regexp.%<method>s`.'
24
+ RESTRICT_ON_SEND = %i[new compile].freeze
25
+
26
+ # @!method redundant_regexp_constructor(node)
27
+ def_node_matcher :redundant_regexp_constructor, <<~PATTERN
28
+ (send
29
+ (const {nil? cbase} :Regexp) {:new :compile}
30
+ (regexp $... (regopt $...)))
31
+ PATTERN
32
+
33
+ def on_send(node)
34
+ return unless (regexp, regopt = redundant_regexp_constructor(node))
35
+
36
+ add_offense(node, message: format(MSG, method: node.method_name)) do |corrector|
37
+ pattern = regexp.map(&:source).join
38
+ regopt = regopt.join
39
+
40
+ corrector.replace(node, "/#{pattern}/#{regopt}")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -44,7 +44,8 @@ module RuboCop
44
44
 
45
45
  def on_regexp(node)
46
46
  each_escape(node) do |char, index, within_character_class|
47
- next if allowed_escape?(node, char, index, within_character_class)
47
+ next if char.valid_encoding? && allowed_escape?(node, char, index,
48
+ within_character_class)
48
49
 
49
50
  location = escape_range_at_index(node, index)
50
51
 
@@ -22,13 +22,18 @@ module RuboCop
22
22
  # return something
23
23
  # end
24
24
  #
25
- # # good
25
+ # # bad
26
26
  # def test
27
27
  # return something if something_else
28
28
  # end
29
29
  #
30
30
  # # good
31
31
  # def test
32
+ # something if something_else
33
+ # end
34
+ #
35
+ # # good
36
+ # def test
32
37
  # if x
33
38
  # elsif y
34
39
  # else
@@ -136,7 +141,7 @@ module RuboCop
136
141
  end
137
142
 
138
143
  def check_if_node(node)
139
- return if node.modifier_form? || node.ternary?
144
+ return if node.ternary?
140
145
 
141
146
  check_branch(node.if_branch)
142
147
  check_branch(node.else_branch)
@@ -62,7 +62,9 @@ module RuboCop
62
62
  end
63
63
 
64
64
  def multiple_statements?(branch)
65
- branch && branch.children.compact.count > 1
65
+ return false unless branch&.begin_type?
66
+
67
+ !branch.children.empty?
66
68
  end
67
69
 
68
70
  def self_assign?(variable, branch)
@@ -73,6 +75,11 @@ module RuboCop
73
75
  add_offense(offense_branch) do |corrector|
74
76
  assignment_value = opposite_branch ? opposite_branch.source : 'nil'
75
77
  replacement = "#{assignment_value} #{keyword} #{if_node.condition.source}"
78
+ if opposite_branch.respond_to?(:heredoc?) && opposite_branch.heredoc?
79
+ replacement += opposite_branch.loc.heredoc_end.with(
80
+ begin_pos: opposite_branch.source_range.end_pos
81
+ ).source
82
+ end
76
83
 
77
84
  corrector.replace(if_node, replacement)
78
85
  end
@@ -198,7 +198,7 @@ module RuboCop
198
198
  end
199
199
 
200
200
  def with_logical_operator?(node)
201
- return unless (parent = node.parent)
201
+ return false unless (parent = node.parent)
202
202
 
203
203
  parent.or_type? || parent.and_type?
204
204
  end
@@ -157,6 +157,8 @@ module RuboCop
157
157
  return delimiter?(node.parent, char)
158
158
  end
159
159
 
160
+ return true unless node.loc.begin
161
+
160
162
  delimiters = [node.loc.begin.source[-1], node.loc.end.source[0]]
161
163
 
162
164
  delimiters.include?(char)