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 false-positives when constant is used with receiver on Rails/DurationArithmetic, Rails/IndexBy, Rails/IndexWIth, and Rails/RequireDependency #866

Merged
merged 1 commit into from
Nov 16, 2022

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Nov 14, 2022

In response to the following comment, I reviewed the use of const _ and found several similar mistakes in the existing code.

cc: @koic


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.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@@ -0,0 +1 @@
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Fix some false-positives by replacing `const _` with `const {nil? cbase}`. ([@r7kamura][])
Copy link
Member

Choose a reason for hiding this comment

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

Can you tweak it? e.g.:

Suggested change
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Fix some false-positives by replacing `const _` with `const {nil? cbase}`. ([@r7kamura][])
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Make `Rails/DurationArithmetic`, `Rails/IndexBy`, `Rails/IndexWIth`, and `Rails/RequireDependency` cops aware of const starts with `::`. ([@r7kamura][])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, the recommended example is more user-friendly and easier to understand 👍

@koic
Copy link
Member

koic commented Nov 14, 2022

Can you add test cases for each cop?

@r7kamura
Copy link
Contributor Author

r7kamura commented Nov 14, 2022

Can you add test cases for each cop?

I am a little hesitant as to whether we should write tests this time because the other places where {nil? cbase} is used have almost not been tested on such no-offense pattern as long as I confirmed. I think we could take a consistent policy that we don't write no-offense test cases for this kind of pattern. Once a test is written here, the consistency will be lost.

Do you think even we should write tests only for these 4 cops just because we happen to be changing them this time? I asked this question because I think it is related to the overall testing policy.

@koic
Copy link
Member

koic commented Nov 14, 2022

A basic policy is that patches to production code changes should add tests. There is no need to extend to existing working code.

@r7kamura
Copy link
Contributor Author

I see, thanks. So I'll add tests for them 👌

@r7kamura r7kamura changed the title Fix some false-positives by replacing const _ with const {nil? cbase} Make some cops aware of const that starts with :: Nov 14, 2022
@r7kamura
Copy link
Contributor Author

r7kamura commented Nov 14, 2022

On second thought, the changes include the eliminations of some false-positives (e.g. Foo::Time.current), so

Make ... aware of const starts with ::

is not quite accurate, is it? In this case, ::Time.current should have been supported originally.

@@ -0,0 +1 @@
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Make `Rails/DurationArithmetic`, `Rails/IndexBy`, `Rails/IndexWIth`, and `Rails/RequireDependency` cops aware of const that starts with `::`. ([@r7kamura][])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about like this?

Suggested change
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Make `Rails/DurationArithmetic`, `Rails/IndexBy`, `Rails/IndexWIth`, and `Rails/RequireDependency` cops aware of const that starts with `::`. ([@r7kamura][])
* [#866](https://github.com/rubocop/rubocop-rails/pull/866): Fix false-positives on `Rails/DurationArithmetic`, `Rails/IndexBy`, `Rails/IndexWIth`, and `Rails/RequireDependency` cops. ([@r7kamura][])

Copy link
Member

Choose a reason for hiding this comment

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

Sure. It would be even better if there were additional explanations of what false positives were fixed.

new(match, node.children.last, 'Hash['.length, ']'.length)
new(match, node.children.last, "#{node.receiver.source}[".length, ']'.length)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a change that was not originally envisioned for the purpose of "Fix false-positives", but after adding tests, I discovered that there is an autocorrection bug where ::Hash[...] is used.

Should I separate a Pull Request to fix this bug?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, you could do it separately because the automatic correction mistake is unclear in the PR's aim to the context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I created the following Pull Request for this autocorrection bug:

@r7kamura r7kamura changed the title Make some cops aware of const that starts with :: Fix false-positivives when the constant has a receiver on some cops Nov 14, 2022
@r7kamura r7kamura changed the title Fix false-positivives when the constant has a receiver on some cops Fix false-positivives when consant is used with receiver on some cops Nov 14, 2022
@r7kamura r7kamura changed the title Fix false-positivives when consant is used with receiver on some cops Fix false-positivives when constant is used with receiver on Rails/DurationArithmetic, Rails/IndexBy, Rails/IndexWIth, and Rails/RequireDependency Nov 14, 2022
@r7kamura r7kamura changed the title Fix false-positivives when constant is used with receiver on Rails/DurationArithmetic, Rails/IndexBy, Rails/IndexWIth, and Rails/RequireDependency Fix false-positives when constant is used with receiver on Rails/DurationArithmetic, Rails/IndexBy, Rails/IndexWIth, and Rails/RequireDependency Nov 14, 2022
@koic
Copy link
Member

koic commented Nov 15, 2022

@r7kamura Looks good to me. Can you resolve the conflicting files?

@koic koic merged commit c680d24 into rubocop:master Nov 16, 2022
@r7kamura r7kamura deleted the feature/fix-const-nil-cbase branch November 16, 2022 02:44
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