Skip to content

Commit

Permalink
Fix an incorrect auto-correct for Layout/DotPosition
Browse files Browse the repository at this point in the history
This PR fixes the following incorrect auto-correct for `Layout/DotPosition`
when using only dot line.

```console
% cat example.rb
foo
  .bar
  .
  baz

% rubocop --only Layout/DotPosition -a
(snip)

Inspecting 1 file
C

Offenses:

example.rb:3:3: C: [Corrected] Layout/DotPosition: Place the . on the
next line, together with the method name.
  .
  ^

1 file inspected, 1 offense detected, 1 offense corrected
```

## Before

```consle
% cat example.rb
foo
  .bar

  .baz

% ruby -c example.rb
example.rb:4: syntax error, unexpected '.', expecting end-of-input
  .baz
```

## After

```console
% cat example.rb
foo
  .bar
  .baz

% ruby -c example.rb
Syntax OK
```
  • Loading branch information
koic authored and bbatsov committed Jun 11, 2021
1 parent cb1c02f commit a20493d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
@@ -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

0 comments on commit a20493d

Please sign in to comment.