Class: Estreet::Expression

Inherits:
Node
  • Object
show all
Defined in:
lib/estreet/expression.rb

Instance Attribute Summary

Attributes inherited from Node

#source_location

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#as_json, #initialize, #loc, #type

Constructor Details

This class inherits a constructor from Estreet::Node

Class Method Details

.coerce(thing) ⇒ Object

Return an expression if at all possible



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/estreet/expression.rb', line 12

def self.coerce(thing)
  case thing
  when Expression
    thing
  when Array
    ArrayExpression.new(thing.map {|element| element })
  when Symbol, String
    Identifier.new(thing)
  else
    raise TypeError, "Can't convert to expression: #{thing.inspect}"
  end
end

Instance Method Details

#[](member) ⇒ Object

creates a computed MemberExpression



37
38
39
40
# File 'lib/estreet/expression.rb', line 37

def [](member)
  member = Literal[member] unless member.is_a? Expression
  MemberExpression.new(self, member, true)
end

#call(*args) ⇒ Object

Creates a call expression with the receiver as the callee and the specified arguments

Parameters:



32
33
34
# File 'lib/estreet/expression.rb', line 32

def call(*args)
  CallExpression.new(self, args)
end

#property(prop) ⇒ Object

creates a non-computed MemberExpression, e.g. ‘object.property` in JS



26
27
28
# File 'lib/estreet/expression.rb', line 26

def property(prop)
  MemberExpression.new(self, Identifier[prop], false)
end

#to_expressionObject



3
4
5
# File 'lib/estreet/expression.rb', line 3

def to_expression
  self
end

#to_statementObject



7
8
9
# File 'lib/estreet/expression.rb', line 7

def to_statement
  ExpressionStatement.new(self, source_location)
end