-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1078 from barunio/block_alignment_autocorrect
Add auto-correct to BlockAlignment cop
- Loading branch information
Showing
7 changed files
with
254 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# encoding: utf-8 | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks whether the end statement of a do end blocks | ||
# is on its own line. | ||
# | ||
# @example | ||
# # bad | ||
# blah do |i| | ||
# foo(i) end | ||
# | ||
# # good | ||
# blah do |i| | ||
# foo(i) | ||
# end | ||
# | ||
# # bad | ||
# blah { |i| | ||
# foo(i) } | ||
# | ||
# # good | ||
# blah { |i| | ||
# foo(i) | ||
# } | ||
class BlockEndNewline < Cop | ||
MSG = 'Expression at %d, %d should be on its own line.' | ||
|
||
def on_block(node) | ||
end_loc = node.loc.end | ||
do_loc = node.loc.begin # Actually it's either do or {. | ||
return if do_loc.line == end_loc.line # Ignore one-liners. | ||
|
||
# If the end is on its own line, there is no offense | ||
return if /^\s*#{end_loc.source}/.match(end_loc.source_line) | ||
|
||
msg = format(MSG, end_loc.line, end_loc.column + 1) | ||
add_offense(node, end_loc, msg) | ||
end | ||
|
||
def autocorrect(node) | ||
@corrections << lambda do |corrector| | ||
indentation = indentation_of_block_start_line(node) | ||
corrector.insert_before(node.loc.end, "\n" + (' ' * indentation)) | ||
end | ||
end | ||
|
||
def indentation_of_block_start_line(node) | ||
match = /\S.*/.match(node.loc.begin.source_line) | ||
match.begin(0) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::Cop::Style::BlockEndNewline do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'does not register an offense for a one-liner' do | ||
inspect_source(cop, ['test do foo end']) | ||
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'does not register an offense for multiline blocks with newlines before '\ | ||
'the end' do | ||
inspect_source(cop, | ||
['test do', | ||
' foo', | ||
'end']) | ||
expect(cop.messages).to be_empty | ||
end | ||
|
||
it 'registers an offense when multiline block end is not on its own line' do | ||
inspect_source(cop, | ||
['test do', | ||
' foo end' | ||
]) | ||
expect(cop.messages) | ||
.to eq(['Expression at 2, 7 should be on its own line.']) | ||
end | ||
|
||
it 'registers an offense when multiline block } is not on its own line' do | ||
inspect_source(cop, | ||
['test {', | ||
' foo }' | ||
]) | ||
expect(cop.messages) | ||
.to eq(['Expression at 2, 7 should be on its own line.']) | ||
end | ||
|
||
it 'autocorrects a do/end block where the end is not on its own line' do | ||
src = ['test do', | ||
' foo end'] | ||
|
||
new_source = autocorrect_source(cop, src) | ||
|
||
expect(new_source).to eq(['test do', | ||
' foo ', | ||
'end'].join("\n")) | ||
end | ||
|
||
it 'autocorrects a {} block where the } is not on its own line' do | ||
src = ['test {', | ||
' foo }'] | ||
|
||
new_source = autocorrect_source(cop, src) | ||
|
||
expect(new_source).to eq(['test {', | ||
' foo ', | ||
'}'].join("\n")) | ||
end | ||
end |