rubocop 0.81.0 → 0.82.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +59 -13
  4. data/lib/rubocop.rb +4 -2
  5. data/lib/rubocop/ast/node.rb +1 -1
  6. data/lib/rubocop/ast/node/array_node.rb +13 -0
  7. data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
  8. data/lib/rubocop/cli.rb +10 -4
  9. data/lib/rubocop/config.rb +9 -1
  10. data/lib/rubocop/config_loader.rb +24 -15
  11. data/lib/rubocop/config_loader_resolver.rb +1 -1
  12. data/lib/rubocop/config_obsoletion.rb +1 -0
  13. data/lib/rubocop/config_validator.rb +16 -0
  14. data/lib/rubocop/cop/badge.rb +5 -5
  15. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
  16. data/lib/rubocop/cop/corrector.rb +48 -24
  17. data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
  18. data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
  19. data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
  20. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
  21. data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
  22. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  23. data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
  24. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
  25. data/lib/rubocop/cop/layout/dot_position.rb +1 -1
  26. data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
  27. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
  28. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
  29. data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
  30. data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
  31. data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
  32. data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
  33. data/lib/rubocop/cop/layout/line_length.rb +2 -2
  34. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  35. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
  36. data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
  37. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
  40. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
  41. data/lib/rubocop/cop/lint/boolean_symbol.rb +2 -2
  42. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  43. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  44. data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
  45. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  46. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
  47. data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
  48. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
  49. data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
  50. data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
  51. data/lib/rubocop/cop/lint/raise_exception.rb +42 -6
  52. data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
  53. data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
  54. data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
  55. data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
  56. data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
  57. data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
  58. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  59. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  60. data/lib/rubocop/cop/naming/constant_name.rb +2 -1
  61. data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
  62. data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
  63. data/lib/rubocop/cop/registry.rb +11 -4
  64. data/lib/rubocop/cop/style/alias.rb +4 -4
  65. data/lib/rubocop/cop/style/and_or.rb +5 -6
  66. data/lib/rubocop/cop/style/array_join.rb +1 -1
  67. data/lib/rubocop/cop/style/case_equality.rb +24 -1
  68. data/lib/rubocop/cop/style/character_literal.rb +2 -2
  69. data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
  70. data/lib/rubocop/cop/style/copyright.rb +1 -1
  71. data/lib/rubocop/cop/style/dir.rb +1 -1
  72. data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
  73. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
  74. data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
  75. data/lib/rubocop/cop/style/each_with_object.rb +3 -3
  76. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  77. data/lib/rubocop/cop/style/even_odd.rb +1 -1
  78. data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
  79. data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
  80. data/lib/rubocop/cop/style/format_string.rb +2 -2
  81. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -3
  82. data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
  83. data/lib/rubocop/cop/style/hash_transform_values.rb +0 -3
  84. data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
  85. data/lib/rubocop/cop/style/lambda.rb +2 -2
  86. data/lib/rubocop/cop/style/lambda_call.rb +2 -2
  87. data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
  88. data/lib/rubocop/cop/style/module_function.rb +2 -2
  89. data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
  90. data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
  91. data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
  92. data/lib/rubocop/cop/style/next.rb +2 -2
  93. data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
  94. data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
  95. data/lib/rubocop/cop/style/not.rb +1 -1
  96. data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
  97. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  98. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
  99. data/lib/rubocop/cop/style/one_line_conditional.rb +1 -1
  100. data/lib/rubocop/cop/style/or_assignment.rb +1 -1
  101. data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
  102. data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
  103. data/lib/rubocop/cop/style/proc.rb +1 -1
  104. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  105. data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
  106. data/lib/rubocop/cop/style/redundant_condition.rb +3 -4
  107. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  108. data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
  109. data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
  110. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
  111. data/lib/rubocop/cop/style/redundant_return.rb +5 -7
  112. data/lib/rubocop/cop/style/redundant_self.rb +1 -1
  113. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
  114. data/lib/rubocop/cop/style/return_nil.rb +1 -1
  115. data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
  116. data/lib/rubocop/cop/style/self_assignment.rb +1 -1
  117. data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
  118. data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
  119. data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
  120. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  121. data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
  122. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -2
  123. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +2 -1
  124. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +3 -0
  125. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  126. data/lib/rubocop/cop/style/unpack_first.rb +0 -4
  127. data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
  128. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  129. data/lib/rubocop/cop/style/word_array.rb +1 -1
  130. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
  131. data/lib/rubocop/formatter/formatter_set.rb +0 -1
  132. data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
  133. data/lib/rubocop/options.rb +7 -1
  134. data/lib/rubocop/processed_source.rb +0 -3
  135. data/lib/rubocop/rspec/cop_helper.rb +1 -1
  136. data/lib/rubocop/rspec/expect_offense.rb +1 -1
  137. data/lib/rubocop/rspec/shared_contexts.rb +0 -4
  138. data/lib/rubocop/runner.rb +1 -1
  139. data/lib/rubocop/target_ruby.rb +2 -2
  140. data/lib/rubocop/version.rb +1 -1
  141. metadata +7 -5
  142. data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  def autocorrect(node)
59
59
  lambda do |corrector|
60
- corrector.replace(node.source_range, corrected(node))
60
+ corrector.replace(node, corrected(node))
61
61
  end
62
62
  end
63
63
 
@@ -38,7 +38,7 @@ module RuboCop
38
38
  replacement_method = replacement_method(arg, method)
39
39
 
40
40
  correction = "#{base_number.source}.#{replacement_method}?"
41
- ->(corrector) { corrector.replace(node.source_range, correction) }
41
+ ->(corrector) { corrector.replace(node, correction) }
42
42
  end
43
43
  end
44
44
 
@@ -97,7 +97,7 @@ module RuboCop
97
97
  autocorrect_expand_path(corrector, current_path, default_dir)
98
98
  elsif (default_dir = pathname_parent_expand_path(node)) ||
99
99
  (default_dir = pathname_new_parent_expand_path(node))
100
- corrector.replace(default_dir.loc.expression, '__dir__')
100
+ corrector.replace(default_dir, '__dir__')
101
101
  remove_parent_method(corrector, default_dir)
102
102
  end
103
103
  end
@@ -145,8 +145,8 @@ module RuboCop
145
145
  else
146
146
  new_path = "'#{parent_path(stripped_current_path)}'"
147
147
 
148
- corrector.replace(current_path.loc.expression, new_path)
149
- corrector.replace(default_dir.loc.expression, '__dir__')
148
+ corrector.replace(current_path, new_path)
149
+ corrector.replace(default_dir, '__dir__')
150
150
  end
151
151
  end
152
152
 
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Style
6
+ # This cop enforces consistency when using exponential notation
7
+ # for numbers in the code (eg 1.2e4). Different styles are supported:
8
+ # - `scientific` which enforces a mantissa between 1 (inclusive)
9
+ # and 10 (exclusive).
10
+ # - `engineering` which enforces the exponent to be a multiple of 3
11
+ # and the mantissa to be between 0.1 (inclusive)
12
+ # and 10 (exclusive).
13
+ # - `integral` which enforces the mantissa to always be a whole number
14
+ # without trailing zeroes.
15
+ #
16
+ # @example EnforcedStyle: scientific (default)
17
+ # # Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
18
+ #
19
+ # # bad
20
+ # 10e6
21
+ # 0.3e4
22
+ # 11.7e5
23
+ # 3.14e0
24
+ #
25
+ # # good
26
+ # 1e7
27
+ # 3e3
28
+ # 1.17e6
29
+ # 3.14
30
+ #
31
+ # @example EnforcedStyle: engineering
32
+ # # Enforces using multiple of 3 exponents,
33
+ # # mantissa should be between 0.1 (inclusive) and 1000 (exclusive)
34
+ #
35
+ # # bad
36
+ # 3.2e7
37
+ # 0.1e5
38
+ # 12e5
39
+ # 1232e6
40
+ #
41
+ # # good
42
+ # 32e6
43
+ # 10e3
44
+ # 1.2e6
45
+ # 1.232e9
46
+ #
47
+ # @example EnforcedStyle: integral
48
+ # # Enforces the mantissa to have no decimal part and no
49
+ # # trailing zeroes.
50
+ #
51
+ # # bad
52
+ # 3.2e7
53
+ # 0.1e5
54
+ # 120e4
55
+ #
56
+ # # good
57
+ # 32e6
58
+ # 1e4
59
+ # 12e5
60
+ #
61
+ class ExponentialNotation < Cop
62
+ include ConfigurableEnforcedStyle
63
+
64
+ def on_float(node)
65
+ add_offense(node) if offense?(node)
66
+ end
67
+
68
+ private
69
+
70
+ def scientific?(node)
71
+ mantissa, = node.source.split('e')
72
+ mantissa =~ /^-?[1-9](\.\d*[0-9])?$/
73
+ end
74
+
75
+ def engineering?(node)
76
+ mantissa, exponent = node.source.split('e')
77
+ return false unless /^-?\d+$/.match?(exponent)
78
+ return false unless (exponent.to_i % 3).zero?
79
+ return false if /^-?\d{4}/.match?(mantissa)
80
+ return false if /^-?0\d/.match?(mantissa)
81
+ return false if /^-?0.0/.match?(mantissa)
82
+
83
+ true
84
+ end
85
+
86
+ def integral(node)
87
+ mantissa, = node.source.split('e')
88
+ mantissa =~ /^-?[1-9](\d*[1-9])?$/
89
+ end
90
+
91
+ def offense?(node)
92
+ return false unless node.source['e']
93
+
94
+ case style
95
+ when :scientific
96
+ !scientific?(node)
97
+ when :engineering
98
+ !engineering?(node)
99
+ when :integral
100
+ !integral(node)
101
+ else
102
+ false
103
+ end
104
+ end
105
+
106
+ def message(_node)
107
+ case style
108
+ when :scientific
109
+ 'Use a mantissa in [1, 10[.'
110
+ when :engineering
111
+ 'Use an exponent divisible by 3 and a mantissa in [0.1, 1000[.'
112
+ when :integral
113
+ 'Use an integer as mantissa, without trailing zero.'
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -104,7 +104,7 @@ module RuboCop
104
104
 
105
105
  corrected = "#{style}(#{node.receiver.source}, #{args})"
106
106
 
107
- corrector.replace(node.loc.expression, corrected)
107
+ corrector.replace(node, corrected)
108
108
  end
109
109
 
110
110
  def autocorrect_to_percent(corrector, node)
@@ -119,7 +119,7 @@ module RuboCop
119
119
  "[#{param_args.map(&:source).join(', ')}]"
120
120
  end
121
121
 
122
- corrector.replace(node.loc.expression, "#{format} % #{args}")
122
+ corrector.replace(node, "#{format} % #{args}")
123
123
  end
124
124
  end
125
125
  end
@@ -60,7 +60,7 @@ module RuboCop
60
60
  end
61
61
 
62
62
  def correct_implicit(node, corrector, method_name)
63
- corrector.replace(node.loc.expression, method_name)
63
+ corrector.replace(node, method_name)
64
64
  correct_args(node, corrector)
65
65
  end
66
66
 
@@ -70,14 +70,14 @@ module RuboCop
70
70
  return correct_implicit(node, corrector, name) unless receiver
71
71
 
72
72
  new_source = receiver.source + ".#{name}"
73
- corrector.replace(node.loc.expression, new_source)
73
+ corrector.replace(node, new_source)
74
74
  end
75
75
 
76
76
  def correct_args(node, corrector)
77
77
  args = node.parent.arguments
78
78
  name, = *args.children.find { |arg| used?(arg) }
79
79
 
80
- corrector.replace(args.source_range, "|#{name}|")
80
+ corrector.replace(args, "|#{name}|")
81
81
  end
82
82
 
83
83
  def kv_range(outer_node)
@@ -144,11 +144,11 @@ module RuboCop
144
144
  # Prefer { :production? => false } over { production?: false } and
145
145
  # similarly for other non-alnum final characters (except quotes,
146
146
  # to prefer { "x y": 1 } over { :"x y" => 1 }).
147
- return false unless sym_name =~ /[\p{Alnum}"']\z/
147
+ return false unless /[\p{Alnum}"']\z/.match?(sym_name)
148
148
  end
149
149
 
150
150
  # Most hash keys can be matched against a simple regex.
151
- return true if sym_name =~ /\A[_a-z]\w*[?!]?\z/i
151
+ return true if /\A[_a-z]\w*[?!]?\z/i.match?(sym_name)
152
152
 
153
153
  # For more complicated hash keys, let the parser validate the syntax.
154
154
  parse("{ #{sym_name}: :foo }").valid_syntax?
@@ -188,11 +188,9 @@ module RuboCop
188
188
  end
189
189
 
190
190
  def autocorrect_hash_rockets(corrector, pair_node)
191
- key = pair_node.key.source_range
192
191
  op = pair_node.loc.operator
193
192
 
194
- corrector.insert_after(key, pair_node.inverse_delimiter(true))
195
- corrector.insert_before(key, ':')
193
+ corrector.wrap(pair_node.key, ':', pair_node.inverse_delimiter(true))
196
194
  corrector.remove(range_with_surrounding_space(range: op))
197
195
  end
198
196
 
@@ -24,11 +24,8 @@ module RuboCop
24
24
  # {a: 1, b: 2}.transform_values { |v| foo(v) }
25
25
  # {a: 1, b: 2}.transform_values { |v| v * v }
26
26
  class HashTransformValues < Cop
27
- extend TargetRubyVersion
28
27
  include HashTransformMethod
29
28
 
30
- minimum_target_ruby_version 2.4
31
-
32
29
  def_node_matcher :on_bad_each_with_object, <<~PATTERN
33
30
  (block
34
31
  ({send csend}
@@ -9,7 +9,7 @@ module RuboCop
9
9
  #
10
10
  # The maximum line length is configured in the `Layout/LineLength`
11
11
  # cop. The tab size is configured in the `IndentationWidth` of the
12
- # `Layout/Tab` cop.
12
+ # `Layout/IndentationStyle` cop.
13
13
  #
14
14
  # @example
15
15
  # # bad
@@ -47,7 +47,7 @@ module RuboCop
47
47
  def on_if(node)
48
48
  msg = if eligible_node?(node)
49
49
  MSG_USE_MODIFIER unless named_capture_in_condition?(node)
50
- elsif node.modifier_form? && too_long_single_line?(node)
50
+ elsif too_long_due_to_modifier?(node)
51
51
  MSG_USE_NORMAL
52
52
  end
53
53
  return unless msg
@@ -63,11 +63,16 @@ module RuboCop
63
63
  else
64
64
  to_modifier_form(node)
65
65
  end
66
- ->(corrector) { corrector.replace(node.source_range, replacement) }
66
+ ->(corrector) { corrector.replace(node, replacement) }
67
67
  end
68
68
 
69
69
  private
70
70
 
71
+ def too_long_due_to_modifier?(node)
72
+ node.modifier_form? && too_long_single_line?(node) &&
73
+ !another_statement_on_same_line?(node)
74
+ end
75
+
71
76
  def ignored_patterns
72
77
  config.for_cop('Layout/LineLength')['IgnoredPatterns'] || []
73
78
  end
@@ -129,6 +134,21 @@ module RuboCop
129
134
  node.ternary? || node.modifier_form? || node.elsif? || node.else?
130
135
  end
131
136
 
137
+ def another_statement_on_same_line?(node)
138
+ line_no = node.source_range.last_line
139
+
140
+ # traverse the AST upwards until we find a 'begin' node
141
+ # we want to look at the following child and see if it is on the
142
+ # same line as this 'if' node
143
+ while node && !node.begin_type?
144
+ index = node.sibling_index
145
+ node = node.parent
146
+ end
147
+
148
+ node && (sibling = node.children[index + 1]) &&
149
+ sibling.source_range.first_line == line_no
150
+ end
151
+
132
152
  def parenthesize?(node)
133
153
  # Parenthesize corrected expression if changing to modifier-if form
134
154
  # would change the meaning of the parent expression
@@ -109,13 +109,13 @@ module RuboCop
109
109
  end
110
110
 
111
111
  def autocorrect_method_to_literal(corrector, node)
112
- corrector.replace(node.send_node.source_range, '->')
112
+ corrector.replace(node.send_node, '->')
113
113
 
114
114
  return unless node.arguments?
115
115
 
116
116
  arg_str = "(#{lambda_arg_string(node.arguments)})"
117
117
 
118
- corrector.insert_after(node.send_node.source_range, arg_str)
118
+ corrector.insert_after(node.send_node, arg_str)
119
119
  corrector.remove(arguments_with_whitespace(node))
120
120
  end
121
121
 
@@ -37,7 +37,7 @@ module RuboCop
37
37
  receiver = node.receiver.source
38
38
  replacement = node.source.sub("#{receiver}.", "#{receiver}.call")
39
39
 
40
- corrector.replace(node.source_range, replacement)
40
+ corrector.replace(node, replacement)
41
41
  else
42
42
  add_parentheses(node, corrector) unless node.parenthesized?
43
43
  corrector.remove(node.loc.selector)
@@ -54,7 +54,7 @@ module RuboCop
54
54
 
55
55
  def add_parentheses(node, corrector)
56
56
  if node.arguments.empty?
57
- corrector.insert_after(node.source_range, '()')
57
+ corrector.insert_after(node, '()')
58
58
  else
59
59
  corrector.replace(args_begin(node), '(')
60
60
  corrector.insert_after(args_end(node), ')')
@@ -72,7 +72,7 @@ module RuboCop
72
72
  between = prev_mixin.loc.expression.end
73
73
  .join(range.begin)
74
74
  # if separated from previous mixin with only whitespace?
75
- if between.source !~ /\S/
75
+ unless /\S/.match?(between.source)
76
76
  range = range.join(between) # then remove that too
77
77
  end
78
78
  range
@@ -98,9 +98,9 @@ module RuboCop
98
98
 
99
99
  lambda do |corrector|
100
100
  if extend_self_node?(node)
101
- corrector.replace(node.source_range, 'module_function')
101
+ corrector.replace(node, 'module_function')
102
102
  else
103
- corrector.replace(node.source_range, 'extend self')
103
+ corrector.replace(node, 'extend self')
104
104
  end
105
105
  end
106
106
  end
@@ -29,7 +29,7 @@ module RuboCop
29
29
 
30
30
  def autocorrect(node)
31
31
  lambda do |corrector|
32
- corrector.replace(node.source_range, to_normal_if(node))
32
+ corrector.replace(node, to_normal_if(node))
33
33
  end
34
34
  end
35
35
 
@@ -82,7 +82,7 @@ module RuboCop
82
82
  end
83
83
 
84
84
  def keyword_end_str(node, node_buf)
85
- if node_buf.source_line(node.loc.end.line) =~ /[^\s\)]/
85
+ if /[^\s\)]/.match?(node_buf.source_line(node.loc.end.line))
86
86
  "\n" + (' ' * node.loc.column) + 'end'
87
87
  else
88
88
  'end'
@@ -77,11 +77,9 @@ module RuboCop
77
77
  if splat_value
78
78
  correct_splat_expansion(corrector, expr, splat_value)
79
79
  elsif node.array_type? && !node.bracketed?
80
- corrector.insert_before(expr, '[')
81
- corrector.insert_after(expr, ']')
80
+ corrector.wrap(expr, '[', ']')
82
81
  elsif requires_parentheses?(node)
83
- corrector.insert_before(expr, '(')
84
- corrector.insert_after(expr, ')')
82
+ corrector.wrap(expr, '(', ')')
85
83
  end
86
84
 
87
85
  corrector.insert_after(expr, '.freeze')
@@ -152,13 +152,13 @@ module RuboCop
152
152
  "next #{node.inverse_keyword} #{node.condition.source}\n" \
153
153
  "#{' ' * node.source_range.column}#{body.source}"
154
154
 
155
- corrector.replace(node.source_range, replacement)
155
+ corrector.replace(node, replacement)
156
156
  end
157
157
 
158
158
  def autocorrect_block(corrector, node)
159
159
  next_code = "next #{node.inverse_keyword} #{node.condition.source}"
160
160
 
161
- corrector.insert_before(node.source_range, next_code)
161
+ corrector.insert_before(node, next_code)
162
162
 
163
163
  corrector.remove(cond_range(node, node.condition))
164
164
  corrector.remove(end_range(node))
@@ -49,7 +49,7 @@ module RuboCop
49
49
  else
50
50
  node.source.sub(/\s*={2,3}\s*nil/, '.nil?')
51
51
  end
52
- ->(corrector) { corrector.replace(node.source_range, new_code) }
52
+ ->(corrector) { corrector.replace(node, new_code) }
53
53
  end
54
54
 
55
55
  private
@@ -111,15 +111,15 @@ module RuboCop
111
111
 
112
112
  return if expr == new_code
113
113
 
114
- ->(corrector) { corrector.replace(node.source_range, new_code) }
114
+ ->(corrector) { corrector.replace(node, new_code) }
115
115
  end
116
116
 
117
117
  def autocorrect_non_nil(node, inner_node)
118
118
  lambda do |corrector|
119
119
  if inner_node.receiver
120
- corrector.replace(node.source_range, inner_node.receiver.source)
120
+ corrector.replace(node, inner_node.receiver.source)
121
121
  else
122
- corrector.replace(node.source_range, 'self')
122
+ corrector.replace(node, 'self')
123
123
  end
124
124
  end
125
125
  end
@@ -127,7 +127,7 @@ module RuboCop
127
127
  def autocorrect_unless_nil(node, receiver)
128
128
  lambda do |corrector|
129
129
  corrector.replace(node.parent.loc.keyword, 'if')
130
- corrector.replace(node.source_range, receiver.source)
130
+ corrector.replace(node, receiver.source)
131
131
  end
132
132
  end
133
133
  end