-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How To Document Namespace That Is Also a Function #955
Comments
You're right, there's not a clean way to document this pattern right now. I agree that this is needed. |
I've had success with this by using the |
Fixed on master. The fix will be included in JSDoc 3.5.0. For the following example, the default template now displays the function signature, parameters, and return types for the /**
* My lib, which does many different things.
* @namespace
*/
var lib = {};
/**
* Provides ajax functionality
* @namespace
* @param {string} foo - the foo
* @return {number} some number
*/
lib.http = function(foo) {};
/**
* Performs a GET request
* @function
*/
lib.http.get = wrap($.get);
/**
* Performs a POST request
* @function
*/
lib.http.post = wrap($.post); This was purely a change to the default template. Third-party templates will need to be updated to support this feature. One important caveat: This feature won't work correctly unless the function you're using as a namespace is actually represented as a function in your code. That's because the template actually looks at your code to figure out that the namespace is also a function. This is necessary because of a fundamental limitation in JSDoc that can't easily be changed. You can deal with this limitation by doing something like this in your code: /**
* My lib, which does many different things.
* @namespace
*/
var lib = {};
// assign a dummy function for JSDoc
/**
* Provides ajax functionality
* @namespace
* @param {string} foo - the foo
* @return {number} some number
*/
lib.http = function(foo) {};
// now assign the actual value
lib.http = lib.namespace('http'); |
3.5.0 * tag '3.5.0': (97 commits) 3.5.0 bump revision; start 3.6.0-dev update 3.5.0 changelog 3.5.0 changelog reformat changelog add yields tag (jsdoc#1388) resolve the path to the JS config file before requiring it (jsdoc#1386) support namespaces that are also functions (jsdoc#955) add hideconstructor tag (jsdoc#952) add package tag (jsdoc#962) autodetect default and repeatable parameters when a function is assigned to a variable (jsdoc#1054) correctly document constructors and instance properties of ES2015 classes (jsdoc#1182) add sourceType config option fix crash when the author tag is empty (jsdoc#1289) add recurseDepth config option (jsdoc#1340) support bigint support import.meta support optional chaining support numeric separators support dynamic import ... # Conflicts: # .eslintrc # README.md # lib/jsdoc/fs.js # lib/jsdoc/opts/args.js # lib/jsdoc/src/astbuilder.js # lib/jsdoc/src/astnode.js # lib/jsdoc/src/handlers.js # lib/jsdoc/src/parser.js # lib/jsdoc/src/visitor.js # lib/jsdoc/src/walker.js # lib/jsdoc/tag/dictionary/definitions.js # lib/jsdoc/tag/validator.js # lib/jsdoc/util/markdown.js # lib/jsdoc/util/templateHelper.js # package.json # templates/default/static/styles/jsdoc-default.css # templates/haruki/publish.js # test/spec-collection.js # test/specs/documentation/defaultparams.js # test/specs/documentation/restparams.js # test/specs/jsdoc/src/astnode.js # test/specs/jsdoc/util/templateHelper.js
I came across an old issue that was similar to mine but seemed to have gotten derailed and closed for not having enough info.. so to hopefully provide better information with a real world scenario (comments shortened for brevity)..
I realize that using a function as a namespace seems odd... I don't often do it.. but there's an elegance in this scenario that I like.
If I want to make an ajax request, I simply do..
If I want the convenient shorthand, it's namespaced nicely for me...
Any alteration of the doc comments that I try.. either
lib.http
is a function and theget
andpost
functions are not present.. orlib.http
is a namespace and it's params and returns are not present.It would be ideal to have everything on its own namespace page (shortened for brevity):
If not possible, having functions as children of functions would at least provide the documentation appropriately..
Thanks for your time!
The text was updated successfully, but these errors were encountered: