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 new Lint/DeprecatedConstants cop #9324

Merged
merged 1 commit into from Jan 5, 2021

Conversation

koic
Copy link
Member

@koic koic commented Jan 1, 2021

This PR adds new Lint/DeprecatedConstants cop.

This cop checks for deprecated constants.

It has DeprecatedConstants option. If there is an alternative method, you can set the value as a string. Or set null to the value if there is no alternative.
By default, NIL, TRUE, FALSE and Random::DEFAULT are configured.

# bad
NIL
TRUE
FALSE
Random::DEFAULT # Return value of Ruby 2 is `Random` instance, Ruby 3.0 is `Random` class.

# good
nil
true
false
Random.new # `::DEFAULT` has been deprecated in Ruby 3, `.new` is compatible with Ruby 2.

The following are these sources of information.


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.

@koic koic force-pushed the add_new_deprecated_constants_cop branch 3 times, most recently from 7df54a1 to abe9fc4 Compare January 1, 2021 17:51
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 4, 2021

Nice cop, but it might also be a good idea to add some data about the Ruby version where something got deprecated to the config as well. This will allow us to have more informative messages.

@koic koic force-pushed the add_new_deprecated_constants_cop branch from abe9fc4 to dbdef11 Compare January 4, 2021 11:20
@koic
Copy link
Member Author

koic commented Jan 4, 2021

That's good idea! I added deprecated version for deprecated constants.


def message(good, bad, deprecated_version)
if deprecated_version
deprecated_message = ", which is deprecated in Ruby #{deprecated_version}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

"deprecated since" would be a slightly better wording here

Copy link
Member Author

Choose a reason for hiding this comment

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

I see! Thank your for the suggestion!

@koic koic force-pushed the add_new_deprecated_constants_cop branch from dbdef11 to 32915a2 Compare January 4, 2021 12:27
'NIL': ['nil', '2.4']
'TRUE': ['true', '2.4']
'FALSE': ['false', '2.4']
'Random::DEFAULT': ['Random.new', '3.0']
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure whether array is good. For example, hash may be preferable. Please let me know if you have feedback 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that hash would be better. This would spare us from having to use explicit nulls.

Copy link
Member Author

Choose a reason for hiding this comment

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

This would spare us from having to use explicit nulls.

Sure! That makes sense :-) I updated it!

@koic koic force-pushed the add_new_deprecated_constants_cop branch from 32915a2 to f33ca14 Compare January 5, 2021 03:45
This PR adds new `Lint/DeprecatedConstants` cop.

This cop checks for deprecated constants.

It has `DeprecatedConstants` config. If there is an alternative method,
you can set alternative value as `Alternative`.
And you can set the deprecated version as `DeprecatedVersion`.
These options can be omitted if they are not needed.
By default, `NIL`, `TRUE`, `FALSE` and `Random::DEFAULT` are configured.

```ruby
# bad
NIL
TRUE
FALSE
Random::DEFAULT # Return value of Ruby 2 is `Random` instance, Ruby 3.0 is `Random` class.

# good
nil
true
false
Random.new # `::DEFAULT` has been deprecated in Ruby 3, `.new` is compatible with Ruby 2.
```

The following are these sources of information.

- Remove (deprecated) `NIL`, `TRUE`, and `FALSE` ... ruby/ruby@62554ca
- Deprecate `Random::DEFAULT` ... https://bugs.ruby-lang.org/issues/17351
@koic koic force-pushed the add_new_deprecated_constants_cop branch from f33ca14 to b95afa7 Compare January 5, 2021 03:48
@bbatsov bbatsov merged commit c0d28ef into rubocop:master Jan 5, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 5, 2021

Thanks!

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