forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_local.rb
46 lines (40 loc) · 1.26 KB
/
env_local.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
# 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.'
LOCAL_ENVIRONMENTS = %i[development? test?].to_set.freeze
minimum_target_rails_version 7.1
# @!method rails_env_local_candidate?(node)
def_node_matcher :rails_env_local_candidate?, <<~PATTERN
(or
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
)
PATTERN
def on_or(node)
rails_env_local_candidate?(node) do |*environments|
next unless environments.to_set == LOCAL_ENVIRONMENTS
add_offense(node) do |corrector|
corrector.replace(node, 'Rails.env.local?')
end
end
end
end
end
end
end