dsherret / ts-morph

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

Question: Why propertyAssignment node don't support getJsDoc method? #1543

Open mortalYoung opened 5 months ago

mortalYoung commented 5 months ago

I could find JSDoc node from ast. But when I can't get it by getJsDoc.

https://ts-ast-viewer.com/#code/PQKhCgAIUgVALAlgZ0iyAXApsjUTDhYAeADgPYBOGkAxuQHa6Y40C8kA3lJL6BL0EwE6dAEMeQwoMhiAXJACMAGkmR+a6HCSp0AI00E1ehQCZVAXwDc4IA

if (Node.isPropertyAssignment(node)) {
    const jsDoc = node
        .getJsDocs()
        .map((doc) => doc.getCommentText())
        .join('\n'); // throw error about type "PropertyAssignment" don't have method "getJsDocs"。
} 

I don't know it's a feature or bug?

ryoppippi commented 5 months ago

This works for me

const code = `
/**
 * This is test
 */
export const test = {
    /**
     * This is a
     */
    a: 1,
    /**
     * This is b
     */
    b: 2,
};
`;

const project = new Project();
const sourceFile = project.createSourceFile("", code);

sourceFile.forEachDescendant((node) => {
  if (Node.isVariableStatement(node)) {
    const docs = node.getJsDocs();
    for (const doc of docs) {
      console.log(doc.getCommentText());
    }
  }
});