Class: RubyLint::ConstantLoader
- Defined in:
- lib/ruby-lint/constant_loader.rb
Overview
The ConstantLoader is an iterator class (using 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
#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.
Constant Summary collapse
- BOOTSTRAP_CONSTS =
Built-in definitions that should be bootstrapped.
%w{Module Class Kernel BasicObject Object}
- BOOTSTRAP_GVARS =
List of global variables that should be bootstrapped.
[ '$!', '$$', '$&', '$\'', '$*', '$+', '$,', '$-0', '$-F', '$-I', '$-K', '$-W', '$-a', '$-d', '$-i', '$-l', '$-p', '$-v', '$-w', '$.', '$/', '$0', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$:', '$;', '$<', '$=', '$>', '$?', '$@', '$DEBUG', '$FILENAME', '$KCODE', '$LOADED_FEATURES', '$LOAD_PATH', '$PROGRAM_NAME', '$SAFE', '$VERBOSE', '$\"', '$\\', '$_', '$`', '$stderr', '$stdin', '$stdout', '$~' ]
Instance Attribute Summary collapse
- #definitions ⇒ RubyLint::Definition::RubyObject readonly
-
#loaded ⇒ Set
readonly
Set containing the loaded constants.
Attributes inherited from Iterator
#arity_cache, #arity_cache Hash containing the amount of arguments for
Instance Method Summary collapse
-
#after_initialize ⇒ Object
Called after a new instance of the class is created.
- #apply(constant) ⇒ Object private
-
#bootstrap ⇒ Object
Bootstraps various core definitions.
-
#load_constant(constant) ⇒ Object
Tries to load the definitions for the given constant.
-
#loaded?(constant) ⇒ TrueClass|FalseClass
Checks if the given constant is already loaded or not.
- #on_const(node) ⇒ Object
- #registry ⇒ RubyLint::Definition::Registry
-
#run(ast) ⇒ Object
Bootstraps various core definitions (Fixnum, Object, etc) and loads the extra constants referred in the supplied AST.
Methods inherited from Iterator
#execute_callback, #initialize, #iterate, #skip_child_nodes!
Constructor Details
This class inherits a constructor from RubyLint::Iterator
Instance Attribute Details
#definitions ⇒ RubyLint::Definition::RubyObject (readonly)
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 |
# File 'lib/ruby-lint/constant_loader.rb', line 19 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', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$:', '$;', '$<', '$=', '$>', '$?', '$@', '$DEBUG', '$FILENAME', '$KCODE', '$LOADED_FEATURES', '$LOAD_PATH', '$PROGRAM_NAME', '$SAFE', '$VERBOSE', '$\"', '$\\', '$_', '$`', '$stderr', '$stdin', '$stdout', '$~' ] ## # 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 |
#loaded ⇒ Set (readonly)
Returns Set containing the loaded constants.
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 |
# File 'lib/ruby-lint/constant_loader.rb', line 19 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', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$:', '$;', '$<', '$=', '$>', '$?', '$@', '$DEBUG', '$FILENAME', '$KCODE', '$LOADED_FEATURES', '$LOAD_PATH', '$PROGRAM_NAME', '$SAFE', '$VERBOSE', '$\"', '$\\', '$_', '$`', '$stderr', '$stdin', '$stdout', '$~' ] ## # 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 |
Instance Method Details
#after_initialize ⇒ Object
Called after a new instance of the class is created.
71 72 73 |
# File 'lib/ruby-lint/constant_loader.rb', line 71 def after_initialize @loaded = Set.new end |
#apply(constant) ⇒ Object (private)
119 120 121 122 123 |
# File 'lib/ruby-lint/constant_loader.rb', line 119 def apply(constant) loaded << constant registry.apply(constant, definitions) end |
#bootstrap ⇒ Object
Bootstraps various core definitions.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby-lint/constant_loader.rb', line 46 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 |
#load_constant(constant) ⇒ Object
Tries to load the definitions for the given constant.
104 105 106 107 108 109 110 111 112 |
# File 'lib/ruby-lint/constant_loader.rb', line 104 def load_constant(constant) return if loaded?(constant) registry.load(constant) return unless registry.include?(constant) apply(constant) end |
#loaded?(constant) ⇒ TrueClass|FalseClass
Checks if the given constant is already loaded or not.
88 89 90 |
# File 'lib/ruby-lint/constant_loader.rb', line 88 def loaded?(constant) return loaded.include?(constant) end |
#on_const(node) ⇒ Object
78 79 80 |
# File 'lib/ruby-lint/constant_loader.rb', line 78 def on_const(node) load_constant(ConstantPath.new(node).root_node[1]) end |
#registry ⇒ RubyLint::Definition::Registry
95 96 97 |
# File 'lib/ruby-lint/constant_loader.rb', line 95 def registry return RubyLint.registry end |
#run(ast) ⇒ Object
Bootstraps various core definitions (Fixnum, Object, etc) and loads the extra constants referred in the supplied AST.
64 65 66 |
# File 'lib/ruby-lint/constant_loader.rb', line 64 def run(ast) ast.each { |node| iterate(node) } end |