forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment_comparison.rb
117 lines (95 loc) · 3.32 KB
/
environment_comparison.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks that Rails.env is compared using `.production?`-like
# methods instead of equality against a string or symbol.
#
# @example
# # bad
# Rails.env == 'production'
#
# # bad, always returns false
# Rails.env == :test
#
# # good
# Rails.env.production?
class EnvironmentComparison < Base
extend AutoCorrector
MSG = 'Favor `%<bang>sRails.env.%<env>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
)
PATTERN
def_node_matcher :comparing_str_env_with_rails_env_on_rhs?, <<~PATTERN
(send
$str
{:== :!=}
(send (const {nil? cbase} :Rails) :env)
)
PATTERN
def_node_matcher :comparing_sym_env_with_rails_env_on_lhs?, <<~PATTERN
(send
(send (const {nil? cbase} :Rails) :env)
{:== :!=}
$sym
)
PATTERN
def_node_matcher :comparing_sym_env_with_rails_env_on_rhs?, <<~PATTERN
(send
$sym
{:== :!=}
(send (const {nil? cbase} :Rails) :env)
)
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
end
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)
end
end
private
def autocorrect(corrector, node)
replacement = build_predicate_method(node)
corrector.replace(node.source_range, replacement)
end
def build_predicate_method(node)
if rails_env_on_lhs?(node)
build_predicate_method_for_rails_env_on_lhs(node)
else
build_predicate_method_for_rails_env_on_rhs(node)
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)}?"
end
end
end
end
end