Commit 7b940a9f authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add property name detection to Ast class.

parent f501d3dc
Loading
Loading
Loading
Loading
+38 −3
Original line number Diff line number Diff line
@@ -34,7 +34,7 @@ module JsDuck
        make_class(to_s(exp["left"]))

      # var foo = Ext.extend("Parent", {})
      elsif var && ext_extend?(var["init"])
      elsif var && var["init"] && ext_extend?(var["init"])
        make_class(to_s(var["id"]), var["init"])

      # var Foo = ...
@@ -54,15 +54,35 @@ module JsDuck
        make_method(to_s(exp["left"]), exp["right"])

      # var foo = function() {}
      elsif var && function?(var["init"])
      elsif var && var["init"] && function?(var["init"])
        make_method(to_s(var["id"]), var["init"])

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

      # foo = ...
      elsif exp && assignment?(exp)
        make_property(to_s(exp["left"]), exp["right"])

      # var foo = ...
      elsif var
        make_property(to_s(var["id"]), var["init"])

      # foo: ...
      elsif property?(ast)
        make_property(key_value(ast["key"]), ast["value"])

      # foo;
      elsif exp && ident?(exp)
        make_property(to_s(exp))

      # "foo";
      elsif exp && string?(exp)
        make_property(to_value(exp))

      else
        {:type => :property}
        make_property()
      end
    end

@@ -104,6 +124,14 @@ module JsDuck
      ast["type"] == "Property"
    end

    def ident?(ast)
      ast["type"] == "Identifier"
    end

    def string?(ast)
      ast["type"] == "Literal" && ast["value"].is_a?(String)
    end

    # Class name begins with upcase char
    def class_name?(name)
      return name.split(/\./).last =~ /\A[A-Z]/
@@ -166,6 +194,13 @@ module JsDuck
      }
    end

    def make_property(name=nil, ast=nil)
      return {
        :type => :property,
        :name => name,
      }
    end

    # -- various helper methods --

    # Turns ObjectExpression into Ruby Hash for easy lookup.  The keys
+54 −0
Original line number Diff line number Diff line
require "jsduck/ast"
require "jsduck/esprima_parser"

describe "JsDuck::Ast detects property with" do
  def detect(string)
    node = JsDuck::EsprimaParser.new(string).parse[0]
    return JsDuck::Ast.new.detect(node[:code])
  end

  describe "name in" do
    it "var declaration" do
      detect("/** */ var foo;")[:name].should == "foo"
    end

    it "var declaration with initialization" do
      detect("/** */ foo = 5;")[:name].should == "foo"
    end

    it "assignment to var" do
      detect("/** */ foo = 5;")[:name].should == "foo"
    end

    it "assignment to object property" do
      detect("/** */ foo.bar.baz = 5;")[:name].should == "foo.bar.baz"
    end

    it "object property" do
      detect(<<-EOS)[:name].should == "foo"
        Foo = {
            /** */
            foo: 5
        };
      EOS
    end

    it "object with string key" do
      detect(<<-EOS)[:name].should == "foo"
        Foo = {
            /** */
            "foo": 5
        };
      EOS
    end

    it "lonely identifier" do
      detect("/** */ foo;")[:name].should == "foo"
    end

    it "lonely string" do
      detect("/** */ 'foo';")[:name].should == "foo"
    end
  end

end