rubocop 0.30.1 → 0.31.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rubocop might be problematic. Click here for more details.

Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +38 -0
  3. data/README.md +10 -0
  4. data/config/default.yml +8 -1
  5. data/config/enabled.yml +28 -0
  6. data/lib/rubocop.rb +7 -0
  7. data/lib/rubocop/cli.rb +3 -2
  8. data/lib/rubocop/config.rb +12 -18
  9. data/lib/rubocop/cop/autocorrect_logic.rb +1 -1
  10. data/lib/rubocop/cop/cop.rb +35 -23
  11. data/lib/rubocop/cop/lint/block_alignment.rb +1 -1
  12. data/lib/rubocop/cop/lint/def_end_alignment.rb +6 -0
  13. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
  14. data/lib/rubocop/cop/lint/each_with_object_argument.rb +29 -0
  15. data/lib/rubocop/cop/lint/end_alignment.rb +5 -0
  16. data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
  17. data/lib/rubocop/cop/lint/space_before_first_arg.rb +1 -1
  18. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +1 -1
  19. data/lib/rubocop/cop/metrics/class_length.rb +1 -25
  20. data/lib/rubocop/cop/metrics/module_length.rb +24 -0
  21. data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
  22. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +2 -2
  23. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +2 -2
  24. data/lib/rubocop/cop/mixin/classish_length.rb +37 -0
  25. data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
  26. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +14 -1
  27. data/lib/rubocop/cop/mixin/min_body_length.rb +19 -0
  28. data/lib/rubocop/cop/mixin/on_method_def.rb +1 -0
  29. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -4
  30. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -3
  31. data/lib/rubocop/cop/mixin/space_inside.rb +1 -1
  32. data/lib/rubocop/cop/mixin/statement_modifier.rb +14 -7
  33. data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
  34. data/lib/rubocop/cop/mixin/unused_argument.rb +1 -3
  35. data/lib/rubocop/cop/performance/count.rb +84 -0
  36. data/lib/rubocop/cop/performance/detect.rb +1 -1
  37. data/lib/rubocop/cop/performance/flat_map.rb +2 -1
  38. data/lib/rubocop/cop/performance/parallel_assignment.rb +79 -0
  39. data/lib/rubocop/cop/performance/reverse_each.rb +1 -3
  40. data/lib/rubocop/cop/performance/sample.rb +25 -18
  41. data/lib/rubocop/cop/performance/size.rb +1 -3
  42. data/lib/rubocop/cop/rails/action_filter.rb +1 -1
  43. data/lib/rubocop/cop/rails/delegate.rb +2 -5
  44. data/lib/rubocop/cop/rails/find_by.rb +1 -1
  45. data/lib/rubocop/cop/rails/find_each.rb +1 -3
  46. data/lib/rubocop/cop/rails/read_write_attribute.rb +1 -3
  47. data/lib/rubocop/cop/rails/time_zone.rb +29 -7
  48. data/lib/rubocop/cop/style/alias.rb +1 -1
  49. data/lib/rubocop/cop/style/align_hash.rb +11 -14
  50. data/lib/rubocop/cop/style/array_join.rb +11 -0
  51. data/lib/rubocop/cop/style/attr.rb +1 -3
  52. data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
  53. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  54. data/lib/rubocop/cop/style/block_delimiters.rb +2 -9
  55. data/lib/rubocop/cop/style/block_end_newline.rb +1 -1
  56. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +1 -1
  57. data/lib/rubocop/cop/style/character_literal.rb +1 -1
  58. data/lib/rubocop/cop/style/class_check.rb +1 -1
  59. data/lib/rubocop/cop/style/class_methods.rb +1 -3
  60. data/lib/rubocop/cop/style/collection_methods.rb +1 -1
  61. data/lib/rubocop/cop/style/colon_method_call.rb +1 -3
  62. data/lib/rubocop/cop/style/command_literal.rb +2 -2
  63. data/lib/rubocop/cop/style/comment_annotation.rb +9 -2
  64. data/lib/rubocop/cop/style/copyright.rb +1 -1
  65. data/lib/rubocop/cop/style/def_with_parentheses.rb +1 -1
  66. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +1 -1
  67. data/lib/rubocop/cop/style/dot_position.rb +1 -1
  68. data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
  69. data/lib/rubocop/cop/style/empty_lines.rb +1 -1
  70. data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +1 -1
  71. data/lib/rubocop/cop/style/empty_literal.rb +2 -6
  72. data/lib/rubocop/cop/style/encoding.rb +1 -1
  73. data/lib/rubocop/cop/style/even_odd.rb +2 -4
  74. data/lib/rubocop/cop/style/extra_spacing.rb +1 -3
  75. data/lib/rubocop/cop/style/guard_clause.rb +1 -11
  76. data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
  77. data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -6
  78. data/lib/rubocop/cop/style/indentation_width.rb +11 -2
  79. data/lib/rubocop/cop/style/infinite_loop.rb +8 -8
  80. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  81. data/lib/rubocop/cop/style/leading_comment_space.rb +1 -3
  82. data/lib/rubocop/cop/style/line_end_concatenation.rb +5 -7
  83. data/lib/rubocop/cop/style/method_call_parentheses.rb +1 -8
  84. data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
  85. data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
  86. data/lib/rubocop/cop/style/multiline_block_layout.rb +11 -8
  87. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  88. data/lib/rubocop/cop/style/multiline_operation_indentation.rb +3 -2
  89. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -1
  90. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  91. data/lib/rubocop/cop/style/negated_while.rb +2 -2
  92. data/lib/rubocop/cop/style/next.rb +1 -11
  93. data/lib/rubocop/cop/style/nil_comparison.rb +3 -5
  94. data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
  95. data/lib/rubocop/cop/style/not.rb +3 -5
  96. data/lib/rubocop/cop/style/numeric_literals.rb +2 -5
  97. data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -1
  98. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
  99. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  100. data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
  101. data/lib/rubocop/cop/style/proc.rb +1 -3
  102. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  103. data/lib/rubocop/cop/style/redundant_exception.rb +6 -8
  104. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  105. data/lib/rubocop/cop/style/redundant_self.rb +2 -2
  106. data/lib/rubocop/cop/style/regexp_literal.rb +2 -2
  107. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  108. data/lib/rubocop/cop/style/semicolon.rb +2 -2
  109. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  110. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  111. data/lib/rubocop/cop/style/single_space_before_first_arg.rb +1 -1
  112. data/lib/rubocop/cop/style/space_after_colon.rb +1 -3
  113. data/lib/rubocop/cop/style/space_after_control_keyword.rb +1 -3
  114. data/lib/rubocop/cop/style/space_after_method_name.rb +1 -3
  115. data/lib/rubocop/cop/style/space_after_not.rb +1 -1
  116. data/lib/rubocop/cop/style/space_around_block_parameters.rb +34 -16
  117. data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
  118. data/lib/rubocop/cop/style/space_around_operators.rb +3 -2
  119. data/lib/rubocop/cop/style/space_before_block_braces.rb +1 -1
  120. data/lib/rubocop/cop/style/space_before_comment.rb +1 -3
  121. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +1 -3
  122. data/lib/rubocop/cop/style/space_inside_block_braces.rb +5 -3
  123. data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +1 -1
  124. data/lib/rubocop/cop/style/space_inside_range_literal.rb +1 -1
  125. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  126. data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
  127. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  128. data/lib/rubocop/cop/style/tab.rb +1 -1
  129. data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -2
  130. data/lib/rubocop/cop/style/trailing_comma.rb +1 -1
  131. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +72 -0
  132. data/lib/rubocop/cop/style/trailing_whitespace.rb +1 -1
  133. data/lib/rubocop/cop/style/trivial_accessors.rb +5 -10
  134. data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
  135. data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
  136. data/lib/rubocop/cop/style/variable_interpolation.rb +2 -4
  137. data/lib/rubocop/cop/style/when_then.rb +1 -3
  138. data/lib/rubocop/cop/style/while_until_do.rb +5 -7
  139. data/lib/rubocop/cop/style/while_until_modifier.rb +4 -6
  140. data/lib/rubocop/cop/style/word_array.rb +1 -1
  141. data/lib/rubocop/cop/util.rb +6 -3
  142. data/lib/rubocop/formatter/progress_formatter.rb +3 -2
  143. data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
  144. data/lib/rubocop/rake_task.rb +2 -2
  145. data/lib/rubocop/runner.rb +2 -11
  146. data/lib/rubocop/string_util.rb +4 -2
  147. data/lib/rubocop/token.rb +3 -1
  148. data/lib/rubocop/version.rb +1 -1
  149. data/relnotes/v0.31.0.md +120 -0
  150. metadata +10 -2
@@ -24,7 +24,8 @@ module RuboCop
24
24
 
25
25
  # The begin and end could also be braces, but we call the
26
26
  # variables do... and end...
27
- do_kw_loc, end_kw_loc = receiver.loc.begin, receiver.loc.end
27
+ do_kw_loc = receiver.loc.begin
28
+ end_kw_loc = receiver.loc.end
28
29
  next if do_kw_loc.line == end_kw_loc.line
29
30
 
30
31
  range =
@@ -50,14 +50,17 @@ module RuboCop
50
50
  # with arguments and the expression start on the same line.
51
51
  _block_start, args, last_expression = node.children
52
52
 
53
- if !args.children.empty? && do_loc.line != args.loc.end.line
54
- add_offense_for_expression(node, args, ARG_MSG)
55
- else
56
- return unless last_expression
57
- expression_loc = last_expression.loc
58
- return unless do_loc.line == expression_loc.line
59
- add_offense_for_expression(node, last_expression, MSG)
53
+ unless args.children.empty?
54
+ line = args.loc.end.nil? ? args.loc.line : args.loc.end.line
55
+ if do_loc.line != line
56
+ add_offense_for_expression(node, args, ARG_MSG)
57
+ end
60
58
  end
59
+
60
+ return unless last_expression
61
+ expression_loc = last_expression.loc
62
+ return unless do_loc.line == expression_loc.line
63
+ add_offense_for_expression(node, last_expression, MSG)
61
64
  end
62
65
 
63
66
  def add_offense_for_expression(node, expr, msg)
@@ -70,7 +73,7 @@ module RuboCop
70
73
  end
71
74
 
72
75
  def autocorrect(node)
73
- @corrections << lambda do |corrector|
76
+ lambda do |corrector|
74
77
  _method, args, block_body = *node
75
78
  unless args.children.empty? ||
76
79
  args.loc.end.line == node.loc.begin.line
@@ -30,7 +30,7 @@ module RuboCop
30
30
  end
31
31
 
32
32
  def autocorrect(node)
33
- @corrections << lambda do |corrector|
33
+ lambda do |corrector|
34
34
  corrector.remove(range_with_surrounding_space(node.loc.begin,
35
35
  :left))
36
36
  end
@@ -29,7 +29,8 @@ module RuboCop
29
29
  return unless receiver
30
30
  return if method_name == :[] # Don't check parameters inside [].
31
31
 
32
- lhs, rhs = left_hand_side(receiver), right_hand_side(node)
32
+ lhs = left_hand_side(receiver)
33
+ rhs = right_hand_side(node)
33
34
  range = offending_range(node, lhs, rhs, style)
34
35
  check(range, node, lhs, rhs)
35
36
  end
@@ -148,7 +149,7 @@ module RuboCop
148
149
 
149
150
  case a.type
150
151
  when :if, :while, :until then condition, = *a
151
- when :for then _, collection, _ = *a
152
+ when :for then _, collection, = *a
152
153
  end
153
154
 
154
155
  if condition || collection
@@ -9,12 +9,13 @@ module RuboCop
9
9
  ' use `if`/`unless` instead.'
10
10
 
11
11
  def on_if(node)
12
+ _condition, _if_branch, else_branch = *node
12
13
  loc = node.loc
13
14
 
14
15
  # discard non-ternary ops
15
16
  return unless loc.respond_to?(:question)
16
17
 
17
- add_offense(node, :expression) if loc.line != loc.colon.line
18
+ add_offense(node, :expression) if loc.line != else_branch.loc.line
18
19
  end
19
20
  end
20
21
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  private
29
29
 
30
30
  def autocorrect(node)
31
- @corrections << lambda do |corrector|
31
+ lambda do |corrector|
32
32
  condition, _body, _rest = *node
33
33
  # look inside parentheses around the condition
34
34
  condition = condition.children.first while condition.type == :begin
@@ -28,10 +28,10 @@ module RuboCop
28
28
  private
29
29
 
30
30
  def autocorrect(node)
31
- @corrections << lambda do |corrector|
31
+ lambda do |corrector|
32
32
  condition, _body, _rest = *node
33
33
  # Look inside parentheses around the condition, if any.
34
- condition, _ = *condition while condition.type == :begin
34
+ condition, = *condition while condition.type == :begin
35
35
  # Unwrap the negated portion of the condition (a send node).
36
36
  pos_condition, _method, = *condition
37
37
  corrector.replace(
@@ -21,6 +21,7 @@ module RuboCop
21
21
  class Next < Cop
22
22
  include IfNode
23
23
  include ConfigurableEnforcedStyle
24
+ include MinBodyLength
24
25
 
25
26
  MSG = 'Use `next` to skip iteration.'
26
27
  ENUMERATORS = [:collect, :detect, :downto, :each, :find, :find_all,
@@ -56,17 +57,6 @@ module RuboCop
56
57
 
57
58
  private
58
59
 
59
- def min_body_length?(node)
60
- (node.loc.end.line - node.loc.keyword.line) > min_body_length
61
- end
62
-
63
- def min_body_length
64
- length = cop_config['MinBodyLength'] || 1
65
- return length if length.is_a?(Integer) && length > 0
66
-
67
- fail 'MinBodyLength needs to be a positive integer!'
68
- end
69
-
70
60
  def enumerator?(method_name)
71
61
  ENUMERATORS.include?(method_name) || /\Aeach_/.match(method_name)
72
62
  end
@@ -29,11 +29,9 @@ module RuboCop
29
29
  private
30
30
 
31
31
  def autocorrect(node)
32
- @corrections << lambda do |corrector|
33
- expr = node.loc.expression
34
- new_code = expr.source.sub(/\s*={2,3}\s*nil/, '.nil?')
35
- corrector.replace(expr, new_code)
36
- end
32
+ expr = node.loc.expression
33
+ new_code = expr.source.sub(/\s*={2,3}\s*nil/, '.nil?')
34
+ ->(corrector) { corrector.replace(expr, new_code) }
37
35
  end
38
36
  end
39
37
  end
@@ -84,7 +84,7 @@ module RuboCop
84
84
  end
85
85
 
86
86
  def autocorrect_comparison(node)
87
- @corrections << lambda do |corrector|
87
+ lambda do |corrector|
88
88
  expr = node.loc.expression
89
89
  new_code =
90
90
  if include_semantic_changes?
@@ -97,7 +97,7 @@ module RuboCop
97
97
  end
98
98
 
99
99
  def autocorrect_non_nil(node, inner_node)
100
- @corrections << lambda do |corrector|
100
+ lambda do |corrector|
101
101
  receiver, _method, _args = *inner_node
102
102
  if receiver
103
103
  corrector.replace(node.loc.expression,
@@ -22,11 +22,9 @@ module RuboCop
22
22
  private
23
23
 
24
24
  def correction(node)
25
- lambda do |corrector|
26
- old_source = node.loc.expression.source
27
- new_source = old_source.sub(/not\s+/, '!')
28
- corrector.replace(node.loc.expression, new_source)
29
- end
25
+ old_source = node.loc.expression.source
26
+ new_source = old_source.sub(/not\s+/, '!')
27
+ ->(corrector) { corrector.replace(node.loc.expression, new_source) }
30
28
  end
31
29
  end
32
30
  end
@@ -46,11 +46,8 @@ module RuboCop
46
46
  end
47
47
 
48
48
  def autocorrect(node)
49
- @corrections << lambda do |corrector|
50
- corrector.replace(
51
- node.loc.expression,
52
- format_number(node)
53
- )
49
+ lambda do |corrector|
50
+ corrector.replace(node.loc.expression, format_number(node))
54
51
  end
55
52
  end
56
53
 
@@ -61,7 +61,7 @@ module RuboCop
61
61
  end
62
62
 
63
63
  def autocorrect(node)
64
- @corrections << lambda do |corrector|
64
+ lambda do |corrector|
65
65
  corrector.remove(node.loc.begin)
66
66
  corrector.remove(node.loc.end)
67
67
  end
@@ -55,7 +55,7 @@ module RuboCop
55
55
  expression_indentation + expression + closing_newline +
56
56
  closing_indentation + closing_delimiter + reg_opt
57
57
 
58
- @corrections << lambda do |corrector|
58
+ lambda do |corrector|
59
59
  corrector.replace(node.loc.expression, corrected_source)
60
60
  end
61
61
  end
@@ -39,7 +39,7 @@ module RuboCop
39
39
  def autocorrect(node)
40
40
  src = node.loc.expression.source
41
41
 
42
- @corrections << lambda do |corrector|
42
+ lambda do |corrector|
43
43
  corrector.replace(node.loc.expression, corrected(src))
44
44
  end
45
45
  end
@@ -13,7 +13,7 @@ module RuboCop
13
13
  end
14
14
 
15
15
  def autocorrect(node)
16
- @corrections << lambda do |corrector|
16
+ lambda do |corrector|
17
17
  backref, = *node
18
18
  parent_type = node.parent ? node.parent.type : nil
19
19
  if [:dstr, :xstr, :regexp].include?(parent_type)
@@ -22,9 +22,7 @@ module RuboCop
22
22
  end
23
23
 
24
24
  def autocorrect(node)
25
- @corrections << lambda do |corrector|
26
- corrector.replace(node.loc.expression, 'proc')
27
- end
25
+ ->(corrector) { corrector.replace(node.loc.expression, 'proc') }
28
26
  end
29
27
  end
30
28
  end
@@ -36,7 +36,7 @@ module RuboCop
36
36
  end
37
37
 
38
38
  def autocorrect(node)
39
- @corrections << lambda do |corrector|
39
+ lambda do |corrector|
40
40
  child = node.children.first
41
41
 
42
42
  begin_indent = node.loc.column
@@ -29,14 +29,12 @@ module RuboCop
29
29
 
30
30
  # switch `raise RuntimeError, 'message'` to `raise 'message'`
31
31
  def autocorrect(node)
32
- @corrections << lambda do |corrector|
33
- start_range = node.loc.expression.begin
34
- no_comma = range_with_surrounding_comma(node.loc.expression.end,
35
- :right)
36
- comma_range = start_range.join(no_comma)
37
- final_range = range_with_surrounding_space(comma_range, :right)
38
- corrector.replace(final_range, '')
39
- end
32
+ start_range = node.loc.expression.begin
33
+ no_comma = range_with_surrounding_comma(node.loc.expression.end,
34
+ :right)
35
+ comma_range = start_range.join(no_comma)
36
+ final_range = range_with_surrounding_space(comma_range, :right)
37
+ ->(corrector) { corrector.replace(final_range, '') }
40
38
  end
41
39
  end
42
40
  end
@@ -28,7 +28,7 @@ module RuboCop
28
28
  private
29
29
 
30
30
  def autocorrect(node)
31
- @corrections << lambda do |corrector|
31
+ lambda do |corrector|
32
32
  if node.children.size > 1
33
33
  kids = node.children.map { |child| child.loc.expression }
34
34
  corrector.insert_before(kids.first, '[')
@@ -103,7 +103,7 @@ module RuboCop
103
103
 
104
104
  def autocorrect(node)
105
105
  receiver, _method_name, *_args = *node
106
- @corrections << lambda do |corrector|
106
+ lambda do |corrector|
107
107
  corrector.remove(receiver.loc.expression)
108
108
  corrector.remove(node.loc.dot)
109
109
  end
@@ -112,7 +112,7 @@ module RuboCop
112
112
  private
113
113
 
114
114
  def on_argument(node)
115
- name, _ = *node
115
+ name, = *node
116
116
  @local_variables << name
117
117
  end
118
118
 
@@ -97,7 +97,7 @@ module RuboCop
97
97
  end
98
98
 
99
99
  def autocorrect(node)
100
- fail CorrectionNotPossible if contains_slash?(node)
100
+ return if contains_slash?(node)
101
101
 
102
102
  if slash_literal?(node)
103
103
  replacement = ['%r', ''].zip(preferred_delimiters).map(&:join)
@@ -105,7 +105,7 @@ module RuboCop
105
105
  replacement = %w(/ /)
106
106
  end
107
107
 
108
- @corrections << lambda do |corrector|
108
+ lambda do |corrector|
109
109
  corrector.replace(node.loc.begin, replacement.first)
110
110
  corrector.replace(node.loc.end, replacement.last)
111
111
  end
@@ -84,7 +84,7 @@ module RuboCop
84
84
  end
85
85
 
86
86
  def apply_autocorrect(node, rhs, operator, new_rhs)
87
- @corrections << lambda do |corrector|
87
+ lambda do |corrector|
88
88
  corrector.insert_before(node.loc.operator, operator)
89
89
  corrector.replace(rhs.loc.expression, new_rhs.loc.expression.source)
90
90
  end
@@ -55,8 +55,8 @@ module RuboCop
55
55
  end
56
56
 
57
57
  def autocorrect(range)
58
- fail CorrectionNotPossible unless range
59
- @corrections << ->(corrector) { corrector.remove(range) }
58
+ return unless range
59
+ ->(corrector) { corrector.remove(range) }
60
60
  end
61
61
  end
62
62
  end
@@ -34,7 +34,7 @@ module RuboCop
34
34
  end
35
35
 
36
36
  def autocorrect(node)
37
- @corrections << lambda do |corrector|
37
+ lambda do |corrector|
38
38
  name =
39
39
  case style
40
40
  when :semantic then command?(:raise, node) ? 'fail' : 'raise'
@@ -32,7 +32,7 @@ module RuboCop
32
32
  eol_comment = processed_source.comments.find do |c|
33
33
  c.loc.line == node.loc.expression.line
34
34
  end
35
- @corrections << lambda do |corrector|
35
+ lambda do |corrector|
36
36
  if body.type == :begin
37
37
  body.children.each do |part|
38
38
  break_line_before(part.loc.expression, node, corrector, 1)
@@ -33,7 +33,7 @@ module RuboCop
33
33
  end
34
34
 
35
35
  def autocorrect(range)
36
- @corrections << ->(corrector) { corrector.replace(range, ' ') }
36
+ ->(corrector) { corrector.replace(range, ' ') }
37
37
  end
38
38
  end
39
39
  end
@@ -30,9 +30,7 @@ module RuboCop
30
30
  end
31
31
 
32
32
  def autocorrect(range)
33
- @corrections << lambda do |corrector|
34
- corrector.insert_after(range, ' ')
35
- end
33
+ ->(corrector) { corrector.insert_after(range, ' ') }
36
34
  end
37
35
  end
38
36
  end
@@ -27,9 +27,7 @@ module RuboCop
27
27
  end
28
28
 
29
29
  def autocorrect(node)
30
- @corrections << lambda do |corrector|
31
- corrector.insert_after(node.loc.keyword, ' ')
32
- end
30
+ ->(corrector) { corrector.insert_after(node.loc.keyword, ' ') }
33
31
  end
34
32
  end
35
33
  end
@@ -30,9 +30,7 @@ module RuboCop
30
30
  end
31
31
 
32
32
  def autocorrect(pos_before_left_paren)
33
- @corrections << lambda do |corrector|
34
- corrector.remove(pos_before_left_paren)
35
- end
33
+ ->(corrector) { corrector.remove(pos_before_left_paren) }
36
34
  end
37
35
  end
38
36
  end
@@ -25,7 +25,7 @@ module RuboCop
25
25
  end
26
26
 
27
27
  def autocorrect(node)
28
- @corrections << lambda do |corrector|
28
+ lambda do |corrector|
29
29
  receiver, _method_name, *_args = *node
30
30
  space_range =
31
31
  Parser::Source::Range.new(node.loc.selector.source_buffer,
@@ -17,7 +17,8 @@ module RuboCop
17
17
 
18
18
  def on_block(node)
19
19
  _method, args, body = *node
20
- opening_pipe, closing_pipe = args.loc.begin, args.loc.end
20
+ opening_pipe = args.loc.begin
21
+ closing_pipe = args.loc.end
21
22
  return unless !args.children.empty? && opening_pipe
22
23
 
23
24
  check_inside_pipes(args.children, opening_pipe, closing_pipe)
@@ -37,25 +38,42 @@ module RuboCop
37
38
  end
38
39
 
39
40
  def check_inside_pipes(args, opening_pipe, closing_pipe)
40
- first, last = args.first.loc.expression, args.last.loc.expression
41
-
42
41
  if style == :no_space
43
- check_no_space(opening_pipe.end_pos, first.begin_pos,
44
- 'Space before first')
45
- check_no_space(last.end_pos, closing_pipe.begin_pos,
46
- 'Space after last')
42
+ check_no_space_style_inside_pipes(args, opening_pipe, closing_pipe)
47
43
  elsif style == :space
48
- check_space(opening_pipe.end_pos, first.begin_pos, first,
49
- 'before first block parameter')
50
- check_space(last.end_pos, closing_pipe.begin_pos, last,
51
- 'after last block parameter')
52
- check_no_space(opening_pipe.end_pos, first.begin_pos - 1,
53
- 'Extra space before first')
54
- check_no_space(last.end_pos + 1, closing_pipe.begin_pos,
55
- 'Extra space after last')
44
+ check_space_style_inside_pipes(args, opening_pipe, closing_pipe)
56
45
  end
57
46
  end
58
47
 
48
+ def check_no_space_style_inside_pipes(args, opening_pipe, closing_pipe)
49
+ first = args.first.loc.expression
50
+ last = args.last.loc.expression
51
+
52
+ check_no_space(opening_pipe.end_pos, first.begin_pos,
53
+ 'Space before first')
54
+ check_no_space(last_end_pos_inside_pipes(last.end_pos),
55
+ closing_pipe.begin_pos, 'Space after last')
56
+ end
57
+
58
+ def check_space_style_inside_pipes(args, opening_pipe, closing_pipe)
59
+ first = args.first.loc.expression
60
+ last = args.last.loc.expression
61
+ last_end_pos = last_end_pos_inside_pipes(last.end_pos)
62
+
63
+ check_space(opening_pipe.end_pos, first.begin_pos, first,
64
+ 'before first block parameter')
65
+ check_space(last_end_pos, closing_pipe.begin_pos, last,
66
+ 'after last block parameter')
67
+ check_no_space(opening_pipe.end_pos, first.begin_pos - 1,
68
+ 'Extra space before first')
69
+ check_no_space(last_end_pos + 1, closing_pipe.begin_pos,
70
+ 'Extra space after last')
71
+ end
72
+
73
+ def last_end_pos_inside_pipes(pos)
74
+ processed_source.buffer.source[pos] == ',' ? pos + 1 : pos
75
+ end
76
+
59
77
  def check_each_arg(args)
60
78
  args.children[1..-1].each do |arg|
61
79
  expr = arg.loc.expression
@@ -79,7 +97,7 @@ module RuboCop
79
97
  end
80
98
 
81
99
  def autocorrect(range)
82
- @corrections << lambda do |corrector|
100
+ lambda do |corrector|
83
101
  case range.source
84
102
  when /^\s+$/ then corrector.remove(range)
85
103
  else corrector.insert_after(range, ' ')