rubocop 1.48.1 → 1.52.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +3 -3
  3. data/config/default.yml +59 -12
  4. data/lib/rubocop/cli/command/execute_runner.rb +7 -2
  5. data/lib/rubocop/cli.rb +6 -6
  6. data/lib/rubocop/config.rb +5 -1
  7. data/lib/rubocop/config_loader.rb +8 -8
  8. data/lib/rubocop/config_obsoletion.rb +2 -2
  9. data/lib/rubocop/cop/autocorrect_logic.rb +28 -12
  10. data/lib/rubocop/cop/base.rb +5 -1
  11. data/lib/rubocop/cop/cop.rb +2 -2
  12. data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -1
  13. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
  14. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
  15. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
  16. data/lib/rubocop/cop/gemspec/development_dependencies.rb +1 -1
  17. data/lib/rubocop/cop/internal_affairs/cop_description.rb +33 -9
  18. data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +2 -2
  19. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
  20. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +2 -2
  21. data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +29 -2
  22. data/lib/rubocop/cop/layout/class_structure.rb +1 -0
  23. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +0 -1
  24. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  25. data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
  26. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
  27. data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
  28. data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
  29. data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
  30. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
  31. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
  32. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
  33. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
  34. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -4
  35. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  36. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
  37. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
  38. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
  39. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
  40. data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
  41. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -0
  43. data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
  44. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +13 -1
  45. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
  46. data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
  47. data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
  48. data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -4
  49. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  50. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +5 -3
  51. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  52. data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +1 -1
  53. data/lib/rubocop/cop/lint/missing_super.rb +3 -0
  54. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  55. data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +2 -2
  56. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  57. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +2 -2
  58. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
  59. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
  60. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  61. data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
  62. data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
  63. data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +23 -9
  64. data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
  65. data/lib/rubocop/cop/lint/useless_assignment.rb +59 -1
  66. data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
  67. data/lib/rubocop/cop/lint/void.rb +69 -9
  68. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  69. data/lib/rubocop/cop/metrics/class_length.rb +1 -0
  70. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  71. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  72. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  73. data/lib/rubocop/cop/mixin/comments_help.rb +7 -3
  74. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  75. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
  76. data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
  77. data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
  78. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  79. data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
  80. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +22 -7
  81. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +11 -3
  82. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  83. data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
  84. data/lib/rubocop/cop/style/attr.rb +11 -1
  85. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  86. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -2
  87. data/lib/rubocop/cop/style/class_equality_comparison.rb +51 -40
  88. data/lib/rubocop/cop/style/collection_compact.rb +19 -6
  89. data/lib/rubocop/cop/style/colon_method_call.rb +2 -2
  90. data/lib/rubocop/cop/style/combinable_loops.rb +26 -6
  91. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  92. data/lib/rubocop/cop/style/copyright.rb +6 -3
  93. data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
  94. data/lib/rubocop/cop/style/dir.rb +1 -1
  95. data/lib/rubocop/cop/style/dir_empty.rb +8 -14
  96. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
  97. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  98. data/lib/rubocop/cop/style/documentation.rb +1 -1
  99. data/lib/rubocop/cop/style/double_negation.rb +2 -2
  100. data/lib/rubocop/cop/style/eval_with_location.rb +5 -5
  101. data/lib/rubocop/cop/style/exact_regexp_match.rb +68 -0
  102. data/lib/rubocop/cop/style/file_empty.rb +3 -3
  103. data/lib/rubocop/cop/style/file_read.rb +2 -2
  104. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  105. data/lib/rubocop/cop/style/guard_clause.rb +2 -0
  106. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
  107. data/lib/rubocop/cop/style/hash_except.rb +23 -12
  108. data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
  109. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  110. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  111. data/lib/rubocop/cop/style/if_inside_else.rb +6 -0
  112. data/lib/rubocop/cop/style/if_unless_modifier.rb +41 -12
  113. data/lib/rubocop/cop/style/invertible_unless_condition.rb +9 -5
  114. data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
  115. data/lib/rubocop/cop/style/map_to_set.rb +4 -1
  116. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +4 -9
  117. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +43 -36
  118. data/lib/rubocop/cop/style/multiline_method_signature.rb +6 -3
  119. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  120. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  121. data/lib/rubocop/cop/style/parallel_assignment.rb +26 -18
  122. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
  123. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  124. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  125. data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
  126. data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
  127. data/lib/rubocop/cop/style/redundant_line_continuation.rb +183 -0
  128. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  129. data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
  130. data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
  131. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  132. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
  133. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -3
  134. data/lib/rubocop/cop/style/regexp_literal.rb +11 -2
  135. data/lib/rubocop/cop/style/require_order.rb +11 -5
  136. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  137. data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
  138. data/lib/rubocop/cop/style/semicolon.rb +12 -1
  139. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  140. data/lib/rubocop/cop/style/sole_nested_conditional.rb +5 -3
  141. data/lib/rubocop/cop/style/special_global_vars.rb +3 -4
  142. data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
  143. data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
  144. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  145. data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
  146. data/lib/rubocop/cop/team.rb +1 -1
  147. data/lib/rubocop/cop/variable_force/assignment.rb +33 -1
  148. data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
  149. data/lib/rubocop/cop/variable_force.rb +1 -0
  150. data/lib/rubocop/cops_documentation_generator.rb +10 -3
  151. data/lib/rubocop/ext/regexp_node.rb +1 -1
  152. data/lib/rubocop/ext/regexp_parser.rb +1 -1
  153. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  154. data/lib/rubocop/options.rb +4 -1
  155. data/lib/rubocop/result_cache.rb +2 -2
  156. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  157. data/lib/rubocop/server/cache.rb +1 -1
  158. data/lib/rubocop/server/client_command/exec.rb +2 -1
  159. data/lib/rubocop/server/helper.rb +1 -1
  160. data/lib/rubocop/server/server_command/exec.rb +1 -1
  161. data/lib/rubocop/target_ruby.rb +3 -2
  162. data/lib/rubocop/version.rb +10 -6
  163. data/lib/rubocop.rb +8 -0
  164. metadata +20 -12
@@ -13,19 +13,46 @@ module RuboCop
13
13
  # # good
14
14
  # node.source
15
15
  #
16
+ # # bad
17
+ # add_offense(node) { |corrector| corrector.replace(node.source_range, prefer) }
18
+ # add_offense(node) { |corrector| corrector.insert_before(node.source_range, prefer) }
19
+ # add_offense(node) { |corrector| corrector.insert_before_multi(node.source_range, prefer) }
20
+ # add_offense(node) { |corrector| corrector.insert_after(node.source_range, prefer) }
21
+ # add_offense(node) { |corrector| corrector.insert_after_multi(node.source_range, prefer) }
22
+ # add_offense(node) { |corrector| corrector.swap(node.source_range, before, after) }
23
+ #
24
+ # # good
25
+ # add_offense(node) { |corrector| corrector.replace(node, prefer) }
26
+ # add_offense(node) { |corrector| corrector.insert_before(node, prefer) }
27
+ # add_offense(node) { |corrector| corrector.insert_before_multi(node, prefer) }
28
+ # add_offense(node) { |corrector| corrector.insert_after(node, prefer) }
29
+ # add_offense(node) { |corrector| corrector.insert_after_multi(node, prefer) }
30
+ # add_offense(node) { |corrector| corrector.swap(node, before, after) }
31
+ #
16
32
  class RedundantSourceRange < Base
17
33
  extend AutoCorrector
18
34
 
19
35
  MSG = 'Remove the redundant `source_range`.'
20
- RESTRICT_ON_SEND = %i[source].freeze
36
+ RESTRICT_ON_SEND = %i[
37
+ source
38
+ replace remove insert_before insert_before_multi insert_after insert_after_multi swap
39
+ ].freeze
21
40
 
22
41
  # @!method redundant_source_range(node)
23
42
  def_node_matcher :redundant_source_range, <<~PATTERN
24
- (send $(send _ :source_range) :source)
43
+ {
44
+ (send $(send _ :source_range) :source)
45
+ (send _ {
46
+ :replace :insert_before :insert_before_multi :insert_after :insert_after_multi
47
+ } $(send _ :source_range) _)
48
+ (send _ :remove $(send _ :source_range))
49
+ (send _ :swap $(send _ :source_range) _ _)
50
+ }
25
51
  PATTERN
26
52
 
27
53
  def on_send(node)
28
54
  return unless (source_range = redundant_source_range(node))
55
+ return if source_range.receiver.send_type? && source_range.receiver.method?(:buffer)
29
56
 
30
57
  selector = source_range.loc.selector
31
58
 
@@ -159,6 +159,7 @@ module RuboCop
159
159
  previous = index
160
160
  end
161
161
  end
162
+ alias on_sclass on_class
162
163
 
163
164
  private
164
165
 
@@ -3,7 +3,6 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- #
7
6
  # Checks the indentation of here document closings.
8
7
  #
9
8
  # @example
@@ -137,7 +137,7 @@ module RuboCop
137
137
  end
138
138
 
139
139
  def current_token(comment)
140
- processed_source.find_token { |token| token.pos == comment.source_range }
140
+ processed_source.tokens.find { |token| token.pos == comment.source_range }
141
141
  end
142
142
 
143
143
  def previous_token(node)
@@ -31,7 +31,7 @@ module RuboCop
31
31
  return unless processed_source.raw_source.include?("\n\n\n")
32
32
 
33
33
  lines = Set.new
34
- processed_source.each_token { |token| lines << token.line }
34
+ processed_source.tokens.each { |token| lines << token.line }
35
35
 
36
36
  each_extra_empty_line(lines.sort) do |range|
37
37
  add_offense(range) do |corrector|
@@ -51,6 +51,8 @@ module RuboCop
51
51
  MSG_BEFORE_FOR_ONLY_BEFORE = 'Keep a blank line before `%<modifier>s`.'
52
52
  MSG_AFTER_FOR_ONLY_BEFORE = 'Remove a blank line after `%<modifier>s`.'
53
53
 
54
+ RESTRICT_ON_SEND = %i[public protected private module_function].freeze
55
+
54
56
  def initialize(config = nil, options = nil)
55
57
  super
56
58
 
@@ -109,6 +109,7 @@ module RuboCop
109
109
  check_other_alignment(node)
110
110
  end
111
111
  end
112
+ alias on_case_match on_case
112
113
 
113
114
  private
114
115
 
@@ -169,7 +170,10 @@ module RuboCop
169
170
  end
170
171
 
171
172
  def alignment_node_for_variable_style(node)
172
- return node.parent if node.case_type? && node.argument? && same_line?(node, node.parent)
173
+ if (node.case_type? || node.case_match_type?) && node.argument? &&
174
+ same_line?(node, node.parent)
175
+ return node.parent
176
+ end
173
177
 
174
178
  assignment = assignment_or_operator_method(node)
175
179
 
@@ -49,7 +49,7 @@ module RuboCop
49
49
 
50
50
  private
51
51
 
52
- def aligned_locations(locs)
52
+ def aligned_locations(locs) # rubocop:disable Metrics/AbcSize
53
53
  return [] if locs.empty?
54
54
 
55
55
  aligned = Set[locs.first.line, locs.last.line]
@@ -57,6 +57,11 @@ module RuboCop
57
57
  col = loc.column
58
58
  aligned << loc.line if col == before.column || col == after.column
59
59
  end
60
+
61
+ # if locs.size > 2 and the size of variable `aligned`
62
+ # has not increased from its initial value, there are not aligned lines.
63
+ return [] if locs.size > 2 && aligned.size == 2
64
+
60
65
  aligned
61
66
  end
62
67
 
@@ -153,9 +153,10 @@ module RuboCop
153
153
  MSG = 'Indent the first argument one step more than %<base>s.'
154
154
 
155
155
  def on_send(node)
156
+ return unless should_check?(node)
157
+ return if same_line?(node, node.first_argument)
156
158
  return if style != :consistent && enforce_first_argument_with_fixed_indentation? &&
157
159
  !enable_layout_first_method_argument_line_break?
158
- return if !node.arguments? || bare_operator?(node) || node.setter_method?
159
160
 
160
161
  indent = base_indentation(node) + configured_indentation_width
161
162
 
@@ -166,6 +167,10 @@ module RuboCop
166
167
 
167
168
  private
168
169
 
170
+ def should_check?(node)
171
+ node.arguments? && !bare_operator?(node) && !node.setter_method?
172
+ end
173
+
169
174
  def autocorrect(corrector, node)
170
175
  AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
171
176
  end
@@ -6,49 +6,40 @@ module RuboCop
6
6
  # Checks for a line break before the first element in a
7
7
  # multi-line array.
8
8
  #
9
- # @example AllowMultilineFinalElement: false (default)
9
+ # @example
10
10
  #
11
- # # bad
12
- # [ :a,
13
- # :b]
11
+ # # bad
12
+ # [ :a,
13
+ # :b]
14
14
  #
15
- # # bad
16
- # [ :a, {
17
- # :b => :c
18
- # }]
15
+ # # good
16
+ # [
17
+ # :a,
18
+ # :b]
19
19
  #
20
- # # good
21
- # [:a, :b]
20
+ # # good
21
+ # [:a, :b]
22
22
  #
23
- # # good
24
- # [
25
- # :a,
26
- # :b]
23
+ # @example AllowMultilineFinalElement: false (default)
27
24
  #
28
- # # good
29
- # [
30
- # :a, {
31
- # :b => :c
32
- # }]
25
+ # # bad
26
+ # [ :a, {
27
+ # :b => :c
28
+ # }]
33
29
  #
34
- # @example AllowMultilineFinalElement: true
30
+ # # good
31
+ # [
32
+ # :a, {
33
+ # :b => :c
34
+ # }]
35
35
  #
36
- # # bad
37
- # [ :a,
38
- # :b]
39
- #
40
- # # good
41
- # [ :a, {
42
- # :b => :c
43
- # }]
36
+ # @example AllowMultilineFinalElement: true
44
37
  #
45
- # # good
46
- # [
47
- # :a,
48
- # :b]
38
+ # # good
39
+ # [:a, {
40
+ # :b => :c
41
+ # }]
49
42
  #
50
- # # good
51
- # [:a, :b]
52
43
  class FirstArrayElementLineBreak < Base
53
44
  include FirstElementLineBreak
54
45
  extend AutoCorrector
@@ -6,17 +6,12 @@ module RuboCop
6
6
  # Checks for a line break before the first element in a
7
7
  # multi-line hash.
8
8
  #
9
- # @example AllowMultilineFinalElement: false (default)
9
+ # @example
10
10
  #
11
11
  # # bad
12
12
  # { a: 1,
13
13
  # b: 2}
14
14
  #
15
- # # bad
16
- # { a: 1, b: {
17
- # c: 3
18
- # }}
19
- #
20
15
  # # good
21
16
  # {
22
17
  # a: 1,
@@ -28,11 +23,14 @@ module RuboCop
28
23
  # c: 3
29
24
  # }}
30
25
  #
31
- # @example AllowMultilineFinalElement: true
26
+ # @example AllowMultilineFinalElement: false (default)
32
27
  #
33
28
  # # bad
34
- # { a: 1,
35
- # b: 2}
29
+ # { a: 1, b: {
30
+ # c: 3
31
+ # }}
32
+ #
33
+ # @example AllowMultilineFinalElement: true
36
34
  #
37
35
  # # bad
38
36
  # { a: 1,
@@ -45,16 +43,6 @@ module RuboCop
45
43
  # c: 3
46
44
  # }}
47
45
  #
48
- # # good
49
- # {
50
- # a: 1,
51
- # b: 2 }
52
- #
53
- # # good
54
- # {
55
- # a: 1, b: {
56
- # c: 3
57
- # }}
58
46
  class FirstHashElementLineBreak < Base
59
47
  include FirstElementLineBreak
60
48
  extend AutoCorrector
@@ -6,73 +6,63 @@ module RuboCop
6
6
  # Checks for a line break before the first argument in a
7
7
  # multi-line method call.
8
8
  #
9
- # @example AllowMultilineFinalElement: false (default)
10
- #
11
- # # bad
12
- # method(foo, bar,
13
- # baz)
14
- #
15
- # # bad
16
- # method(foo, bar, {
17
- # baz: "a",
18
- # qux: "b",
19
- # })
9
+ # @example
20
10
  #
21
- # # good
22
- # method(
23
- # foo, bar,
24
- # baz)
11
+ # # bad
12
+ # method(foo, bar,
13
+ # baz)
25
14
  #
26
- # # good
27
- # method(
28
- # foo, bar, {
29
- # baz: "a",
30
- # qux: "b",
31
- # })
15
+ # # good
16
+ # method(
17
+ # foo, bar,
18
+ # baz)
32
19
  #
33
20
  # # ignored
34
21
  # method foo, bar,
35
22
  # baz
36
23
  #
37
- # @example AllowMultilineFinalElement: true
24
+ # @example AllowMultilineFinalElement: false (default)
25
+ #
26
+ # # bad
27
+ # method(foo, bar, {
28
+ # baz: "a",
29
+ # qux: "b",
30
+ # })
38
31
  #
39
- # # bad
40
- # method(foo, bar,
41
- # baz)
32
+ # # good
33
+ # method(
34
+ # foo, bar, {
35
+ # baz: "a",
36
+ # qux: "b",
37
+ # })
42
38
  #
43
- # # bad
44
- # method(foo,
45
- # bar,
46
- # {
47
- # baz: "a",
48
- # qux: "b",
49
- # }
50
- # )
39
+ # @example AllowMultilineFinalElement: true
51
40
  #
52
- # # good
53
- # method(foo, bar, {
41
+ # # bad
42
+ # method(foo,
43
+ # bar,
44
+ # {
54
45
  # baz: "a",
55
46
  # qux: "b",
56
- # })
47
+ # }
48
+ # )
57
49
  #
58
- # # good
59
- # method(
60
- # foo, bar,
61
- # baz)
50
+ # # good
51
+ # method(foo, bar, {
52
+ # baz: "a",
53
+ # qux: "b",
54
+ # })
62
55
  #
63
- # # good
64
- # method(
65
- # foo,
66
- # bar,
67
- # {
68
- # baz: "a",
69
- # qux: "b",
70
- # }
71
- # )
56
+ # # good
57
+ # method(
58
+ # foo,
59
+ # bar,
60
+ # {
61
+ # baz: "a",
62
+ # qux: "b",
63
+ # }
64
+ # )
72
65
  #
73
- # # ignored
74
- # method foo, bar,
75
- # baz
76
66
  class FirstMethodArgumentLineBreak < Base
77
67
  include FirstElementLineBreak
78
68
  extend AutoCorrector
@@ -6,69 +6,52 @@ module RuboCop
6
6
  # Checks for a line break before the first parameter in a
7
7
  # multi-line method parameter definition.
8
8
  #
9
- # @example AllowMultilineFinalElement: false (default)
10
- #
11
- # # bad
12
- # def method(foo, bar,
13
- # baz)
14
- # do_something
15
- # end
16
- #
17
- # # bad
18
- # def method(foo, bar, baz = {
19
- # :a => "b",
20
- # })
21
- # do_something
22
- # end
9
+ # @example
23
10
  #
24
- # # good
25
- # def method(
26
- # foo, bar,
27
- # baz)
28
- # do_something
29
- # end
11
+ # # bad
12
+ # def method(foo, bar,
13
+ # baz)
14
+ # do_something
15
+ # end
30
16
  #
31
- # # good
32
- # def method(
33
- # foo, bar, baz = {
34
- # :a => "b",
35
- # })
36
- # do_something
37
- # end
17
+ # # good
18
+ # def method(
19
+ # foo, bar,
20
+ # baz)
21
+ # do_something
22
+ # end
38
23
  #
39
- # # ignored
40
- # def method foo,
41
- # bar
42
- # do_something
43
- # end
24
+ # # ignored
25
+ # def method foo,
26
+ # bar
27
+ # do_something
28
+ # end
44
29
  #
45
- # @example AllowMultilineFinalElement: true
30
+ # @example AllowMultilineFinalElement: false (default)
46
31
  #
47
- # # bad
48
- # def method(foo, bar,
49
- # baz)
50
- # do_something
51
- # end
32
+ # # bad
33
+ # def method(foo, bar, baz = {
34
+ # :a => "b",
35
+ # })
36
+ # do_something
37
+ # end
52
38
  #
53
- # # good
54
- # def method(foo, bar, baz = {
55
- # :a => "b",
56
- # })
57
- # do_something
58
- # end
39
+ # # good
40
+ # def method(
41
+ # foo, bar, baz = {
42
+ # :a => "b",
43
+ # })
44
+ # do_something
45
+ # end
59
46
  #
60
- # # good
61
- # def method(
62
- # foo, bar,
63
- # baz)
64
- # do_something
65
- # end
47
+ # @example AllowMultilineFinalElement: true
66
48
  #
67
- # # ignored
68
- # def method foo,
69
- # bar
70
- # do_something
71
- # end
49
+ # # good
50
+ # def method(foo, bar, baz = {
51
+ # :a => "b",
52
+ # })
53
+ # do_something
54
+ # end
72
55
  #
73
56
  class FirstMethodParameterLineBreak < Base
74
57
  include FirstElementLineBreak
@@ -67,7 +67,7 @@ module RuboCop
67
67
 
68
68
  outermost_send = outermost_send_on_same_line(heredoc_arg)
69
69
  return unless outermost_send
70
- return if end_keyword_before_closing_parentesis?(node)
70
+ return if end_keyword_before_closing_parenthesis?(node)
71
71
  return if subsequent_closing_parentheses_in_same_line?(outermost_send)
72
72
  return if exist_argument_between_heredoc_end_and_closing_parentheses?(node)
73
73
 
@@ -159,7 +159,7 @@ module RuboCop
159
159
 
160
160
  # Closing parenthesis helpers.
161
161
 
162
- def end_keyword_before_closing_parentesis?(parenthesized_send_node)
162
+ def end_keyword_before_closing_parenthesis?(parenthesized_send_node)
163
163
  parenthesized_send_node.ancestors.any? do |ancestor|
164
164
  ancestor.loc.respond_to?(:end) && ancestor.loc.end&.source == 'end'
165
165
  end
@@ -228,9 +228,9 @@ module RuboCop
228
228
  end
229
229
 
230
230
  def find_most_bottom_of_heredoc_end(arguments)
231
- arguments.map do |argument|
231
+ arguments.filter_map do |argument|
232
232
  argument.loc.heredoc_end.end_pos if argument.loc.respond_to?(:heredoc_end)
233
- end.compact.max
233
+ end.max
234
234
  end
235
235
 
236
236
  # Internal trailing comma helpers.
@@ -34,7 +34,7 @@ module RuboCop
34
34
  private
35
35
 
36
36
  def first_token
37
- processed_source.find_token { |t| !t.text.start_with?('#') }
37
+ processed_source.tokens.find { |t| !t.text.start_with?('#') }
38
38
  end
39
39
 
40
40
  def space_before(token)
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Ensures that each item in a multi-line array
7
7
  # starts on a separate line.
8
8
  #
9
- # @example AllowMultilineFinalElement: false (default)
9
+ # @example
10
10
  #
11
11
  # # bad
12
12
  # [
@@ -14,11 +14,6 @@ module RuboCop
14
14
  # c
15
15
  # ]
16
16
  #
17
- # # bad
18
- # [ a, b, foo(
19
- # bar
20
- # )]
21
- #
22
17
  # # good
23
18
  # [
24
19
  # a,
@@ -35,34 +30,20 @@ module RuboCop
35
30
  # )
36
31
  # ]
37
32
  #
38
- # @example AllowMultilineFinalElement: true
33
+ # @example AllowMultilineFinalElement: false (default)
39
34
  #
40
35
  # # bad
41
- # [
42
- # a, b,
43
- # c
44
- # ]
45
- #
46
- # # good
47
- # [ a, b, foo(
36
+ # [a, b, foo(
48
37
  # bar
49
38
  # )]
50
39
  #
51
- # # good
52
- # [
53
- # a,
54
- # b,
55
- # c
56
- # ]
40
+ # @example AllowMultilineFinalElement: true
57
41
  #
58
42
  # # good
59
- # [
60
- # a,
61
- # b,
62
- # foo(
63
- # bar
64
- # )
65
- # ]
43
+ # [a, b, foo(
44
+ # bar
45
+ # )]
46
+ #
66
47
  class MultilineArrayLineBreaks < Base
67
48
  include MultilineElementLineBreaks
68
49
  extend AutoCorrector
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Ensures that each key in a multi-line hash
7
7
  # starts on a separate line.
8
8
  #
9
- # @example AllowMultilineFinalElement: false (default)
9
+ # @example
10
10
  #
11
11
  # # bad
12
12
  # {
@@ -14,11 +14,6 @@ module RuboCop
14
14
  # c: 3
15
15
  # }
16
16
  #
17
- # # bad
18
- # { a: 1, b: {
19
- # c: 3,
20
- # }}
21
- #
22
17
  # # good
23
18
  # {
24
19
  # a: 1,
@@ -34,34 +29,20 @@ module RuboCop
34
29
  # }
35
30
  # }
36
31
  #
37
- # @example AllowMultilineFinalElement: true
32
+ # @example AllowMultilineFinalElement: false (default)
38
33
  #
39
34
  # # bad
40
- # {
41
- # a: 1, b: 2,
42
- # c: 3
43
- # }
44
- #
45
- # # good
46
35
  # { a: 1, b: {
47
36
  # c: 3,
48
37
  # }}
49
38
  #
50
- # # good
51
- # {
52
- # a: 1,
53
- # b: 2,
54
- # c: 3
55
- # }
56
- #
39
+ # @example AllowMultilineFinalElement: true
57
40
  #
58
41
  # # good
59
- # {
60
- # a: 1,
61
- # b: {
62
- # c: 3,
63
- # }
64
- # }
42
+ # { a: 1, b: {
43
+ # c: 3,
44
+ # }}
45
+ #
65
46
  class MultilineHashKeyLineBreaks < Base
66
47
  include MultilineElementLineBreaks
67
48
  extend AutoCorrector