j3141592653589793238
j3141592653589793238

Reputation: 1894

How to use JSDoc with destructured imports?

I am using VSCode as my NodeJS editor and the Preview JSDOC extension.

I wonder how I can use JSDoc with restructured imports. See the example below.

I created a module, Module A:

/**
 * Some function description
 * @return a string + 1234
 */
const myFuncA = (someParams) => {
  return `${someParams} 1234` // or whatever
};

/**
 * This is a string.
 */
const myPropB = "A string";

// ...

module.exports = {
  myFuncA, 
  myPropB
}

In module B, I'd like to use some properties or functions from module A: Module B:

const { myFuncA } = require('../moduleA');

const propA = myFuncA(4321);

However, when hovering over myFuncA (or when typing myFuncA(...), the description is not shown.

I'm new to JSDoc, however, their docs don't cover my specific case.

Upvotes: 1

Views: 573

Answers (1)

Andrew Duensing
Andrew Duensing

Reputation: 195

I was searching for answer for this as well, and I think the best thing for this solution is to use the @memberof module:<your-module-name> syntax.

So in Module B at the top you would add something like this

/** module MyModuleName */

const { myFuncA } = require('../moduleA');

Then in Module A you would add the @memberof module:<module-name> decorator.

/**
 * Some function description
 * @return a string + 1234
 * @memberof module:MyModuleName
 */
const myFuncA = (someParams) => {
  return `${someParams} 1234` // or whatever
};

Extra Information: As I was looking for a solution to this problem, I had the assumption that JSDoc would resolve imports. JSDoc does not resolve imports, which makes a lot of sense. If you want a function to belong to another module, you have to let JSDocs know. Credit to this github issue thread which helped me settle on this answer.

Upvotes: 1

Related Questions