Skip to content

Fix Style/ClassAndModuleChildren cop error on compact enforced style and unindented body #13634

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

Conversation

viralpraxis
Copy link
Contributor

If Style/ClassAndModuleChildren cop is configured with enforced style compact, the following code leads to and error (tree clobbering):

module M
class C
def main; TOPLEVEL_BINDING; end
end
end

I patched the cop to skip indentation autocorrection in such cases; they should be handled by Layout/IndentationWidth.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@viralpraxis viralpraxis marked this pull request as ready for review December 29, 2024 22:41
def unindent(corrector, node)
return if node.body.children.last.nil?

column_delta = configured_indentation_width - leading_spaces(node.body.children.last).size
last_children_leading_spaces = leading_spaces(node.body.children.last)
Copy link
Member

Choose a reason for hiding this comment

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

Since it is a single node, wouldn't last_child be more appropriate than last_children?

Suggested change
last_children_leading_spaces = leading_spaces(node.body.children.last)
last_child_leading_spaces = leading_spaces(node.body.children.last)

@@ -125,14 +125,19 @@ def remove_end(corrector, body)
corrector.remove(range)
end

# rubocop:disable Metrics/AbcSize
def unindent(corrector, node)
return if node.body.children.last.nil?
Copy link
Member

Choose a reason for hiding this comment

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

To avoid Metrics/AbcSize, replacing it with unless could be considered.

Suggested change
return if node.body.children.last.nil?
return unless node.body.children.last

…yle and unindented body

If `Style/ClassAndModuleChildren` cop is configured with enforced style `compact`,
the following code leads to and error (tree clobbering):

```ruby
module M
class C
def main; TOPLEVEL_BINDING; end
end
end
```

I patched the cop to skip indentation autocorrection in such cases;
they should be handled by `Layout/IndentationWidth`.
@viralpraxis viralpraxis force-pushed the fix-style-class-and-module-children-cop-error-on-unindented-body branch from 8adae2d to 8b7f077 Compare January 5, 2025 14:34
@koic koic merged commit f0255b6 into rubocop:master Jan 5, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy