ts-morph
ts-morph copied to clipboard
`Node.isJSDocable` does not include enough node types that are documetable
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.
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 {}
}
I am seeing a similar situation for VariableDeclaration
s.
Simple constant declaration such as
/**
* @internal
*/
export const gcTombstoneBlobKey = "__tombstones";
is not JSDocableNode
. VariableDeclaration
and others are missing from Node.isJSDocable
's list.
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.