-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
final.rb
50 lines (44 loc) 路 1.4 KB
/
final.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
# frozen_string_literal: true
# typed: false
module T::Private::Final
module NoInherit
def inherited(arg)
super(arg)
raise "#{self} was declared as final and cannot be inherited"
end
end
module NoIncludeExtend
def included(arg)
super(arg)
raise "#{self} was declared as final and cannot be included"
end
def extended(arg)
super(arg)
raise "#{self} was declared as final and cannot be extended"
end
end
def self.declare(mod)
if !mod.is_a?(Module)
raise "#{mod} is not a class or module and cannot be declared as final with `final!`"
end
if final_module?(mod)
raise "#{mod} was already declared as final and cannot be re-declared as final"
end
if T::AbstractUtils.abstract_module?(mod)
raise "#{mod} was already declared as abstract and cannot be declared as final"
end
if T::Private::Sealed.sealed_module?(mod)
raise "#{mod} was already declared as sealed and cannot be declared as final"
end
mod.extend(mod.is_a?(Class) ? NoInherit : NoIncludeExtend)
mark_as_final_module(mod)
mark_as_final_module(mod.singleton_class)
T::Private::Methods.install_hooks(mod)
end
def self.final_module?(mod)
mod.instance_variable_defined?(:@sorbet_final_module)
end
private_class_method def self.mark_as_final_module(mod)
mod.instance_variable_set(:@sorbet_final_module, true)
end
end