class JsDuck::Js::Evaluator

Evaluates Esprima AST node into Ruby object

Public Instance Methods

base_css_prefix?(ast) click to toggle source

True when MemberExpression == Ext.baseCSSPrefix

# File lib/jsduck/js/evaluator.rb, line 59
def base_css_prefix?(ast)
  ast["computed"] == false &&
    ast["object"]["type"] == "Identifier" &&
    ast["object"]["name"] == "Ext" &&
    ast["property"]["type"] == "Identifier" &&
    ast["property"]["name"] == "baseCSSPrefix"
end
key_value(key) click to toggle source

Turns object property key into string value

# File lib/jsduck/js/evaluator.rb, line 54
def key_value(key)
  key["type"] == "Identifier" ? key["name"] : key["value"]
end
to_value(ast) click to toggle source

Converts AST node into a value.

  • String literals become Ruby strings

  • Number literals become Ruby numbers

  • Regex literals become :regexp symbols

  • Array expressions become Ruby arrays

  • etc

For anything it doesn't know how to evaluate (like a function expression) it throws exception.

# File lib/jsduck/js/evaluator.rb, line 18
def to_value(ast)
  case ast["type"]
  when "ArrayExpression"
    ast["elements"].map {|e| to_value(e) }
  when "ObjectExpression"
    h = {}
    ast["properties"].each do |p|
      key = key_value(p["key"])
      value = to_value(p["value"])
      h[key] = value
    end
    h
  when "BinaryExpression"
    if ast["operator"] == "+"
      to_value(ast["left"]) + to_value(ast["right"])
    else
      throw "Unable to handle operator: " + ast["operator"]
    end
  when "MemberExpression"
    if base_css_prefix?(ast)
      "x-"
    else
      throw "Unable to handle this MemberExpression"
    end
  when "Literal"
    if ast["raw"] =~ /\A\//
      :regexp
    else
      ast["value"]
    end
  else
    throw "Unknown node type: " + ast["type"]
  end
end