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

Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only #1968

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

Conversation

corsonknowles
Copy link
Contributor

@corsonknowles corsonknowles commented Oct 13, 2024

This PR obsoletes our newly introduced StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference.

This changes VerifiedDoubleReference to no longer support string style, as string references are not verifying when the class has not yet been loaded.

From the RSpec docs:

No checking will happen if the underlying object or class is not defined, but when run with it present (either as a full spec run or by explicitly preloading collaborators) a failure will be triggered if an invalid method is being stubbed or a method is called with an invalid number of arguments.

This PR essentially reverts nearly all of

This uses the Obsoletion feature to point leading edge branch users to a revised VerifiedDoubleReference instead.


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 corsonknowles changed the title Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleRerence for constants only Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only Oct 13, 2024
@corsonknowles
Copy link
Contributor Author

@pirj @bquorning Does this look like the right approach to you as outlined in the comments here?

@lucthev also tagging you as a courtesy since you identified the duplication here.

@corsonknowles corsonknowles marked this pull request as ready for review October 13, 2024 10:28
@corsonknowles corsonknowles requested a review from a team as a code owner October 13, 2024 10:28
Copy link
Member

@ydah ydah left a comment

Choose a reason for hiding this comment

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

Since this cop has been released once, it might be better not to delete or rename it until the next major version update, but to encourage migration with warning and changelogs, soft deprecation, and proceed with deletion and renaming in the next major version update.

@bquorning bquorning mentioned this pull request Oct 26, 2024
3 tasks
@corsonknowles corsonknowles changed the title Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only Obsolete StringAsInstanceDoubleConstant in favor of VerifiedDoubleReference for constants only Oct 28, 2024
@corsonknowles
Copy link
Contributor Author

I rebased and prepped this so it can be ready for the Monthly release

@bquorning
Copy link
Collaborator

The code looks good, but I am not sure if removing string from SupportedStyles in RSpec/VerifiedDoubleReference is enough of a breaking change that we should wait until v4? @pirj I think (/hope) you know the major/minor release rules better than I.

@pirj
Copy link
Member

pirj commented Nov 17, 2024

I’d check if we can avoid errors by using the obsoletion config.
If removal of the option errors out, it should go to v4.
But even if the above would error out, we can soft-deprecate the option by ignoring it, or printing a warning if it’s detected to use the deprecated style. It’s a hack, but worth it to push this out earlier.

@pirj
Copy link
Member

pirj commented Nov 17, 2024

So what happens if you add the following to .rubocop.yml?

RSpec/StringAsInstanceDoubleConstant:
  Enabled: false

RSpec/VerifiedDoubleReference:
  EnforcedStyle: string

and run rubocop?

# This cop can be configured in your configuration using the
# `EnforcedStyle` option and supports `--auto-gen-config`.
# @safety
# This cop is unsafe because the correction requires loading the class.
Copy link
Member

Choose a reason for hiding this comment

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

Nice!

@bquorning
Copy link
Collaborator

So what happens if you add the following to .rubocop.yml?

RSpec/StringAsInstanceDoubleConstant:
  Enabled: false

RSpec/VerifiedDoubleReference:
  EnforcedStyle: string

and run rubocop?

I tried that and first got this error:

Error: The `RSpec/StringAsInstanceDoubleConstant` cop has been renamed to `RSpec/VerifiedDoubleReference`.
(obsolete configuration found in .rubocop.yml, please update it)

I removed the obsolete configuration and ran rubocop again, this time getting a successful output, but got a warning:

Warning: RSpec/VerifiedDoubleReference does not support EnforcedStyle parameter.

Supported parameters are:

  - Enabled

@pirj
Copy link
Member

pirj commented Nov 17, 2024

If we used the changed_parameters, this would also result in an error, not a warning, right?

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

LGTM without the renamed in obsoletion conf.
If changed_parameters results in a warning, please add it.

Thank you!

@bquorning
Copy link
Collaborator

Both changed_parameters and changed_enforced_styles can be configures with a severity, so we could do e.g.

changed_enforced_styles:
  - cops: RSpec/VerifiedDoubleReference
    parameters: EnforcedStyle
    value: string
    reason: String references are not verifying unless the class is loaded.
    severity: warning

Of course, users will now get two warnings instead of one:

Warning: obsolete `EnforcedStyle: string` (for `RSpec/VerifiedDoubleReference`) found in .rubocop.yml
String references are not verifying unless the class is loaded.
Warning: RSpec/VerifiedDoubleReference does not support EnforcedStyle parameter.

Supported parameters are:

  - Enabled

@bquorning
Copy link
Collaborator

I think the correct thing to do is to add a changed_enforced_styles entry into config/obsoletion.yml. That RuboCop chooses to emit two different warnings is perhaps a bug, but I can live with that.

@corsonknowles Do you want to add this last change?

@corsonknowles
Copy link
Contributor Author

Thanks @bquorning -- what about the EnforcedStyle parameter no longer being needed, regardless of value?

@bquorning
Copy link
Collaborator

Ah, that is true. Perhaps the changed_parameters obsoletion config is better then.

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