Skip to content

Make Rails/EnvironmentComparison aware of Rails.env.to_sym #1481

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1481](https://github.com/rubocop/rubocop-rails/pull/1481): Recognize `Rails.env.to_sym` in `Rails/EnvironmentComparison`. ([@lovro-bikic][])
104 changes: 56 additions & 48 deletions lib/rubocop/cop/rails/environment_comparison.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
module RuboCop
module Cop
module Rails
# Checks that Rails.env is compared using `.production?`-like
# Checks that `Rails.env` is compared using `.production?`-like
# methods instead of equality against a string or symbol.
#
# @example
# # bad
# Rails.env == 'production'
# Rails.env.to_sym == :production
#
# # bad, always returns false
# Rails.env == :test
Expand All @@ -18,26 +19,40 @@ module Rails
class EnvironmentComparison < Base
extend AutoCorrector

MSG = 'Favor `%<bang>sRails.env.%<env>s?` over `%<source>s`.'
MSG = 'Favor `%<prefer>s` over `%<source>s`.'

SYM_MSG = 'Do not compare `Rails.env` with a symbol, it will always evaluate to `false`.'

RESTRICT_ON_SEND = %i[== !=].freeze

def_node_matcher :comparing_str_env_with_rails_env_on_lhs?, <<~PATTERN
(send
(send (const {nil? cbase} :Rails) :env)
{:== :!=}
$str
)
def_node_matcher :comparing_env_with_rails_env_on_lhs?, <<~PATTERN
{
(send
(send (const {nil? cbase} :Rails) :env)
{:== :!=}
$str
)
(send
(send (send (const {nil? cbase} :Rails) :env) :to_sym)
{:== :!=}
$sym
)
}
PATTERN

def_node_matcher :comparing_str_env_with_rails_env_on_rhs?, <<~PATTERN
(send
$str
{:== :!=}
(send (const {nil? cbase} :Rails) :env)
)
def_node_matcher :comparing_env_with_rails_env_on_rhs?, <<~PATTERN
{
(send
$str
{:== :!=}
(send (const {nil? cbase} :Rails) :env)
)
(send
$sym
{:== :!=}
(send (send (const {nil? cbase} :Rails) :env) :to_sym)
)
}
PATTERN

def_node_matcher :comparing_sym_env_with_rails_env_on_lhs?, <<~PATTERN
Expand All @@ -56,59 +71,52 @@ class EnvironmentComparison < Base
)
PATTERN

def_node_matcher :content, <<~PATTERN
({str sym} $_)
PATTERN

def on_send(node)
if (env_node = comparing_str_env_with_rails_env_on_lhs?(node) ||
comparing_str_env_with_rails_env_on_rhs?(node))
env, = *env_node
bang = node.method?(:!=) ? '!' : ''
message = format(MSG, bang: bang, env: env, source: node.source)

add_offense(node, message: message) do |corrector|
autocorrect(corrector, node)
end
check_env_comparison_with_rails_env(node)
check_sym_env_comparison_with_rails_env(node)
end

private

def check_env_comparison_with_rails_env(node)
return unless comparing_env_with_rails_env_on_lhs?(node) || comparing_env_with_rails_env_on_rhs?(node)

replacement = build_predicate_method(node)
message = format(MSG, prefer: replacement, source: node.source)

add_offense(node, message: message) do |corrector|
corrector.replace(node, replacement)
end
end

def check_sym_env_comparison_with_rails_env(node)
return unless comparing_sym_env_with_rails_env_on_lhs?(node) || comparing_sym_env_with_rails_env_on_rhs?(node)

add_offense(node, message: SYM_MSG) do |corrector|
autocorrect(corrector, node)
replacement = build_predicate_method(node)
corrector.replace(node, replacement)
end
end

private
def build_predicate_method(node)
bang = node.method?(:!=) ? '!' : ''

def autocorrect(corrector, node)
replacement = build_predicate_method(node)
receiver, argument = extract_receiver_and_argument(node)
receiver = receiver.receiver if receiver.method?(:to_sym)

corrector.replace(node, replacement)
"#{bang}#{receiver.source}.#{argument.value}?"
end

def build_predicate_method(node)
def extract_receiver_and_argument(node)
if rails_env_on_lhs?(node)
build_predicate_method_for_rails_env_on_lhs(node)
[node.receiver, node.first_argument]
else
build_predicate_method_for_rails_env_on_rhs(node)
[node.first_argument, node.receiver]
end
end

def rails_env_on_lhs?(node)
comparing_str_env_with_rails_env_on_lhs?(node) || comparing_sym_env_with_rails_env_on_lhs?(node)
end

def build_predicate_method_for_rails_env_on_lhs(node)
bang = node.method?(:!=) ? '!' : ''

"#{bang}#{node.receiver.source}.#{content(node.first_argument)}?"
end

def build_predicate_method_for_rails_env_on_rhs(node)
bang = node.method?(:!=) ? '!' : ''

"#{bang}#{node.first_argument.source}.#{content(node.receiver)}?"
comparing_env_with_rails_env_on_lhs?(node) || comparing_sym_env_with_rails_env_on_lhs?(node)
end
end
end
Expand Down
64 changes: 64 additions & 0 deletions spec/rubocop/cop/rails/environment_comparison_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,70 @@
end
end

context 'when comparing `Rails.env.to_sym` to a symbol' do
context 'when using equals' do
it 'registers an offense and corrects when `Rails.env.to_sym` is used on LHS' do
expect_offense(<<~RUBY)
Rails.env.to_sym == :production
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `Rails.env.production?` over `Rails.env.to_sym == :production`.
RUBY

expect_correction(<<~RUBY)
Rails.env.production?
RUBY
end

it 'registers an offense and corrects when `Rails.env.to_sym` is used on RHS' do
expect_offense(<<~RUBY)
:production == Rails.env.to_sym
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `Rails.env.production?` over `:production == Rails.env.to_sym`.
RUBY

expect_correction(<<~RUBY)
Rails.env.production?
RUBY
end
end

context 'when using not equals' do
it 'registers an offense and corrects when `Rails.env.to_sym` is used on LHS' do
expect_offense(<<~RUBY)
Rails.env.to_sym != :production
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `!Rails.env.production?` over `Rails.env.to_sym != :production`.
RUBY

expect_correction(<<~RUBY)
!Rails.env.production?
RUBY
end

it 'registers an offense and corrects when `Rails.env.to_sym` is used on RHS' do
expect_offense(<<~RUBY)
:production != Rails.env.to_sym
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `!Rails.env.production?` over `:production != Rails.env.to_sym`.
RUBY

expect_correction(<<~RUBY)
!Rails.env.production?
RUBY
end
end
end

context 'when comparing `Rails.env.to_sym` to a string' do
it 'does not register when `Rails.env.to_sym` is used on LHS' do
expect_no_offenses(<<~RUBY)
Rails.env.to_sym == 'production'
RUBY
end

it 'does not register when `Rails.env.to_sym` is used on RHS' do
expect_no_offenses(<<~RUBY)
'production' == Rails.env.to_sym
RUBY
end
end

it 'does not register an offense when using `#good_method`' do
expect_no_offenses(<<~RUBY)
Rails.env.production?
Expand Down