Class: RuboCop::Cop::Rails::EnvLocal
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Rails::EnvLocal
show all
- Extended by:
- AutoCorrector, TargetRailsVersion
- Defined in:
- lib/rubocop/cop/rails/env_local.rb
Overview
Checks for usage of ‘Rails.env.development? || Rails.env.test?` which can be replaced with `Rails.env.local?`, introduced in Rails 7.1.
Constant Summary
collapse
- MSG =
'Use `Rails.env.local?` instead.'
- MSG_NEGATED =
'Use `!Rails.env.local?` instead.'
- LOCAL_ENVIRONMENTS =
%i[development? test?].to_set.freeze
TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API
Instance Method Summary
collapse
minimum_target_rails_version, support_target_rails_version?
Instance Method Details
#not_rails_env_local?(node) ⇒ Object
33
34
35
|
# File 'lib/rubocop/cop/rails/env_local.rb', line 33
def_node_matcher :not_rails_env_local?, <<~PATTERN
(send #rails_env_local? :!)
PATTERN
|
#on_and(node) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/rubocop/cop/rails/env_local.rb', line 57
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
|
#on_or(node) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/rubocop/cop/rails/env_local.rb', line 37
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
|
#rails_env_local?(node) ⇒ Object
28
29
30
|
# File 'lib/rubocop/cop/rails/env_local.rb', line 28
def_node_matcher :rails_env_local?, <<~PATTERN
(send (send (const {cbase nil? } :Rails) :env) $%LOCAL_ENVIRONMENTS)
PATTERN
|