microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.01k stars 12.29k forks source link

`getJSDocTags` detects decorators inside code blocks as JSDoc tags #58992

Open JeanMeche opened 1 week ago

JeanMeche commented 1 week ago

πŸ”Ž Search Terms

πŸ•— 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

RyanCavanaugh commented 1 week ago

We don't have markdown parsing or code block detection features in JS Doc at the moment