Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an incorrect auto-correct for Layout/DotPosition #9867

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#9867](https://github.com/rubocop/rubocop/pull/9867): Fix an incorrect auto-correct for `Layout/DotPosition` when using only dot line. ([@koic][])
8 changes: 7 additions & 1 deletion lib/rubocop/cop/layout/dot_position.rb
Expand Up @@ -24,6 +24,7 @@ module Layout
# method
class DotPosition < Base
include ConfigurableEnforcedStyle
include RangeHelp
extend AutoCorrector

def on_send(node)
Expand All @@ -42,7 +43,12 @@ def on_send(node)
private

def autocorrect(corrector, dot, node)
corrector.remove(dot)
dot_range = if processed_source[dot.line - 1].strip == '.'
range_by_whole_lines(dot, include_final_newline: true)
else
dot
end
corrector.remove(dot_range)
case style
when :leading
corrector.insert_before(selector_range(node), dot.source)
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/layout/dot_position_spec.rb
Expand Up @@ -34,6 +34,22 @@
RUBY
end

it 'registers an offense for only dot line' do
expect_offense(<<~RUBY)
foo
.bar
.
^ Place the . on the next line, together with the method name.
baz
RUBY

expect_correction(<<~RUBY)
foo
.bar
.baz
RUBY
end

it 'accepts leading do in multi-line method call' do
expect_no_offenses(<<~RUBY)
something
Expand Down