Microsoft/TypeScript

Synthetic comment duplicated 3 times on class with experimental decorators

Open

#54,742 建立於 2023年6月22日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)TypeScript (6,726 fork)batch import
BugDomain: Comment EmitHelp Wanted

倉庫指標

Star
 (48,455 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 9 個 PR)

描述

Bug Report

🔎 Search Terms

setSyntheticLeadingComments, transformer

🕗 Version & Regression Information

  • This changed between versions 5.0 and 5.1

In 5.0 TypeScript already emitted the comment twice. In 5.1 it emits it 3 times.

(+1 on both if you count the comment on the __decorate call)

💻 Code

Repro: https://github.com/frigus02/test-ts-triple-synthesized-comment

const FILE = ` 
@decorate()
class Foo {}
`;

const myTransformer = () => {
  return (sf) => {
    ts.setSyntheticLeadingComments(sf.statements[0], [
      {text: 'from transformer', kind: ts.SyntaxKind.MultiLineCommentTrivia, pos: -1, end: -1},
    ]);
    return sf;
  };
};

const result = ts.transpileModule(FILE, {
  compilerOptions: {
    experimentalDecorators: true,
    module: ts.ModuleKind.CommonJS,
    target: ts.ScriptTarget.ESNext,
  },
  transformers: {
    before: [myTransformer],
  }
});

console.log(result.outputText);

🙁 Actual behavior

/*from transformer*/ let /*from transformer*/ Foo = /*from transformer*/ class Foo {
};
/*from transformer*/ Foo = __decorate([
    decorate()
], Foo);

The comment between let and Foo is problematic for our use of tsickle and Closure Compiler. But I think the issue is more general. I'd expect the comment to appear only once in the output.

This seems very similar to https://github.com/microsoft/TypeScript/issues/45696

We validated that the same fix (calling setSyntheticLeadingComments(expression, undefined)) after setOriginalNode here and here would fix this. Are you open to such a fix? If so, I'd be happy to send a PR.

🙂 Expected behavior

/*from transformer*/ let Foo = class Foo {
};
Foo = __decorate([
    decorate()
], Foo);

貢獻者指南