Class: RubyLint::ConstantLoader

Inherits:
Iterator
  • Object
show all
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.

Returns:

  • (Array)
%w{Module Class Kernel BasicObject Object}
BOOTSTRAP_GVARS =

List of global variables that should be bootstrapped.

Returns:

  • (Array)
[
  '$!', '$$', '$&', '$\'', '$*', '$+', '$,', '$-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

Attributes inherited from Iterator

#arity_cache, #arity_cache Hash containing the amount of arguments for

Instance Method Summary collapse

Methods inherited from Iterator

#execute_callback, #initialize, #iterate, #skip_child_nodes!

Constructor Details

This class inherits a constructor from RubyLint::Iterator

Instance Attribute Details

#definitionsRubyLint::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

#loadedSet (readonly)

Returns Set containing the loaded constants.

Returns:

  • (Set)

    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_initializeObject

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)

Parameters:



119
120
121
122
123
# File 'lib/ruby-lint/constant_loader.rb', line 119

def apply(constant)
  loaded << constant

  registry.apply(constant, definitions)
end

#bootstrapObject

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.

Parameters:



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.

Parameters:

Returns:

  • (TrueClass|FalseClass)


88
89
90
# File 'lib/ruby-lint/constant_loader.rb', line 88

def loaded?(constant)
  return loaded.include?(constant)
end

#on_const(node) ⇒ Object

Parameters:

  • node (RubyLint::Node)


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

#registryRubyLint::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.

Parameters:



64
65
66
# File 'lib/ruby-lint/constant_loader.rb', line 64

def run(ast)
  ast.each { |node| iterate(node) }
end