Skip to content

Constructor function methods:Add two missing tag lookups #47742

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

Merged
merged 1 commit into from
Feb 9, 2022

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Feb 4, 2022

  1. During name resolution, @param and @return tags should walk up through the jsdoc comment and then jump to the host function. Previously they did not, which would cause them to not resolve type parameters bound in the scope of a host that was not a sibling of the comment. The example from Failing to resolve multiple JSDOC @template tags #46618 is a prototype method:
/**
 * @template {T}
 * @param {T} t
 */
C.prototype.m = function (t) {
}
  1. During name resolution, prototype methods are supposed to resolve types both from the host function's location and from the containing class' location. The containing class lookup happens in a separate call to resolveName. Previously, the code that finds the containing class only worked for the above style of comment, which is on the outer ExpressionStatement, but not for the below style, which is on the function expression itself:
C.prototype.m =
  /**
   * @template {T}
   * @param {T} t
   */
  function (t) {
}

Fixes #46618

1. During name resolution, `@param` and `@return` tags should walk up
through the jsdoc comment and then jump to the host function. Previously they
did not, which would cause them to not resolve type parameters bound in
the scope of a host that was not a sibling of the comment. The example
from #46618 is a prototype method:

```js
/**
 * @template {T}
 * @param {T} t
 */
C.prototype.m = function (t) {
}
```

2. During name resolution, prototype methods are supposed to resolve
types both from the host function's location and from the containing
class' location. The containing class lookup happens in a separate call
to `resolveName`. Previously, the code that finds the containing class
only worked for the above style of comment, which is on the outer
ExpressionStatement, but not for the below style, which is on the
function expression itself:

```js
C.prototype.m =
  /**
   * @template {T}
   * @param {T} t
   */
  function (t) {
}
```
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Feb 4, 2022
isExpressionStatement(host) &&
isBinaryExpression(host.expression) &&
getAssignmentDeclarationKind(host.expression) === AssignmentDeclarationKind.PrototypeProperty) {
// X.prototype.m = /** @param {K} p */ function () { } <-- look for K on X's declaration
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 comment was wrong before. The new code implements this.

const symbol = getSymbolOfNode(host.expression.left);
if (symbol) {
return getDeclarationOfJSPrototypeContainer(symbol);
}
}
if (host && isFunctionExpression(host) && isPrototypePropertyAssignment(host.parent) && isExpressionStatement(host.parent.parent)) {
// X.prototype.m = /** @param {K} p */ function () { } <-- look for K on X's declaration
Copy link
Member

Choose a reason for hiding this comment

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

not sure if this is something we have to worry about here in this function, but what happens if we have conflicting tags with the same name in the top-level and nested scopes?

Copy link
Member Author

Choose a reason for hiding this comment

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

The nearest tag to the parameter, the one on the function expression, resolves first. You can also put a @type on the parameter itself, and that resolves before any @param tag.

Conflicting tags in the same comment should resolve to the first tag.

@sandersn sandersn merged commit d5c3015 into main Feb 9, 2022
@sandersn sandersn deleted the fix-two-ctor-func-tag-lookups branch February 9, 2022 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failing to resolve multiple JSDOC @template tags
3 participants