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 #6289] Naming/FileName: Add ExpectMatchingClass option #7780

Merged
merged 1 commit into from May 29, 2020

Conversation

jschneid
Copy link
Contributor

@jschneid jschneid commented Mar 6, 2020

#6289 describes a limitation of the existing ExpectMatchingDefinition: true option on the Naming/FileName cop: It generates false positives when run on a project with classes which (by design) aren't namespaced with modules to match their file paths.

This PR addresses that issue by adding a new option to the Naming/FileName cop: CheckDefinitionPathHierarchy. This defaults to true; if set to false, it modifies the ExpectMatchingDefinition option to only check whether each source file's class or module name matches the file name -- not whether the nested module hierarchy matches the subdirectory path.


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@jschneid jschneid force-pushed the FileNameExpectMatchingClass branch from 96fabf8 to 920905a Compare March 6, 2020 15:28
@jschneid jschneid changed the title [Fix rubocop-hq#6289] Naming/FileName: Add ExpectMatchingClass option [Fix #6289] Naming/FileName: Add ExpectMatchingClass option Mar 6, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented Mar 21, 2020

If RuboCop is configured to enable both the ExpectMatchingDefinition and ExpectMatchingClass options, then the ExpectMatchingDefinition option (which is more strict) takes precedence.

I think it's much better to just add an option for checking the path hierarchy instead, as it seems the new option you introduced is the same as the other sans the path checking. For me the suggested solution is confusing, but valuable - let's simplify it. :-)

@jschneid
Copy link
Contributor Author

jschneid commented Apr 3, 2020

I think it's much better to just add an option for checking the path hierarchy instead, as it seems the new option you introduced is the same as the other sans the path checking. For me the suggested solution is confusing, but valuable - let's simplify it. :-)

Bozhidar, thanks for the comment! I'd like to fix up this PR to address your suggestion; however, I'm not entirely sure what you're suggesting -- could you please clarify?

The ExpectMatchingDefinition option on the Naming/FileName cop, as it exists today, when enabled, essentially enforces two additional checks:

  1. That the file's module namespace matches its filesystem path;
  2. That the name of the class or module defined in the file matches the filename.

This PR's new proposed option (ExpectMatchingClass), when enabled, checks only item 2 above.

Are you suggesting that we add an additional new option that would check only item 1 above? Such that we'd end up with Naming/FileName cop options:

  • Existing option ExpectMatchingDefinition, which checks items 1 + 2 above;
  • New option ExpectMatchingClass, which checks only item 2;
  • Another new option -- ExpectMatchingPathHierarchy, perhaps? -- which checks only item 1?

I'd be happy to take that on -- I just want to check my understanding of whether that's what you're suggesting, first. 🙂

@jschneid
Copy link
Contributor Author

@bbatsov, this is 100% not urgent, but I wanted to bump my above question for you, just in case you hadn't seen it yet. 🙂

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 17, 2020

Sorry about the slow reply. My bad, my previous reply was completely wrong. I was under the impression that the cop already was checking for some definition and I had assumed you wanted to limit the full path checking and focus only on checking for a definition. I saw this was not the case, so it seems everything's good with your PR. Just rebase to resolve the merge conflict, please.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 17, 2020

Ah, forget what I said. I'm too tired for meaningful replies. Actually seems I wasn't wrong last time around! What I meant was that instead of having ExpectMatchingClass and ExpectMatchingDefinition we can have something like CheckDefinitionPathHieararchy which would alter the behaviour of ExpectMatchingDefinition. Alternatively we can make ExpectMatchingDefinition accept non-boolean values, so you can specify there something like match_hierarchy, match_filename, etc. I'm just thinking that one of those approaches will result in more understandable configuration options.

@jschneid jschneid force-pushed the FileNameExpectMatchingClass branch 3 times, most recently from 0d85eb5 to d03dfb9 Compare May 1, 2020 22:25
@jschneid
Copy link
Contributor Author

jschneid commented May 2, 2020

Ok! As per your suggestion, @bbatsov, I've adjusted this PR to instead add a new CheckDefinitionPathHierarchy option to the Naming/FileName cop. Details are in the updated PR description, above.

@jschneid jschneid force-pushed the FileNameExpectMatchingClass branch from d03dfb9 to 640f458 Compare May 29, 2020 13:26
@jschneid
Copy link
Contributor Author

jschneid commented May 29, 2020

I've rebased this PR to fix the typical merge conflict in the CHANGELOG.md (which occurs over time as other PRs are merged).

@bbatsov, is there anything else I can do to help with this at this point? 🙂

@bbatsov bbatsov merged commit 32bbfce into rubocop:master May 29, 2020
@bbatsov
Copy link
Collaborator

bbatsov commented May 29, 2020

Thanks and sorry for forgetting about this PR!

@jschneid jschneid deleted the FileNameExpectMatchingClass branch May 29, 2020 18:14
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.

None yet

2 participants