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

Accept simple freezed constants in Layout/ClassStructure and correctly handle class methods #11329

Merged
merged 2 commits into from Dec 25, 2022

Conversation

fatkodima
Copy link
Contributor

This PR fixes 2 problems:

  1. Previously, rubocop hasn't correctly detected and autocorrected class methods (as you can see from the 2nd commit), because of the misspelling.
  2. Simple .freeze constants are considered as "dynamic constants" and are ignored from the consideration. Changed that.

This PR also fixes #8034.

As a follow up to this, I will extend this cop to be able to detect private constants and class methods, to avoid having disables like

# rubocop:disable Layout/ClassStructure
EMPTY_OFFENSES = [].freeze
private_constant :EMPTY_OFFENSES
# rubocop:enable Layout/ClassStructure
- that constant is used in the method that follows and makes sense to keep it near that method (not at the top as Layout/ClassStructure currently suggests, thats why # rubocop:disable).

@bbatsov bbatsov merged commit 7935008 into rubocop:master Dec 25, 2022
@bbatsov
Copy link
Collaborator

bbatsov commented Dec 25, 2022

Looks good. Thanks!

koic added a commit to rubocop/rubocop-performance that referenced this pull request Dec 25, 2022
koic added a commit to rubocop/rubocop-rails that referenced this pull request Dec 25, 2022
koic added a commit to rubocop/rubocop-minitest that referenced this pull request Dec 25, 2022
koic added a commit to rubocop/rubocop-ast that referenced this pull request Dec 25, 2022
Follow up rubocop/rubocop#11329

```console
% bundle exec rake
(snip)

Offenses:

lib/rubocop/ast/node_pattern.rb:105:7: C: [Correctable] Layout/ClassStructure: public_class_methods is supposed to appear before initializer.
      def self.descend(element, &block) ...
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
lib/rubocop/ast/processed_source.rb:23:7: C: [Correctable] Layout/ClassStructure: constants is supposed to appear before public_class_methods.
      INVALID_LEVELS = %i[error fatal].freeze
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tasks/changelog.rb:121:3: C: [Correctable] Layout/ClassStructure: public_class_methods is supposed to appear before initializer.
  def self.pending? ...
  ^^^^^^^^^^^^^^^^^

162 files inspected, 3 offenses detected, 3 offenses autocorrectable
```
@fatkodima fatkodima deleted the class_structure-fixes branch December 25, 2022 10:18
MarttiCheng added a commit to MarttiCheng/Rubocop-Performance that referenced this pull request Sep 28, 2023
SerhiiMisiura added a commit to SerhiiMisiura/Rubocop-Performance that referenced this pull request Oct 5, 2023
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.

Auto-correction of Layout/ClassStructure cop breaks codes
2 participants