-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #10568. This cop checks for consistent usage of `ENV['HOME']`. If `nil` is used as the second argument of `ENV.fetch`, it is treated as a bad case like `ENV[]`. This avoids conflicts with `Style/FetchEnvVar`'s autocorrection. ## Safety The cop is unsafe because The result when `nil` is assigned to `ENV['HOME']` changes: ```ruby ENV['HOME'] = nil ENV['HOME'] # => nil Dir.home # => '/home/foo' ``` ## Example ```ruby # bad ENV['HOME'] ENV.fetch('HOME', nil) # good Dir.home # good ENV.fetch('HOME', default) ```
- Loading branch information
Showing
7 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10568](https://github.com/rubocop/rubocop/issues/10568): Add new `Style/EnvHome` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for consistent usage of `ENV['HOME']`. If `nil` is used as | ||
# the second argument of `ENV.fetch`, it is treated as a bad case like `ENV[]`. | ||
# | ||
# @safety | ||
# The cop is unsafe because the result when `nil` is assigned to `ENV['HOME']` changes: | ||
# | ||
# [source,ruby] | ||
# ---- | ||
# ENV['HOME'] = nil | ||
# ENV['HOME'] # => nil | ||
# Dir.home # => '/home/foo' | ||
# ---- | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# ENV['HOME'] | ||
# ENV.fetch('HOME', nil) | ||
# | ||
# # good | ||
# Dir.home | ||
# | ||
# # good | ||
# ENV.fetch('HOME', default) | ||
# | ||
class EnvHome < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `Dir.home` instead.' | ||
RESTRICT_ON_SEND = %i[[] fetch].freeze | ||
|
||
# @!method env_home?(node) | ||
def_node_matcher :env_home?, <<~PATTERN | ||
(send | ||
(const {cbase nil?} :ENV) {:[] :fetch} | ||
(str "HOME") | ||
...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless env_home?(node) | ||
return if node.arguments.count == 2 && !node.arguments[1].nil_type? | ||
|
||
add_offense(node) do |corrector| | ||
corrector.replace(node, 'Dir.home') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::EnvHome, :config do | ||
it "registers and corrects an offense when using `ENV['HOME']`" do | ||
expect_offense(<<~RUBY) | ||
ENV['HOME'] | ||
^^^^^^^^^^^ Use `Dir.home` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir.home | ||
RUBY | ||
end | ||
|
||
it "registers and corrects an offense when using `ENV.fetch('HOME')`" do | ||
expect_offense(<<~RUBY) | ||
ENV.fetch('HOME') | ||
^^^^^^^^^^^^^^^^^ Use `Dir.home` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir.home | ||
RUBY | ||
end | ||
|
||
it "registers and corrects an offense when using `ENV.fetch('HOME', nil)`" do | ||
expect_offense(<<~RUBY) | ||
ENV.fetch('HOME', nil) | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `Dir.home` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir.home | ||
RUBY | ||
end | ||
|
||
it "registers and corrects an offense when using `::ENV['HOME']`" do | ||
expect_offense(<<~RUBY) | ||
::ENV['HOME'] | ||
^^^^^^^^^^^^^ Use `Dir.home` instead. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Dir.home | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `Dir.home`' do | ||
expect_no_offenses(<<~RUBY) | ||
Dir.home | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when using `ENV['HOME'] = '/home/foo'`" do | ||
expect_no_offenses(<<~RUBY) | ||
ENV['HOME'] = '/home/foo' | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when using `ENV.fetch('HOME', default)`" do | ||
expect_no_offenses(<<~RUBY) | ||
ENV.fetch('HOME', default) | ||
RUBY | ||
end | ||
end |