microsoft / microsoft/TypeScript

`getJSDocTags` detects decorators inside code blocks as JSDoc tags

Open
#58,992 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms
  • jsdoc
  • getJSDocTags
  • decorators
🕗 Version & Regression Information

This is faulty behavior

⏯ Playground Link

https://stackblitz.com/edit/gettextofjsdoccomment-d88fzj?file=main.ts,package.json

💻 Code
import ts from 'typescript';

const filename = 'example.ts';
const code = `
/** 
 * 
 * @usageNotes
 *
 * Use \`afterNextRender\` to read or write the DOM once,
 * for example to initialize a non-Angular library.
 *
 * ### Example
 * \`\`\`ts
 * @Component({
 *   selector: 'my-chart-cmp',
 *   template: \`<div #chart>{{ ... }}</div>\`,
 * })
 * export class MyChartCmp {
 *   @ViewChild('chart') chartRef: ElementRef;
 *   chart: MyChart|null;
 *
 *   constructor() {
 * 
 *   }
 * }
 * \`\`\`
 */
class Foo  {};
`;

const sourceFile = ts.createSourceFile(
  filename,
  code,
  ts.ScriptTarget.ESNext,
  true
);

const visitNode = (node: ts.Node) => {
  ts.forEachChild(node, visitNode);
  const tags = ts.getJSDocTags(node);
  if (tags.length) {
    tags.forEach((tag) => {
      const comment = tag.comment!.slice(1) as any;

      console.log(
        '> ',
        tag.tagName.getText(),
        ' | ',
        ts.getTextOfJSDocComment(comment)
      );
    });
  }
};

ts.forEachChild(sourceFile, visitNode);
🙁 Actual behavior

We get 2 distinct tags:

>  usageNotes  |  se `afterNextRender` to read or write the DOM once,
for example to initialize a non-Angular library.

### Example
```ts
>  Component  |  {
selector: 'my-chart-cmp',
template: `<div #chart>{{ ... }}</div>`,
})
export class MyChartCmp {
>  ViewChild  |  'chart') chartRef: ElementRef;
chart: MyChart|null;

constructor() {

}
}
🙂 Expected behavior

Only one tag should be extracted : usagenotes

Additional information about the issue

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the getJSDocTags API and reproduce the behavior using the StackBlitz example in the issue. Trace how the JSDoc comment is parsed around the fenced code block, then verify that only the usageNotes tag is extracted and the decorator-like text is ignored.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.