TypeScript getTextOfJSDocComment引入了JSDoc注解中的空格

lh80um4z  于 8个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(134)

🔎 搜索词

getJSDocTags
getTextOfJSDocComment

🕗 版本与回归信息

在5.5.4版本上进行测试,之前版本也存在。

⏯ Playground链接

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

💻 代码

  1. import ts from 'typescript';
  2. const filename = 'example.ts';
  3. const code = `
  4. /**
  5. *
  6. * @see {@link entry()}
  7. */
  8. class Foo {};
  9. `;
  10. const sourceFile = ts.createSourceFile(
  11. filename,
  12. code,
  13. ts.ScriptTarget.ESNext,
  14. true
  15. );
  16. const visitNode = (node: ts.Node) => {
  17. ts.forEachChild(node, visitNode);
  18. const tags = ts.getJSDocTags(node);
  19. if (tags.length) {
  20. tags.forEach((tag) => {
  21. const comment = tag.comment!.slice(1) as any;
  22. console.log(
  23. tag.tagName.getText(),
  24. '> ',
  25. ts.getTextOfJSDocComment(comment)
  26. );
  27. //console.log(comment);
  28. });
  29. }
  30. };
  31. ts.forEachChild(sourceFile, visitNode);

🙁 实际行为

getTextOfJSDocComment 返回 {@link entry ()}。注意两者之间的空格。

🙂 预期行为

getTextOfJSDocComment 返回 {@link entry()}。注意两者之间的空格。

关于此问题的其他信息

这是一个 #58584 的变体。
还注意到 {@link core/inject} 变为 {@link core /inject} 时也会出现这个问题。

qxsslcnc

qxsslcnc1#

解析器处理这些情况如下:

  1. // "entry" - name
  2. // "()" - text
  1. // "code" - name
  2. // "/inject" - text

TypeScript/src/compiler/utilitiesPublic.ts
第1289行: 936a79b
| | constspace=link.name&&(link.text===""||link.text.startsWith("://")) ? "" : " "; |
格式化程序在 nametext 之间添加空格,只有两种情况下省略空格。@sandersn,我们是否应该在解析器中添加特殊节点,还是最好直接在格式化程序中处理这些已知情况( '/''(' )?

相关问题