#JSDoc and Compiler API

1 messages · Page 1 of 1 (latest)

fleet kayak
#

I'm working with the compiler API for a project and finding it difficult to get at the JSDoc associated with a node. When debugging, the node will have a jsDoc property, but I can't figure out the proper way to get TS to understand that the node has jsDoc or the proper way to retrieve the doc comment from that.

<ref *2> NodeObject {
  pos: 173,
  end: 243,
  kind: 303,
  id: 0,
  flags: 524288,
  modifierFlagsCache: 0,
  transformFlags: 0,
  parent: <ref *1> NodeObject {
  },
  original: undefined,
  emitNode: undefined,
  symbol: undefined,
  localSymbol: undefined,
  name: IdentifierObject {
  },
  initializer: <ref *3> NodeObject {
  },
  jsDoc: [
    NodeObject {
      pos: 175,
      end: 203,
      kind: 320,
      id: 0,
      flags: 17301504,
      modifierFlagsCache: 0,
      transformFlags: 0,
      parent: [Circular *2],
      original: undefined,
      emitNode: undefined,
      comment: 'JSDoc we want to keep',
      tags: undefined
    }
  ]
}```

I can't find a type predicate what will allow access to this property. I can just brute force it, but wanted to see if there was something more official. Thanks!
#

This seems to work but I'm not entirely sure that it is right:

const fullPosition = [node.getFullStart(), node.getEnd()];
const sf = node.getSourceFile();
const result = ts.getLeadingCommentRanges(sf.text, fullPosition[0]);
if (result) {
    result.forEach((r) => {
        const commentText = sf.text.substring(r.pos, r.end);
        console.log({ commentText });
    });
}```
willow herald
#

@fleet kayak Don't have a specific answer but 1) You might check ts-morph as it's usually a more ergonomic way to work with the compiler and also#compiler-internals-and-api is probably more likely to get an answer

sick laurel
#

You might be able to check against the kind property? I don't remember if everything is a discriminated union or not but it should be ScriptKind.JSDoc

fleet kayak
#

There are type predicates for when you are on a node that is a JSDoc itself (or internals therein). Even looking through the type declarations I don't see jsDoc specifically, so I'm guessing that is an internal. In some places, I am able to use the node's getStart() with the includeJsDocComment (which makes me think I should change up that call a bit in my example)

#

But, yeah I will check in the compiler channel shortly. Thought I would start here. Thank you 🙂

#

(ts-morph seems nice too... )