Skip to content

Commit cb00a0c

Browse files
committed
Merge pull request rubocop#2216 from karreiro/autocorrect_parentheses_bug
[Fix rubocop#2212] Handle methods without parentheses in auto-correct
2 parents 6a263d5 + 7bdcd16 commit cb00a0c

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bug Fixes
66

7+
* [#2212](https://github.com/bbatsov/rubocop/issues/2212): Handle methods without parentheses in auto-correct. ([@karreiro][])
78
* [#2214](https://github.com/bbatsov/rubocop/pull/2214): Fix `File name too long error` when `STDIN` option is provided. ([@mrfoto][])
89
* [#2217](https://github.com/bbatsov/rubocop/issues/2217): Allow block arguments in `Style/SymbolProc`. ([@lumeet][])
910

@@ -1592,3 +1593,4 @@
15921593
[@MGerrior]: https://github.com/MGerrior
15931594
[@imtayadeway]: https://github.com/imtayadeway
15941595
[@mrfoto]: https://github.com/mrfoto
1596+
[@karreiro]: https://github.com/karreiro

lib/rubocop/cop/performance/string_replacement.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class StringReplacement < Cop
2828
TR = 'tr'.freeze
2929
BANG = '!'.freeze
3030
SINGLE_QUOTE = "'".freeze
31-
CLOSING_PAREN = ')'.freeze
3231

3332
def on_send(node)
3433
_string, method, first_param, second_param = *node
@@ -164,13 +163,17 @@ def require_double_quotes?(string)
164163
StringHelp::ESCAPED_CHAR_REGEXP =~ string
165164
end
166165

166+
def method_suffix(node)
167+
node.loc.end ? node.loc.end.source : ''
168+
end
169+
167170
def remove_second_param(corrector, node, first_param)
168171
end_range =
169172
Parser::Source::Range.new(node.loc.expression.source_buffer,
170173
first_param.loc.expression.end_pos,
171174
node.loc.expression.end_pos)
172175

173-
corrector.replace(end_range, CLOSING_PAREN)
176+
corrector.replace(end_range, method_suffix(node))
174177
end
175178
end
176179
end

spec/rubocop/cop/performance/string_replacement_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@
434434

435435
expect(new_source).to eq("'ab'.delete('a')")
436436
end
437+
438+
it 'corrects when there are no brackets' do
439+
new_source = autocorrect_source(cop, "'abc'.gsub! 'a', ''")
440+
441+
expect(new_source).to eq("'abc'.delete! 'a'")
442+
end
437443
end
438444
end
439445
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy