Skip to content

fix(41818): Detached JSDoc class docstring using @extends crashes tsc#41858

Merged
sandersn merged 1 commit into
microsoft:masterfrom
a-tarasyuk:fix/41818
Dec 18, 2020
Merged

fix(41818): Detached JSDoc class docstring using @extends crashes tsc#41858
sandersn merged 1 commit into
microsoft:masterfrom
a-tarasyuk:fix/41818

Conversation

@a-tarasyuk
Copy link
Copy Markdown
Contributor

Fixes #41818

In case of multiple documentation comments before a JavaScript symbol, JSDoc only associates the last comment with the symbol.

/**
 * @constructor
 */
class A {
    constructor() {}
}

/**
 * @extends {A}
 */

/**
 * @constructor
 */
class B extends A {
    constructor() {
        super();
    }
}
checkJSDocAugmentsTag checks
function checkJSDocAugmentsTag(node: JSDocAugmentsTag): void {
    const classLike = getEffectiveJSDocHost(node);
    if (!classLike || !isClassDeclaration(classLike) && !isClassExpression(classLike)) {
        error(classLike, Diagnostics.JSDoc_0_is_not_attached_to_a_class, idText(node.tagName));
        return;
    }
    const augmentsTags = getJSDocTags(classLike).filter(isJSDocAugmentsTag);
    Debug.assert(augmentsTags.length > 0);
}
/**
 * @extends {A}
 */

getEffectiveJSDocHost returns node related to class B extends A {}. getJSDocTags(classLike) uses the comment associates with class B extends A {}

/**
 * @constructor
 */

that doesn't have @extends. For that reason TS throws Debug.assert(augmentsTags.length > 0);.

I suppose in this case getEffectiveJSDocHost should return undefined, to throw error JSDoc_0_is_not_attached_to_a_class

/**
 * @extends {A}
 */

Or do we need to check isJSDocAugmentsTag in all JSDoc comments?

/cc @sandersn

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Dec 7, 2020
@sandersn
Copy link
Copy Markdown
Member

Your first solution is the right one. getEffectiveJSDocHost needs to understand that only the jsdoc immediately before a node is used for tags. And checkJSDocAugmentsTag should log the error for the example above.

Copy link
Copy Markdown
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

I like the checking you added to getEffectiveJSDocHost but it should be pushed down to getJSDocHost, if possible.

Comment thread src/compiler/utilities.ts Outdated
Comment thread src/compiler/checker.ts Outdated
Comment thread src/compiler/utilities.ts Outdated
@sandersn sandersn merged commit a763600 into microsoft:master Dec 18, 2020
sandersn added a commit that referenced this pull request Apr 14, 2021
JSDoc typedefs don't actually have hosts, because they're not
semantically attached to a declaration. However, the parser still
attaches them to some declaration (or statement), but that declaration
is not related to the typedef.

Previously, delayedBindJSDocTypedefTag used getJSDocHost to walk past
the unrelated declaration, but #41858 correctly started categorising
typedefs as unattached, with no host, so the binder began falling
back to file scope.

The path to skip the unrelated declaration is always the same, though, so this
PR uses `typeAlias.parent.parent` instead of `getJSDocHost(typeAlias)`.
sandersn added a commit that referenced this pull request Apr 14, 2021
JSDoc typedefs don't actually have hosts, because they're not
semantically attached to a declaration. However, the parser still
attaches them to some declaration (or statement), but that declaration
is not related to the typedef.

Previously, delayedBindJSDocTypedefTag used getJSDocHost to walk past
the unrelated declaration, but #41858 correctly started categorising
typedefs as unattached, with no host, so the binder began falling
back to file scope.

The path to skip the unrelated declaration is always the same, though, so this
PR uses `typeAlias.parent.parent` instead of `getJSDocHost(typeAlias)`.
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Detached JSDoc class docstring using @extends crashes tsc

4 participants