Intellisense JSDoc for Mixins and/or Factory Functions

Viewed 524

My Problem

I'm currently using Visual Code Studio, the lastest version. I'm trying to get the intellisense to show up for the methods on the instance created by a factory function. The methods are going to be applied via object composition (so added directly as properties on the object).

so the function that acts as a constructore basically returns:

function makeWrappedObj() {
  var obj = { /* random data */ }; // Then add methods to obj
  Object.keys(methods).forEach(key => a[key] = methods[key]; );
  return obj;
}
var methods = {
  /**
   * Yay documentation
   * @returns {Object}
   */
  method1: function() { return null; }
};
var instance = makeWrappedObj();
instance.method1( // What documentation to show up here
instance. // and here

Is the basic idea. Here's someone doing something similar. I'll address that in the third possible solution.

Solutions I've tried

1) @class on makeWrappedObj I think only works if you attach methods on to makeWrappedObj.prototype which is not what I'm doing so that doesn't work. Unless I'm misunderstanding something.

2) @namespace solution

/* @namespace ClassName */

/* @returns {ClassName}
function createWrappedObj() {
  var obj = { /* random data */ }; // Then add methods to obj
  Object.keys(methods).forEach(key => a[key] = methods[key]; );
  return obj;
}

var methods = {
/**
 * Currently the soultion I'm using
 * @memberof ClassName
 * @param {number} a
 **/
  method1: function (a)  {}
};
var instance = makeWrapperObj();
instance.method1( // Only shows documentation here

So this sort of works. The two problems are that:

  • methods on instances only get documentation when you type out the full instance.method( and not in the instance. case--not really a big deal
  • if you have a method name that is the same as an in-built function, eg. toString, instance.toString( will block any documentation you have from showing and show the native toString default documentation instaed.

3) @typedef like the link above.

/** Can be placed anywhere
 * @typedef {ClassName}
 * @property {function} method1
 * dunno how to document arguments with this method
 * but it gets the intellisense menu to pop up for "instance." after dot
 */

/* @returns {ClassName} */
function createdWrappedObject() {
  var obj = { /* random data */ }; // Then add methods to obj
  Object.keys(methods).forEach(key => a[key] = methods[key]; );
  return obj;
}
    
var methods = {
  method1: function (a)  {}
};
var instance = makeWrappedObj();
instance. // Yay
instance.method1( // Doesn't work

Has the benefit of showing up when one types instance. however has a few disadvantages

  • Do not know how to specify arguments passed to method1. Maybe it's not possible.
  • @typedef requires all documentations to be specified in its own comment block. So I have to be redundant between specific documentation on the methods and the typedef
  • loses the docummentation once you write out the entire method up until the open paren. Sort of workable...

Note: I am willing to just assign all the methods manually one by one instead of doing a foreach on all the keys of the methods mixin.

Also I do not know how to specify the parameters for a function passed as a. This should be possible since Array.map( specifies three arguments for its function call.

The goal is to get documentation when someone imports this as a library with require, or just includes it as a library in browser side script.

0 Answers
Related