This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
/
constant_loader.rb
125 lines (108 loc) · 3.15 KB
/
constant_loader.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
module RubyLint
##
# The ConstantLoader is an iterator class (using {RubyLint::Iterator}) that
# iterates over an AST and tries to load the corresponding built-in
# definitions. For example, if it finds a constant node for the `ERB` class
# it will apply the definitions for `ERB` to the ones set in
# {RubyLint::ConstantLoader#definitions}.
#
# This class also takes care of bootstrapping the target definitions so that
# the bare minimum definitions (e.g. Module and Object) are always available.
# Global variables are also bootstrapped.
#
# @!attribute [r] loaded
# @return [Set] Set containing the loaded constants.
#
# @!attribute [r] definitions
# @return [RubyLint::Definition::RubyObject]
#
class ConstantLoader < Iterator
attr_reader :loaded, :definitions
##
# Built-in definitions that should be bootstrapped.
#
# @return [Array]
#
BOOTSTRAP_CONSTS = %w{Module Class Kernel BasicObject Object}
##
# List of global variables that should be bootstrapped.
#
# @return [Array]
#
BOOTSTRAP_GVARS = [
'$!', '$$', '$&', '$\'', '$*', '$+', '$,', '$-0', '$-F', '$-I', '$-K',
'$-W', '$-a', '$-d', '$-i', '$-l', '$-p', '$-v', '$-w', '$.', '$/', '$0',
'$:', '$;', '$<', '$=', '$>', '$?', '$@', '$DEBUG', '$FILENAME', '$KCODE',
'$LOADED_FEATURES', '$LOAD_PATH', '$PROGRAM_NAME', '$SAFE', '$VERBOSE',
'$\"', '$\\', '$_', '$`', '$stderr', '$stdin', '$stdout', '$~'
# Regexp $1, $2,... $99 are lazy loaded when first used
]
##
# Bootstraps various core definitions.
#
def bootstrap
types = VariablePredicates::RUBY_CLASSES.values
(BOOTSTRAP_CONSTS | types).each do |name|
load_constant(name)
end
BOOTSTRAP_GVARS.each do |gvar|
definitions.define_global_variable(gvar)
end
end
##
# Bootstraps various core definitions (Fixnum, Object, etc) and loads the
# extra constants referred in the supplied AST.
#
# @param [Array<RubyLint::AST::Node>] ast
#
def run(ast)
ast.each { |node| iterate(node) }
end
##
# Called after a new instance of the class is created.
#
def after_initialize
@loaded = Set.new
end
##
# @param [RubyLint::Node] node
#
def on_const(node)
load_constant(ConstantPath.new(node).root_node[1])
end
##
# Checks if the given constant is already loaded or not.
#
# @param [String] constant
# @return [TrueClass|FalseClass]
#
def loaded?(constant)
return loaded.include?(constant)
end
##
# @return [RubyLint::Definition::Registry]
#
def registry
return RubyLint.registry
end
##
# Tries to load the definitions for the given constant.
#
# @param [String] constant
#
def load_constant(constant)
return if loaded?(constant)
registry.load(constant)
return unless registry.include?(constant)
apply(constant)
end
private
##
# @param [String] constant
#
def apply(constant)
loaded << constant
registry.apply(constant, definitions)
end
end # ConstantLoader
end # RubyLint