Skip to content
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

Don't inherit jsdoc tags from overloaded signatures #43165

Merged
merged 1 commit into from Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions src/services/services.ts
Expand Up @@ -550,7 +550,7 @@ namespace ts {

getJsDocTags(): JSDocTagInfo[] {
if (this.jsDocTags === undefined) {
this.jsDocTags = this.declaration ? getJsDocTags([this.declaration], this.checker) : [];
this.jsDocTags = this.declaration ? getJsDocTagsOfSignature(this.declaration, this.checker) : [];
}
return this.jsDocTags;
}
Expand All @@ -565,15 +565,13 @@ namespace ts {
return getJSDocTags(node).some(tag => tag.tagName.text === "inheritDoc");
}

function getJsDocTags(declarations: Declaration[], checker: TypeChecker): JSDocTagInfo[] {
let tags = JsDoc.getJsDocTagsFromDeclarations(declarations);
if (tags.length === 0 || declarations.some(hasJSDocInheritDocTag)) {
forEachUnique(declarations, declaration => {
const inheritedTags = findBaseOfDeclaration(checker, declaration, symbol => symbol.getJsDocTags());
if (inheritedTags) {
tags = [...inheritedTags, ...tags];
}
});
function getJsDocTagsOfSignature(declaration: Declaration, checker: TypeChecker): JSDocTagInfo[] {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only called here, and is short, so I wanted to inline it, but it was non-trivial so I skipped it to keep the diff smaller. I couldn't help myself simplifying out the unused declarations: Declaration[] to declaration: Declaration, though.

let tags = JsDoc.getJsDocTagsFromDeclarations([declaration]);
if (tags.length === 0 || hasJSDocInheritDocTag(declaration)) {
const inheritedTags = findBaseOfDeclaration(checker, declaration, symbol => symbol.declarations?.length === 1 ? symbol.getJsDocTags() : undefined);
if (inheritedTags) {
tags = [...inheritedTags, ...tags];
}
}
return tags;
}
Expand All @@ -592,7 +590,7 @@ namespace ts {
return doc;
}

function findBaseOfDeclaration<T>(checker: TypeChecker, declaration: Declaration, cb: (symbol: Symbol) => T[]): T[] | undefined {
function findBaseOfDeclaration<T>(checker: TypeChecker, declaration: Declaration, cb: (symbol: Symbol) => T[] | undefined): T[] | undefined {
return firstDefined(declaration.parent ? getAllSuperTypeNodes(declaration.parent) : emptyArray, superTypeNode => {
const symbol = checker.getPropertyOfType(checker.getTypeAtLocation(superTypeNode), declaration.symbol.name);
return symbol ? cb(symbol) : undefined;
Expand Down
140 changes: 140 additions & 0 deletions tests/baselines/reference/deprecatedInheritedJSDocOverload.baseline
@@ -0,0 +1,140 @@
[
{
"marker": {
"fileName": "/tests/cases/fourslash/deprecatedInheritedJSDocOverload.ts",
"position": 1183,
"name": ""
},
"quickInfo": {
"kind": "method",
"kindModifiers": "",
"textSpan": {
"start": 1174,
"length": 9
},
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "method",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "ThingWithDeprecations",
"kind": "interfaceName"
},
{
"text": "<",
"kind": "punctuation"
},
{
"text": "void",
"kind": "keyword"
},
{
"text": ">",
"kind": "punctuation"
},
{
"text": ".",
"kind": "punctuation"
},
{
"text": "subscribe",
"kind": "methodName"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "observer",
"kind": "parameterName"
},
{
"text": "?",
"kind": "punctuation"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "PartialObserver",
"kind": "interfaceName"
},
{
"text": "<",
"kind": "punctuation"
},
{
"text": "void",
"kind": "keyword"
},
{
"text": ">",
"kind": "punctuation"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "Subscription",
"kind": "interfaceName"
},
{
"text": " ",
"kind": "space"
},
{
"text": "(",
"kind": "punctuation"
},
{
"text": "+",
"kind": "operator"
},
{
"text": "2",
"kind": "numericLiteral"
},
{
"text": " ",
"kind": "space"
},
{
"text": "overloads",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
}
],
"documentation": []
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is the assertion we're interested in

}
}
]
30 changes: 30 additions & 0 deletions tests/cases/fourslash/deprecatedInheritedJSDocOverload.ts
@@ -0,0 +1,30 @@

//// interface PartialObserver<T> {}
//// interface Subscription {}
//// interface Unsubscribable {}
////
//// export interface Subscribable<T> {
//// subscribe(observer?: PartialObserver<T>): Unsubscribable;
//// /** @deprecated Base deprecation 1 */
//// subscribe(next: null | undefined, error: null | undefined, complete: () => void): Unsubscribable;
//// /** @deprecated Base deprecation 2 */
//// subscribe(next: null | undefined, error: (error: any) => void, complete?: () => void): Unsubscribable;
//// /** @deprecated Base deprecation 3 */
//// subscribe(next: (value: T) => void, error: null | undefined, complete: () => void): Unsubscribable;
//// subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Unsubscribable;
//// }

//// interface ThingWithDeprecations<T> extends Subscribable<T> {
//// subscribe(observer?: PartialObserver<T>): Subscription;
//// /** @deprecated 'real' deprecation */
//// subscribe(next: null | undefined, error: null | undefined, complete: () => void): Subscription;
//// /** @deprecated 'real' deprecation */
//// subscribe(next: null | undefined, error: (error: any) => void, complete?: () => void): Subscription;
//// }

//// declare const a: ThingWithDeprecations<void>
//// a.subscribe/**/(() => {
//// console.log('something happened');
//// });

verify.baselineQuickInfo();