rubocop 0.75.0 → 0.79.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +341 -316
  4. data/lib/rubocop.rb +48 -31
  5. data/lib/rubocop/ast/builder.rb +43 -41
  6. data/lib/rubocop/ast/node.rb +5 -1
  7. data/lib/rubocop/ast/node/block_node.rb +2 -0
  8. data/lib/rubocop/ast/node/def_node.rb +11 -0
  9. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  10. data/lib/rubocop/ast/node/return_node.rb +24 -0
  11. data/lib/rubocop/ast/traversal.rb +11 -3
  12. data/lib/rubocop/cli.rb +11 -227
  13. data/lib/rubocop/cli/command.rb +21 -0
  14. data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
  15. data/lib/rubocop/cli/command/base.rb +33 -0
  16. data/lib/rubocop/cli/command/execute_runner.rb +76 -0
  17. data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
  18. data/lib/rubocop/cli/command/show_cops.rb +80 -0
  19. data/lib/rubocop/cli/command/version.rb +17 -0
  20. data/lib/rubocop/cli/environment.rb +21 -0
  21. data/lib/rubocop/comment_config.rb +2 -2
  22. data/lib/rubocop/config.rb +8 -1
  23. data/lib/rubocop/config_loader.rb +20 -20
  24. data/lib/rubocop/config_loader_resolver.rb +2 -1
  25. data/lib/rubocop/config_obsoletion.rb +73 -11
  26. data/lib/rubocop/config_validator.rb +77 -110
  27. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  28. data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
  29. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  30. data/lib/rubocop/cop/commissioner.rb +15 -7
  31. data/lib/rubocop/cop/cop.rb +31 -6
  32. data/lib/rubocop/cop/corrector.rb +8 -7
  33. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  34. data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
  35. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  36. data/lib/rubocop/cop/generator.rb +3 -4
  37. data/lib/rubocop/cop/generator/configuration_injector.rb +2 -2
  38. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  39. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
  40. data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
  41. data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
  42. data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +3 -2
  43. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  44. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  45. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  46. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  47. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  48. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  49. data/lib/rubocop/cop/layout/extra_spacing.rb +1 -1
  50. data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +14 -12
  51. data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
  52. data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
  53. data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
  54. data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +16 -8
  55. data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +5 -5
  56. data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
  57. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +41 -114
  58. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  59. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  60. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
  61. data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
  62. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  63. data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
  64. data/lib/rubocop/cop/layout/space_around_operators.rb +32 -7
  65. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  66. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  67. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  68. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  69. data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
  70. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  71. data/lib/rubocop/cop/lint/debugger.rb +2 -2
  72. data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
  73. data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
  74. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  75. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  76. data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
  77. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  78. data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
  79. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  80. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +26 -26
  81. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +10 -12
  82. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  83. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +6 -6
  84. data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +1 -1
  85. data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
  86. data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
  87. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  88. data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
  89. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  90. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  91. data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
  92. data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
  93. data/lib/rubocop/cop/lint/void.rb +7 -26
  94. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  95. data/lib/rubocop/cop/metrics/method_length.rb +1 -1
  96. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  97. data/lib/rubocop/cop/migration/department_name.rb +16 -1
  98. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  99. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -7
  100. data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
  101. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  102. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  103. data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
  104. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  105. data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -4
  106. data/lib/rubocop/cop/mixin/trailing_comma.rb +14 -9
  107. data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
  108. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  109. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
  110. data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +4 -4
  111. data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
  112. data/lib/rubocop/cop/offense.rb +11 -0
  113. data/lib/rubocop/cop/registry.rb +8 -3
  114. data/lib/rubocop/cop/style/alias.rb +1 -1
  115. data/lib/rubocop/cop/style/array_join.rb +1 -1
  116. data/lib/rubocop/cop/style/attr.rb +10 -2
  117. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
  118. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  119. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  120. data/lib/rubocop/cop/style/copyright.rb +11 -7
  121. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  122. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  123. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  124. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  125. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  126. data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
  127. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  128. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  129. data/lib/rubocop/cop/style/format_string.rb +10 -7
  130. data/lib/rubocop/cop/style/format_string_token.rb +15 -34
  131. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
  132. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  133. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  134. data/lib/rubocop/cop/style/if_unless_modifier.rb +45 -3
  135. data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
  136. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  137. data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
  138. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  139. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +24 -227
  140. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +168 -0
  141. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  142. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  143. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  144. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  145. data/lib/rubocop/cop/style/multiline_when_then.rb +6 -2
  146. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  147. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
  148. data/lib/rubocop/cop/style/next.rb +5 -5
  149. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  150. data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
  151. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  152. data/lib/rubocop/cop/style/option_hash.rb +3 -3
  153. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  154. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  155. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  156. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  157. data/lib/rubocop/cop/style/redundant_parentheses.rb +3 -3
  158. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  159. data/lib/rubocop/cop/style/redundant_return.rb +39 -29
  160. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
  161. data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
  162. data/lib/rubocop/cop/style/semicolon.rb +13 -2
  163. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  164. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  165. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
  166. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  167. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  168. data/lib/rubocop/cop/team.rb +5 -0
  169. data/lib/rubocop/cop/util.rb +1 -1
  170. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  171. data/lib/rubocop/cop/variable_force.rb +7 -5
  172. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  173. data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
  174. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
  175. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  176. data/lib/rubocop/formatter/formatter_set.rb +16 -16
  177. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  178. data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
  179. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  180. data/lib/rubocop/formatter/tap_formatter.rb +9 -6
  181. data/lib/rubocop/node_pattern.rb +4 -2
  182. data/lib/rubocop/options.rb +20 -26
  183. data/lib/rubocop/processed_source.rb +1 -1
  184. data/lib/rubocop/rake_task.rb +1 -0
  185. data/lib/rubocop/result_cache.rb +24 -8
  186. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  187. data/lib/rubocop/runner.rb +50 -29
  188. data/lib/rubocop/target_finder.rb +12 -6
  189. data/lib/rubocop/target_ruby.rb +151 -0
  190. data/lib/rubocop/version.rb +1 -1
  191. metadata +50 -34
  192. data/lib/rubocop/cop/mixin/safe_mode.rb +0 -24
@@ -54,9 +54,10 @@ module RuboCop
54
54
  }.freeze
55
55
 
56
56
  def on_send(node)
57
- return if node.each_ancestor(:send, :block).any? do |ancestor|
58
- ignored_method?(ancestor.method_name)
59
- end
57
+ return if ignored_method?(node.method_name) ||
58
+ node.each_ancestor(:send, :block).any? do |ancestor|
59
+ ignored_method?(ancestor.method_name)
60
+ end
60
61
 
61
62
  numeric, replacement = check(node)
62
63
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
 
29
29
  def on_args(node)
30
30
  return if super_used?(node)
31
- return if whitelist.include?(node.parent.method_name.to_s)
31
+ return if allowlist.include?(node.parent.method_name.to_s)
32
32
 
33
33
  option_hash(node) do |options|
34
34
  add_offense(options)
@@ -37,8 +37,8 @@ module RuboCop
37
37
 
38
38
  private
39
39
 
40
- def whitelist
41
- cop_config['Whitelist'] || []
40
+ def allowlist
41
+ cop_config['Allowlist'] || []
42
42
  end
43
43
 
44
44
  def suspicious_name?(arg_name)
@@ -88,27 +88,27 @@ module RuboCop
88
88
  end
89
89
 
90
90
  def contains_preferred_delimiter?(node, type)
91
- preferred_delimiters = preferred_delimiters_for(type)
92
- node
93
- .children.map { |n| string_source(n) }.compact
94
- .any? { |s| preferred_delimiters.any? { |d| s.include?(d) } }
91
+ contains_delimiter?(node, preferred_delimiters_for(type))
95
92
  end
96
93
 
97
94
  def include_same_character_as_used_for_delimiter?(node, type)
98
95
  return false unless %w[%w %i].include?(type)
99
96
 
100
97
  used_delimiters = matchpairs(begin_source(node)[-1])
101
- escaped_delimiters = used_delimiters.map { |d| "\\#{d}" }.join('|')
98
+ contains_delimiter?(node, used_delimiters)
99
+ end
102
100
 
101
+ def contains_delimiter?(node, delimiters)
102
+ delimiters_regexp = Regexp.union(delimiters)
103
103
  node
104
104
  .children.map { |n| string_source(n) }.compact
105
- .any? { |s| Regexp.new(escaped_delimiters) =~ s }
105
+ .any? { |s| delimiters_regexp =~ s }
106
106
  end
107
107
 
108
108
  def string_source(node)
109
109
  if node.is_a?(String)
110
110
  node
111
- elsif node.respond_to?(:type) && node.str_type?
111
+ elsif node.respond_to?(:type) && (node.str_type? || node.sym_type?)
112
112
  node.source
113
113
  end
114
114
  end
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # %w/swim run bike/
15
15
  # %w[shirt pants shoes]
16
16
  # %W(apple #{fruit} grape)
17
- class UnneededCapitalW < Cop
17
+ class RedundantCapitalW < Cop
18
18
  include PercentLiteral
19
19
 
20
20
  MSG = 'Do not use `%W` unless interpolation is needed. ' \
@@ -30,11 +30,11 @@ module RuboCop
30
30
  # c
31
31
  # end
32
32
  #
33
- class UnneededCondition < Cop
33
+ class RedundantCondition < Cop
34
34
  include RangeHelp
35
35
 
36
36
  MSG = 'Use double pipes `||` instead.'
37
- UNNEEDED_CONDITION = 'This condition is not needed.'
37
+ REDUNDANT_CONDITION = 'This condition is not needed.'
38
38
 
39
39
  def on_if(node)
40
40
  return if node.elsif_conditional?
@@ -61,7 +61,7 @@ module RuboCop
61
61
 
62
62
  def message(node)
63
63
  if node.modifier_form? || !node.else_branch
64
- UNNEEDED_CONDITION
64
+ REDUNDANT_CONDITION
65
65
  else
66
66
  MSG
67
67
  end
@@ -15,7 +15,7 @@ module RuboCop
15
15
  #
16
16
  # # good if @var is already a String
17
17
  # @var
18
- class UnneededInterpolation < Cop
18
+ class RedundantInterpolation < Cop
19
19
  include PercentLiteral
20
20
 
21
21
  MSG = 'Prefer `to_s` over string interpolation.'
@@ -19,7 +19,7 @@ module RuboCop
19
19
  def_node_matcher :square_brackets?,
20
20
  '(send {(send _recv _msg) str array hash} :[] ...)'
21
21
  def_node_matcher :range_end?, '^^{irange erange}'
22
- def_node_matcher :method_node_and_args, '$(send _recv _msg $...)'
22
+ def_node_matcher :method_node_and_args, '$(call _recv _msg $...)'
23
23
  def_node_matcher :rescue?, '{^resbody ^^resbody}'
24
24
  def_node_matcher :arg_in_call_with_block?,
25
25
  '^^(block (send _ _ equal?(%0) ...) ...)'
@@ -102,7 +102,7 @@ module RuboCop
102
102
  return offense(begin_node, 'a variable') if node.variable?
103
103
  return offense(begin_node, 'a constant') if node.const_type?
104
104
 
105
- check_send(begin_node, node) if node.send_type?
105
+ check_send(begin_node, node) if node.call_type?
106
106
  end
107
107
 
108
108
  def check_send(begin_node, node)
@@ -195,7 +195,7 @@ module RuboCop
195
195
  end
196
196
 
197
197
  def method_call_with_redundant_parentheses?(node)
198
- return false unless node.send_type?
198
+ return false unless node.call_type?
199
199
  return false if node.prefix_not?
200
200
  return false if range_end?(node)
201
201
 
@@ -17,7 +17,7 @@ module RuboCop
17
17
  # time = "8 o'clock"
18
18
  # question = '"What did you say?"'
19
19
  #
20
- class UnneededPercentQ < Cop
20
+ class RedundantPercentQ < Cop
21
21
  MSG = 'Use `%<q_type>s` only for strings that contain both ' \
22
22
  'single quotes and double quotes%<extra>s.'
23
23
  DYNAMIC_MSG = ', or for dynamic strings that contain ' \
@@ -35,6 +35,18 @@ module RuboCop
35
35
  # end
36
36
  # end
37
37
  #
38
+ # @example AllowMultipleReturnValues: false (default)
39
+ # # bad
40
+ # def test
41
+ # return x, y
42
+ # end
43
+ #
44
+ # @example AllowMultipleReturnValues: true
45
+ # # good
46
+ # def test
47
+ # return x, y
48
+ # end
49
+ #
38
50
  class RedundantReturn < Cop
39
51
  include RangeHelp
40
52
 
@@ -42,33 +54,42 @@ module RuboCop
42
54
  MULTI_RETURN_MSG = 'To return multiple values, use an array.'
43
55
 
44
56
  def on_def(node)
45
- return unless node.body
46
-
47
57
  check_branch(node.body)
48
58
  end
49
59
  alias on_defs on_def
50
60
 
51
- def autocorrect(node) # rubocop:disable Metrics/MethodLength
61
+ def autocorrect(node)
52
62
  lambda do |corrector|
53
- unless arguments?(node.children)
54
- corrector.replace(node.source_range, 'nil')
55
- next
63
+ if node.arguments?
64
+ correct_with_arguments(node, corrector)
65
+ else
66
+ correct_without_arguments(node, corrector)
56
67
  end
57
-
58
- return_value, = *node
59
- if node.children.size > 1
60
- add_brackets(corrector, node)
61
- elsif return_value.hash_type?
62
- add_braces(corrector, return_value) unless return_value.braces?
63
- end
64
- return_kw = range_with_surrounding_space(range: node.loc.keyword,
65
- side: :right)
66
- corrector.remove(return_kw)
67
68
  end
68
69
  end
69
70
 
70
71
  private
71
72
 
73
+ def correct_without_arguments(return_node, corrector)
74
+ corrector.replace(return_node.source_range, 'nil')
75
+ end
76
+
77
+ def correct_with_arguments(return_node, corrector)
78
+ if return_node.arguments.size > 1
79
+ add_brackets(corrector, return_node)
80
+ elsif hash_without_braces?(return_node.first_argument)
81
+ add_braces(corrector, return_node.first_argument)
82
+ end
83
+
84
+ keyword = range_with_surrounding_space(range: return_node.loc.keyword,
85
+ side: :right)
86
+ corrector.remove(keyword)
87
+ end
88
+
89
+ def hash_without_braces?(node)
90
+ node.hash_type? && !node.braces?
91
+ end
92
+
72
93
  def add_brackets(corrector, node)
73
94
  kids = node.children.map(&:source_range)
74
95
  corrector.insert_before(kids.first, '[')
@@ -81,13 +102,6 @@ module RuboCop
81
102
  corrector.insert_after(kids.last, '}')
82
103
  end
83
104
 
84
- def arguments?(args)
85
- return false if args.empty?
86
- return true if args.size > 1
87
-
88
- !args.first.begin_type? || !args.first.children.empty?
89
- end
90
-
91
105
  # rubocop:disable Metrics/CyclomaticComplexity
92
106
  def check_branch(node)
93
107
  return unless node
@@ -136,12 +150,8 @@ module RuboCop
136
150
  end
137
151
 
138
152
  def check_begin_node(node)
139
- expressions = *node
140
- last_expr = expressions.last
141
-
142
- return unless last_expr&.return_type?
143
-
144
- check_return_node(last_expr)
153
+ last_expr = node.children.last
154
+ check_branch(last_expr)
145
155
  end
146
156
 
147
157
  def allow_multiple_return_values?
@@ -49,13 +49,13 @@ module RuboCop
49
49
  # # good
50
50
  # arr.max_by(&:foo)
51
51
  #
52
- class UnneededSort < Cop
52
+ class RedundantSort < Cop
53
53
  include RangeHelp
54
54
 
55
55
  MSG = 'Use `%<suggestion>s` instead of '\
56
56
  '`%<sorter>s...%<accessor_source>s`.'
57
57
 
58
- def_node_matcher :unneeded_sort?, <<~MATCHER
58
+ def_node_matcher :redundant_sort?, <<~MATCHER
59
59
  {
60
60
  (send $(send _ $:sort ...) ${:last :first})
61
61
  (send $(send _ $:sort ...) ${:[] :at :slice} {(int 0) (int -1)})
@@ -72,7 +72,7 @@ module RuboCop
72
72
  MATCHER
73
73
 
74
74
  def on_send(node)
75
- unneeded_sort?(node) do |sort_node, sorter, accessor|
75
+ redundant_sort?(node) do |sort_node, sorter, accessor|
76
76
  range = range_between(
77
77
  sort_node.loc.selector.begin_pos,
78
78
  node.loc.expression.end_pos
@@ -87,7 +87,7 @@ module RuboCop
87
87
  end
88
88
 
89
89
  def autocorrect(node)
90
- sort_node, sorter, accessor = unneeded_sort?(node)
90
+ sort_node, sorter, accessor = redundant_sort?(node)
91
91
 
92
92
  lambda do |corrector|
93
93
  # Remove accessor, e.g. `first` or `[-1]`.
@@ -127,7 +127,7 @@ module RuboCop
127
127
  end
128
128
 
129
129
  def base(accessor, arg)
130
- if accessor == :first || (arg&.zero?)
130
+ if accessor == :first || arg&.zero?
131
131
  'min'
132
132
  elsif accessor == :last || arg == -1
133
133
  'max'
@@ -112,15 +112,14 @@ module RuboCop
112
112
  end
113
113
 
114
114
  def autocorrect(node)
115
- _check, body, = node.node_parts
116
- _checked_variable, matching_receiver, = extract_parts(node)
117
- method_call, = matching_receiver.parent
115
+ body = node.node_parts[1]
116
+ method_call = method_call(node)
118
117
 
119
118
  lambda do |corrector|
120
119
  corrector.remove(begin_range(node, body))
121
120
  corrector.remove(end_range(node, body))
122
121
  corrector.insert_before(method_call.loc.dot, '&')
123
- handle_comments(corrector, method_call)
122
+ handle_comments(corrector, node, method_call)
124
123
 
125
124
  add_safe_nav_to_all_methods_in_chain(corrector, method_call, body)
126
125
  end
@@ -128,18 +127,30 @@ module RuboCop
128
127
 
129
128
  private
130
129
 
131
- def handle_comments(corrector, method_call)
132
- return if processed_source.comments.empty?
130
+ def handle_comments(corrector, node, method_call)
131
+ comments = comments(node)
132
+ return if comments.empty?
133
133
 
134
- comments = processed_source.comments.map(&:text).join("\n")
135
134
  corrector.insert_before(method_call.loc.expression,
136
- comments + "\n")
135
+ "#{comments.map(&:text).join("\n")}\n")
136
+ end
137
+
138
+ def comments(node)
139
+ processed_source.comments.select do |comment|
140
+ comment.loc.first_line > node.loc.first_line &&
141
+ comment.loc.last_line < node.loc.last_line
142
+ end
137
143
  end
138
144
 
139
145
  def allowed_if_condition?(node)
140
146
  node.else? || node.elsif? || node.ternary?
141
147
  end
142
148
 
149
+ def method_call(node)
150
+ _checked_variable, matching_receiver, = extract_parts(node)
151
+ matching_receiver.parent
152
+ end
153
+
143
154
  def extract_parts(node)
144
155
  case node.type
145
156
  when :if
@@ -6,6 +6,9 @@ module RuboCop
6
6
  # This cop checks for multiple expressions placed on the same line.
7
7
  # It also checks for lines terminated with a semicolon.
8
8
  #
9
+ # This cop has `AllowAsExpressionSeparator` configuration option.
10
+ # It allows `;` to separate several expressions on the same line.
11
+ #
9
12
  # @example
10
13
  # # bad
11
14
  # foo = 1; bar = 2;
@@ -15,6 +18,14 @@ module RuboCop
15
18
  # foo = 1
16
19
  # bar = 2
17
20
  # baz = 3
21
+ #
22
+ # @example AllowAsExpressionSeparator: false (default)
23
+ # # bad
24
+ # foo = 1; bar = 2
25
+ #
26
+ # @example AllowAsExpressionSeparator: true
27
+ # # good
28
+ # foo = 1; bar = 2
18
29
  class Semicolon < Cop
19
30
  include RangeHelp
20
31
 
@@ -36,8 +47,8 @@ module RuboCop
36
47
  return if exprs.size < 2
37
48
 
38
49
  # create a map matching lines to the number of expressions on them
39
- exprs_lines = exprs.map { |e| e.source_range.line }
40
- lines = exprs_lines.group_by { |i| i }
50
+ exprs_lines = exprs.map(&:first_line)
51
+ lines = exprs_lines.group_by(&:itself)
41
52
 
42
53
  lines.each do |line, expr_on_line|
43
54
  # Every line with more than one expression on it is a
@@ -160,17 +160,15 @@ module RuboCop
160
160
  end
161
161
 
162
162
  def format_message(english, regular, global)
163
- if !regular.empty? && !english.empty?
163
+ if regular.empty?
164
+ format(MSG_ENGLISH, prefer: format_list(english), global: global)
165
+ elsif english.empty?
166
+ format(MSG_REGULAR, prefer: format_list(regular), global: global)
167
+ else
164
168
  format(MSG_BOTH,
165
169
  prefer: format_list(english),
166
170
  regular: format_list(regular),
167
171
  global: global)
168
- elsif !regular.empty?
169
- format(MSG_REGULAR, prefer: format_list(regular), global: global)
170
- elsif !english.empty?
171
- format(MSG_ENGLISH, prefer: format_list(english), global: global)
172
- else
173
- raise 'Bug in SpecialGlobalVars - global var w/o preferred vars!'
174
172
  end
175
173
  end
176
174
 
@@ -20,8 +20,14 @@ module RuboCop
20
20
  # a, *b, _ = foo()
21
21
  # # => The correction `a, *b, = foo()` is a syntax error
22
22
  #
23
- # # good if AllowNamedUnderscoreVariables is true
23
+ # @example AllowNamedUnderscoreVariables: true (default)
24
+ # # good
24
25
  # a, b, _something = foo()
26
+ #
27
+ # @example AllowNamedUnderscoreVariables: false
28
+ # # bad
29
+ # a, b, _something = foo()
30
+ #
25
31
  class TrailingUnderscoreVariable < Cop
26
32
  include SurroundingSpace
27
33
  include RangeHelp
@@ -60,7 +60,7 @@ module RuboCop
60
60
  when :module
61
61
  return true
62
62
  else
63
- return true if pnode.method_name == :instance_eval
63
+ return true if pnode.method?(:instance_eval)
64
64
  end
65
65
  end
66
66
  false
@@ -95,9 +95,9 @@ module RuboCop
95
95
  cop_config['IgnoreClassMethods']
96
96
  end
97
97
 
98
- def whitelist
99
- whitelist = cop_config['Whitelist']
100
- Array(whitelist).map(&:to_sym) + [:initialize]
98
+ def allowed_methods
99
+ allowed_methods = cop_config['AllowedMethods']
100
+ Array(allowed_methods).map(&:to_sym) + [:initialize]
101
101
  end
102
102
 
103
103
  def dsl_writer?(method_name)
@@ -124,7 +124,7 @@ module RuboCop
124
124
  PATTERN
125
125
 
126
126
  def allowed_method?(node)
127
- whitelist.include?(node.method_name) ||
127
+ allowed_methods.include?(node.method_name) ||
128
128
  exact_name_match? && !names_match?(node)
129
129
  end
130
130