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

False positive in Naming/ClassAndModuleCamelCase when using `module_parent` #8063

Closed
pdobb opened this issue May 29, 2020 · 0 comments
Closed

False positive in Naming/ClassAndModuleCamelCase when using `module_parent` #8063

pdobb opened this issue May 29, 2020 · 0 comments

Comments

@pdobb
Copy link

@pdobb pdobb commented May 29, 2020

Naming/ClassAndModuleCamelCase: Use CamelCase for classes and modules. (https://rubystyle.guide#camelcase-classes)

Now that Zeitwerk is out, I prefer defining classes in a single line -- namespace and all.

# OLD (Bad)
namespace MyModule
  class MyPrimaryClass
  end
end

# NEW (Good)
class MyModule::MyPrimaryClass
end

Sometimes, however, I want to be able to define a minor, sibling class on the current namespace without creating a new file.

# Old Way (Bad -- non-collapsed namespace/class)
namespace MyModule
  # Defines: MyModule::MyPrimaryClass (Good)
  class MyPrimaryClass
  end
 
  # Defines: MyModule::MySiblingClass (Good)
  class MySiblingClass
  end
end

# New Way (Good -- collapsed namespace/class)
class MyModule::MyPrimaryClass
  # Bad
  # - Don't want this, because it defines a sub-class, not a sibling class: 
  #   MyModule::MyPrimaryClass::MySiblingClass
  class MySiblingClass
  end

  # Good
  # - Defines the intended sibling class: MyModule::MySiblingClass
  class module_parent::MySiblingClass
  end
end

Expected behavior

The Naming/ClassAndModuleCamelCase cop should allow module_parent::MySiblingClass since this is valid syntax that produces the wanted result and since module_parent is a class method and is not (nor is intended to be) a constant name.

Perhaps the cop can allow a few keywords that aren't camel-cased as part of a class name? Namely module_parent. (I'm not sure if others would be relevant here.)

This fix, if accepted, might add the following to the documentation for this cop:

# good
class module_parent::MyClass
end

Actual behavior

Offense raised on this line class module_parent::MySiblingClass:

Naming/ClassAndModuleCamelCase: Use CamelCase for classes and modules. (https://rubystyle.guide#camelcase-classes)

Steps to reproduce the problem

  1. Define:
# app/models/my_module/my_primary_class.rb
class MyModule::MyPrimaryClass
  class module_parent::MySiblingClass
  end
end
  1. Run: rubocop app/models/my_module/my_primary_class.rb

RuboCop version

$ [bundle exec] rubocop -V
0.84.0 (using Parser 2.7.1.2, rubocop-ast 0.0.3, running on ruby 2.6.6 x86_64-darwin19)
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 30, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 30, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 31, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 31, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 31, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 31, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue May 31, 2020
…AndModuleCamelCase`

Closes rubocop-hq#8063
bbatsov added a commit that referenced this issue May 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

1 participant
You can’t perform that action at this time.