rubocop 1.40.0 → 1.43.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +2 -2
  4. data/config/default.yml +52 -2
  5. data/lib/rubocop/cli.rb +1 -1
  6. data/lib/rubocop/config.rb +34 -11
  7. data/lib/rubocop/config_loader.rb +9 -0
  8. data/lib/rubocop/config_loader_resolver.rb +5 -1
  9. data/lib/rubocop/config_validator.rb +1 -1
  10. data/lib/rubocop/cop/badge.rb +9 -4
  11. data/lib/rubocop/cop/base.rb +83 -66
  12. data/lib/rubocop/cop/commissioner.rb +8 -3
  13. data/lib/rubocop/cop/cop.rb +29 -29
  14. data/lib/rubocop/cop/corrector.rb +23 -11
  15. data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
  16. data/lib/rubocop/cop/internal_affairs/cop_description.rb +3 -1
  17. data/lib/rubocop/cop/layout/class_structure.rb +32 -11
  18. data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
  19. data/lib/rubocop/cop/layout/empty_lines.rb +2 -0
  20. data/lib/rubocop/cop/layout/extra_spacing.rb +10 -6
  21. data/lib/rubocop/cop/layout/first_array_element_line_break.rb +38 -2
  22. data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +49 -2
  23. data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +61 -2
  24. data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +52 -2
  25. data/lib/rubocop/cop/layout/indentation_style.rb +7 -2
  26. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +5 -0
  27. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +11 -5
  28. data/lib/rubocop/cop/layout/line_length.rb +2 -0
  29. data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +51 -2
  30. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  31. data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +49 -2
  32. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +53 -2
  33. data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +58 -2
  34. data/lib/rubocop/cop/layout/redundant_line_break.rb +2 -2
  35. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  36. data/lib/rubocop/cop/layout/trailing_empty_lines.rb +1 -1
  37. data/lib/rubocop/cop/layout/trailing_whitespace.rb +11 -4
  38. data/lib/rubocop/cop/lint/constant_resolution.rb +4 -0
  39. data/lib/rubocop/cop/lint/debugger.rb +3 -1
  40. data/lib/rubocop/cop/lint/duplicate_branch.rb +0 -2
  41. data/lib/rubocop/cop/lint/duplicate_methods.rb +19 -8
  42. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +10 -5
  43. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +19 -0
  44. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -1
  45. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  46. data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
  47. data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
  48. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +8 -19
  49. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
  50. data/lib/rubocop/cop/lint/useless_rescue.rb +71 -0
  51. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +5 -3
  52. data/lib/rubocop/cop/metrics/class_length.rb +1 -1
  53. data/lib/rubocop/cop/metrics/module_length.rb +1 -1
  54. data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
  55. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  56. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +4 -4
  57. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -2
  58. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  59. data/lib/rubocop/cop/mixin/allowed_identifiers.rb +2 -2
  60. data/lib/rubocop/cop/mixin/annotation_comment.rb +13 -6
  61. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +21 -9
  62. data/lib/rubocop/cop/mixin/first_element_line_break.rb +11 -7
  63. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +14 -2
  64. data/lib/rubocop/cop/mixin/line_length_help.rb +8 -1
  65. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -3
  66. data/lib/rubocop/cop/mixin/multiline_element_line_breaks.rb +5 -3
  67. data/lib/rubocop/cop/mixin/percent_array.rb +3 -5
  68. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
  69. data/lib/rubocop/cop/mixin/require_library.rb +2 -0
  70. data/lib/rubocop/cop/mixin/rescue_node.rb +3 -3
  71. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
  72. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  73. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +2 -0
  74. data/lib/rubocop/cop/naming/inclusive_language.rb +4 -1
  75. data/lib/rubocop/cop/registry.rb +28 -25
  76. data/lib/rubocop/cop/security/compound_hash.rb +2 -1
  77. data/lib/rubocop/cop/style/alias.rb +9 -1
  78. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  79. data/lib/rubocop/cop/style/concat_array_literals.rb +86 -0
  80. data/lib/rubocop/cop/style/documentation.rb +11 -5
  81. data/lib/rubocop/cop/style/guard_clause.rb +17 -9
  82. data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
  83. data/lib/rubocop/cop/style/hash_syntax.rb +11 -7
  84. data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -0
  85. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -3
  86. data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
  87. data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -1
  88. data/lib/rubocop/cop/style/map_to_set.rb +61 -0
  89. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +12 -9
  90. data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
  91. data/lib/rubocop/cop/style/min_max_comparison.rb +73 -0
  92. data/lib/rubocop/cop/style/missing_else.rb +13 -1
  93. data/lib/rubocop/cop/style/operator_method_call.rb +15 -1
  94. data/lib/rubocop/cop/style/redundant_constant_base.rb +13 -0
  95. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +39 -0
  96. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  97. data/lib/rubocop/cop/style/redundant_string_escape.rb +6 -3
  98. data/lib/rubocop/cop/style/require_order.rb +63 -9
  99. data/lib/rubocop/cop/style/select_by_regexp.rb +6 -2
  100. data/lib/rubocop/cop/style/semicolon.rb +2 -1
  101. data/lib/rubocop/cop/style/signal_exception.rb +8 -6
  102. data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
  103. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
  104. data/lib/rubocop/cop/style/word_array.rb +41 -0
  105. data/lib/rubocop/cop/style/yoda_expression.rb +81 -0
  106. data/lib/rubocop/cop/style/zero_length_predicate.rb +31 -14
  107. data/lib/rubocop/cop/team.rb +29 -29
  108. data/lib/rubocop/cop/util.rb +31 -4
  109. data/lib/rubocop/cop/variable_force.rb +0 -3
  110. data/lib/rubocop/cops_documentation_generator.rb +33 -11
  111. data/lib/rubocop/directive_comment.rb +1 -1
  112. data/lib/rubocop/file_patterns.rb +43 -0
  113. data/lib/rubocop/formatter.rb +2 -0
  114. data/lib/rubocop/options.rb +1 -1
  115. data/lib/rubocop/path_util.rb +38 -22
  116. data/lib/rubocop/result_cache.rb +1 -1
  117. data/lib/rubocop/runner.rb +10 -3
  118. data/lib/rubocop/target_finder.rb +1 -1
  119. data/lib/rubocop/target_ruby.rb +0 -1
  120. data/lib/rubocop/version.rb +1 -1
  121. data/lib/rubocop.rb +7 -1
  122. metadata +16 -10
  123. data/lib/rubocop/optimized_patterns.rb +0 -38
@@ -22,6 +22,34 @@ module RuboCop
22
22
  end
23
23
  end
24
24
 
25
+ def self.support_autocorrect?
26
+ method_defined?(:autocorrect)
27
+ end
28
+
29
+ def self.joining_forces
30
+ return unless method_defined?(:join_force?)
31
+
32
+ cop = new
33
+ Force.all.select { |force_class| cop.join_force?(force_class) }
34
+ end
35
+
36
+ ### Deprecated registry access
37
+
38
+ # @deprecated Use Registry.global
39
+ def self.registry
40
+ Registry.global
41
+ end
42
+
43
+ # @deprecated Use Registry.all
44
+ def self.all
45
+ Registry.all
46
+ end
47
+
48
+ # @deprecated Use Registry.qualified_cop_name
49
+ def self.qualified_cop_name(name, origin)
50
+ Registry.qualified_cop_name(name, origin)
51
+ end
52
+
25
53
  def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
26
54
  @v0_argument = node_or_range
27
55
  range = find_location(node_or_range, location)
@@ -45,17 +73,6 @@ module RuboCop
45
73
  self.class.support_autocorrect?
46
74
  end
47
75
 
48
- def self.support_autocorrect?
49
- method_defined?(:autocorrect)
50
- end
51
-
52
- def self.joining_forces
53
- return unless method_defined?(:join_force?)
54
-
55
- cop = new
56
- Force.all.select { |force_class| cop.join_force?(force_class) }
57
- end
58
-
59
76
  # @deprecated
60
77
  def corrections
61
78
  # warn 'Cop#corrections is deprecated' TODO
@@ -76,28 +93,11 @@ module RuboCop
76
93
  super
77
94
  end
78
95
 
79
- ### Deprecated registry access
80
-
81
- # @deprecated Use Registry.global
82
- def self.registry
83
- Registry.global
84
- end
85
-
86
- # @deprecated Use Registry.all
87
- def self.all
88
- Registry.all
89
- end
90
-
91
- # @deprecated Use Registry.qualified_cop_name
92
- def self.qualified_cop_name(name, origin)
93
- Registry.qualified_cop_name(name, origin)
94
- end
95
-
96
96
  private
97
97
 
98
98
  def begin_investigation(processed_source)
99
99
  super
100
- @offenses = @current_offenses
100
+ @offenses = current_offenses
101
101
  @last_corrector = @current_corrector
102
102
  end
103
103
 
@@ -11,6 +11,20 @@ module RuboCop
11
11
  class Corrector < ::Parser::Source::TreeRewriter
12
12
  NOOP_CONSUMER = ->(diagnostic) {} # noop
13
13
 
14
+ # Duck typing for get to a ::Parser::Source::Buffer
15
+ def self.source_buffer(source)
16
+ source = source.processed_source if source.respond_to?(:processed_source)
17
+ source = source.buffer if source.respond_to?(:buffer)
18
+ source = source.source_buffer if source.respond_to?(:source_buffer)
19
+
20
+ unless source.is_a? ::Parser::Source::Buffer
21
+ raise TypeError, 'Expected argument to lead to a Parser::Source::Buffer ' \
22
+ "but got #{source.inspect}"
23
+ end
24
+
25
+ source
26
+ end
27
+
14
28
  # @param source [Parser::Source::Buffer, or anything
15
29
  # leading to one via `(processed_source.)buffer`]
16
30
  #
@@ -64,18 +78,16 @@ module RuboCop
64
78
  remove(to_remove)
65
79
  end
66
80
 
67
- # Duck typing for get to a ::Parser::Source::Buffer
68
- def self.source_buffer(source)
69
- source = source.processed_source if source.respond_to?(:processed_source)
70
- source = source.buffer if source.respond_to?(:buffer)
71
- source = source.source_buffer if source.respond_to?(:source_buffer)
72
-
73
- unless source.is_a? ::Parser::Source::Buffer
74
- raise TypeError, 'Expected argument to lead to a Parser::Source::Buffer ' \
75
- "but got #{source.inspect}"
76
- end
81
+ # Swaps sources at the given ranges.
82
+ #
83
+ # @param [Parser::Source::Range, RuboCop::AST::Node] node_or_range1
84
+ # @param [Parser::Source::Range, RuboCop::AST::Node] node_or_range2
85
+ def swap(node_or_range1, node_or_range2)
86
+ range1 = to_range(node_or_range1)
87
+ range2 = to_range(node_or_range2)
77
88
 
78
- source
89
+ replace(range1, range2.source)
90
+ replace(range2, range1.source)
79
91
  end
80
92
 
81
93
  private
@@ -58,8 +58,13 @@ module RuboCop
58
58
  FORBIDDEN_MSG = 'Dependency version specification is forbidden.'
59
59
  VERSION_SPECIFICATION_REGEX = /^\s*[~<>=]*\s*[0-9.]+/.freeze
60
60
 
61
- # @!method add_dependency_method_declarations(node)
62
- def_node_search :add_dependency_method_declarations, <<~PATTERN
61
+ ADD_DEPENDENCY_METHODS = %i[
62
+ add_dependency add_runtime_dependency add_development_dependency
63
+ ].freeze
64
+ RESTRICT_ON_SEND = ADD_DEPENDENCY_METHODS
65
+
66
+ # @!method add_dependency_method_declaration?(node)
67
+ def_node_matcher :add_dependency_method_declaration?, <<~PATTERN
63
68
  (send
64
69
  (lvar #match_block_variable_name?) #add_dependency_method? ...)
65
70
  PATTERN
@@ -74,18 +79,15 @@ module RuboCop
74
79
  (send _ #add_dependency_method? <(hash <(pair (sym {:branch :ref :tag}) (str _)) ...>) ...>)
75
80
  PATTERN
76
81
 
77
- def on_new_investigation
78
- return if processed_source.blank?
79
-
80
- add_dependency_method_nodes.each do |node|
81
- next if allowed_gem?(node)
82
+ def on_send(node)
83
+ return unless add_dependency_method_declaration?(node)
84
+ return if allowed_gem?(node)
82
85
 
83
- if offense?(node)
84
- add_offense(node)
85
- opposite_style_detected
86
- else
87
- correct_style_detected
88
- end
86
+ if offense?(node)
87
+ add_offense(node)
88
+ opposite_style_detected
89
+ else
90
+ correct_style_detected
89
91
  end
90
92
  end
91
93
 
@@ -116,11 +118,7 @@ module RuboCop
116
118
  end
117
119
 
118
120
  def add_dependency_method?(method_name)
119
- method_name.to_s.end_with?('_dependency')
120
- end
121
-
122
- def add_dependency_method_nodes
123
- add_dependency_method_declarations(processed_source.ast)
121
+ ADD_DEPENDENCY_METHODS.include?(method_name)
124
122
  end
125
123
 
126
124
  def offense?(node)
@@ -28,8 +28,9 @@ module RuboCop
28
28
  /^\s+# This cop (?<special>#{SPECIAL_WORDS.join('|')})?\s*(?<word>.+?) .*/.freeze
29
29
  REPLACEMENT_REGEX = /^\s+# This cop (#{SPECIAL_WORDS.join('|')})?\s*(.+?) /.freeze
30
30
 
31
+ # rubocop:disable Metrics/CyclomaticComplexity
31
32
  def on_class(node)
32
- return unless (module_node = node.parent)
33
+ return unless (module_node = node.parent) && node.parent_class
33
34
 
34
35
  description_beginning = first_comment_line(module_node)
35
36
  return unless description_beginning
@@ -48,6 +49,7 @@ module RuboCop
48
49
  end
49
50
  end
50
51
  end
52
+ # rubocop:enable Metrics/CyclomaticComplexity
51
53
 
52
54
  private
53
55
 
@@ -132,25 +132,19 @@ module RuboCop
132
132
  # end
133
133
  # end
134
134
  #
135
- # @see https://rubystyle.guide#consistent-classes
136
135
  class ClassStructure < Base
137
136
  include VisibilityHelp
138
137
  extend AutoCorrector
139
138
 
140
139
  HUMANIZED_NODE_TYPE = {
141
140
  casgn: :constants,
142
- defs: :class_methods,
141
+ defs: :public_class_methods,
143
142
  def: :public_methods,
144
143
  sclass: :class_singleton
145
144
  }.freeze
146
145
 
147
146
  MSG = '`%<category>s` is supposed to appear before `%<previous>s`.'
148
147
 
149
- # @!method dynamic_constant?(node)
150
- def_node_matcher :dynamic_constant?, <<~PATTERN
151
- (casgn nil? _ (send ...))
152
- PATTERN
153
-
154
148
  # Validates code style on class declaration.
155
149
  # Add offense when find a node out of expected order.
156
150
  def on_class(class_node)
@@ -222,7 +216,7 @@ module RuboCop
222
216
  def walk_over_nested_class_definition(class_node)
223
217
  class_elements(class_node).each do |node|
224
218
  classification = classify(node)
225
- next if ignore?(classification)
219
+ next if ignore?(node, classification)
226
220
 
227
221
  yield node, classification
228
222
  end
@@ -240,17 +234,20 @@ module RuboCop
240
234
  end
241
235
  end
242
236
 
243
- def ignore?(classification)
237
+ def ignore?(node, classification)
244
238
  classification.nil? ||
245
239
  classification.to_s.end_with?('=') ||
246
- expected_order.index(classification).nil?
240
+ expected_order.index(classification).nil? ||
241
+ private_constant?(node)
247
242
  end
248
243
 
249
244
  def ignore_for_autocorrect?(node, sibling)
250
245
  classification = classify(node)
251
246
  sibling_class = classify(sibling)
252
247
 
253
- ignore?(sibling_class) || classification == sibling_class || dynamic_constant?(node)
248
+ ignore?(sibling, sibling_class) ||
249
+ classification == sibling_class ||
250
+ dynamic_constant?(node)
254
251
  end
255
252
 
256
253
  def humanize_node(node)
@@ -262,6 +259,30 @@ module RuboCop
262
259
  HUMANIZED_NODE_TYPE[node.type] || node.type
263
260
  end
264
261
 
262
+ def dynamic_constant?(node)
263
+ return false unless node.casgn_type? && node.namespace.nil?
264
+
265
+ expression = node.expression
266
+ expression.send_type? &&
267
+ !(expression.method?(:freeze) && expression.receiver&.recursive_basic_literal?)
268
+ end
269
+
270
+ def private_constant?(node)
271
+ return false unless node.casgn_type? && node.namespace.nil?
272
+ return false unless (parent = node.parent)
273
+
274
+ parent.each_child_node(:send) do |child_node|
275
+ return true if marked_as_private_constant?(child_node, node.name)
276
+ end
277
+ false
278
+ end
279
+
280
+ def marked_as_private_constant?(node, name)
281
+ return false unless node.method?(:private_constant)
282
+
283
+ node.arguments.any? { |arg| (arg.sym_type? || arg.str_type?) && arg.value == name }
284
+ end
285
+
265
286
  def source_range_with_comment(node)
266
287
  begin_pos, end_pos =
267
288
  if (node.def_type? && !node.method?(:initialize)) ||
@@ -154,7 +154,9 @@ module RuboCop
154
154
  end
155
155
 
156
156
  def less_indented?(line)
157
- /^\s*(end\b|[)}\]])/.match?(line)
157
+ rule = config.for_cop('Layout/AccessModifierIndentation')['EnforcedStyle'] == 'outdent'
158
+ access_modifier = 'private|protected|public'
159
+ /\A\s*(end\b|[)}\]])/.match?(line) || (rule && /\A\s*(#{access_modifier})\b/.match?(line))
158
160
  end
159
161
 
160
162
  def two_alternatives?(line)
@@ -27,6 +27,8 @@ module RuboCop
27
27
 
28
28
  def on_new_investigation
29
29
  return if processed_source.tokens.empty?
30
+ # Quick check if we possibly have consecutive blank lines.
31
+ return unless processed_source.raw_source.include?("\n\n\n")
30
32
 
31
33
  lines = Set.new
32
34
  processed_source.each_token { |token| lines << token.line }
@@ -119,12 +119,16 @@ module RuboCop
119
119
  def ignored_ranges(ast)
120
120
  return [] unless ast
121
121
 
122
- @ignored_ranges ||= on_node(:pair, ast).map do |pair|
123
- next if pair.parent.single_line?
124
-
125
- key, value = *pair
126
- key.source_range.end_pos...value.source_range.begin_pos
127
- end.compact
122
+ @ignored_ranges ||= begin
123
+ ranges = []
124
+ on_node(:pair, ast) do |pair|
125
+ next if pair.parent.single_line?
126
+
127
+ key, value = *pair
128
+ ranges << (key.source_range.end_pos...value.source_range.begin_pos)
129
+ end
130
+ ranges
131
+ end
128
132
  end
129
133
 
130
134
  def force_equal_sign_alignment?
@@ -6,17 +6,49 @@ module RuboCop
6
6
  # Checks for a line break before the first element in a
7
7
  # multi-line array.
8
8
  #
9
- # @example
9
+ # @example AllowMultilineFinalElement: false (default)
10
+ #
11
+ # # bad
12
+ # [ :a,
13
+ # :b]
14
+ #
15
+ # # bad
16
+ # [ :a, {
17
+ # :b => :c
18
+ # }]
19
+ #
20
+ # # good
21
+ # [:a, :b]
22
+ #
23
+ # # good
24
+ # [
25
+ # :a,
26
+ # :b]
27
+ #
28
+ # # good
29
+ # [
30
+ # :a, {
31
+ # :b => :c
32
+ # }]
33
+ #
34
+ # @example AllowMultilineFinalElement: true
10
35
  #
11
36
  # # bad
12
37
  # [ :a,
13
38
  # :b]
14
39
  #
15
40
  # # good
41
+ # [ :a, {
42
+ # :b => :c
43
+ # }]
44
+ #
45
+ # # good
16
46
  # [
17
47
  # :a,
18
48
  # :b]
19
49
  #
50
+ # # good
51
+ # [:a, :b]
20
52
  class FirstArrayElementLineBreak < Base
21
53
  include FirstElementLineBreak
22
54
  extend AutoCorrector
@@ -26,7 +58,7 @@ module RuboCop
26
58
  def on_array(node)
27
59
  return if !node.loc.begin && !assignment_on_same_line?(node)
28
60
 
29
- check_children_line_break(node, node.children)
61
+ check_children_line_break(node, node.children, ignore_last: ignore_last_element?)
30
62
  end
31
63
 
32
64
  private
@@ -35,6 +67,10 @@ module RuboCop
35
67
  source = node.source_range.source_line[0...node.loc.column]
36
68
  /\s*=\s*$/.match?(source)
37
69
  end
70
+
71
+ def ignore_last_element?
72
+ !!cop_config['AllowMultilineFinalElement']
73
+ end
38
74
  end
39
75
  end
40
76
  end
@@ -6,16 +6,55 @@ module RuboCop
6
6
  # Checks for a line break before the first element in a
7
7
  # multi-line hash.
8
8
  #
9
- # @example
9
+ # @example AllowMultilineFinalElement: false (default)
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
+ #
15
20
  # # good
16
21
  # {
17
22
  # a: 1,
18
23
  # b: 2 }
24
+ #
25
+ # # good
26
+ # {
27
+ # a: 1, b: {
28
+ # c: 3
29
+ # }}
30
+ #
31
+ # @example AllowMultilineFinalElement: true
32
+ #
33
+ # # bad
34
+ # { a: 1,
35
+ # b: 2}
36
+ #
37
+ # # bad
38
+ # { a: 1,
39
+ # b: {
40
+ # c: 3
41
+ # }}
42
+ #
43
+ # # good
44
+ # { a: 1, b: {
45
+ # c: 3
46
+ # }}
47
+ #
48
+ # # good
49
+ # {
50
+ # a: 1,
51
+ # b: 2 }
52
+ #
53
+ # # good
54
+ # {
55
+ # a: 1, b: {
56
+ # c: 3
57
+ # }}
19
58
  class FirstHashElementLineBreak < Base
20
59
  include FirstElementLineBreak
21
60
  extend AutoCorrector
@@ -25,7 +64,15 @@ module RuboCop
25
64
  def on_hash(node)
26
65
  # node.loc.begin tells us whether the hash opens with a {
27
66
  # If it doesn't, Style/FirstMethodArgumentLineBreak will handle it
28
- check_children_line_break(node, node.children) if node.loc.begin
67
+ return unless node.loc.begin
68
+
69
+ check_children_line_break(node, node.children, ignore_last: ignore_last_element?)
70
+ end
71
+
72
+ private
73
+
74
+ def ignore_last_element?
75
+ !!cop_config['AllowMultilineFinalElement']
29
76
  end
30
77
  end
31
78
  end
@@ -6,17 +6,70 @@ 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
9
+ # @example AllowMultilineFinalElement: false (default)
10
10
  #
11
11
  # # bad
12
12
  # method(foo, bar,
13
13
  # baz)
14
14
  #
15
+ # # bad
16
+ # method(foo, bar, {
17
+ # baz: "a",
18
+ # qux: "b",
19
+ # })
20
+ #
15
21
  # # good
16
22
  # method(
17
23
  # foo, bar,
18
24
  # baz)
19
25
  #
26
+ # # good
27
+ # method(
28
+ # foo, bar, {
29
+ # baz: "a",
30
+ # qux: "b",
31
+ # })
32
+ #
33
+ # # ignored
34
+ # method foo, bar,
35
+ # baz
36
+ #
37
+ # @example AllowMultilineFinalElement: true
38
+ #
39
+ # # bad
40
+ # method(foo, bar,
41
+ # baz)
42
+ #
43
+ # # bad
44
+ # method(foo,
45
+ # bar,
46
+ # {
47
+ # baz: "a",
48
+ # qux: "b",
49
+ # }
50
+ # )
51
+ #
52
+ # # good
53
+ # method(foo, bar, {
54
+ # baz: "a",
55
+ # qux: "b",
56
+ # })
57
+ #
58
+ # # good
59
+ # method(
60
+ # foo, bar,
61
+ # baz)
62
+ #
63
+ # # good
64
+ # method(
65
+ # foo,
66
+ # bar,
67
+ # {
68
+ # baz: "a",
69
+ # qux: "b",
70
+ # }
71
+ # )
72
+ #
20
73
  # # ignored
21
74
  # method foo, bar,
22
75
  # baz
@@ -38,10 +91,16 @@ module RuboCop
38
91
  last_arg = args.last
39
92
  args.concat(args.pop.children) if last_arg&.hash_type? && !last_arg&.braces?
40
93
 
41
- check_method_line_break(node, args)
94
+ check_method_line_break(node, args, ignore_last: ignore_last_element?)
42
95
  end
43
96
  alias on_csend on_send
44
97
  alias on_super on_send
98
+
99
+ private
100
+
101
+ def ignore_last_element?
102
+ !!cop_config['AllowMultilineFinalElement']
103
+ end
45
104
  end
46
105
  end
47
106
  end
@@ -6,7 +6,7 @@ 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
9
+ # @example AllowMultilineFinalElement: false (default)
10
10
  #
11
11
  # # bad
12
12
  # def method(foo, bar,
@@ -14,6 +14,13 @@ module RuboCop
14
14
  # do_something
15
15
  # end
16
16
  #
17
+ # # bad
18
+ # def method(foo, bar, baz = {
19
+ # :a => "b",
20
+ # })
21
+ # do_something
22
+ # end
23
+ #
17
24
  # # good
18
25
  # def method(
19
26
  # foo, bar,
@@ -21,11 +28,48 @@ module RuboCop
21
28
  # do_something
22
29
  # end
23
30
  #
31
+ # # good
32
+ # def method(
33
+ # foo, bar, baz = {
34
+ # :a => "b",
35
+ # })
36
+ # do_something
37
+ # end
38
+ #
24
39
  # # ignored
25
40
  # def method foo,
26
41
  # bar
27
42
  # do_something
28
43
  # end
44
+ #
45
+ # @example AllowMultilineFinalElement: true
46
+ #
47
+ # # bad
48
+ # def method(foo, bar,
49
+ # baz)
50
+ # do_something
51
+ # end
52
+ #
53
+ # # good
54
+ # def method(foo, bar, baz = {
55
+ # :a => "b",
56
+ # })
57
+ # do_something
58
+ # end
59
+ #
60
+ # # good
61
+ # def method(
62
+ # foo, bar,
63
+ # baz)
64
+ # do_something
65
+ # end
66
+ #
67
+ # # ignored
68
+ # def method foo,
69
+ # bar
70
+ # do_something
71
+ # end
72
+ #
29
73
  class FirstMethodParameterLineBreak < Base
30
74
  include FirstElementLineBreak
31
75
  extend AutoCorrector
@@ -33,9 +77,15 @@ module RuboCop
33
77
  MSG = 'Add a line break before the first parameter of a multi-line method parameter list.'
34
78
 
35
79
  def on_def(node)
36
- check_method_line_break(node, node.arguments)
80
+ check_method_line_break(node, node.arguments, ignore_last: ignore_last_element?)
37
81
  end
38
82
  alias on_defs on_def
83
+
84
+ private
85
+
86
+ def ignore_last_element?
87
+ !!cop_config['AllowMultilineFinalElement']
88
+ end
39
89
  end
40
90
  end
41
91
  end
@@ -40,10 +40,13 @@ module RuboCop
40
40
  MSG = '%<type>s detected in indentation.'
41
41
 
42
42
  def on_new_investigation
43
- str_ranges = string_literal_ranges(processed_source.ast)
43
+ str_ranges = nil
44
44
 
45
45
  processed_source.lines.each.with_index(1) do |line, lineno|
46
46
  next unless (range = find_offense(line, lineno))
47
+
48
+ # Perform costly calculation only when needed.
49
+ str_ranges ||= string_literal_ranges(processed_source.ast)
47
50
  next if in_string_literal?(str_ranges, range)
48
51
 
49
52
  add_offense(range) { |corrector| autocorrect(corrector, range) }
@@ -90,7 +93,8 @@ module RuboCop
90
93
  # which lines start inside a string literal?
91
94
  return [] if ast.nil?
92
95
 
93
- ast.each_node(:str, :dstr).with_object(Set.new) do |str, ranges|
96
+ ranges = Set.new
97
+ ast.each_node(:str, :dstr) do |str|
94
98
  loc = str.location
95
99
 
96
100
  if str.heredoc?
@@ -99,6 +103,7 @@ module RuboCop
99
103
  ranges << loc.expression
100
104
  end
101
105
  end
106
+ ranges
102
107
  end
103
108
 
104
109
  def message(_node)