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

Add RSpec/StringAsInstanceDoubleConstant #1957

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

corsonknowles
Copy link
Contributor

@corsonknowles corsonknowles commented Sep 17, 2024

Addresses #1136

Adds a cop which can autocorrect from String declarations for instance_double to Class declarations. Symbol declarations are not affected.

Autocorrect is not marked as safe for the class instantiation reasons mentioned in the original issue. However, we have run this as safe autocorrect internally for years, and have not met with any issues.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@corsonknowles
Copy link
Contributor Author

corsonknowles commented Sep 17, 2024

Getting

* generated /Users/.../Documents/GitHub/rubocop-rspec/docs/modules/ROOT/pages//cops_rspec.adoc
rake aborted!
Please run `bundle exec rake generate_cops_documentation` and add docs/ to the commit.
/Users/.../Documents/GitHub/rubocop-rspec/Rakefile:79:in `block in <top (required)>'
/Users/.../.rbenv/versions/3.3.0/bin/ruby_executable_hooks:22:in `eval'
/Users/.../.rbenv/versions/3.3.0/bin/ruby_executable_hooks:22:in `<main>'
Tasks: TOP => default => confirm_documentation
(See full trace by running task with --trace)

However, have run rake generate_cops_documentation

.rubocop.yml Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
.rubocop.yml Outdated Show resolved Hide resolved
@bquorning bquorning mentioned this pull request Sep 18, 2024
6 tasks
@bquorning
Copy link
Collaborator

However, have run rake generate_cops_documentation

Did you also “add docs/ to the commit”? When I run the rake task I see a diff in the docs folder.

@pirj
Copy link
Member

pirj commented Sep 18, 2024

There are some wip rubocop cop naming guidelines, and they insist on naming cops so that they highlight the problem. This should be “StringifiedInstabceDoubleConstant”

@corsonknowles corsonknowles changed the title Add RSpec/NoStringifiedNoStringifiedInstanceDoubleConstant Add RSpec/StringAsInstanceDoubleConstant Sep 18, 2024
@corsonknowles corsonknowles marked this pull request as ready for review September 18, 2024 13:40
@corsonknowles corsonknowles requested a review from a team as a code owner September 18, 2024 13:40
@corsonknowles
Copy link
Contributor Author

corsonknowles commented Sep 18, 2024

Did you also “add docs/ to the commit”? When I run the rake task I see a diff in the docs folder.

Looks like my rake problems were mostly self-inflicted, I tried to add my own documentation while I was following the contribution guidelines.

For ease of new development, I'd recommend updating all of our rake task feedback to say "bundle exec rake etc" so it's easier to copy and execute

Addresses rubocop#1136

Adds a cop which can autocorrect from String declarations for instance_double to Class declarations. Symbol declarations are not affected.
Copy link

@technicalpickles technicalpickles left a comment

Choose a reason for hiding this comment

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

I am biased since it was extracted from our app, but in favor of this 👍🏻

Interestingly, instance_double does support a string as the name, at least according to its rdoc. I am looking at some of the implementation (instance_double and ObjectReference.for(doubled_class) but I couldn't tell you what happens for classes vs strings.

Copy link
Collaborator

@bquorning bquorning left a comment

Choose a reason for hiding this comment

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

Two questions:

  1. Could / should we make the same rule for class_double too?
  2. Shouldn’t we also detect and replace symbols? As I understand the source, calling instance_double with a symbol won’t even work.

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.

4 participants