Class: Solargraph::Rspec::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/rspec/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.

Parameters:

  • ast (Parser::AST::Node)
  • comments (Hash) (defaults to: {})


90
91
92
93
94
# File 'lib/solargraph/rspec/walker.rb', line 90

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

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



84
85
86
# File 'lib/solargraph/rspec/walker.rb', line 84

def ast
  @ast
end

#commentsObject (readonly)

Returns the value of attribute comments.



86
87
88
# File 'lib/solargraph/rspec/walker.rb', line 86

def comments
  @comments
end

Class Method Details

.from_source(source) ⇒ Object

Parameters:

  • source (Solargraph::Source)


79
80
81
# File 'lib/solargraph/rspec/walker.rb', line 79

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

.normalize_ast(source) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/solargraph/rspec/walker.rb', line 68

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

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

Instance Method Details

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

Parameters:

  • node_type (Symbol)
  • args (Array) (defaults to: [])
  • block (Proc)


99
100
101
# File 'lib/solargraph/rspec/walker.rb', line 99

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

#walkvoid

This method returns an undefined value.



104
105
106
# File 'lib/solargraph/rspec/walker.rb', line 104

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