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

feat(cop): add Platanus/EnvInModule cop #13

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ Platanus/SingularSerializer:
Include:
- "app/serializers/**/*.rb"
VersionAdded: "<<next>>"

Platanus/EnvInModule:
Description: 'Prefer retrieving environment variables in a dedicated module.'
Enabled: true
Include:
- 'app/**/*.rb'
VersionAdded: '<<next>>'

63 changes: 63 additions & 0 deletions lib/rubocop/cop/platanus/env_in_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

module RuboCop
module Cop
module Platanus
# Environment variables should not be retrieved directly. Prefer retrieving them in a
# dedicated module. This centralizes their location, enhances their functionality and makes
# them easier to stub.
#
# @example
#
# # bad
# class Foo
# BAR = ENV.fetch('BAR')
#
# def foo_method
# BAR
# end
# end
#
# # bad
# class Foo
# BAR = ENV['BAR']
#
# def foo_method
# BAR
# end
# end
#
# # good
# module EnvironmentVariables
# extend self
#
# BAR = ENV.fetch('BAR')
#
# def bar
# BAR
# end
# end
#
# class Foo
# def foo_method
# EnvironmentVariables.bar
# end
# end
#
class EnvInModule < Base
MSG = 'Use dedicated module for environment variables retrieval instead.'

# @!method env_retrieve?(node)
def_node_matcher :env_retrieve?, <<~PATTERN
(send (const nil? :ENV) { :fetch | :[] } ...)
PATTERN

def on_send(node)
return unless env_retrieve?(node)

add_offense(node)
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/platanus_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
require_relative 'platanus/no_where_aasm_state'
require_relative 'platanus/pundit_in_application_controller'
require_relative 'platanus/singular_serializer'
require_relative 'platanus/env_in_module'
41 changes: 41 additions & 0 deletions spec/rubocop/cop/platanus/env_in_module_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::Platanus::EnvInModule, :config do
let(:config) { RuboCop::Config.new }

it 'registers an offense when using `ENV.fetch`' do
expect_offense <<~RUBY
class Foo
BAR = ENV.fetch('BAR')
^^^^^^^^^^^^^^^^ Use dedicated module for environment variables retrieval instead.

def foo_method
BAR
end
end
RUBY
end

it 'registers an offense when using `ENV[]`' do
expect_offense <<~RUBY
class Foo
BAR = ENV['BAR']
^^^^^^^^^^ Use dedicated module for environment variables retrieval instead.

def foo_method
BAR
end
end
RUBY
end

it 'does not register an offense when not using `ENV.fetch` or `ENV`' do
expect_no_offenses <<~RUBY
class Foo
def foo_method
EnviromentVariables.bar
end
end
RUBY
end
end