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
@@ -3,7 +3,16 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Enforces using // or %r around regular expressions.
6
+ # Enforces using `//` or `%r` around regular expressions.
7
+ #
8
+ # NOTE: The following `%r` cases using a regexp starts with a blank or `=`
9
+ # as a method argument allowed to prevent syntax errors.
10
+ #
11
+ # [source,ruby]
12
+ # ----
13
+ # do_something %r{ regexp} # `do_something / regexp/` is an invalid syntax.
14
+ # do_something %r{=regexp} # `do_something /=regexp/` is an invalid syntax.
15
+ # ----
7
16
  #
8
17
  # @example EnforcedStyle: slashes (default)
9
18
  # # bad
@@ -151,7 +160,7 @@ module RuboCop
151
160
 
152
161
  def allowed_omit_parentheses_with_percent_r_literal?(node)
153
162
  return false unless node.parent&.call_type?
154
- return true if node.content.start_with?(' ')
163
+ return true if node.content.start_with?(' ', '=')
155
164
 
156
165
  enforced_style = config.for_cop('Style/MethodCallWithArgsParentheses')['EnforcedStyle']
157
166
 
@@ -88,10 +88,7 @@ module RuboCop
88
88
  return unless previous_older_sibling
89
89
 
90
90
  add_offense(node, message: format(MSG, name: node.method_name)) do |corrector|
91
- corrector.swap(
92
- range_with_comments_and_lines(previous_older_sibling),
93
- range_with_comments_and_lines(node.parent.if_type? ? node.parent : node)
94
- )
91
+ autocorrect(corrector, node, previous_older_sibling)
95
92
  end
96
93
  end
97
94
 
@@ -109,11 +106,20 @@ module RuboCop
109
106
  break unless sibling&.send_type? && sibling&.method?(node.method_name)
110
107
  break unless sibling.arguments? && !sibling.receiver
111
108
  break unless in_same_section?(sibling, node)
109
+ break unless node.first_argument.str_type? && sibling.first_argument.str_type?
112
110
 
113
- node.first_argument.source < sibling.first_argument.source
111
+ node.first_argument.value < sibling.first_argument.value
114
112
  end
115
113
  end
116
114
 
115
+ def autocorrect(corrector, node, previous_older_sibling)
116
+ range1 = range_with_comments_and_lines(previous_older_sibling)
117
+ range2 = range_with_comments_and_lines(node.parent.if_type? ? node.parent : node)
118
+
119
+ corrector.remove(range2)
120
+ corrector.insert_before(range1, range2.source)
121
+ end
122
+
117
123
  def search_node(node)
118
124
  node.parent.if_type? ? node.parent : node
119
125
  end
@@ -3,9 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks for uses of rescue in its modifier form.
7
- #
8
- # The cop to check `rescue` in its modifier form is added for following
6
+ # Checks for uses of `rescue` in its modifier form is added for following
9
7
  # reasons:
10
8
  #
11
9
  # * The syntax of modifier form `rescue` can be misleading because it
@@ -3,9 +3,13 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Enforces consistency between 'return nil' and 'return'.
6
+ # Enforces consistency between `return nil` and `return`.
7
7
  #
8
- # Supported styles are: return, return_nil.
8
+ # This cop is disabled by default. Because there seems to be a perceived semantic difference
9
+ # between `return` and `return nil`. The former can be seen as just halting evaluation,
10
+ # while the latter might be used when the return value is of specific concern.
11
+ #
12
+ # Supported styles are `return` and `return_nil`.
9
13
  #
10
14
  # @example EnforcedStyle: return (default)
11
15
  # # bad
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks if `return` or `return nil` is used in predicate method definitions.
7
+ #
8
+ # @safety
9
+ # Autocorrection is marked as unsafe because the change of the return value
10
+ # from `nil` to `false` could potentially lead to incompatibility issues.
11
+ #
12
+ # @example
13
+ # # bad
14
+ # def foo?
15
+ # return if condition
16
+ #
17
+ # do_something?
18
+ # end
19
+ #
20
+ # # bad
21
+ # def foo?
22
+ # return nil if condition
23
+ #
24
+ # do_something?
25
+ # end
26
+ #
27
+ # # good
28
+ # def foo?
29
+ # return false if condition
30
+ #
31
+ # do_something?
32
+ # end
33
+ #
34
+ # @example AllowedMethods: ['foo?']
35
+ # # good
36
+ # def foo?
37
+ # return if condition
38
+ #
39
+ # do_something?
40
+ # end
41
+ #
42
+ # @example AllowedPatterns: [/foo/]
43
+ # # good
44
+ # def foo?
45
+ # return if condition
46
+ #
47
+ # do_something?
48
+ # end
49
+ #
50
+ class ReturnNilInPredicateMethodDefinition < Base
51
+ extend AutoCorrector
52
+ include AllowedMethods
53
+ include AllowedPattern
54
+
55
+ MSG = 'Return `false` instead of `nil` in predicate methods.'
56
+
57
+ # @!method return_nil?(node)
58
+ def_node_matcher :return_nil?, <<~PATTERN
59
+ {(return) (return (nil))}
60
+ PATTERN
61
+
62
+ def on_def(node)
63
+ return unless node.predicate_method?
64
+ return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)
65
+ return unless (body = node.body)
66
+
67
+ body.each_descendant(:return) do |return_node|
68
+ register_offense(return_node, 'return false') if return_nil?(return_node)
69
+ end
70
+
71
+ return unless (nil_node = nil_node_at_the_end_of_method_body(body))
72
+
73
+ register_offense(nil_node, 'false')
74
+ end
75
+ alias on_defs on_def
76
+
77
+ private
78
+
79
+ def nil_node_at_the_end_of_method_body(body)
80
+ return body if body.nil_type?
81
+ return unless body.begin_type?
82
+ return unless (last_child = body.children.last)
83
+
84
+ last_child if last_child.is_a?(AST::Node) && last_child.nil_type?
85
+ end
86
+
87
+ def register_offense(offense_node, replacement)
88
+ add_offense(offense_node) do |corrector|
89
+ corrector.replace(offense_node, replacement)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -84,6 +84,7 @@ module RuboCop
84
84
  }
85
85
  PATTERN
86
86
 
87
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
87
88
  def on_send(node)
88
89
  return unless (block_node = node.block_node)
89
90
  return if block_node.body&.begin_type?
@@ -91,11 +92,14 @@ module RuboCop
91
92
  return unless (regexp_method_send_node = extract_send_node(block_node))
92
93
  return if match_predicate_without_receiver?(regexp_method_send_node)
93
94
 
94
- opposite = opposite?(regexp_method_send_node)
95
+ replacement = replacement(regexp_method_send_node, node)
96
+ return if target_ruby_version <= 2.2 && replacement == 'grep_v'
97
+
95
98
  regexp = find_regexp(regexp_method_send_node, block_node)
96
99
 
97
- register_offense(node, block_node, regexp, opposite)
100
+ register_offense(node, block_node, regexp, replacement)
98
101
  end
102
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
99
103
 
100
104
  private
101
105
 
@@ -105,9 +109,15 @@ module RuboCop
105
109
  node.hash_type? || creates_hash?(node) || env_const?(node)
106
110
  end
107
111
 
108
- def register_offense(node, block_node, regexp, opposite)
109
- method_name = node.method_name.to_sym
110
- replacement = opposite ? OPPOSITE_REPLACEMENTS[method_name] : REPLACEMENTS[method_name]
112
+ def replacement(regexp_method_send_node, node)
113
+ opposite = opposite?(regexp_method_send_node)
114
+
115
+ method_name = node.method_name
116
+
117
+ opposite ? OPPOSITE_REPLACEMENTS[method_name] : REPLACEMENTS[method_name]
118
+ end
119
+
120
+ def register_offense(node, block_node, regexp, replacement)
111
121
  message = format(MSG, replacement: replacement, original_method: node.method_name)
112
122
 
113
123
  add_offense(block_node, message: message) do |corrector|
@@ -62,9 +62,6 @@ module RuboCop
62
62
  private
63
63
 
64
64
  def check_for_line_terminator_or_opener
65
- # Make the obvious check first
66
- return unless processed_source.raw_source.include?(';')
67
-
68
65
  each_semicolon do |line, column, token_before_semicolon|
69
66
  register_semicolon(line, column, false, token_before_semicolon)
70
67
  end
@@ -90,8 +87,11 @@ module RuboCop
90
87
  0
91
88
  elsif exist_semicolon_before_right_curly_brace?(tokens)
92
89
  -3
93
- elsif exist_semicolon_after_left_curly_brace?(tokens)
90
+ elsif exist_semicolon_after_left_curly_brace?(tokens) ||
91
+ exist_semicolon_after_left_string_interpolation_brace?(tokens)
94
92
  2
93
+ elsif exist_semicolon_before_right_string_interpolation_brace?(tokens)
94
+ -4
95
95
  end
96
96
  end
97
97
 
@@ -103,6 +103,14 @@ module RuboCop
103
103
  tokens[1]&.left_curly_brace? && tokens[2]&.semicolon?
104
104
  end
105
105
 
106
+ def exist_semicolon_before_right_string_interpolation_brace?(tokens)
107
+ tokens[-3]&.type == :tSTRING_DEND && tokens[-4]&.semicolon?
108
+ end
109
+
110
+ def exist_semicolon_after_left_string_interpolation_brace?(tokens)
111
+ tokens[1]&.type == :tSTRING_DBEG && tokens[2]&.semicolon?
112
+ end
113
+
106
114
  def register_semicolon(line, column, after_expression, token_before_semicolon = nil)
107
115
  range = source_range(processed_source.buffer, line, column)
108
116
 
@@ -198,7 +198,7 @@ module RuboCop
198
198
  end
199
199
 
200
200
  def command_or_kernel_call?(name, node)
201
- return unless node.method?(name)
201
+ return false unless node.method?(name)
202
202
 
203
203
  node.command?(name) || kernel_call?(node, name)
204
204
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for single-line `do`...`end` block.
7
+ #
8
+ # In practice a single line `do`...`end` is autocorrected when `EnforcedStyle: semantic`
9
+ # in `Style/BlockDelimiters`. The autocorrection maintains the `do` ... `end` syntax to
10
+ # preserve semantics and does not change it to `{`...`}` block.
11
+ #
12
+ # @example
13
+ #
14
+ # # bad
15
+ # foo do |arg| bar(arg) end
16
+ #
17
+ # # good
18
+ # foo do |arg|
19
+ # bar(arg)
20
+ # end
21
+ #
22
+ # # bad
23
+ # ->(arg) do bar(arg) end
24
+ #
25
+ # # good
26
+ # ->(arg) { bar(arg) }
27
+ #
28
+ class SingleLineDoEndBlock < Base
29
+ extend AutoCorrector
30
+
31
+ MSG = 'Prefer multiline `do`...`end` block.'
32
+
33
+ def on_block(node)
34
+ return if !node.single_line? || node.braces?
35
+
36
+ add_offense(node) do |corrector|
37
+ corrector.insert_after(do_line(node), "\n")
38
+
39
+ node_body = node.body
40
+
41
+ if node_body.respond_to?(:heredoc?) && node_body.heredoc?
42
+ corrector.remove(node.loc.end)
43
+ corrector.insert_after(node_body.loc.heredoc_end, "\nend")
44
+ else
45
+ corrector.insert_after(node_body, "\n")
46
+ end
47
+ end
48
+ end
49
+ alias on_numblock on_block
50
+
51
+ private
52
+
53
+ def do_line(node)
54
+ if node.numblock_type? || node.arguments.children.empty? || node.send_node.lambda_literal?
55
+ node.loc.begin
56
+ else
57
+ node.arguments
58
+ end
59
+ end
60
+
61
+ def x(corrector, node); end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -135,7 +135,7 @@ module RuboCop
135
135
 
136
136
  def disallow_endless_method_style?
137
137
  endless_method_config = config.for_cop('Style/EndlessMethod')
138
- return false unless endless_method_config['Enabled']
138
+ return true unless endless_method_config['Enabled']
139
139
 
140
140
  endless_method_config['EnforcedStyle'] == 'disallow'
141
141
  end
@@ -173,7 +173,9 @@ module RuboCop
173
173
  end
174
174
 
175
175
  def correct_for_comment(corrector, node, if_branch)
176
- comments = processed_source.ast_with_comments[if_branch]
176
+ comments = processed_source.ast_with_comments[if_branch].select do |comment|
177
+ comment.loc.line < if_branch.condition.first_line
178
+ end
177
179
  comment_text = comments.map(&:text).join("\n") << "\n"
178
180
 
179
181
  corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
@@ -186,7 +188,9 @@ module RuboCop
186
188
  begin_pos = condition.first_argument.source_range.begin_pos
187
189
  return if end_pos > begin_pos
188
190
 
189
- corrector.replace(range_between(end_pos, begin_pos), '(')
191
+ range = range_between(end_pos, begin_pos)
192
+ corrector.remove(range)
193
+ corrector.insert_after(range, '(')
190
194
  corrector.insert_after(condition.last_argument, ')')
191
195
  end
192
196
 
@@ -3,8 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- #
7
- # This cop looks for uses of Perl-style global variables.
6
+ # Looks for uses of Perl-style global variables.
8
7
  # Correcting to global variables in the 'English' library
9
8
  # will add a require statement to the top of the file if
10
9
  # enabled by RequireEnglish config.
@@ -234,9 +233,9 @@ module RuboCop
234
233
  end
235
234
 
236
235
  def matching_styles(global)
237
- STYLE_VARS_MAP.map do |style, vars|
236
+ STYLE_VARS_MAP.filter_map do |style, vars|
238
237
  style if vars.values.flatten(1).include? global
239
- end.compact
238
+ end
240
239
  end
241
240
 
242
241
  def english_name_replacement(preferred_name, node)
@@ -3,22 +3,42 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Style
6
- # Checks that quotes inside the string interpolation
6
+ # Checks that quotes inside string, symbol, and regexp interpolations
7
7
  # match the configured preference.
8
8
  #
9
9
  # @example EnforcedStyle: single_quotes (default)
10
10
  # # bad
11
- # result = "Tests #{success ? "PASS" : "FAIL"}"
11
+ # string = "Tests #{success ? "PASS" : "FAIL"}"
12
+ # symbol = :"Tests #{success ? "PASS" : "FAIL"}"
13
+ # heredoc = <<~TEXT
14
+ # Tests #{success ? "PASS" : "FAIL"}
15
+ # TEXT
16
+ # regexp = /Tests #{success ? "PASS" : "FAIL"}/
12
17
  #
13
18
  # # good
14
- # result = "Tests #{success ? 'PASS' : 'FAIL'}"
19
+ # string = "Tests #{success ? 'PASS' : 'FAIL'}"
20
+ # symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
21
+ # heredoc = <<~TEXT
22
+ # Tests #{success ? 'PASS' : 'FAIL'}
23
+ # TEXT
24
+ # regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
15
25
  #
16
26
  # @example EnforcedStyle: double_quotes
17
27
  # # bad
18
- # result = "Tests #{success ? 'PASS' : 'FAIL'}"
28
+ # string = "Tests #{success ? 'PASS' : 'FAIL'}"
29
+ # symbol = :"Tests #{success ? 'PASS' : 'FAIL'}"
30
+ # heredoc = <<~TEXT
31
+ # Tests #{success ? 'PASS' : 'FAIL'}
32
+ # TEXT
33
+ # regexp = /Tests #{success ? 'PASS' : 'FAIL'}/
19
34
  #
20
35
  # # good
21
- # result = "Tests #{success ? "PASS" : "FAIL"}"
36
+ # string = "Tests #{success ? "PASS" : "FAIL"}"
37
+ # symbol = :"Tests #{success ? "PASS" : "FAIL"}"
38
+ # heredoc = <<~TEXT
39
+ # Tests #{success ? "PASS" : "FAIL"}
40
+ # TEXT
41
+ # regexp = /Tests #{success ? "PASS" : "FAIL"}/
22
42
  class StringLiteralsInInterpolation < Base
23
43
  include ConfigurableEnforcedStyle
24
44
  include StringLiteralsHelp
@@ -29,6 +49,11 @@ module RuboCop
29
49
  StringLiteralCorrector.correct(corrector, node, style)
30
50
  end
31
51
 
52
+ # Cop classes that include the StringHelp module usually ignore regexp
53
+ # nodes. Not so for this cop, which is why we override the on_regexp
54
+ # definition with an empty one.
55
+ def on_regexp(node); end
56
+
32
57
  private
33
58
 
34
59
  def message(_node)
@@ -22,6 +22,15 @@ module RuboCop
22
22
  # # bad
23
23
  # [:foo, :bar, :baz]
24
24
  #
25
+ # # bad (contains spaces)
26
+ # %i[foo\ bar baz\ quux]
27
+ #
28
+ # # bad (contains [] with spaces)
29
+ # %i[foo \[ \]]
30
+ #
31
+ # # bad (contains () with spaces)
32
+ # %i(foo \( \))
33
+ #
25
34
  # @example EnforcedStyle: brackets
26
35
  # # good
27
36
  # [:foo, :bar, :baz]
@@ -40,6 +49,15 @@ module RuboCop
40
49
 
41
50
  PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
42
51
  ARRAY_MSG = 'Use %<prefer>s for an array of symbols.'
52
+ DELIMITERS = ['[', ']', '(', ')'].freeze
53
+ SPECIAL_GVARS = %w[
54
+ $! $" $$ $& $' $* $+ $, $/ $; $: $. $< $= $> $? $@ $\\ $_ $` $~ $0
55
+ $-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w
56
+ ].freeze
57
+ REDEFINABLE_OPERATORS = %w(
58
+ | ^ & <=> == === =~ > >= < <= << >>
59
+ + - * / % ** ~ +@ -@ [] []= ` ! != !~
60
+ ).freeze
43
61
 
44
62
  class << self
45
63
  attr_accessor :largest_brackets
@@ -47,7 +65,7 @@ module RuboCop
47
65
 
48
66
  def on_array(node)
49
67
  if bracketed_array_of?(:sym, node)
50
- return if symbols_contain_spaces?(node)
68
+ return if complex_content?(node)
51
69
 
52
70
  check_bracketed_array(node, 'i')
53
71
  elsif node.percent_literal?(:symbol)
@@ -57,13 +75,24 @@ module RuboCop
57
75
 
58
76
  private
59
77
 
60
- def symbols_contain_spaces?(node)
78
+ def complex_content?(node)
61
79
  node.children.any? do |sym|
62
- content, = *sym
63
- content.to_s.include?(' ')
80
+ return false if DELIMITERS.include?(sym.source)
81
+
82
+ content = *sym
83
+ content = content.map { |c| c.is_a?(AST::Node) ? c.source : c }.join
84
+ content_without_delimiter_pairs = content.gsub(/(\[[^\s\[\]]*\])|(\([^\s\(\)]*\))/, '')
85
+
86
+ content.include?(' ') || DELIMITERS.any? do |delimiter|
87
+ content_without_delimiter_pairs.include?(delimiter)
88
+ end
64
89
  end
65
90
  end
66
91
 
92
+ def invalid_percent_array_contents?(node)
93
+ complex_content?(node)
94
+ end
95
+
67
96
  def build_bracketed_array(node)
68
97
  return '[]' if node.children.empty?
69
98
 
@@ -88,15 +117,6 @@ module RuboCop
88
117
  end
89
118
 
90
119
  def symbol_without_quote?(string)
91
- special_gvars = %w[
92
- $! $" $$ $& $' $* $+ $, $/ $; $: $. $< $= $> $? $@ $\\ $_ $` $~ $0
93
- $-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w
94
- ]
95
- redefinable_operators = %w(
96
- | ^ & <=> == === =~ > >= < <= << >>
97
- + - * / % ** ~ +@ -@ [] []= ` ! != !~
98
- )
99
-
100
120
  # method name
101
121
  /\A[a-zA-Z_]\w*[!?]?\z/.match?(string) ||
102
122
  # instance / class variable
@@ -104,8 +124,8 @@ module RuboCop
104
124
  # global variable
105
125
  /\A\$[1-9]\d*\z/.match?(string) ||
106
126
  /\A\$[a-zA-Z_]\w*\z/.match?(string) ||
107
- special_gvars.include?(string) ||
108
- redefinable_operators.include?(string)
127
+ SPECIAL_GVARS.include?(string) ||
128
+ REDEFINABLE_OPERATORS.include?(string)
109
129
  end
110
130
  end
111
131
  end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # Checks for the use of `YAML.load`, `YAML.safe_load`, and `YAML.parse` with
7
+ # `File.read` argument.
8
+ #
9
+ # NOTE: `YAML.safe_load_file` was introduced in Ruby 3.0.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # YAML.load(File.read(path))
15
+ # YAML.parse(File.read(path))
16
+ #
17
+ # # good
18
+ # YAML.load_file(path)
19
+ # YAML.parse_file(path)
20
+ #
21
+ # # bad
22
+ # YAML.safe_load(File.read(path)) # Ruby 3.0 and newer
23
+ #
24
+ # # good
25
+ # YAML.safe_load_file(path) # Ruby 3.0 and newer
26
+ #
27
+ class YAMLFileRead < Base
28
+ extend AutoCorrector
29
+
30
+ MSG = 'Use `%<prefer>s` instead.'
31
+ RESTRICT_ON_SEND = %i[load safe_load parse].freeze
32
+
33
+ # @!method yaml_file_read?(node)
34
+ def_node_matcher :yaml_file_read?, <<~PATTERN
35
+ (send
36
+ (const {cbase nil?} :YAML) _
37
+ (send
38
+ (const {cbase nil?} :File) :read $_) $...)
39
+ PATTERN
40
+
41
+ def on_send(node)
42
+ return if node.method?(:safe_load) && target_ruby_version <= 2.7
43
+ return unless (file_path, rest_arguments = yaml_file_read?(node))
44
+
45
+ range = offense_range(node)
46
+ rest_arguments = if rest_arguments.empty?
47
+ ''
48
+ else
49
+ ", #{rest_arguments.map(&:source).join(', ')}"
50
+ end
51
+ prefer = "#{node.method_name}_file(#{file_path.source}#{rest_arguments})"
52
+
53
+ add_offense(range, message: format(MSG, prefer: prefer)) do |corrector|
54
+ corrector.replace(range, prefer)
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def offense_range(node)
61
+ node.loc.selector.join(node.source_range.end)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -118,16 +118,18 @@ module RuboCop
118
118
  node.comparison_method? && !noncommutative_operator?(node)
119
119
  end
120
120
 
121
+ # rubocop:disable Metrics/CyclomaticComplexity
121
122
  def valid_yoda?(node)
122
- lhs = node.receiver
123
- rhs = node.first_argument
123
+ return true unless (rhs = node.first_argument)
124
124
 
125
+ lhs = node.receiver
125
126
  return true if (constant_portion?(lhs) && constant_portion?(rhs)) ||
126
127
  (!constant_portion?(lhs) && !constant_portion?(rhs)) ||
127
128
  interpolation?(lhs)
128
129
 
129
130
  enforce_yoda? ? constant_portion?(lhs) : constant_portion?(rhs)
130
131
  end
132
+ # rubocop:enable Metrics/CyclomaticComplexity
131
133
 
132
134
  def message(node)
133
135
  format(MSG, source: node.source)
@@ -19,22 +19,23 @@ module RuboCop
19
19
  # differently on different classes, and are not guaranteed to
20
20
  # have the same result if reversed.
21
21
  #
22
- # @example SupportedOperators: ['*', '+', '&'']
22
+ # @example SupportedOperators: ['*', '+', '&', '|', '^'] (default)
23
23
  # # bad
24
- # 1 + x
25
24
  # 10 * y
25
+ # 1 + x
26
26
  # 1 & z
27
+ # 1 | x
28
+ # 1 ^ x
27
29
  # 1 + CONST
28
30
  #
29
31
  # # good
30
- # 60 * 24
31
- # x + 1
32
32
  # y * 10
33
+ # x + 1
33
34
  # z & 1
35
+ # x | 1
36
+ # x ^ 1
34
37
  # CONST + 1
35
- #
36
- # # good
37
- # 1 | x
38
+ # 60 * 24
38
39
  #
39
40
  class YodaExpression < Base
40
41
  extend AutoCorrector
@@ -112,7 +112,7 @@ module RuboCop
112
112
  end
113
113
 
114
114
  def external_dependency_checksum
115
- keys = cops.map(&:external_dependency_checksum).compact
115
+ keys = cops.filter_map(&:external_dependency_checksum)
116
116
  Digest::SHA1.hexdigest(keys.join)
117
117
  end
118
118