Commit 1270090d authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Basic auto-detect for Ext.define(function) syntax.

A patch by Patrick Gleichmann.

Specs by me.

Refs #527
parent ad19c6d3
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -106,7 +106,15 @@ module JsDuck
        cls[:members] = []
        cls[:code_type] = :ext_define

        ast["arguments"][1].each_property do |key, value, pair|
        definition_node = ast["arguments"][1]

        # Support both function and object notation
        if definition_node.function_expression?
          definition_node = definition_node.return_statement_expression
          return if definition_node.nil?
        end

        definition_node.each_property do |key, value, pair|
          if tag = Js::ExtDefine.get_tag_by_pattern(key)
            tag.parse_ext_define(cls, value)
          else
+27 −0
Original line number Diff line number Diff line
@@ -133,6 +133,33 @@ module JsDuck
        return nil
      end

      # Returns the return statement's expression for the current function
      # expression.
      def return_statement_expression
        return unless function_expression?

        node = child("body")
        if node.block_statement?
          node["body"].each do |node|
            if node.return_statement?
              node = node["argument"]
              return node.object_expression? ? node : nil
            end
          end
        end 

        return nil
      end


      def block_statement?
        @node["type"] == "BlockStatement"
      end

      def return_statement?
        @node["type"] == "ReturnStatement"
      end

      # Returns line number in parsed source where the Node resides.
      def linenr
        # Get line number from third place at range array.
+31 −1
Original line number Diff line number Diff line
@@ -32,13 +32,20 @@ describe "JsDuck::Js::Ast detects class with" do
      detect("/** */ var MyClass = Ext.extend(Your.Class, {  });")[:name].should == "MyClass"
    end

    it "Ext.define()" do
    it "Ext.define() with object literal" do
      detect(<<-EOS)[:name].should == "MyClass"
        /** */
        Ext.define('MyClass', {
        });
      EOS
    end

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

  describe "extends in" do
@@ -77,6 +84,29 @@ describe "JsDuck::Js::Ast detects class with" do
      EOS
    end

    it "Ext.define() with function returning object" do
      detect(<<-EOS)[:extends].should == "Your.Class"
        /** */
        Ext.define('MyClass', function() {
            return {extend: "Your.Class"};
        });
      EOS
    end

    # TODO: Doesn't work at the moment
    #
    # it "Ext.define() with function returning two possible objects" do
    #   detect(<<-EOS)[:extends].should == "Ext.Base"
    #     /** */
    #     Ext.define('MyClass', function() {
    #         if (someCondition) {
    #             return {extend: "Your.Class1"};
    #         }
    #         return {extend: "Your.Class2"};
    #     });
    #   EOS
    # end

    it "Ext.define() with no extend: in config object" do
      detect(<<-EOS)[:extends].should == "Ext.Base"
        /** */