user210757
user210757

Reputation: 7386

Preserving jsdoc comments and VS Code intellisense with babel

I have a javascript library for communicating with server APIS, written in modern ECMAScript.

It is fully documented with JSDoc comments:

/**
 *  @class - TODOS API Client class
 */
class todosApi {
    /**
   * Gets Todos, given the parameters
   * @param {number} personId
   * @param {number} [year]
   * @param {number} [month]
   * @param {number} [todoTypeId]
   * @returns {Object} - api response object, data will be array of todos
   */
  fetchTodos = async (....
}

When using this API in the unit tests in this project, in Visual Studio code, I have excellent intellisense from these comments, and it's a beautiful thing.

However, this library is used by/referenced in a separate react application created with create-react-app. When I run this through babel to transpile into a format that is consumable by my create-react-app app, it ends up like this:

/**
 *  @class - TODOS API Client class
 */
class todosApi {
          _defineProperty(this, "fetchTodos", async (personId, eventYear, eventMonth, todoTypeId) => {
    }

And I lose my intellisense for fetchTodos, and actually the class itself because of how it is exported in and index.js file. babel does have the option to include comments by default, however the class gets a little mangled in transpiling and loses some comments.

Is there any way to transpile and still preserve this intellisense for VS Code?

Upvotes: 1

Views: 1007

Answers (1)

Nabrok
Nabrok

Reputation: 109

Use tsd-jsdoc to create a types.d.ts file.

In your package.json add a script to run ...

jsdoc -r src -t node_modules/tsd-jsdoc/dist -d lib

And set types to lib/types.d.js.

Include that script as part of prepublishOnly so it runs before every npm publish.

Upvotes: 1

Related Questions