Sha256: c4f79e2746875711847bd9b705b38e0d627915b36fd91954662eb034611fb125

Contents?: true

Size: 1.9 KB

Versions: 30

Compression:

Stored size: 1.9 KB

Contents

module JsDuck
  module Js

    # Evaluates Esprima AST node into Ruby object
    class Evaluator

      # 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.
      #
      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

      # Turns object property key into string value
      def key_value(key)
        key["type"] == "Identifier" ? key["name"] : key["value"]
      end

      # True when MemberExpression == Ext.baseCSSPrefix
      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

    end

  end
end

Version data entries

30 entries across 30 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/js/evaluator.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/js/evaluator.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/js/evaluator.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/js/evaluator.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/js/evaluator.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/js/evaluator.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.10 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.9 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.8 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.7 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.5 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.4 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.3 lib/jsduck/js/evaluator.rb
jsduck-troopjs-0.0.1 lib/jsduck/js/evaluator.rb
jsduck-6.0.0beta lib/jsduck/js/evaluator.rb
jsduck-5.3.4 lib/jsduck/js/evaluator.rb
jsduck-5.3.3 lib/jsduck/js/evaluator.rb
jsduck-5.3.2 lib/jsduck/js/evaluator.rb
jsduck-5.3.1 lib/jsduck/js/evaluator.rb