dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
4.98k stars 195 forks source link

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

Open KrofDrakula opened 1 year ago

KrofDrakula commented 1 year ago

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.

10xjs commented 1 year ago

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 {}
}
jason-ha commented 7 months ago

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 commented 7 months ago

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.