From d6e2d7321e508f907af39f144a4260c2f56f42d3 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo <nene@triin.net> Date: Mon, 20 Jun 2011 13:11:42 +0300 Subject: [PATCH] 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. --- lib/jsduck/class.rb | 9 +++++++++ spec/class_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/lib/jsduck/class.rb b/lib/jsduck/class.rb index 6ed4156c..e77114a1 100644 --- a/lib/jsduck/class.rb +++ b/lib/jsduck/class.rb @@ -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| diff --git a/spec/class_spec.rb b/spec/class_spec.rb index c04580c3..99cb41dc 100644 --- a/spec/class_spec.rb +++ b/spec/class_spec.rb @@ -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 -- GitLab