rubocop 0.21.0 → 0.22.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 (159) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/{rubocop-todo.yml → .rubocop_todo.yml} +5 -5
  4. data/CHANGELOG.md +41 -0
  5. data/README.md +21 -11
  6. data/config/default.yml +33 -10
  7. data/config/disabled.yml +0 -4
  8. data/config/enabled.yml +27 -1
  9. data/lib/rubocop.rb +17 -1
  10. data/lib/rubocop/config.rb +32 -27
  11. data/lib/rubocop/config_loader.rb +21 -8
  12. data/lib/rubocop/cop/cop.rb +8 -8
  13. data/lib/rubocop/cop/lint/block_alignment.rb +22 -22
  14. data/lib/rubocop/cop/lint/condition_position.rb +3 -5
  15. data/lib/rubocop/cop/lint/debugger.rb +4 -5
  16. data/lib/rubocop/cop/lint/end_alignment.rb +11 -10
  17. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -11
  18. data/lib/rubocop/cop/lint/require_parentheses.rb +7 -7
  19. data/lib/rubocop/cop/lint/rescue_exception.rb +3 -3
  20. data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +6 -7
  21. data/lib/rubocop/cop/lint/unreachable_code.rb +2 -3
  22. data/lib/rubocop/cop/lint/unused_block_argument.rb +2 -15
  23. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -14
  24. data/lib/rubocop/cop/lint/useless_comparison.rb +4 -5
  25. data/lib/rubocop/cop/lint/void.rb +6 -8
  26. data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +4 -2
  27. data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +1 -3
  28. data/lib/rubocop/cop/mixin/code_length.rb +5 -5
  29. data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
  30. data/lib/rubocop/cop/mixin/percent_literal.rb +2 -3
  31. data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -5
  32. data/lib/rubocop/cop/mixin/space_inside.rb +10 -11
  33. data/lib/rubocop/cop/mixin/unused_argument.rb +35 -0
  34. data/lib/rubocop/cop/offense.rb +1 -1
  35. data/lib/rubocop/cop/rails/action_filter.rb +6 -8
  36. data/lib/rubocop/cop/rails/default_scope.rb +2 -4
  37. data/lib/rubocop/cop/rails/delegate.rb +2 -0
  38. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +2 -3
  39. data/lib/rubocop/cop/rails/output.rb +2 -3
  40. data/lib/rubocop/cop/rails/read_write_attribute.rb +3 -4
  41. data/lib/rubocop/cop/rails/validation.rb +2 -3
  42. data/lib/rubocop/cop/style/alias.rb +3 -3
  43. data/lib/rubocop/cop/style/align_hash.rb +4 -4
  44. data/lib/rubocop/cop/style/align_parameters.rb +13 -1
  45. data/lib/rubocop/cop/style/and_or.rb +2 -5
  46. data/lib/rubocop/cop/style/array_join.rb +3 -4
  47. data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -3
  48. data/lib/rubocop/cop/style/block_comments.rb +2 -3
  49. data/lib/rubocop/cop/style/block_nesting.rb +2 -3
  50. data/lib/rubocop/cop/style/blocks.rb +9 -9
  51. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +5 -5
  52. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  53. data/lib/rubocop/cop/style/class_methods.rb +3 -5
  54. data/lib/rubocop/cop/style/collection_methods.rb +8 -11
  55. data/lib/rubocop/cop/style/comment_annotation.rb +9 -9
  56. data/lib/rubocop/cop/style/comment_indentation.rb +66 -0
  57. data/lib/rubocop/cop/style/constant_name.rb +3 -3
  58. data/lib/rubocop/cop/style/cyclomatic_complexity.rb +5 -5
  59. data/lib/rubocop/cop/style/deprecated_hash_methods.rb +6 -6
  60. data/lib/rubocop/cop/style/each_with_object.rb +38 -0
  61. data/lib/rubocop/cop/style/empty_lines.rb +8 -8
  62. data/lib/rubocop/cop/style/empty_lines_around_body.rb +7 -7
  63. data/lib/rubocop/cop/style/encoding.rb +6 -7
  64. data/lib/rubocop/cop/style/end_of_line.rb +10 -10
  65. data/lib/rubocop/cop/style/file_name.rb +11 -7
  66. data/lib/rubocop/cop/style/guard_clause.rb +32 -22
  67. data/lib/rubocop/cop/style/indentation_width.rb +25 -26
  68. data/lib/rubocop/cop/style/lambda.rb +8 -9
  69. data/lib/rubocop/cop/style/leading_comment_space.rb +4 -5
  70. data/lib/rubocop/cop/style/line_end_concatenation.rb +29 -6
  71. data/lib/rubocop/cop/style/line_length.rb +9 -9
  72. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +7 -6
  73. data/lib/rubocop/cop/style/module_function.rb +3 -4
  74. data/lib/rubocop/cop/style/multiline_if_then.rb +3 -3
  75. data/lib/rubocop/cop/style/negated_if.rb +2 -0
  76. data/lib/rubocop/cop/style/next.rb +80 -0
  77. data/lib/rubocop/cop/style/nil_comparison.rb +2 -3
  78. data/lib/rubocop/cop/style/non_nil_check.rb +15 -3
  79. data/lib/rubocop/cop/style/not.rb +4 -4
  80. data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
  81. data/lib/rubocop/cop/style/op_method.rb +3 -5
  82. data/lib/rubocop/cop/style/parameter_lists.rb +4 -4
  83. data/lib/rubocop/cop/style/parentheses_around_condition.rb +5 -6
  84. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +5 -5
  85. data/lib/rubocop/cop/style/predicate_name.rb +2 -4
  86. data/lib/rubocop/cop/style/redundant_self.rb +11 -12
  87. data/lib/rubocop/cop/style/self_assignment.rb +8 -13
  88. data/lib/rubocop/cop/style/semicolon.rb +16 -19
  89. data/lib/rubocop/cop/style/signal_exception.rb +10 -11
  90. data/lib/rubocop/cop/style/single_line_block_params.rb +2 -3
  91. data/lib/rubocop/cop/style/single_line_methods.rb +3 -4
  92. data/lib/rubocop/cop/style/space_after_colon.rb +9 -9
  93. data/lib/rubocop/cop/style/space_after_control_keyword.rb +3 -3
  94. data/lib/rubocop/cop/style/space_after_method_name.rb +3 -3
  95. data/lib/rubocop/cop/style/space_after_not.rb +3 -4
  96. data/lib/rubocop/cop/style/space_around_operators.rb +5 -4
  97. data/lib/rubocop/cop/style/space_before_comment.rb +29 -0
  98. data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +6 -7
  99. data/lib/rubocop/cop/style/symbol_array.rb +2 -3
  100. data/lib/rubocop/cop/style/tab.rb +8 -8
  101. data/lib/rubocop/cop/style/trailing_blank_lines.rb +10 -10
  102. data/lib/rubocop/cop/style/trailing_whitespace.rb +6 -7
  103. data/lib/rubocop/cop/style/trivial_accessors.rb +3 -4
  104. data/lib/rubocop/cop/style/unless_else.rb +2 -3
  105. data/lib/rubocop/cop/style/unneeded_capital_w.rb +3 -3
  106. data/lib/rubocop/cop/style/unneeded_percent_x.rb +26 -0
  107. data/lib/rubocop/cop/style/when_then.rb +3 -3
  108. data/lib/rubocop/cop/style/while_until_do.rb +3 -6
  109. data/lib/rubocop/cop/style/word_array.rb +8 -7
  110. data/lib/rubocop/cop/team.rb +9 -11
  111. data/lib/rubocop/cop/util.rb +2 -3
  112. data/lib/rubocop/cop/variable_force.rb +2 -3
  113. data/lib/rubocop/cop/variable_force/variable_table.rb +1 -2
  114. data/lib/rubocop/file_inspector.rb +5 -0
  115. data/lib/rubocop/formatter/clang_style_formatter.rb +3 -4
  116. data/lib/rubocop/formatter/disabled_config_formatter.rb +6 -6
  117. data/lib/rubocop/options.rb +14 -11
  118. data/lib/rubocop/target_finder.rb +12 -1
  119. data/lib/rubocop/version.rb +1 -1
  120. data/relnotes/v0.21.0.md +11 -0
  121. data/relnotes/v0.22.0.md +77 -0
  122. data/spec/rubocop/cli_spec.rb +86 -40
  123. data/spec/rubocop/comment_config_spec.rb +1 -1
  124. data/spec/rubocop/config_loader_spec.rb +3 -3
  125. data/spec/rubocop/config_spec.rb +14 -3
  126. data/spec/rubocop/cop/force_spec.rb +1 -1
  127. data/spec/rubocop/cop/lint/unused_block_argument_spec.rb +164 -99
  128. data/spec/rubocop/cop/lint/unused_method_argument_spec.rb +195 -85
  129. data/spec/rubocop/cop/lint/void_spec.rb +8 -0
  130. data/spec/rubocop/cop/rails/delegate_spec.rb +17 -0
  131. data/spec/rubocop/cop/rails/output_spec.rb +3 -3
  132. data/spec/rubocop/cop/severity_spec.rb +5 -5
  133. data/spec/rubocop/cop/style/align_parameters_spec.rb +108 -0
  134. data/spec/rubocop/cop/style/block_nesting_spec.rb +3 -3
  135. data/spec/rubocop/cop/style/class_and_module_children_spec.rb +12 -10
  136. data/spec/rubocop/cop/style/comment_indentation_spec.rb +174 -0
  137. data/spec/rubocop/cop/style/each_with_object_spec.rb +44 -0
  138. data/spec/rubocop/cop/style/end_of_line_spec.rb +3 -5
  139. data/spec/rubocop/cop/style/guard_clause_spec.rb +135 -57
  140. data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +7 -7
  141. data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +75 -1
  142. data/spec/rubocop/cop/style/line_length_spec.rb +7 -7
  143. data/spec/rubocop/cop/style/negated_if_spec.rb +5 -1
  144. data/spec/rubocop/cop/style/next_spec.rb +210 -0
  145. data/spec/rubocop/cop/style/non_nil_check_spec.rb +44 -13
  146. data/spec/rubocop/cop/style/self_assignment_spec.rb +1 -1
  147. data/spec/rubocop/cop/style/space_around_operators_spec.rb +1 -0
  148. data/spec/rubocop/cop/style/space_before_comment_spec.rb +34 -0
  149. data/spec/rubocop/cop/style/unneeded_percent_x_spec.rb +34 -0
  150. data/spec/rubocop/cop/style/while_until_modifier_spec.rb +2 -2
  151. data/spec/rubocop/cop/util_spec.rb +2 -2
  152. data/spec/rubocop/cop/variable_force_spec.rb +1 -1
  153. data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +4 -4
  154. data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +2 -2
  155. data/spec/rubocop/formatter/offense_count_formatter_spec.rb +2 -2
  156. data/spec/rubocop/options_spec.rb +3 -0
  157. data/spec/support/shared_context.rb +1 -3
  158. data/spec/support/statement_modifier_helper.rb +2 -2
  159. metadata +20 -3
@@ -24,10 +24,8 @@ module Rubocop
24
24
 
25
25
  def check(node, method_name, _args, _body)
26
26
  prefix_blacklist.each do |prefix|
27
- if method_name.to_s.start_with?(prefix)
28
- add_offense(node, :name,
29
- message(method_name.to_s, prefix))
30
- end
27
+ next unless method_name.to_s.start_with?(prefix)
28
+ add_offense(node, :name, message(method_name.to_s, prefix))
31
29
  end
32
30
  end
33
31
 
@@ -92,14 +92,13 @@ module Rubocop
92
92
 
93
93
  def on_send(node)
94
94
  receiver, method_name, *_args = *node
95
- if receiver && receiver.type == :self
96
- unless operator?(method_name) || keyword?(method_name) ||
97
- constant_name?(method_name) ||
98
- @allowed_send_nodes.include?(node) ||
99
- @local_variables.include?(method_name)
100
- add_offense(node, :expression)
101
- end
102
- end
95
+ return unless receiver && receiver.type == :self
96
+ return if operator?(method_name) || keyword?(method_name) ||
97
+ constant_name?(method_name) ||
98
+ @allowed_send_nodes.include?(node) ||
99
+ @local_variables.include?(method_name)
100
+
101
+ add_offense(node, :expression)
103
102
  end
104
103
 
105
104
  def autocorrect(node)
@@ -134,10 +133,10 @@ module Rubocop
134
133
  end
135
134
 
136
135
  def allow_self(node)
137
- if node.type == :send
138
- receiver, _method_name, *_args = *node
139
- @allowed_send_nodes << node if receiver && receiver.type == :self
140
- end
136
+ return unless node.type == :send
137
+
138
+ receiver, _method_name, *_args = *node
139
+ @allowed_send_nodes << node if receiver && receiver.type == :self
141
140
  end
142
141
  end
143
142
  end
@@ -15,6 +15,7 @@ module Rubocop
15
15
  class SelfAssignment < Cop
16
16
  include AST::Sexp
17
17
 
18
+ MSG = 'Use self-assignment shorthand `%s=`.'
18
19
  OPS = [:+, :-, :*, :**, :/, :|, :&]
19
20
 
20
21
  def on_lvasgn(node)
@@ -29,9 +30,10 @@ module Rubocop
29
30
  check(node, :cvar)
30
31
  end
31
32
 
33
+ private
34
+
32
35
  def check(node, var_type)
33
36
  var_name, rhs = *node
34
-
35
37
  return unless rhs
36
38
 
37
39
  if rhs.type == :send
@@ -43,29 +45,22 @@ module Rubocop
43
45
 
44
46
  def check_send_node(node, rhs, var_name, var_type)
45
47
  receiver, method_name, *_args = *rhs
46
-
47
48
  return unless OPS.include?(method_name)
48
49
 
49
50
  target_node = s(var_type, var_name)
51
+ return unless receiver == target_node
50
52
 
51
- if receiver == target_node
52
- add_offense(node,
53
- :expression,
54
- "Use self-assignment shorthand `#{method_name}=`.")
55
- end
53
+ add_offense(node, :expression, format(MSG, method_name))
56
54
  end
57
55
 
58
56
  def check_boolean_node(node, rhs, var_name, var_type)
59
57
  first_operand, _second_operand = *rhs
60
58
 
61
59
  target_node = s(var_type, var_name)
60
+ return unless first_operand == target_node
62
61
 
63
- if first_operand == target_node
64
- operator = rhs.loc.operator.source
65
- add_offense(node,
66
- :expression,
67
- "Use self-assignment shorthand `#{operator}=`.")
68
- end
62
+ operator = rhs.loc.operator.source
63
+ add_offense(node, :expression, format(MSG, operator))
69
64
  end
70
65
  end
71
66
  end
@@ -16,25 +16,23 @@ module Rubocop
16
16
  end
17
17
 
18
18
  def on_begin(node)
19
- unless cop_config['AllowAsExpressionSeparator']
20
- exprs = node.children
19
+ return if cop_config['AllowAsExpressionSeparator']
20
+ exprs = node.children
21
21
 
22
- return if exprs.size < 2
22
+ return if exprs.size < 2
23
23
 
24
- # create a map matching lines to the number of expressions on them
25
- exprs_lines = exprs.map { |e| e.loc.expression.line }
26
- lines = exprs_lines.group_by { |i| i }
24
+ # create a map matching lines to the number of expressions on them
25
+ exprs_lines = exprs.map { |e| e.loc.expression.line }
26
+ lines = exprs_lines.group_by { |i| i }
27
27
 
28
- # every line with more than 1 expression on it is an offense
29
- lines.each do |line, expr_on_line|
30
- if expr_on_line.size > 1
31
- # TODO: Find the correct position of the semicolon. We don't
32
- # know if the first semicolon on the line is a separator of
33
- # expressions. It's just a guess.
34
- column = @processed_source[line - 1].index(';')
35
- convention_on(line, column, !:last_on_line)
36
- end
37
- end
28
+ # every line with more than 1 expression on it is an offense
29
+ lines.each do |line, expr_on_line|
30
+ next unless expr_on_line.size > 1
31
+ # TODO: Find the correct position of the semicolon. We don't know
32
+ # if the first semicolon on the line is a separator of
33
+ # expressions. It's just a guess.
34
+ column = @processed_source[line - 1].index(';')
35
+ convention_on(line, column, !:last_on_line)
38
36
  end
39
37
  end
40
38
 
@@ -46,9 +44,8 @@ module Rubocop
46
44
  end
47
45
 
48
46
  tokens_for_lines.each do |line, tokens|
49
- if tokens.last.type == :tSEMI
50
- convention_on(line, tokens.last.pos.column, :last_on_line)
51
- end
47
+ next unless tokens.last.type == :tSEMI
48
+ convention_on(line, tokens.last.pos.column, :last_on_line)
52
49
  end
53
50
  end
54
51
 
@@ -11,15 +11,14 @@ module Rubocop
11
11
  RAISE_MSG = 'Use `raise` instead of `fail` to rethrow exceptions.'
12
12
 
13
13
  def on_rescue(node)
14
- if style == :semantic
15
- begin_node, *rescue_nodes, _else_node = *node
14
+ return unless style == :semantic
16
15
 
17
- check_for(:raise, begin_node)
16
+ begin_node, *rescue_nodes, _else_node = *node
17
+ check_for(:raise, begin_node)
18
18
 
19
- rescue_nodes.each do |rescue_node|
20
- check_for(:fail, rescue_node)
21
- allow(:raise, rescue_node)
22
- end
19
+ rescue_nodes.each do |rescue_node|
20
+ check_for(:fail, rescue_node)
21
+ allow(:raise, rescue_node)
23
22
  end
24
23
  end
25
24
 
@@ -65,10 +64,10 @@ module Rubocop
65
64
 
66
65
  if style == :semantic
67
66
  each_command(method_name, node) do |send_node|
68
- unless ignored_node?(send_node)
69
- add_offense(send_node, :selector, message(method_name))
70
- ignore_node(send_node)
71
- end
67
+ next if ignored_node?(send_node)
68
+
69
+ add_offense(send_node, :selector, message(method_name))
70
+ ignore_node(send_node)
72
71
  end
73
72
  else
74
73
  _receiver, selector, _args = *node
@@ -24,10 +24,9 @@ module Rubocop
24
24
  args = *args_node
25
25
 
26
26
  return true unless args.all? { |n| n.type == :arg }
27
+ return if args_match?(method_name, args)
27
28
 
28
- unless args_match?(method_name, args)
29
- add_offense(args_node, :expression, message(method_name))
30
- end
29
+ add_offense(args_node, :expression, message(method_name))
31
30
  end
32
31
 
33
32
  private
@@ -21,11 +21,10 @@ module Rubocop
21
21
  end_line = node.loc.end.line
22
22
 
23
23
  empty_body = body.nil?
24
+ return unless start_line == end_line && !(allow_empty? && empty_body)
24
25
 
25
- if start_line == end_line && !(allow_empty? && empty_body)
26
- @body = body
27
- add_offense(node, :expression)
28
- end
26
+ @body = body
27
+ add_offense(node, :expression)
29
28
  end
30
29
 
31
30
  def autocorrect(node)
@@ -11,18 +11,18 @@ module Rubocop
11
11
 
12
12
  def on_pair(node)
13
13
  oper = node.loc.operator
14
- if oper.is?(':') && oper.source_buffer.source[oper.end_pos] =~ /\S/
15
- add_offense(oper, oper)
16
- end
14
+ return unless oper.is?(':') &&
15
+ oper.source_buffer.source[oper.end_pos] =~ /\S/
16
+
17
+ add_offense(oper, oper)
17
18
  end
18
19
 
19
20
  def on_if(node)
20
- if ternary_op?(node)
21
- colon = node.loc.colon
22
- if colon.source_buffer.source[colon.end_pos] =~ /\S/
23
- add_offense(colon, colon)
24
- end
25
- end
21
+ return unless ternary_op?(node)
22
+ colon = node.loc.colon
23
+ return unless colon.source_buffer.source[colon.end_pos] =~ /\S/
24
+
25
+ add_offense(colon, colon)
26
26
  end
27
27
 
28
28
  def autocorrect(range)
@@ -15,9 +15,9 @@ module Rubocop
15
15
  exp = node.loc.expression
16
16
  kw = node.loc.keyword
17
17
  kw_offset = kw.begin_pos - exp.begin_pos
18
- if exp.source[kw_offset..-1].start_with?(kw.source + '(')
19
- add_offense(node, kw)
20
- end
18
+ return unless exp.source[kw_offset..-1].start_with?(kw.source + '(')
19
+
20
+ add_offense(node, kw)
21
21
  end
22
22
 
23
23
  KEYWORDS.each do |keyword|
@@ -24,9 +24,9 @@ module Rubocop
24
24
  pos_before_left_paren = Parser::Source::Range.new(expr.source_buffer,
25
25
  expr.begin_pos - 1,
26
26
  expr.begin_pos)
27
- if pos_before_left_paren.source =~ /\s/
28
- add_offense(pos_before_left_paren, pos_before_left_paren)
29
- end
27
+ return unless pos_before_left_paren.source =~ /\s/
28
+
29
+ add_offense(pos_before_left_paren, pos_before_left_paren)
30
30
  end
31
31
 
32
32
  def autocorrect(pos_before_left_paren)
@@ -18,11 +18,10 @@ module Rubocop
18
18
  _receiver, method_name, *_args = *node
19
19
 
20
20
  return unless method_name == :!
21
+ return unless node.loc.expression.source =~ /^!\s+\w+/
21
22
 
22
- if node.loc.expression.source =~ /^!\s+\w+/
23
- # TODO: Improve source range to highlight the redundant whitespace.
24
- add_offense(node, :selector)
25
- end
23
+ # TODO: Improve source range to highlight the redundant whitespace.
24
+ add_offense(node, :selector)
26
25
  end
27
26
 
28
27
  def autocorrect(node)
@@ -15,10 +15,10 @@ module Rubocop
15
15
  end
16
16
 
17
17
  def on_if(node)
18
- if node.loc.respond_to?(:question)
19
- check_operator(node.loc.question)
20
- check_operator(node.loc.colon)
21
- end
18
+ return unless node.loc.respond_to?(:question)
19
+
20
+ check_operator(node.loc.question)
21
+ check_operator(node.loc.colon)
22
22
  end
23
23
 
24
24
  def on_resbody(node)
@@ -42,6 +42,7 @@ module Rubocop
42
42
 
43
43
  def unary_operation?(node)
44
44
  whole, selector = node.loc.expression, node.loc.selector
45
+ return unless selector
45
46
  operator?(selector) && whole.begin_pos == selector.begin_pos
46
47
  end
47
48
 
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ module Rubocop
4
+ module Cop
5
+ module Style
6
+ # This cop checks for missing space between a token and a comment on the
7
+ # same line.
8
+ class SpaceBeforeComment < Cop
9
+ MSG = 'Put a space before an end-of-line comment.'
10
+
11
+ def investigate(processed_source)
12
+ processed_source.tokens.each_cons(2) do |t1, t2|
13
+ next unless t2.type == :tCOMMENT
14
+ next unless t1.pos.line == t2.pos.line
15
+ add_offense(t2.pos, t2.pos) if t1.pos.end == t2.pos.begin
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def autocorrect(range)
22
+ @corrections << lambda do |corrector|
23
+ corrector.insert_before(range, ' ')
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -8,14 +8,13 @@ module Rubocop
8
8
  MSG = 'Put a space before the modifier keyword.'
9
9
 
10
10
  def on_if(node)
11
- if modifier?(node)
12
- kw = node.loc.keyword
13
- b = kw.begin_pos
14
- left_of_kw = Parser::Source::Range.new(kw.source_buffer, b - 1, b)
15
- add_offense(node, left_of_kw) unless left_of_kw.is?(' ')
16
- end
17
- end
11
+ return unless modifier?(node)
18
12
 
13
+ kw = node.loc.keyword
14
+ b = kw.begin_pos
15
+ left_of_kw = Parser::Source::Range.new(kw.source_buffer, b - 1, b)
16
+ add_offense(node, left_of_kw) unless left_of_kw.is?(' ')
17
+ end
19
18
  alias_method :on_while, :on_if
20
19
  alias_method :on_until, :on_if
21
20
 
@@ -14,9 +14,8 @@ module Rubocop
14
14
 
15
15
  def on_array(node)
16
16
  # %i and %I were introduced in Ruby 2.0
17
- unless RUBY_VERSION < '2.0.0'
18
- add_offense(node, :expression) if array_of?(:sym, node)
19
- end
17
+ return if RUBY_VERSION < '2.0.0'
18
+ add_offense(node, :expression) if array_of?(:sym, node)
20
19
  end
21
20
  end
22
21
  end
@@ -10,14 +10,14 @@ module Rubocop
10
10
  def investigate(processed_source)
11
11
  processed_source.lines.each_with_index do |line, index|
12
12
  match = line.match(/^( *)\t/)
13
- if match
14
- spaces = match.captures[0]
15
- add_offense(nil,
16
- source_range(processed_source.buffer,
17
- processed_source[0...index],
18
- spaces.length, 1),
19
- MSG)
20
- end
13
+ next unless match
14
+
15
+ spaces = match.captures[0]
16
+ add_offense(nil,
17
+ source_range(processed_source.buffer,
18
+ processed_source[0...index],
19
+ spaces.length, 1),
20
+ MSG)
21
21
  end
22
22
  end
23
23
  end
@@ -16,16 +16,16 @@ module Rubocop
16
16
  blank_lines = whitespace_at_end.count("\n") - 1
17
17
  wanted_blank_lines = style == :final_newline ? 0 : 1
18
18
 
19
- if blank_lines != wanted_blank_lines
20
- begin_pos = sb.source.length - whitespace_at_end.length
21
- autocorrect_range = Parser::Source::Range.new(sb, begin_pos,
22
- sb.source.length)
23
- begin_pos += "\n".length unless whitespace_at_end.length == 0
24
- report_range = Parser::Source::Range.new(sb, begin_pos,
25
- sb.source.length)
26
- add_offense(autocorrect_range, report_range,
27
- message(wanted_blank_lines, blank_lines))
28
- end
19
+ return unless blank_lines != wanted_blank_lines
20
+
21
+ begin_pos = sb.source.length - whitespace_at_end.length
22
+ autocorrect_range = Parser::Source::Range.new(sb, begin_pos,
23
+ sb.source.length)
24
+ begin_pos += "\n".length unless whitespace_at_end.length == 0
25
+ report_range = Parser::Source::Range.new(sb, begin_pos,
26
+ sb.source.length)
27
+ add_offense(autocorrect_range, report_range,
28
+ message(wanted_blank_lines, blank_lines))
29
29
  end
30
30
 
31
31
  private
@@ -9,13 +9,12 @@ module Rubocop
9
9
 
10
10
  def investigate(processed_source)
11
11
  processed_source.lines.each_with_index do |line, index|
12
- if line =~ /.*[ \t]+$/
13
- range = source_range(processed_source.buffer,
14
- processed_source[0...index],
15
- line.rstrip.length,
16
- line.length - line.rstrip.length)
17
- add_offense(range, range)
18
- end
12
+ next unless line =~ /.*[ \t]+$/
13
+ range = source_range(processed_source.buffer,
14
+ processed_source[0...index],
15
+ line.rstrip.length,
16
+ line.length - line.rstrip.length)
17
+ add_offense(range, range)
19
18
  end
20
19
  end
21
20