ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

`Node.isJSDocable` does not include enough node types that are documetable

Open KrofDrakula opened this issue 2 years ago • 3 comments

Describe the bug

Version: 17.0.1

When trying to extract JSDocs for a property assignment, the underlying compiler node provides the documentation, but ts-morph does not provide an accessor method, and mistakenly identifies PropertyAssignment as not JSDocable.

To Reproduce

Run npm test in the console to demonstrate the test passing but types erroring out:

https://stackblitz.com/edit/vitejs-vite-an78pr?file=src/index.test.ts

Property 'jsDoc' does not exist on type 'PropertyAssignment'.ts(2339)

The passing test illustrates that the JSDoc is available on the compilerNode, but the wrapped type does not contain the jsDoc property, nor does it provide a getJsDocs() method on the node, even though it should.

KrofDrakula avatar Jan 09 '23 15:01 KrofDrakula

Here's a quick workaround.

import { JSDocableNode, PropertyAssignment } from "ts-morph";

// HACK: Patch the PropertyAssignment class to add JSDoc support
// https://github.com/dsherret/ts-morph/issues/1379
const properties = Object.getOwnPropertyDescriptors(
  JSDocableNode(PropertyAssignment as any).prototype
);
delete (properties as any).constructor;
Object.defineProperties(PropertyAssignment.prototype, properties);

declare module "ts-morph" {
  interface PropertyAssignment extends JSDocableNode {}
}

10xjs avatar Jun 05 '23 03:06 10xjs

I am seeing a similar situation for VariableDeclarations. Simple constant declaration such as

/**
 * @internal
 */
export const gcTombstoneBlobKey = "__tombstones";

is not JSDocableNode. VariableDeclaration and others are missing from Node.isJSDocable's list.

jason-ha avatar Apr 01 '24 19:04 jason-ha

For the case I hit, I was enumerating ExportedDeclarations and trying to find tags. After further fiddling I found that walking ancestors of VariableDeclaration will get to VariableDeclarationList and then VariableStatement which is JSDocable. So, I guess VariableDeclaration is not missing, just different compared to other exports I've come across.

jason-ha avatar Apr 01 '24 21:04 jason-ha