rubocop 0.75.0 → 0.76.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +53 -54
  4. data/lib/rubocop.rb +10 -9
  5. data/lib/rubocop/ast/builder.rb +1 -0
  6. data/lib/rubocop/ast/node.rb +4 -0
  7. data/lib/rubocop/ast/node/return_node.rb +24 -0
  8. data/lib/rubocop/cli.rb +7 -4
  9. data/lib/rubocop/comment_config.rb +2 -2
  10. data/lib/rubocop/config.rb +7 -0
  11. data/lib/rubocop/config_loader.rb +1 -1
  12. data/lib/rubocop/config_loader_resolver.rb +2 -1
  13. data/lib/rubocop/config_obsoletion.rb +9 -0
  14. data/lib/rubocop/config_validator.rb +24 -15
  15. data/lib/rubocop/cop/commissioner.rb +15 -7
  16. data/lib/rubocop/cop/cop.rb +10 -6
  17. data/lib/rubocop/cop/corrector.rb +8 -7
  18. data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
  19. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  20. data/lib/rubocop/cop/layout/align_hash.rb +6 -2
  21. data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
  22. data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
  23. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
  24. data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
  25. data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
  26. data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
  27. data/lib/rubocop/cop/layout/indent_assignment.rb +2 -1
  28. data/lib/rubocop/cop/layout/indent_first_argument.rb +9 -7
  29. data/lib/rubocop/cop/layout/indent_first_hash_element.rb +1 -1
  30. data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
  31. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
  32. data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
  33. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +7 -4
  34. data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
  35. data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
  36. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -8
  37. data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
  38. data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +23 -23
  39. data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +6 -8
  40. data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
  41. data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +5 -5
  42. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
  43. data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
  44. data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
  45. data/lib/rubocop/cop/lint/void.rb +7 -26
  46. data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
  47. data/lib/rubocop/cop/metrics/line_length.rb +1 -4
  48. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
  49. data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
  50. data/lib/rubocop/cop/mixin/statement_modifier.rb +5 -2
  51. data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -6
  52. data/lib/rubocop/cop/naming/file_name.rb +12 -5
  53. data/lib/rubocop/cop/registry.rb +1 -1
  54. data/lib/rubocop/cop/style/attr.rb +2 -2
  55. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +6 -6
  56. data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
  57. data/lib/rubocop/cop/style/copyright.rb +11 -7
  58. data/lib/rubocop/cop/style/documentation_method.rb +44 -0
  59. data/lib/rubocop/cop/style/double_cop_disable_directive.rb +2 -2
  60. data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
  61. data/lib/rubocop/cop/style/empty_literal.rb +2 -2
  62. data/lib/rubocop/cop/style/empty_method.rb +5 -5
  63. data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
  64. data/lib/rubocop/cop/style/format_string.rb +10 -7
  65. data/lib/rubocop/cop/style/format_string_token.rb +15 -34
  66. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +10 -0
  67. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  68. data/lib/rubocop/cop/style/if_unless_modifier.rb +9 -2
  69. data/lib/rubocop/cop/style/infinite_loop.rb +4 -3
  70. data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
  71. data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
  72. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +20 -20
  73. data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
  74. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
  75. data/lib/rubocop/cop/style/nested_modifier.rb +4 -2
  76. data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
  77. data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
  78. data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
  79. data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
  80. data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
  81. data/lib/rubocop/cop/style/redundant_return.rb +37 -21
  82. data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +4 -4
  83. data/lib/rubocop/cop/style/safe_navigation.rb +19 -8
  84. data/lib/rubocop/cop/style/semicolon.rb +13 -2
  85. data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
  86. data/lib/rubocop/cop/util.rb +1 -1
  87. data/lib/rubocop/cop/utils/format_string.rb +10 -18
  88. data/lib/rubocop/cop/variable_force.rb +7 -5
  89. data/lib/rubocop/formatter/clang_style_formatter.rb +8 -3
  90. data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -12
  91. data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
  92. data/lib/rubocop/formatter/formatter_set.rb +16 -16
  93. data/lib/rubocop/formatter/pacman_formatter.rb +3 -3
  94. data/lib/rubocop/formatter/simple_text_formatter.rb +7 -3
  95. data/lib/rubocop/formatter/tap_formatter.rb +8 -3
  96. data/lib/rubocop/node_pattern.rb +3 -1
  97. data/lib/rubocop/options.rb +16 -22
  98. data/lib/rubocop/result_cache.rb +1 -1
  99. data/lib/rubocop/runner.rb +32 -27
  100. data/lib/rubocop/target_finder.rb +12 -6
  101. data/lib/rubocop/version.rb +1 -1
  102. metadata +12 -11
@@ -73,11 +73,11 @@ module RuboCop
73
73
  end
74
74
 
75
75
  def corrected(node)
76
- has_parentheses = parentheses?(node.arguments)
77
-
78
- arguments = node.arguments? ? node.arguments.source : ''
79
- extra_space = node.arguments? && !has_parentheses ? ' ' : ''
80
- scope = node.receiver ? "#{node.receiver.source}." : ''
76
+ if node.arguments?
77
+ arguments = node.arguments.source
78
+ extra_space = ' ' unless parentheses?(node.arguments)
79
+ end
80
+ scope = node.receiver ? "#{node.receiver.source}." : ''
81
81
 
82
82
  signature = [scope, node.method_name, extra_space, arguments].join
83
83
 
@@ -160,7 +160,7 @@ module RuboCop
160
160
  def depth(current_path)
161
161
  paths = current_path.split(File::SEPARATOR)
162
162
 
163
- paths.reject { |path| path == '.' }.count
163
+ paths.count { |path| path != '.' }
164
164
  end
165
165
 
166
166
  def parent_path(current_path)
@@ -94,10 +94,12 @@ module RuboCop
94
94
  private
95
95
 
96
96
  def autocorrect_from_percent(corrector, node)
97
- args = if %i[array hash].include?(node.first_argument.type)
98
- node.first_argument.children.map(&:source).join(', ')
97
+ percent_rhs = node.first_argument
98
+ args = case percent_rhs.type
99
+ when :array, :hash
100
+ percent_rhs.children.map(&:source).join(', ')
99
101
  else
100
- node.first_argument.source
102
+ percent_rhs.source
101
103
  end
102
104
 
103
105
  corrected = "#{style}(#{node.receiver.source}, #{args})"
@@ -106,14 +108,15 @@ module RuboCop
106
108
  end
107
109
 
108
110
  def autocorrect_to_percent(corrector, node)
109
- format = node.first_argument.source
111
+ format_arg, *param_args = node.arguments
112
+ format = format_arg.source
110
113
 
111
- args = if node.arguments.size == 2
112
- arg = node.arguments.last
114
+ args = if param_args.one?
115
+ arg = param_args.last
113
116
 
114
117
  arg.hash_type? ? "{ #{arg.source} }" : arg.source
115
118
  else
116
- "[#{node.arguments[1..-1].map(&:source).join(', ')}]"
119
+ "[#{param_args.map(&:source).join(', ')}]"
117
120
  end
118
121
 
119
122
  corrector.replace(node.loc.expression, "#{format} % #{args}")
@@ -8,7 +8,7 @@ module RuboCop
8
8
  # **Note:**
9
9
  # `unannotated` style cop only works for strings
10
10
  # which are passed as arguments to those methods:
11
- # `sprintf`, `format`, `%`.
11
+ # `printf`, `sprintf`, `format`, `%`.
12
12
  # The reason is that *unannotated* format is very similar
13
13
  # to encoded URLs or Date/Time formatting strings.
14
14
  #
@@ -41,10 +41,7 @@ module RuboCop
41
41
  class FormatStringToken < Cop
42
42
  include ConfigurableEnforcedStyle
43
43
 
44
- FORMAT_STRING_METHODS = %i[sprintf format %].freeze
45
-
46
44
  def on_str(node)
47
- return if placeholder_argument?(node)
48
45
  return if node.each_ancestor(:xstr, :regexp).any?
49
46
 
50
47
  tokens(node) do |detected_style, token_range|
@@ -61,14 +58,16 @@ module RuboCop
61
58
 
62
59
  private
63
60
 
64
- def includes_format_methods?(node)
65
- node.each_ancestor(:send).any? do |ancestor|
66
- FORMAT_STRING_METHODS.include?(ancestor.method_name)
67
- end
68
- end
61
+ def_node_matcher :format_string_in_typical_context?, <<~PATTERN
62
+ {
63
+ ^(send _ {:format :sprintf :printf} %0 ...)
64
+ ^(send %0 :% _)
65
+ }
66
+ PATTERN
69
67
 
70
68
  def unannotated_format?(node, detected_style)
71
- detected_style == :unannotated && !includes_format_methods?(node)
69
+ detected_style == :unannotated &&
70
+ !format_string_in_typical_context?(node)
72
71
  end
73
72
 
74
73
  def message(detected_style)
@@ -95,11 +94,7 @@ module RuboCop
95
94
  if source_map.is_a?(Parser::Source::Map::Heredoc)
96
95
  source_map.heredoc_body
97
96
  elsif source_map.begin
98
- slice_source(
99
- source_map.expression,
100
- source_map.expression.begin_pos + 1,
101
- source_map.expression.end_pos - 1
102
- )
97
+ source_map.expression.adjust(begin_pos: +1, end_pos: -1)
103
98
  else
104
99
  source_map.expression
105
100
  end
@@ -109,31 +104,17 @@ module RuboCop
109
104
  format_string = RuboCop::Cop::Utils::FormatString.new(contents.source)
110
105
 
111
106
  format_string.format_sequences.each do |seq|
107
+ next if seq.percent?
108
+
112
109
  detected_style = seq.style
113
- token = slice_source(
114
- contents,
115
- contents.begin_pos + seq.begin_pos,
116
- contents.begin_pos + seq.end_pos
110
+ token = contents.begin.adjust(
111
+ begin_pos: seq.begin_pos,
112
+ end_pos: seq.end_pos
117
113
  )
118
114
 
119
115
  yield(detected_style, token)
120
116
  end
121
117
  end
122
-
123
- def slice_source(source_range, new_begin, new_end)
124
- Parser::Source::Range.new(
125
- source_range.source_buffer,
126
- new_begin,
127
- new_end
128
- )
129
- end
130
-
131
- def placeholder_argument?(node)
132
- return false unless node.parent
133
- return true if node.parent.pair_type?
134
-
135
- placeholder_argument?(node.parent)
136
- end
137
118
  end
138
119
  end
139
120
  end
@@ -11,6 +11,9 @@ module RuboCop
11
11
  # encoding comment. The frozen string literal comment is only valid in
12
12
  # Ruby 2.3+.
13
13
  #
14
+ # Note that the cop will ignore files where the comment exists but is set
15
+ # to `false` instead of `true`.
16
+ #
14
17
  # @example EnforcedStyle: always (default)
15
18
  # # The `always` style will always add the frozen string literal comment
16
19
  # # to a file, regardless of the Ruby version or if `freeze` or `<<` are
@@ -27,6 +30,13 @@ module RuboCop
27
30
  # # ...
28
31
  # end
29
32
  #
33
+ # # good
34
+ # # frozen_string_literal: false
35
+ #
36
+ # module Bar
37
+ # # ...
38
+ # end
39
+ #
30
40
  # @example EnforcedStyle: never
31
41
  # # The `never` will enforce that the frozen string literal comment does
32
42
  # # not exist in a file.
@@ -168,10 +168,10 @@ module RuboCop
168
168
  end
169
169
 
170
170
  def autocorrect_ruby19(corrector, pair_node)
171
- key = pair_node.key
171
+ key = pair_node.key.source_range
172
172
  op = pair_node.loc.operator
173
173
 
174
- range = range_between(key.source_range.begin_pos, op.end_pos)
174
+ range = key.join(op)
175
175
  range = range_with_surrounding_space(range: range, side: :right)
176
176
 
177
177
  space = argument_without_space?(pair_node.parent) ? ' ' : ''
@@ -70,8 +70,15 @@ module RuboCop
70
70
  return false unless max_line_length
71
71
 
72
72
  range = node.source_range
73
- range.first_line == range.last_line &&
74
- range.last_column > max_line_length
73
+ return false unless range.first_line == range.last_line
74
+ return false unless line_length_enabled_at_line?(range.first_line)
75
+
76
+ range.last_column > max_line_length
77
+ end
78
+
79
+ def line_length_enabled_at_line?(line)
80
+ processed_source.comment_config
81
+ .cop_enabled_at_line?('Metrics/LineLength', line)
75
82
  end
76
83
 
77
84
  def named_capture_in_condition?(node)
@@ -97,12 +97,13 @@ module RuboCop
97
97
  end
98
98
 
99
99
  def modifier_replacement(node)
100
+ body = node.body
100
101
  if node.single_line?
101
- 'loop { ' + node.body.source + ' }'
102
+ 'loop { ' + body.source + ' }'
102
103
  else
103
- indentation = node.body.loc.expression.source_line[LEADING_SPACE]
104
+ indentation = body.source_range.source_line[LEADING_SPACE]
104
105
 
105
- ['loop do', node.body.source.gsub(/^/, configured_indent),
106
+ ['loop do', body.source.gsub(/^/, configured_indent),
106
107
  'end'].join("\n#{indentation}")
107
108
  end
108
109
  end
@@ -94,20 +94,25 @@ module RuboCop
94
94
  end
95
95
 
96
96
  def autocorrect(node)
97
+ if node.block_type?
98
+ correct_inverse_block(node)
99
+ elsif node.send_type?
100
+ correct_inverse_method(node)
101
+ end
102
+ end
103
+
104
+ def correct_inverse_method(node)
97
105
  method_call, _lhs, method, _rhs = inverse_candidate?(node)
106
+ return unless method_call && method
98
107
 
99
- if method_call && method
100
- lambda do |corrector|
101
- corrector.remove(not_to_receiver(node, method_call))
102
- corrector.replace(method_call.loc.selector,
103
- inverse_methods[method].to_s)
108
+ lambda do |corrector|
109
+ corrector.remove(not_to_receiver(node, method_call))
110
+ corrector.replace(method_call.loc.selector,
111
+ inverse_methods[method].to_s)
104
112
 
105
- if EQUALITY_METHODS.include?(method)
106
- corrector.remove(end_parentheses(node, method_call))
107
- end
113
+ if EQUALITY_METHODS.include?(method)
114
+ corrector.remove(end_parentheses(node, method_call))
108
115
  end
109
- else
110
- correct_inverse_block(node)
111
116
  end
112
117
  end
113
118
 
@@ -122,18 +127,19 @@ module RuboCop
122
127
  end
123
128
 
124
129
  def correct_inverse_selector(block, corrector)
125
- selector = block.loc.selector.source
130
+ selector_loc = block.loc.selector
131
+ selector = selector_loc.source
126
132
 
127
133
  if NEGATED_EQUALITY_METHODS.include?(selector.to_sym)
128
134
  selector[0] = '='
129
- corrector.replace(block.loc.selector, selector)
135
+ corrector.replace(selector_loc, selector)
130
136
  else
131
137
  if block.loc.dot
132
138
  range = dot_range(block.loc)
133
139
  corrector.remove(range)
134
140
  end
135
141
 
136
- corrector.remove(block.loc.selector)
142
+ corrector.remove(selector_loc)
137
143
  end
138
144
  end
139
145
 
@@ -26,13 +26,14 @@ module RuboCop
26
26
  'those strings.'
27
27
  CONCAT_TOKEN_TYPES = %i[tPLUS tLSHFT].freeze
28
28
  SIMPLE_STRING_TOKEN_TYPE = :tSTRING
29
- COMPLEX_STRING_EDGE_TOKEN_TYPES = %i[tSTRING_BEG tSTRING_END].freeze
29
+ COMPLEX_STRING_BEGIN_TOKEN = :tSTRING_BEG
30
+ COMPLEX_STRING_END_TOKEN = :tSTRING_END
30
31
  HIGH_PRECEDENCE_OP_TOKEN_TYPES = %i[tSTAR2 tPERCENT tDOT
31
32
  tLBRACK2].freeze
32
33
  QUOTE_DELIMITERS = %w[' "].freeze
33
34
 
34
35
  def self.autocorrect_incompatible_with
35
- [Style::UnneededInterpolation]
36
+ [Style::RedundantInterpolation]
36
37
  end
37
38
 
38
39
  def investigate(processed_source)
@@ -59,9 +60,7 @@ module RuboCop
59
60
  def check_token_set(index)
60
61
  predecessor, operator, successor = processed_source.tokens[index, 3]
61
62
 
62
- return unless eligible_successor?(successor) &&
63
- eligible_operator?(operator) &&
64
- eligible_predecessor?(predecessor)
63
+ return unless eligible_token_set?(predecessor, operator, successor)
65
64
 
66
65
  return if operator.line == successor.line
67
66
 
@@ -72,6 +71,12 @@ module RuboCop
72
71
  add_offense(operator.pos, location: operator.pos)
73
72
  end
74
73
 
74
+ def eligible_token_set?(predecessor, operator, successor)
75
+ eligible_successor?(successor) &&
76
+ eligible_operator?(operator) &&
77
+ eligible_predecessor?(predecessor)
78
+ end
79
+
75
80
  def eligible_successor?(successor)
76
81
  successor && standard_string_literal?(successor)
77
82
  end
@@ -91,13 +96,12 @@ module RuboCop
91
96
 
92
97
  def token_after_last_string(successor, base_index)
93
98
  index = base_index + 3
94
- begin_token, end_token = COMPLEX_STRING_EDGE_TOKEN_TYPES
95
- if successor.type == begin_token
99
+ if successor.type == COMPLEX_STRING_BEGIN_TOKEN
96
100
  ends_to_find = 1
97
101
  while ends_to_find.positive?
98
102
  case processed_source.tokens[index].type
99
- when begin_token then ends_to_find += 1
100
- when end_token then ends_to_find -= 1
103
+ when COMPLEX_STRING_BEGIN_TOKEN then ends_to_find += 1
104
+ when COMPLEX_STRING_END_TOKEN then ends_to_find -= 1
101
105
  end
102
106
  index += 1
103
107
  end
@@ -109,7 +113,7 @@ module RuboCop
109
113
  case token.type
110
114
  when SIMPLE_STRING_TOKEN_TYPE
111
115
  true
112
- when *COMPLEX_STRING_EDGE_TOKEN_TYPES
116
+ when COMPLEX_STRING_BEGIN_TOKEN, COMPLEX_STRING_END_TOKEN
113
117
  QUOTE_DELIMITERS.include?(token.text)
114
118
  else
115
119
  false
@@ -64,20 +64,6 @@ module RuboCop
64
64
  # # okay with `^assert` listed in `IgnoredPatterns`
65
65
  # assert_equal 'test', x
66
66
  #
67
- # # IgnoreMacros: true (default)
68
- #
69
- # # good
70
- # class Foo
71
- # bar :baz
72
- # end
73
- #
74
- # # IgnoreMacros: false
75
- #
76
- # # bad
77
- # class Foo
78
- # bar :baz
79
- # end
80
- #
81
67
  # @example EnforcedStyle: omit_parentheses
82
68
  #
83
69
  # # bad
@@ -92,7 +78,21 @@ module RuboCop
92
78
  # # good
93
79
  # foo.enforce strict: true
94
80
  #
95
- # # AllowParenthesesInMultilineCall: false (default)
81
+ # @example IgnoreMacros: true (default)
82
+ #
83
+ # # good
84
+ # class Foo
85
+ # bar :baz
86
+ # end
87
+ #
88
+ # @example IgnoreMacros: false
89
+ #
90
+ # # bad
91
+ # class Foo
92
+ # bar :baz
93
+ # end
94
+ #
95
+ # @example AllowParenthesesInMultilineCall: false (default)
96
96
  #
97
97
  # # bad
98
98
  # foo.enforce(
@@ -103,7 +103,7 @@ module RuboCop
103
103
  # foo.enforce \
104
104
  # strict: true
105
105
  #
106
- # # AllowParenthesesInMultilineCall: true
106
+ # @example AllowParenthesesInMultilineCall: true
107
107
  #
108
108
  # # good
109
109
  # foo.enforce(
@@ -114,7 +114,7 @@ module RuboCop
114
114
  # foo.enforce \
115
115
  # strict: true
116
116
  #
117
- # # AllowParenthesesInChaining: false (default)
117
+ # @example AllowParenthesesInChaining: false (default)
118
118
  #
119
119
  # # bad
120
120
  # foo().bar(1)
@@ -122,7 +122,7 @@ module RuboCop
122
122
  # # good
123
123
  # foo().bar 1
124
124
  #
125
- # # AllowParenthesesInChaining: true
125
+ # @example AllowParenthesesInChaining: true
126
126
  #
127
127
  # # good
128
128
  # foo().bar(1)
@@ -130,7 +130,7 @@ module RuboCop
130
130
  # # good
131
131
  # foo().bar 1
132
132
  #
133
- # # AllowParenthesesInCamelCaseMethod: false (default)
133
+ # @example AllowParenthesesInCamelCaseMethod: false (default)
134
134
  #
135
135
  # # bad
136
136
  # Array(1)
@@ -138,7 +138,7 @@ module RuboCop
138
138
  # # good
139
139
  # Array 1
140
140
  #
141
- # # AllowParenthesesInCamelCaseMethod: true
141
+ # @example AllowParenthesesInCamelCaseMethod: true
142
142
  #
143
143
  # # good
144
144
  # Array(1)
@@ -113,22 +113,30 @@ module RuboCop
113
113
  lambda do |corrector|
114
114
  if node.args_type?
115
115
  # offense is registered on args node when parentheses are unwanted
116
- corrector.replace(node.loc.begin, ' ')
117
- corrector.remove(node.loc.end)
116
+ correct_arguments(node, corrector)
118
117
  else
119
- args_expr = node.arguments.source_range
120
- args_with_space = range_with_surrounding_space(range: args_expr,
121
- side: :left)
122
- just_space = range_between(args_with_space.begin_pos,
123
- args_expr.begin_pos)
124
- corrector.replace(just_space, '(')
125
- corrector.insert_after(args_expr, ')')
118
+ correct_definition(node, corrector)
126
119
  end
127
120
  end
128
121
  end
129
122
 
130
123
  private
131
124
 
125
+ def correct_arguments(arg_node, corrector)
126
+ corrector.replace(arg_node.loc.begin, ' ')
127
+ corrector.remove(arg_node.loc.end)
128
+ end
129
+
130
+ def correct_definition(def_node, corrector)
131
+ arguments_range = def_node.arguments.source_range
132
+ args_with_space = range_with_surrounding_space(range: arguments_range,
133
+ side: :left)
134
+ leading_space = range_between(args_with_space.begin_pos,
135
+ arguments_range.begin_pos)
136
+ corrector.replace(leading_space, '(')
137
+ corrector.insert_after(arguments_range, ')')
138
+ end
139
+
132
140
  def require_parentheses?(args)
133
141
  style == :require_parentheses ||
134
142
  (style == :require_no_parentheses_except_multiline &&