forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunknown_env.rb
63 lines (54 loc) · 1.5 KB
/
unknown_env.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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks that environments called with `Rails.env` predicates
# exist.
#
# @example
# # bad
# Rails.env.proudction?
#
# # good
# Rails.env.production?
class UnknownEnv < Cop
include NameSimilarity
MSG = 'Unknown environment `%<name>s`.'
MSG_SIMILAR = 'Unknown environment `%<name>s`. ' \
'Did you mean `%<similar>s`?'
def_node_matcher :unknown_environment?, <<-PATTERN
(send
(send
{(const nil? :Rails) (const (cbase) :Rails)}
:env)
$#unknown_env_name?)
PATTERN
def on_send(node)
unknown_environment?(node) do |name|
add_offense(node, location: :selector, message: message(name))
end
end
private
def collect_variable_like_names(_scope)
environments.map { |env| env + '?' }
end
def message(name)
similar = find_similar_name(name.to_s, [])
if similar
format(MSG_SIMILAR, name: name, similar: similar)
else
format(MSG, name: name)
end
end
def unknown_env_name?(name)
name = name.to_s
name.end_with?('?') &&
!environments.include?(name[0..-2])
end
def environments
cop_config['Environments']
end
end
end
end
end