Fix duplicate JSDoc @typedef and @callback comments in declaration emit#62979
Fix duplicate JSDoc @typedef and @callback comments in declaration emit#62979heathdutton wants to merge 1 commit intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where JSDoc @typedef and @callback comments were being duplicated in TypeScript declaration files. The comments were appearing both as converted TypeScript type declarations (correct) and as the original JSDoc comments (duplicate).
Changes:
- Modified the
shouldWriteCommentfunction in the emitter to detect and skip@typedefand@callbackcomments during declaration emit - Updated baseline test files to reflect the removal of duplicate JSDoc comments
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/compiler/emitter.ts | Added logic to filter out @typedef and @callback comments when emitting declarations |
| tests/baselines/reference/*.js | Updated baseline expectations to reflect removal of duplicate JSDoc comments in declaration files |
| // Skip @typedef and @callback comments - they're converted to type declarations | ||
| if (end !== undefined) { | ||
| const commentText = text.slice(pos, end); | ||
| if (commentText.includes("@typedef") || commentText.includes("@callback")) { |
There was a problem hiding this comment.
The string matching for '@typedef' and '@callback' could produce false positives if these strings appear in comment content that isn't actually a typedef or callback declaration (e.g., in documentation text or code examples). Consider using a more precise pattern match that checks for these tags at word boundaries, such as matching /@typedef\b/ and /@callback\b/ to ensure they are actual JSDoc tags.
| if (commentText.includes("@typedef") || commentText.includes("@callback")) { | |
| if (/@typedef\b/.test(commentText) || /@callback\b/.test(commentText)) { |
| const commentText = text.slice(pos, end); | ||
| if (commentText.includes("@typedef") || commentText.includes("@callback")) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Creating a substring via slice() for every comment during emit could impact performance when processing files with many comments. Consider checking for the presence of '@typedef' or '@callback' using indexOf() with boundary checks on the original string instead of creating a new substring.
| const commentText = text.slice(pos, end); | |
| if (commentText.includes("@typedef") || commentText.includes("@callback")) { | |
| return false; | |
| } | |
| const typedefIndex = text.indexOf("@typedef", pos); | |
| if (typedefIndex !== -1 && typedefIndex < end) { | |
| return false; | |
| } | |
| const callbackIndex = text.indexOf("@callback", pos); | |
| if (callbackIndex !== -1 && callbackIndex < end) { | |
| return false; | |
| } |
|
Both suggestions would deviate from existing codebase conventions, but happy to make a change if a human replies. |
Fixes #62453.
When emitting declaration files from JavaScript with JSDoc
@typedefand@callbacktags, the comments were being emitted twice: once as converted TypeScript type declarations (correct) and once as raw JSDoc comments (duplicate).The fix skips emitting
@typedefand@callbackcomments during declaration emit since they are already represented as type declarations.