-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from rainforestapp/RF-18143/pp/Lint-NoEnv-cop
[RF-18143] Port Lint/NoEnv cop from rf-stylez, fix it, make it autocorrect
- Loading branch information
Showing
9 changed files
with
174 additions
and
7 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 |
---|---|---|
|
@@ -10,3 +10,6 @@ | |
# rspec failure tracking | ||
.rspec_status | ||
Gemfile.lock | ||
|
||
# editors | ||
.vscode/ |
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 |
---|---|---|
|
@@ -5,19 +5,28 @@ require 'get_env/version' | |
Gem::Specification.new do |spec| | ||
spec.name = 'get_env' | ||
spec.version = GetEnv::VERSION | ||
spec.authors = ['Simon Mathieu', 'Bartek Kruszczynski', 'Jonathan Barber'] | ||
spec.email = ['[email protected]', '[email protected]'] | ||
spec.authors = [ | ||
'Simon Mathieu', | ||
'Bartek Kruszczynski', | ||
'Jonathan Barber', | ||
'Paul Padier'] | ||
spec.email = [ | ||
'[email protected]', | ||
'[email protected]', | ||
'[email protected]'] | ||
|
||
spec.summary = 'Read values from ENV in a reasonable way' | ||
spec.description = 'Read values from ENV in a reasonable way' | ||
spec.homepage = 'https://github.com/rainforestapp/get_env' | ||
spec.license = 'MIT' | ||
|
||
spec.files = ['lib/get_env.rb', 'lib/get_env/version.rb'] | ||
spec.files = Dir['lib/**/*'] | ||
spec.test_files = Dir['spec/**/*'] | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_development_dependency 'bundler' | ||
spec.add_development_dependency 'rake' | ||
spec.add_development_dependency 'rspec' | ||
spec.add_development_dependency 'rspec_junit_formatter' | ||
spec.add_development_dependency 'rubocop' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../rubocop/cop/lint/no_env' |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module GetEnv | ||
VERSION = '0.1.4' | ||
VERSION = '0.2.0' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# @example | ||
# # bad | ||
# ENV[] | ||
# | ||
# # bad | ||
# ENV.fetch(..) | ||
# | ||
# # good | ||
# GetEnv[] | ||
# | ||
# # good | ||
# GetEnv.fetch(...) | ||
class NoENV < Cop | ||
MSG = 'Use `GetEnv` instead of `ENV`.' | ||
|
||
def_node_matcher :is_ENV_index?, '(send (const nil? :ENV) :[] _key)' | ||
def_node_matcher :is_ENV_fetch?, '(send (const nil? :ENV) :fetch _key ...)' | ||
|
||
def on_const(node) | ||
parent = node.parent | ||
return unless is_ENV_index?(parent) || is_ENV_fetch?(parent) | ||
|
||
add_offense(node) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
if Gem::Version.new(RuboCop::Version.version) >= Gem::Version.new('0.82.0') | ||
corrector.replace(node, 'GetEnv') | ||
else | ||
corrector.replace(node.loc.expression, 'GetEnv') | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rubocop' | ||
require 'rubocop/rspec/support' | ||
require_relative '../../../../lib/rubocop/cop/lint/no_env' | ||
|
||
RSpec.describe RuboCop::Cop::Lint::NoENV do | ||
include RuboCop::RSpec::ExpectOffense | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
subject(:cop) { described_class.new(config) } | ||
|
||
describe '#[]' do | ||
it 'registers an offense when using `ENV`' do | ||
expect_offense(<<~RUBY) | ||
FOO = ENV['FOO'] | ||
^^^ Use `GetEnv` instead of `ENV`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
FOO = GetEnv['FOO'] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `GetEnv`' do | ||
expect_no_offenses(<<~RUBY) | ||
FOO = GetEnv['FOO'] | ||
RUBY | ||
end | ||
end | ||
|
||
describe '#fetch' do | ||
it 'registers an offense when using `ENV`' do | ||
expect_offense(<<~RUBY) | ||
do_the_thing(ENV.fetch('FOO')) | ||
^^^ Use `GetEnv` instead of `ENV`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
do_the_thing(GetEnv.fetch('FOO')) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `ENV` with a default value' do | ||
expect_offense(<<~RUBY) | ||
a = x + ENV.fetch('FOO', 42) | ||
^^^ Use `GetEnv` instead of `ENV`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
a = x + GetEnv.fetch('FOO', 42) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `GetEnv`' do | ||
expect_no_offenses(<<~RUBY) | ||
do_the_thing(GetEnv.fetch('FOO')) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `GetEnv` with a default value' do | ||
expect_no_offenses(<<~RUBY) | ||
a = x + GetEnv.fetch('FOO', 42) | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using `ENV` for a method not defined by `GetEnv`' do | ||
expect_no_offenses(<<~RUBY) | ||
ENV.map do |var| | ||
foo << var.lowercase | ||
end | ||
RUBY | ||
end | ||
end |