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
Enhance the autocorrect for Style/FetchEnvVar
#10585
Enhance the autocorrect for Style/FetchEnvVar
#10585
Conversation
1518b26
to
0432e87
Compare
0432e87
to
41b8400
Compare
@@ -0,0 +1 @@ | |||
* [#10585](https://github.com/rubocop/rubocop/pull/10585): Enhance the autocorrect for `RSpec/FetchEnvVar`. ([@johnny-miyake][]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RSpec? :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, thanks!
config/default.yml
Outdated
@@ -3538,6 +3538,9 @@ Style/FetchEnvVar: | |||
VersionAdded: '1.28' | |||
# Environment variables to be excluded from the inspection. | |||
AllowedVars: [] | |||
# - `SecondArgOfFetch` puts the RHS to the second argument of `ENV.fetch`. (default) | |||
# - `BlockContent` puts the RHS into a block. | |||
BasicLiteralRhs: 'SecondArgOfFetch' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to me that adds extra complexity for the end users - I generally don't see why someone would want to put basic literals in a block, as there are no performance gains for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your point. If the option doesn't have much benefit for most people, I'm willing to remove it as the less code or options, the better.
Some developers don't want to have multiple ways to write code, so I thought it might be convenient for them, but it may be better we concerned about that when many people really need it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I'm of the "less is more" school of thought, so I prefer to add options only when there's some demand for them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. I removed the option. 3797914
RSpec/FetchEnvVar
Style/FetchEnvVar
I added some additional test cases and fixed the code to make them pass. |
As I noticed in this PR, I think most
User can get the |
Yeah, that sounds correct. This problem somehow looks silmilar to #10582 . |
Still, to me this seems like the type of thing that can be optimized as the |
Just to confirm, the above comment means that the both codes below are read as “Get 'TERM' environment variable if it's set, otherwise ENV['TERM'] || something
ENV.fetch('TERM', something) |
It seems to me that this is what @koic meant, but he has to clarify himself. We can always tackle this separately in a different cop, although perhaps that'd be an overkill. |
I'm sorry, I was misunderstanding 💦 The above comment makes sense. So, there is no problem correcting it with compatible API. |
#10578
This PR enhances the autocorrect for
Style/FetchEnvVar
.When an
ENV[]
is the LHS of||
, the autocorrect makes the RHS the default value ofENV.fetch
.Examples
Please refer to the RSpec file for more examples.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.