diff --git a/README.md b/README.md
index 23444f72731a9613ebc203827eb9ad5ccdb6701e..62461a07499cf61c01b580ed5c57ed32ab1ed91b 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,11 @@ Basically JsDuck thinks that the following doc-comment really sucks:
*
{@link Ext.form.TextField#stripCharsRe stripCharsRe}
:
* filter characters after being typed in, but before being validated
*
+ *
+ * @constructor Creates a new TextField
+ * @param {Object} config Configuration options
+ *
+ * @xtype textfield
*/
Ext.form.TextField = Ext.extend(Ext.form.Field, {
@@ -69,13 +74,17 @@ it would like that you wrote comments like that instead:
* filter out keystrokes before any validation occurs
* - `{@link Ext.form.TextField#stripCharsRe stripCharsRe}` :
* filter characters after being typed in, but before being validated
+ *
+ * @xtype textfield
*/
Ext.form.TextField = Ext.extend(Ext.form.Field, {
As you can see, JsDuck supports formatting comments with friendly
[Markdown][] syntax. And it can infer several things from the code
(like @class and @extends in this case), so you don't have to repeat
-yourself.
+yourself. Also the constructor documentation is inherited from parent
+class - so you don't have to restate that it takes a config object
+again.
That's basically it. Have fun.
@@ -121,7 +130,6 @@ currently missing:
* List of subclasses
* Tree of parent classes
* Syntax highlighting of code examples
-* Constructor first in methods list
* Search, not just searching from official ExtJS documentation
* Support for custom @tags
diff --git a/lib/jsduck/class.rb b/lib/jsduck/class.rb
index 951cf2998c0824e3067c1e493bf68a22b2d8f551..a95744a5e6cbf778a9844831c9107597d75bf920 100644
--- a/lib/jsduck/class.rb
+++ b/lib/jsduck/class.rb
@@ -25,9 +25,30 @@ module JsDuck
end
# Returns array of all public members of particular type in a class,
- # sorted by name. See members_hash for details.
+ # sorted by name.
+ #
+ # For methods the the constructor is listed as first method having
+ # the same name as class itself.
+ #
+ # See members_hash for details.
def members(type)
- members_hash(type).values.sort {|a,b| a[:name] <=> b[:name] }
+ ms = members_hash(type).values.sort {|a,b| a[:name] <=> b[:name] }
+ type == :method ? constructor_first(ms) : ms
+ end
+
+ # If methods list contains constructor, rename it with class name
+ # and move into beginning of methods list.
+ def constructor_first(ms)
+ constr = ms.find {|m| m[:name] == "constructor" }
+ if constr
+ ms.delete(constr)
+ # Clone it. Otherwise the search for "constructor" from this
+ # class will return nothing as we have renamed it.
+ constr2 = constr.clone
+ constr2[:name] = short_name
+ ms.unshift(constr2)
+ end
+ ms
end
# Returns hash of public members of class (and parent classes).
diff --git a/spec/class_spec.rb b/spec/class_spec.rb
index 5ad5fd4490ded875d75dca024ae83ec60d845ba4..be2738842d75edd50c458ce7c7542f86532dbca7 100644
--- a/spec/class_spec.rb
+++ b/spec/class_spec.rb
@@ -43,6 +43,33 @@ describe JsDuck::Class do
end
end
+ describe "#members(:method)" do
+ before do
+ @classes = {}
+ @parent = JsDuck::Class.new({
+ :name => "ParentClass",
+ :method => [
+ {:name => "baz", :member => "ParentClass"},
+ {:name => "constructor", :member => "ParentClass"},
+ ]
+ }, @classes);
+ @classes["ParentClass"] = @parent
+ @child = JsDuck::Class.new({
+ :name => "ChildClass",
+ :extends => "ParentClass",
+ :method => [
+ {:name => "foo", :member => "ChildClass"}
+ ]
+ }, @classes);
+ @classes["ChildClass"] = @child
+ end
+
+ it "returns constructor as first method" do
+ ms = @child.members(:method)
+ ms.first[:name].should == "ChildClass"
+ end
+ end
+
describe "#inherits_from" do
before do