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

Can't get JSDoc text for type alias property

Open DaniGuardiola opened this issue 3 years ago • 1 comments

Describe the bug

I can't seem to obtain the JSDoc text for a type alias property. getProperties return an array of Symbol, and Symbol doesn't expose a getJsDocs method, which seems weird to me since it does expose the getJsDocsTags method.

Version: 16.0.0

To Reproduce

This is my code:

import { Project } from "ts-morph";

const TSCONFIG_FILE_PATH = "./tsconfig.json";

const project = new Project({ tsConfigFilePath: TSCONFIG_FILE_PATH });

const iconTypesFile = project.getSourceFile("src/components/icon/types.ts");

const iconOptionsType = iconTypesFile
  ?.getTypeAliasOrThrow("AtlasIconOptions")
  .getType();

const iconOptionsProperties = iconOptionsType?.getProperties();

const extractedProperties = iconOptionsProperties?.map((p) => ({
  name: p.getName(),
  type: p.getValueDeclaration()?.getType(),
  jsDocsText: "TODO", // <- I can't use p.getJsDocs() here for some reason
  jsDocsTags: p
    .getJsDocTags()
    .map((tag) => ({ name: tag.getName(), text: tag.getText() })),
}));

extractedProperties?.forEach((x) => console.log(x));

The referenced file contains the type, which is a plain type literal with a bunch of properties that have JSDoc comments.

Expected behavior

The getJsDocs method is available and I can obtain the text.

DaniGuardiola avatar Sep 17 '22 21:09 DaniGuardiola

I figured it out, I'm accessing the related PropertySignature node and get the docs from there, like this more or less:

symbol.getDeclarations()[0].getStructure().docs

Of course, it needs the Node.isPropertySignature type guard to make sure that's what it actually is.

DaniGuardiola avatar Sep 17 '22 23:09 DaniGuardiola

There's also:

Node.isJSDocable(declaration)

diegohaz avatar Oct 24 '22 19:10 diegohaz

@diegohaz ah, that's a good one, thanks

DaniGuardiola avatar Oct 27 '22 12:10 DaniGuardiola