Skip to content

Commit

Permalink
Add auto-correct to DotPosition cop
Browse files Browse the repository at this point in the history
  • Loading branch information
yous committed Aug 27, 2014
1 parent 06ce37e commit cc9e810
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* `UselessAssignment` cop now suggests a variable name for possible typos if there's a variable-ish identifier similar to the unused variable name in the same scope. ([@yujinakayama][])
* [#1272](https://github.com/bbatsov/rubocop/issues/1272): `Tab` cop does auto-correction. ([@yous][])
* [#1274](https://github.com/bbatsov/rubocop/issues/1274): `MultilineIfThen` cop does auto-correction. ([@bbatsov][])
* [#1279](https://github.com/bbatsov/rubocop/issues/1279): `DotPosition` cop does auto-correction. ([@yous][])

### Bugs fixed

Expand Down
20 changes: 20 additions & 0 deletions lib/rubocop/cop/style/dot_position.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ def proper_dot_position?(node)
when :trailing then dot_line != selector_line
end
end

def autocorrect(node)
receiver, _method_name, *_args = *node
if node.loc.selector
selector = node.loc.selector
else
# l.(1) has no selector, so we use the opening parenthesis instead
selector = node.loc.begin
end

@corrections << lambda do |corrector|
corrector.remove(node.loc.dot)
case style
when :leading
corrector.insert_before(selector, '.')
when :trailing
corrector.insert_after(receiver.loc.expression, '.')
end
end
end
end
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/rubocop/cop/style/dot_position_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@
inspect_source(cop, ['something.method_name'])
expect(cop.offenses).to be_empty
end

it 'auto-corrects trailing dot in multi-line call' do
new_source = autocorrect_source(cop, ['something.',
' method_name'])
expect(new_source).to eq(['something',
' .method_name'].join("\n"))
end

it 'auto-corrects trailing dot in multi-line call without selector' do
new_source = autocorrect_source(cop, ['something.',
' (1)'])
expect(new_source).to eq(['something',
' .(1)'].join("\n"))
end

it 'auto-corrects correct + opposite style' do
new_source = autocorrect_source(cop, ['something',
' .method_name',
'something.',
' method_name'])
expect(new_source).to eq(['something',
' .method_name',
'something',
' .method_name'].join("\n"))
end
end

context 'Trailing dots style' do
Expand Down Expand Up @@ -87,5 +112,19 @@
'compact.join("\n")'])
expect(cop.offenses).to be_empty
end

it 'auto-corrects leading dot in multi-line call' do
new_source = autocorrect_source(cop, ['something',
' .method_name'])
expect(new_source).to eq(['something.',
' method_name'].join("\n"))
end

it 'auto-corrects leading dot in multi-line call without selector' do
new_source = autocorrect_source(cop, ['something',
' .(1)'])
expect(new_source).to eq(['something.',
' (1)'].join("\n"))
end
end
end

0 comments on commit cc9e810

Please sign in to comment.