8000 New cop DotPosition · rubocop/rubocop@e41175f · GitHub
[go: up one dir, main page]

Skip to content

Commit e41175f

Browse files
author
Bozhidar Batsov
committed
New cop DotPosition
1 parent 0597f35 commit e41175f

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* New cop `LiteralInCondition` tracks uses of literals in the conditions of `if/while/until`.
2929
* New cop `BeginBlock` tracks uses of `BEGIN` blocks.
3030
* New cop `EndBlock` tracks uses of `END` blocks.
31+
* New cop `DotPosition` tracks the dot position in multi-line method calls.
3132
* Add support for auto-correction of some offences with `-a`/`--auto-correct`.
3233

3334
### Changes

config/enabled.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ EndInMethod:
388388
LiteralInCondition:
389389
Enabled: true
390390

391+
# Checks the position of the dot in multi-line method calls.
392+
DotPosition:
393+
Enabled: true
394+
391395
## Rails
392396

393397
# Use sexy validations.

lib/rubocop.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
require 'rubocop/cop/style/constant_name'
5050
require 'rubocop/cop/style/def_parentheses'
5151
require 'rubocop/cop/style/documentation'
52+
require 'rubocop/cop/style/dot_position'
5253
require 'rubocop/cop/style/empty_line_between_defs'
5354
require 'rubocop/cop/style/empty_lines'
5455
require 'rubocop/cop/style/empty_literal'

lib/rubocop/cop/style/dot_position.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# encoding: utf-8
2+
3+
module Rubocop
4+
module Cop
5+
module Style
6+
# This cop checks the . position in multi-line method calls.
7+
class DotPosition < Cop
8+
MSG = 'Place the . on the next line, together with the method name.'
9+
10+
def on_send(node)
11+
return unless node.loc.dot
12+
13+
dot_line = node.loc.dot.line
14+
selector_line = node.loc.selector.line
15+
16+
if dot_line != selector_line
17+
add_offence(:convention, node.loc.dot, MSG)
18+
end
19+
20+
super
21+
end
22+
end
23+
end
24+
end
25+
end

spec/rubocop/config_store_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ module Rubocop
4747
Config.should_receive(:configuration_file_for).once.with('dir/' +
4848
'subdir')
4949
# The stub returns the same config path for dir and dir/subdir.
50-
Config.should_receive(:configuration_from_file).once.
51-
with('dir/.rubocop.yml')
50+
Config.should_receive(:configuration_from_file).once
51+
.with('dir/.rubocop.yml')
5252

5353
ConfigStore.for('dir/file2')
5454
ConfigStore.for('dir/file2')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
module Rubocop
6+
module Cop
7+
module Style
8+
describe DotPosition do
9+
let(:cop) { DotPosition.new }
10+
11+
it 'registers an offence for trailing dot in multi-line method call' do
12+
inspect_source(cop, ['something.',
13+
' method_name'])
14+
expect(cop.offences.size).to eq(1)
15+
end
16+
17+
it 'accepts leading do in multi-line method call' do
18+
inspect_source(cop, ['something', 77DB
19+
' .method_name'])
20+
expect(cop.offences).to be_empty
21+
end
22+
23+
it 'does not err on method call with no dots' do
24+
inspect_source(cop, ['puts something'])
25+
expect(cop.offences).to be_empty
26+
end
27+
end
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)
0