forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_local.rb
More file actions
93 lines (75 loc) · 2.47 KB
/
env_local.rb
File metadata and controls
93 lines (75 loc) · 2.47 KB
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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Checks for usage of `Rails.env.development? || Rails.env.test?` which
# can be replaced with `Rails.env.local?`, introduced in Rails 7.1.
#
# @example
#
# # bad
# Rails.env.development? || Rails.env.test?
#
# # good
# Rails.env.local?
#
class EnvLocal < Base
extend AutoCorrector
extend TargetRailsVersion
MSG = 'Use `Rails.env.local?` instead.'
MSG_NEGATED = 'Use `!Rails.env.local?` instead.'
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze
minimum_target_rails_version 7.1
# @!method rails_env_local?(node)
def_node_matcher :rails_env_local?, <<~PATTERN
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
PATTERN
# @!method not_rails_env_local?(node)
def_node_matcher :not_rails_env_local?, <<~PATTERN
(send #rails_env_local? :!)
PATTERN
def on_or(node)
lhs, rhs = *node.children
return unless rails_env_local?(rhs)
nodes = [rhs]
if rails_env_local?(lhs)
nodes << lhs
elsif lhs.or_type? && rails_env_local?(lhs.rhs)
nodes << lhs.rhs
end
return unless environments(nodes).to_set == LOCAL_ENVIRONMENTS
range = offense_range(nodes)
add_offense(range) do |corrector|
corrector.replace(range, 'Rails.env.local?')
end
end
def on_and(node)
lhs, rhs = *node.children
return unless not_rails_env_local?(rhs)
nodes = [rhs]
if not_rails_env_local?(lhs)
nodes << lhs
elsif lhs.operator_keyword? && not_rails_env_local?(lhs.rhs)
nodes << lhs.rhs
end
return unless environments(nodes).to_set == LOCAL_ENVIRONMENTS
range = offense_range(nodes)
add_offense(range, message: MSG_NEGATED) do |corrector|
corrector.replace(range, '!Rails.env.local?')
end
end
private
def environments(nodes)
if nodes[0].method?(:!)
nodes.map { |node| node.receiver.method_name }
else
nodes.map(&:method_name)
end
end
def offense_range(nodes)
nodes[1].source_range.begin.join(nodes[0].source_range.end)
end
end
end
end
end