Skip to content
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

Closed
michaeldrotar opened this issue Mar 18, 2015 · 3 comments
Closed

How To Document Namespace That Is Also a Function #955

michaeldrotar opened this issue Mar 18, 2015 · 3 comments
Labels
Milestone

Comments

@michaeldrotar
Copy link

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)..

/**
  My lib, which does many different things.
  @namespace
*/
var lib = {};

/**
  Provides ajax functionality

  @alias lib.http
  @namespace
*/
lib.http = lib.namespace('http'); // Create the http namespace to hold my http functions

/**
  Performs a highly configurable http request.
  @alias lib.http
  @method
*/
lib.http = lib.namespace('http', wrap($.http)); // Wrap jQuery's http method inside of a
// Promise and have the method throw actual errors.. by the definition of my namespace function,
// this wrapped function takes the place of the object that previously defined the namespace
// since functions can still have properties and methods.. any children of the previous object
// get attached to this function instead

/**
  Performs a GET request
  @alias get
  @memberOf lib.http
*/
lib.http.get = wrap($.get); // Wrap jQuery's get method inside of a Promise, etc..

/**
  Performs a POST request
  @alias post
  @memberOf lib.http
*/
lib.http.post = wrap($.post); // Wrap jQuery's post method inside of a Promise, etc...

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..

lib.http('myUrl', { /* settings */ }).then(function(response) {
});

If I want the convenient shorthand, it's namespaced nicely for me...

lib.http.get('myUrl').then(function(response) {
});

Any alteration of the doc comments that I try.. either lib.http is a function and the get and post functions are not present.. or lib.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):

lib. http
Provides ajax functionality
Source: lib.http.js, line 5

(static) lib.http(url, settings) -> Promise
Performs a highly configurable http request.
Parameters:
Source lib.http.js, line 50
Throws:
Returns:

Methods

(static) get(url, settings) -> Promise
Performs a GET request
...

(static) post(url, settings) -> Promise
Performs a POST request
...

If not possible, having functions as children of functions would at least provide the documentation appropriately..

lib
My lib, which does many different things
Source: lib.http.js, line 5

Namespaces
one
two

Methods

(static) http(url, settings) -> Promise
Performs a highly configurable http request.
Parameters:
Source lib.http.js, line 50
Throws:
Returns:

(static) http.get(url, settings) -> Promise
Performs a GET request
...

(static) http.post(url, settings) -> Promise
Performs a POST request
...

Thanks for your time!

@hegemonic
Copy link
Contributor

You're right, there's not a clean way to document this pattern right now. I agree that this is needed.

@hegemonic hegemonic added this to the 3.4.0 milestone Mar 19, 2015
@nwoltman
Copy link

I've had success with this by using the @variation tag. For instance, all you'd have to do is add @variation 1 to the lib.http method, to have it documented as a separate member of the lib namespace and have get and post documented as part of the lib.http namespace.
When doing this, you just have to remember to write lib.http(1) if you want to reference the lib.http method instead of the namespace.

@hegemonic
Copy link
Contributor

hegemonic commented Jul 8, 2017

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 lib.http method/namespace:

/**
 * 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');

lheberlie added a commit to bsvensson/jsdoc that referenced this issue Aug 15, 2017
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants