Class: Solargraph::Arc::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/arc/walker.rb

Defined Under Namespace

Classes: Hook

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast) ⇒ Walker

Returns a new instance of Walker.



28
29
30
31
# File 'lib/solargraph/arc/walker.rb', line 28

def initialize(ast)
  @ast   = ast
  @hooks = Hash.new([])
end

Class Method Details

.from_source(source) ⇒ Object



24
25
26
# File 'lib/solargraph/arc/walker.rb', line 24

def self.from_source(source)
  self.new(self.normalize_ast(source))
end

.normalize_ast(source) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/solargraph/arc/walker.rb', line 14

def self.normalize_ast(source)
  ast = source.node

  if ast.is_a?(::Parser::AST::Node)
    ast
  else
    NodeParser.parse(source.code, source.filename)
  end
end

Instance Method Details

#on(node_type, args = [], &block) ⇒ Object



33
34
35
# File 'lib/solargraph/arc/walker.rb', line 33

def on(node_type, args=[], &block)
  @hooks[node_type] << Hook.new(node_type, args, &block)
end

#walkObject



37
38
39
40
41
42
43
# File 'lib/solargraph/arc/walker.rb', line 37

def walk
  if @ast.is_a?(Array)
    @ast.each { |node| traverse(node) }
  else
    traverse(@ast)
  end
end