Commit 6baa6547 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Fix how identifiers are treated by Evaluator.

Identifiers now throw an exception when attempting to evaluate.

A separate key_to_s method now takes care of turning object keys into
strings.
parent bb616f8b
Loading
Loading
Loading
Loading
+18 −8
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ module JsDuck

      # foo: function() {}
      elsif property?(ast) && function?(ast["value"])
        make_method(to_value(ast["key"]), ast["value"])
        make_method(key_to_s(ast["key"]), ast["value"])

      else
        {:type => :property}
@@ -158,22 +158,32 @@ module JsDuck
      return classes.all? {|c| c.is_a? String } ? classes : []
    end

    def make_method(name, ast=nil)
      return {
        :type => :method,
        :name => name,
        :params => (ast && !empty_fn?(ast)) ? ast["params"].map {|p| to_s(p) } : []
      }
    end

    # -- various helper methods --

    # Turns ObjectExpression into Ruby Hash for easy lookup.  The keys
    # are turned into strings, but values are left as is for further
    # processing.
    def object_expression_to_hash(ast)
      h = {}
      if ast && ast["type"] == "ObjectExpression"
        ast["properties"].each do |p|
          h[to_value(p["key"])] = p["value"]
          h[key_to_s(p["key"])] = p["value"]
        end
      end
      return h
    end

    def make_method(name, ast=nil)
      return {
        :type => :method,
        :name => name,
        :params => (ast && !empty_fn?(ast)) ? ast["params"].map {|p| to_s(p) } : []
      }
    # Converts object expression property key to string value
    def key_to_s(key)
      key["type"] == "Identifier" ? key["name"] : key["value"]
    end

    # Fully serializes the node
+0 −2
Original line number Diff line number Diff line
@@ -25,8 +25,6 @@ module JsDuck
          h[key] = value
        end
        h
      when "Identifier"
        ast["name"]
      when "Literal"
        ast["value"]
      else
+9 −0
Original line number Diff line number Diff line
@@ -208,5 +208,14 @@ describe "JsDuck::Ast detects class with" do
        });
      EOS
    end

    it "Ext.define() with mixins as identifier" do
      detect(<<-EOS)[:mixins].should == []
        /** */
        Ext.define('MyClass', {
            mixins: someVar
        });
      EOS
    end
  end
end