gozala@ijarti:~/Projects/meta-doc (master)$ narwhalRhino 1.7 release 3 PRERELEASE 2009 04 05js> require("meta-doc/toolkit").doc()toolkit moduleprovides utilities for getting a live docs@type modulejs> typeof require("/Users/gozala/Projects/meta-doc/examples/functions.js").examplefunctionjs> require("/Users/gozala/Projects/meta-doc/examples/functions.js").example.doc()This is a function foo which is supposed to do something.But the best thing is that it has nice documentation in it.@param {String} a first argument@param {String} b second argument@returns {String} Concatinated string literaljs> require.doc()Not documentedjs>Of course source is also available around: http://gist.github.com/219927
2009/10/28 George Moschovitis <george.mo...@gmail.com>:Interesting,too bad, JavaScript multi line strings suck :(
On the #helma IRC channel we've come up with a twist that fixes this,
using an E4X XML literal:
http://helma.pastebin.com/m7c2e0258
http://helma.pastebin.com/m58060200
This makes multiline comments easy. The only downside is you have to
be xml-wellformed and escape '<', '>' etc (and you need E4X, of
course).
Hannes
And another idea, in terms of actually being able to do
someFunction.doc()... I believe Rhino actually supports parsing JSDocs
into the AST. I have suggested before [1] that being able to optionally
access Rhino's great AST API from a function object would provide some
pretty impressive introspective capabilities (like implementing a nice
.doc() method), especially in combination with the JSDoc support.
Obviously using something like JSDoc is much more elegant than having an
array instantiation (or worse yet, E4X code execution) in all your
functions (probably not even feasible in hot functions)
[1]
http://markmail.org/message/qzieujftptnyxvj3#query:+page:1+mid:6xodxibbqfx6epmw+state:results
Kris
As an alternative idea, the approach taken in Persevere and also
supported in Dojo is providing metadata through JSON schema data
structures. Granted this more focused on class structures, rather than
individual functions, but for class style structures, this provides a
very comprehensive and portable approach, IMO, with a well-defined
mechanism for discovering information about methods. There actually
isn't really any library or helper code needed, it is just a convention,
but it basically just implies that you create strucutures such that one
could do:
MyClass.methods.foo.description -> the description of method foo
MyClass.methods.foo.parameters[0].type -> the expected type of method
foo's first parameter
MyClass.properties.bar.type -> the expected type of property bar
And you can even do things like leveraging this information to add
optional type checking:
http://www.sitepen.com/blog/2009/06/23/unobtrusive-javascript-typing-via-json-schema-interfaces/
This is all pretty language agnostic, so the definitions are very
portable as well.
And another idea, in terms of actually being able to do
someFunction.doc()... I believe Rhino actually supports parsing JSDocs
into the AST.
2009/10/28 George Moschovitis <george.mo...@gmail.com>:
> Interesting,On the #helma IRC channel we've come up with a twist that fixes this,
> too bad, JavaScript multi line strings suck :(
using an E4X XML literal:
http://helma.pastebin.com/m7c2e0258
http://helma.pastebin.com/m58060200
This makes multiline comments easy. The only downside is you have to
be xml-wellformed and escape '<', '>' etc (and you need E4X, of
course).
I don't think that run-time introspection is critical; the key is that
making documentation *must* be easy or it's just not going to happen,
whether we bless it or not. I think the jsdoc comments are probably
the way to go for the time being. It's even possible that we could
add optional hooks to the module loader that would scrape jsdocs
dynamically and monkey-patch the module's exports on completion.
Kris Kowal
module.doc = "Some simple math functions.";
exports.add = function (a, b) {
return a + b;
};
exports.add.doc = "Adds two objects.";
exports.subtract = function (a, b) {
return a - b;
};
exports.subtract.doc = "Subtracts two objects.";
Or maybe:
module.addDoc("Some simple math functions.");
module.addDoc("add", "Adds two objects.");
exports.add = function (a, b) {
return a + b;
};
module.addDoc("subtract", "Subtracts two objects.");
exports.subtract = function (a, b) {
return a - b;
};
Ryan
Ack, Sorry, I thought this was the CommonJS forum!
+1 for jsdoc - people know how to write compliant docs
Christoph