-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Js doc overloads #50789
Js doc overloads #50789
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1639,6 +1639,7 @@ namespace ts { | |
case SyntaxKind.JSDocTag: | ||
case SyntaxKind.JSDocClassTag: | ||
case SyntaxKind.JSDocOverrideTag: | ||
case SyntaxKind.JSDocOverloadTag: | ||
return emitJSDocSimpleTag(node as JSDocTag); | ||
case SyntaxKind.JSDocAugmentsTag: | ||
case SyntaxKind.JSDocImplementsTag: | ||
|
@@ -3660,13 +3661,13 @@ namespace ts { | |
|
||
function hasTrailingCommentsAtPosition(pos: number) { | ||
let result = false; | ||
forEachTrailingCommentRange(currentSourceFile?.text || "", pos + 1, () => result = true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are these additional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sandersn I did this because I wanted to avoid
would result in:
Being able to specify However this seems to only be one of many weird consequences of synthesizing declarations, which as you suggested does not seem to be the proper approach anyway. |
||
forEachTrailingCommentRange(currentSourceFile?.text || "", pos + 1, /*end*/ undefined, () => result = true); | ||
return result; | ||
} | ||
|
||
function hasLeadingCommentsAtPosition(pos: number) { | ||
let result = false; | ||
forEachLeadingCommentRange(currentSourceFile?.text || "", pos + 1, () => result = true); | ||
forEachLeadingCommentRange(currentSourceFile?.text || "", pos + 1, /*end*/ undefined, () => result = true); | ||
return result; | ||
} | ||
|
||
|
@@ -5444,7 +5445,7 @@ namespace ts { | |
// Emit leading comments if the position is not synthesized and the node | ||
// has not opted out from emitting leading comments. | ||
if (!skipLeadingComments) { | ||
emitLeadingComments(pos, /*isEmittedNode*/ node.kind !== SyntaxKind.NotEmittedStatement); | ||
emitLeadingComments(pos, end, /*isEmittedNode*/ node.kind !== SyntaxKind.NotEmittedStatement); | ||
} | ||
|
||
if (!skipLeadingComments || (pos >= 0 && (emitFlags & EmitFlags.NoLeadingComments) !== 0)) { | ||
|
@@ -5543,7 +5544,7 @@ namespace ts { | |
|
||
enterComment(); | ||
if (!skipTrailingComments) { | ||
emitLeadingComments(detachedRange.end, /*isEmittedNode*/ true); | ||
emitLeadingComments(detachedRange.end, /*end*/ undefined, /*isEmittedNode*/ true); | ||
if (hasWrittenComment && !writer.isAtStartOfLine()) { | ||
writer.writeLine(); | ||
} | ||
|
@@ -5576,15 +5577,15 @@ namespace ts { | |
return prevNodeIndex !== undefined && prevNodeIndex > -1 && parentNodeArray!.indexOf(nextNode) === prevNodeIndex + 1; | ||
} | ||
|
||
function emitLeadingComments(pos: number, isEmittedNode: boolean) { | ||
function emitLeadingComments(pos: number, end: number | undefined, isEmittedNode: boolean) { | ||
hasWrittenComment = false; | ||
|
||
if (isEmittedNode) { | ||
if (pos === 0 && currentSourceFile?.isDeclarationFile) { | ||
forEachLeadingCommentToEmit(pos, emitNonTripleSlashLeadingComment); | ||
forEachLeadingCommentToEmit(pos, end, emitNonTripleSlashLeadingComment); | ||
} | ||
else { | ||
forEachLeadingCommentToEmit(pos, emitLeadingComment); | ||
forEachLeadingCommentToEmit(pos, end, emitLeadingComment); | ||
} | ||
} | ||
else if (pos === 0) { | ||
|
@@ -5596,7 +5597,7 @@ namespace ts { | |
// /// <reference-path ...> | ||
// interface F {} | ||
// The first /// will NOT be removed while the second one will be removed even though both node will not be emitted | ||
forEachLeadingCommentToEmit(pos, emitTripleSlashLeadingComment); | ||
forEachLeadingCommentToEmit(pos, end, emitTripleSlashLeadingComment); | ||
} | ||
} | ||
|
||
|
@@ -5644,7 +5645,7 @@ namespace ts { | |
return; | ||
} | ||
|
||
emitLeadingComments(pos, /*isEmittedNode*/ true); | ||
emitLeadingComments(pos, /*end*/ undefined, /*isEmittedNode*/ true); | ||
} | ||
|
||
function emitTrailingComments(pos: number) { | ||
|
@@ -5705,22 +5706,22 @@ namespace ts { | |
} | ||
} | ||
|
||
function forEachLeadingCommentToEmit(pos: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) => void) { | ||
function forEachLeadingCommentToEmit(pos: number, end: number | undefined, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean, rangePos: number) => void) { | ||
// Emit the leading comments only if the container's pos doesn't match because the container should take care of emitting these comments | ||
if (currentSourceFile && (containerPos === -1 || pos !== containerPos)) { | ||
if (hasDetachedComments(pos)) { | ||
forEachLeadingCommentWithoutDetachedComments(cb); | ||
} | ||
else { | ||
forEachLeadingCommentRange(currentSourceFile.text, pos, cb, /*state*/ pos); | ||
forEachLeadingCommentRange(currentSourceFile.text, pos, end, cb, /*state*/ pos); | ||
} | ||
} | ||
} | ||
|
||
function forEachTrailingCommentToEmit(end: number, cb: (commentPos: number, commentEnd: number, kind: SyntaxKind, hasTrailingNewLine: boolean) => void) { | ||
// Emit the trailing comments only if the container's end doesn't match because the container should take care of emitting these comments | ||
if (currentSourceFile && (containerEnd === -1 || (end !== containerEnd && end !== declarationListContainerEnd))) { | ||
forEachTrailingCommentRange(currentSourceFile.text, end, cb); | ||
forEachTrailingCommentRange(currentSourceFile.text, end, /*end*/ undefined, cb); | ||
} | ||
} | ||
|
||
|
@@ -5739,7 +5740,7 @@ namespace ts { | |
detachedCommentsInfo = undefined; | ||
} | ||
|
||
forEachLeadingCommentRange(currentSourceFile.text, pos, cb, /*state*/ pos); | ||
forEachLeadingCommentRange(currentSourceFile.text, pos, /*end*/ undefined, cb, /*state*/ pos); | ||
} | ||
|
||
function emitDetachedCommentsAndUpdateCommentsInfo(range: TextRange) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Synthesising declarations is not a technique that works in the typescript compiler. A better approach would be to make sure all the jsdoc nodes are still available to the checker. In the checker, make sure that each
@overload
results in a signature. You'll want to start near getSignatureFromDeclaration, or its callers, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sandersn Agreed. I've also observed other BUGs related to IDE integrations that are coming from the fact that I am trying to synthesize those declarations, e.g. "did not expect identifier in trivia".
Thank you for this suggestion! I've already tried that and it seems to be working much better:
https://github.com/microsoft/TypeScript/compare/main...apendua:js-doc-overload-tag?expand=1
How we should go about that? Should I close this pull request and open a new one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is up to you. I am fine with proceeding with this one; I squash merge PRs since it makes the git history easier to search.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sandersn I ended up opening a new PR here: #51234
I also used this opportunity to rebase onto the latest
main
. Thank you again for all your suggestions!