Commit d6e2d732 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Don't inherit static members in singleton classes.

Actually for any singleton just say it has no static members,
but if there are static members defined, print warning.
parent ed4fe0c0
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -96,6 +96,15 @@ module JsDuck
    # When parent and child have members with same name,
    # member from child overrides tha parent member.
    def members_hash(type, context=:members)
      # Singletons have no static members
      if @doc[:singleton] && context == :statics
        # Warn if singleton has static members
        if @doc[context][type].length > 0
          Logger.instance.warn("Singleton class #{@doc[:name]} can't have static members, remove the @static tag.")
        end
        return {}
      end

      all_members = parent ? parent.members_hash(type, context) : {}

      mixins.each do |mix|
+40 −0
Original line number Diff line number Diff line
@@ -64,6 +64,15 @@ describe JsDuck::Class do
        });
      @classes["ChildClass"] = @child
      @child.relations = @classes

      @singletonChild = JsDuck::Class.new({
          :name => "Singleton",
          :extends => "ParentClass",
          :mixins => ["MixinClass"],
          :singleton => true,
        });
      @classes["Singleton"] = @singletonChild
      @singletonChild.relations = @classes
    end

    it "returns constructor as first method" do
@@ -113,6 +122,21 @@ describe JsDuck::Class do
      it "overrides parent class members with the same name" do
        @members["foo"][:owner].should == "ChildClass"
      end

      describe "singleton class" do
        before do
          @members = @singletonChild.members_hash(:method)
        end

        it "inherits all instance members from parent" do
          @members.should have_key("baz")
          @members.should have_key("foo")
        end

        it "inherites all instace members from mixins" do
          @members.should have_key("xxx")
        end
      end
    end

    describe "(:method, :statics)" do
@@ -143,6 +167,22 @@ describe JsDuck::Class do
      it "inherits inheritableStatics from mixins" do
        @members.should have_key("mixinB")
      end

      describe "singleton class" do
        before do
          @members = @singletonChild.members_hash(:method, :statics)
        end

        it "doesn't inherit any static members from parent" do
          @members.should_not have_key("parentA")
          @members.should_not have_key("parentB")
        end

        it "doesn't inherit any static members from mixins" do
          @members.should_not have_key("mixinA")
          @members.should_not have_key("mixinB")
        end
      end
    end
  end