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 #11237] Fix Layout/CommentIndentation comment aligned with acc… #11343

Merged
merged 1 commit into from
Dec 28, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11237](https://github.com/rubocop/rubocop/issues/11237): Fix `Layout/CommentIndentation` comment aligned with access modifier indentation when EnforcedStyle is outdent. ([@soroktree][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/layout/comment_indentation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ def correct_indentation(next_line)
end

def less_indented?(line)
/^\s*(end\b|[)}\]])/.match?(line)
rule = config.for_cop('Layout/AccessModifierIndentation')['EnforcedStyle'] == 'outdent'
access_modifier = 'private|protected|public'
/\A\s*(end\b|[)}\]])/.match?(line) || (rule && /\A\s*(#{access_modifier})\b/.match?(line))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to extract /\A\s*(#{access_modifier})\b/ into a constant or inline access_modifier into it, because this method call will always generate unnecessary allocations (needs measuring to know the impact).

end

def two_alternatives?(line)
Expand Down
51 changes: 51 additions & 0 deletions spec/rubocop/cop/layout/comment_indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,55 @@ def compile_sequence(seq, seq_var)
RUBY
end
end

context 'when `Layout/AccessModifierIndentation EnforcedStyle: outdent`' do
let(:allow_for_alignment) { true }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is not needed, because you redefine config in this block (the single place where it was used).

let(:indentation_width) { 2 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest removing this line (and from the top of the file) and inlining its value into the call, because it is never changed in the test file.

let(:config) do
RuboCop::Config.new(
'Layout/AccessModifierIndentation' => {
'Enabled' => true,
'EnforcedStyle' => 'outdent'
},
'Layout/CommentIndentation' => {
'Enabled' => true
},
'Layout/IndentationWidth' => {
'Width' => indentation_width
}
)
end

it 'does not register an offense with indentation if aligned with code on previous line' do
expect_no_offenses(<<~RUBY)
class A
# rubocop:disable
def foo
end
# rubocop:enable

private

def bar
end
end
RUBY
end

it 'registers an offense with indentation if aligned with access modifier on next line' do
expect_offense(<<~RUBY)
class A
# rubocop:disable
def foo
end
# rubocop:enable
^^^^^^^^^^^^^^^^ Incorrect indentation detected (column 0 instead of 2).
private

def bar
end
end
RUBY
end
end
end