Class: Solargraph::Rails::Walker

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

Defined Under Namespace

Classes: Hook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast, comments = {}) ⇒ Walker

Returns a new instance of Walker.



61
62
63
64
65
# File 'lib/solargraph/rails/walker.rb', line 61

def initialize(ast, comments = {})
  @comments = comments
  @ast = ast
  @hooks = Hash.new([])
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



60
61
62
# File 'lib/solargraph/rails/walker.rb', line 60

def ast
  @ast
end

#commentsObject (readonly)

Returns the value of attribute comments.



60
61
62
# File 'lib/solargraph/rails/walker.rb', line 60

def comments
  @comments
end

Class Method Details

.from_source(source) ⇒ Object



56
57
58
# File 'lib/solargraph/rails/walker.rb', line 56

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

.normalize_ast(source) ⇒ Object



52
53
54
# File 'lib/solargraph/rails/walker.rb', line 52

def self.normalize_ast(source)
  NodeParser.parse_with_comments(source.code, source.filename)
end

Instance Method Details

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



67
68
69
# File 'lib/solargraph/rails/walker.rb', line 67

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

#walkObject



71
72
73
# File 'lib/solargraph/rails/walker.rb', line 71

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