Class: Esprima::AST

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/esprima/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree, environment = nil) ⇒ AST

Returns a new instance of AST.



9
10
11
12
# File 'lib/esprima/ast.rb', line 9

def initialize(tree, environment = nil)
  @esprima = @environment || Esprima.new_environment
  @tree = tree
end

Instance Attribute Details

#esprimaObject (readonly)

Returns the value of attribute esprima.



7
8
9
# File 'lib/esprima/ast.rb', line 7

def esprima
  @esprima
end

#treeObject (readonly)

Returns the value of attribute tree.



7
8
9
# File 'lib/esprima/ast.rb', line 7

def tree
  @tree
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/esprima/ast.rb', line 26

def [](key)
  @tree[key]
end

#eachObject



30
31
32
33
34
35
36
37
38
# File 'lib/esprima/ast.rb', line 30

def each
  if block_given?
    each_node { |node| yield Esprima::AST.new(node, @esprima) }
  else
    nodes = []
    each_node { |node| nodes << Esprima::AST.new(node, @esprima) }
    nodes.to_enum
  end
end

#each_rawObject



40
41
42
43
44
45
46
47
48
# File 'lib/esprima/ast.rb', line 40

def each_raw
  if block_given?
    each_raw_node(tree) { |node| yield node }
  else
    nodes = []
    each_raw_node(tree) { |node| nodes << node }
    nodes.to_enum
  end
end

#is_hash?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/esprima/ast.rb', line 22

def is_hash?
  @tree.is_a?(Hash)
end

#is_list?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/esprima/ast.rb', line 18

def is_list?
  @tree.is_a?(Array)
end

#to_ecma(options = {}) ⇒ Object



14
15
16
# File 'lib/esprima/ast.rb', line 14

def to_ecma(options = {})
  Escodegen::Generator.new.generate(@tree, options)
end