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

Naming/FileName cop's ExpectMatchingDefinition doesn't work with custom directories #6289

Closed
patbl opened this issue Sep 14, 2018 · 7 comments
Labels
enhancement stale Issues that haven't been active in a while

Comments

@patbl
Copy link

patbl commented Sep 14, 2018

Is your feature request related to a problem? Please describe.

In Rails, you can add directories to config.autoload_paths, and these directories don't have to conform to the standard Rails namespacing conventions.

For example, suppose you have the file app/models/news/photo_story.rb. Normally, you'd have to name the class in that file News::PhotoStory. But if you add app/models/news to config.autoload_paths, you can instead name the file PhotoStory, and autoloading will still work.

This usually isn't the best thing to do, but people do it.

If you have ExpectMatchingDefinition: true, Rubocop will say something like

photo_story.rb should define a class or module called News::PhotoStory.

Describe the solution you'd like

It would be nice if you could specify autoload_paths in the Rubocop configuration, and have Rubocop instead look for unnamespaced classes in those directories. Something like

Naming/FileName:
  Enabled: true
  ExpectMatchingDefinition: true
  CustomDirectories:
    - app/models/news

Then it would omit the News:: prefix when looking for constants in those files. It would still report an offense if you named the class something like PhotosStory.

Describe alternatives you've considered

I could exclude the relevant paths, but then I'd lose out the check for those files (including the snake-case filename check).

@stale
Copy link

stale bot commented May 8, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label May 8, 2019
@stale
Copy link

stale bot commented Jun 7, 2019

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

@toupeira
Copy link

@patbl I wonder if you found a solution for this in the meantime?

I saw there's a new(ish) setting CheckDefinitionPathHierarchy: false which disables the path verification altogether, but it would be nice if we could keep that verification and define custom directories like you suggested.

In my case my projects are all in a ~/src directory, so the code at

start = %w[lib spec test src]
always detects that as the "project root". So e.g. for a path ~/src/project/app/models/user.rb it wants a Project::App::Models namespace 😁

Related discussion: https://stackoverflow.com/questions/61689211/rubocop-naming-filename-cop-complains-about-standard-files-when-expectmatchingde

/cc @jschneid @bbatsov

@patbl
Copy link
Author

patbl commented Oct 23, 2021

@patbl I wonder if you found a solution for this in the meantime?

I added all the custom paths to the Exclude list for this cop. Maybe using CheckDefinitionPathHierarchy: false would be better—less thorough but more comprehensive and consistent.

@grosser
Copy link
Contributor

grosser commented Oct 28, 2021

I'd love a CheckDefinitionPathHierarchyRoots: ["foo"]
because I have parts/ folder and it does not complain about anything inside of there :/

@grosser
Copy link
Contributor

grosser commented Oct 28, 2021

opened #10220

@grosser
Copy link
Contributor

grosser commented Oct 29, 2021

PR that should fix it #10224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement stale Issues that haven't been active in a while
Projects
None yet
Development

No branches or pull requests

4 participants