forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rubocop#5050] Add Style/TrailingBodyOnClass cop
- Loading branch information
1 parent
5b6d701
commit a99a2be
Showing
11 changed files
with
220 additions
and
37 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,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common methods shared by TrailingBody cops | ||
module TrailingBody | ||
def trailing_body?(node) | ||
body = node.to_a.reverse[0] | ||
body && node.multiline? && | ||
body_on_first_line?(node, body) | ||
end | ||
|
||
def body_on_first_line?(node, body) | ||
node.source_range.first_line == body.source_range.first_line | ||
end | ||
|
||
def first_part_of(body) | ||
if body.begin_type? | ||
body.children.first.source_range | ||
else | ||
body.source_range | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for trailing code after the class definition. | ||
# | ||
# @example | ||
# # bad | ||
# class Foo; def foo; end | ||
# end | ||
# | ||
# # good | ||
# class Foo | ||
# def foo; end | ||
# end | ||
# | ||
class TrailingBodyOnClass < Cop | ||
include Alignment | ||
include TrailingBody | ||
|
||
MSG = 'Place the first line of class body on its own line.'.freeze | ||
|
||
def on_class(node) | ||
return unless trailing_body?(node) | ||
|
||
add_offense(node, location: first_part_of(node.to_a.last)) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
LineBreakCorrector.correct_trailing_body( | ||
configured_width: configured_indentation_width, | ||
corrector: corrector, | ||
node: node, | ||
processed_source: processed_source | ||
) | ||
end | ||
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
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,83 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::TrailingBodyOnClass do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) do | ||
RuboCop::Config.new('Layout/IndentationWidth' => { 'Width' => 2 }) | ||
end | ||
|
||
it 'registers an offense when body trails after class definition' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
class Foo; body | ||
^^^^ Place the first line of class body on its own line. | ||
end | ||
class Bar; def bar; end | ||
^^^^^^^^^^^^ Place the first line of class body on its own line. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers offense with multi-line class' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
class Foo; body | ||
^^^^ Place the first line of class body on its own line. | ||
def bar | ||
qux | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts regular class' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class Foo | ||
def no_op; end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'accepts class inheritance' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
class Foo < Bar | ||
end | ||
RUBY | ||
end | ||
|
||
it 'auto-corrects body after class definition' do | ||
corrected = autocorrect_source(['class Foo; body ', | ||
'end'].join("\n")) | ||
expect(corrected).to eq ['class Foo ', | ||
' body ', | ||
'end'].join("\n") | ||
end | ||
|
||
it 'auto-corrects with comment after body' do | ||
corrected = autocorrect_source(['class BarQux; foo # comment', | ||
'end'].join("\n")) | ||
expect(corrected).to eq ['# comment', | ||
'class BarQux ', | ||
' foo ', | ||
'end'].join("\n") | ||
end | ||
|
||
it 'auto-corrects when there are multiple semicolons' do | ||
corrected = autocorrect_source(['class Bar; def bar; end', | ||
'end'].join("\n")) | ||
expect(corrected).to eq ['class Bar ', | ||
' def bar; end', | ||
'end'].join("\n") | ||
end | ||
|
||
context 'when class is not on first line of processed_source' do | ||
it 'auto-correct offense' do | ||
corrected = autocorrect_source(['', | ||
' class Foo; body ', | ||
' end'].join("\n")) | ||
expect(corrected).to eq ['', | ||
' class Foo ', | ||
' body ', | ||
' end'].join("\n") | ||
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