Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an incorrect auto-correct for Layout/ClassStructure #8907

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@

* [#8892](https://github.com/rubocop-hq/rubocop/issues/8892): Fix an error for `Style/StringConcatenation` when correcting nested concatenable parts. ([@fatkodima][])
* [#8781](https://github.com/rubocop-hq/rubocop/issues/8781): Fix handling of comments in `Style/SafeNavigation` autocorrection. ([@dvandersluis][])
* [#8907](https://github.com/rubocop-hq/rubocop/pull/8907): Fix an incorrect auto-correct for `Layout/ClassStructure` when heredoc constant is defined after public method. ([@koic][])

### Changes

Expand Down
7 changes: 7 additions & 0 deletions lib/rubocop/cop/layout/class_structure.rb
Expand Up @@ -265,6 +265,9 @@ def source_range_with_comment(node)
end

def end_position_for(node)
heredoc = find_heredoc(node)
return heredoc.location.heredoc_end.end_pos + 1 if heredoc

end_line = buffer.line_for_position(node.loc.expression.end_pos)
buffer.line_range(end_line).end_pos
end
Expand All @@ -284,6 +287,10 @@ def start_line_position(node)
buffer.line_range(node.loc.line).begin_pos - 1
end

def find_heredoc(node)
node.each_node(:str, :dstr, :xstr).find(&:heredoc?)
end

def buffer
processed_source.buffer
end
Expand Down
75 changes: 75 additions & 0 deletions spec/rubocop/cop/layout/class_structure_spec.rb
Expand Up @@ -214,4 +214,79 @@ class Foo
end
end
end

it 'registers an offense and corrects when str heredoc constant is defined after public method' do
expect_offense(<<~RUBY)
class Foo
def do_something
end

CONSTANT = <<~EOS
^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`.
str
EOS
end
RUBY

expect_correction(<<~RUBY)
class Foo
CONSTANT = <<~EOS
str
EOS

def do_something
end
end
RUBY
end

it 'registers an offense and corrects when dstr heredoc constant is defined after public method' do
expect_offense(<<~'RUBY')
class Foo
def do_something
end

CONSTANT = <<~EOS
^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`.
#{str}
EOS
end
RUBY

expect_correction(<<~'RUBY')
class Foo
CONSTANT = <<~EOS
#{str}
EOS

def do_something
end
end
RUBY
end

it 'registers an offense and corrects when xstr heredoc constant is defined after public method' do
expect_offense(<<~'RUBY')
class Foo
def do_something
end

CONSTANT = <<~`EOS`
^^^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`.
str
EOS
end
RUBY

expect_correction(<<~'RUBY')
class Foo
CONSTANT = <<~`EOS`
str
EOS

def do_something
end
end
RUBY
end
end