diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4c9a98951fae8..59ce935546bf5 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1453,6 +1453,12 @@ namespace ts { location = location.parent; } break; + case SyntaxKind.JSDocTypedefTag: + case SyntaxKind.JSDocCallbackTag: + // js type aliases do not resolve names from their host, so skip past it + lastLocation = location; + location = getJSDocHost(location).parent; + continue; } if (isSelfReferenceLocation(location)) { lastSelfReferenceLocation = location; @@ -2153,25 +2159,31 @@ namespace ts { */ function resolveEntityNameFromJSSpecialAssignment(name: Identifier, meaning: SymbolFlags) { if (isJSDocTypeReference(name.parent)) { - const host = getJSDocHost(name.parent); - if (host) { - const secondaryLocation = getJSSpecialAssignmentSymbol(getJSDocHost(name.parent.parent.parent as JSDocTag)); - return secondaryLocation && resolveName(secondaryLocation, name.escapedText, meaning, /*nameNotFoundMessage*/ undefined, name, /*isUse*/ true); + const secondaryLocation = getJSSpecialAssignmentLocation(name.parent); + if (secondaryLocation) { + return resolveName(secondaryLocation, name.escapedText, meaning, /*nameNotFoundMessage*/ undefined, name, /*isUse*/ true); } } } - function getJSSpecialAssignmentSymbol(host: HasJSDoc): Declaration | undefined { - if (isPropertyAssignment(host) && isFunctionLike(host.initializer)) { - const symbol = getSymbolOfNode(host.initializer); - return symbol && symbol.valueDeclaration; + function getJSSpecialAssignmentLocation(node: TypeReferenceNode): Declaration | undefined { + const typeAlias = findAncestor(node, node => !(isJSDocNode(node) || node.flags & NodeFlags.JSDoc) ? "quit" : isJSDocTypeAlias(node)); + if (typeAlias) { + return; } - else if (isExpressionStatement(host) && - isBinaryExpression(host.expression) && - getSpecialPropertyAssignmentKind(host.expression) === SpecialPropertyAssignmentKind.PrototypeProperty) { + const host = getJSDocHost(node); + if (host && + isExpressionStatement(host) && + isBinaryExpression(host.expression) && + getSpecialPropertyAssignmentKind(host.expression) === SpecialPropertyAssignmentKind.PrototypeProperty) { const symbol = getSymbolOfNode(host.expression.left); return symbol && symbol.parent.valueDeclaration; } + const sig = getHostSignatureFromJSDocHost(host); + if (sig) { + const symbol = getSymbolOfNode(sig); + return symbol && symbol.valueDeclaration; + } } function resolveExternalModuleName(location: Node, moduleReferenceExpression: Expression): Symbol { @@ -9554,7 +9566,16 @@ namespace ts { // parameters that are in scope (and therefore potentially referenced). For type literals that // aren't the right hand side of a generic type alias declaration we optimize by reducing the // set of type parameters to those that are possibly referenced in the literal. - const declaration = symbol.declarations[0]; + let declaration = symbol.declarations[0]; + if (isInJavaScriptFile(declaration)) { + const paramTag = findAncestor(declaration, isJSDocParameterTag); + if (paramTag) { + const paramSymbol = getParameterSymbolFromJSDoc(paramTag); + if (paramSymbol) { + declaration = paramSymbol.valueDeclaration; + } + } + } let outerTypeParameters = getOuterTypeParameters(declaration, /*includeThisTypes*/ true); if (isJavaScriptConstructor(declaration)) { const templateTagParameters = getTypeParametersFromDeclaration(declaration as DeclarationWithTypeParameters); diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7339907547a57..6e440e01ecfbc 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1865,8 +1865,7 @@ namespace ts { // * @returns {number} // */ // var x = function(name) { return name.length; } - if (parent.parent && - (getSingleVariableOfVariableStatement(parent.parent) === node)) { + if (parent.parent && (getSingleVariableOfVariableStatement(parent.parent) === node)) { getJSDocCommentsAndTagsWorker(parent.parent); } if (parent.parent && parent.parent.parent && @@ -1913,8 +1912,11 @@ namespace ts { return parameter && parameter.symbol; } - export function getHostSignatureFromJSDoc(node: JSDocTag): SignatureDeclaration | undefined { - const host = getJSDocHost(node); + export function getHostSignatureFromJSDoc(node: Node): SignatureDeclaration | undefined { + return getHostSignatureFromJSDocHost(getJSDocHost(node)); + } + + export function getHostSignatureFromJSDocHost(host: HasJSDoc): SignatureDeclaration | undefined { const decl = getSourceOfDefaultedAssignment(host) || getSourceOfAssignment(host) || getSingleInitializerOfVariableStatementOrPropertyDeclaration(host) || diff --git a/tests/baselines/reference/paramTagTypeResolution.symbols b/tests/baselines/reference/paramTagTypeResolution.symbols new file mode 100644 index 0000000000000..1584e2fcb95c6 --- /dev/null +++ b/tests/baselines/reference/paramTagTypeResolution.symbols @@ -0,0 +1,23 @@ +=== tests/cases/conformance/jsdoc/main.js === +var f = require('./first'); +>f : Symbol(f, Decl(main.js, 0, 3)) +>require : Symbol(require) +>'./first' : Symbol("tests/cases/conformance/jsdoc/first", Decl(first.js, 0, 0)) + +f(1, n => { }) +>f : Symbol(f, Decl(main.js, 0, 3)) +>n : Symbol(n, Decl(main.js, 1, 4)) + +=== tests/cases/conformance/jsdoc/first.js === +/** @template T + * @param {T} x + * @param {(t: T) => void} k + */ +module.exports = function (x, k) { return k(x) } +>module : Symbol(export=, Decl(first.js, 0, 0)) +>exports : Symbol(export=, Decl(first.js, 0, 0)) +>x : Symbol(x, Decl(first.js, 4, 27)) +>k : Symbol(k, Decl(first.js, 4, 29)) +>k : Symbol(k, Decl(first.js, 4, 29)) +>x : Symbol(x, Decl(first.js, 4, 27)) + diff --git a/tests/baselines/reference/paramTagTypeResolution.types b/tests/baselines/reference/paramTagTypeResolution.types new file mode 100644 index 0000000000000..daf7749c6bb84 --- /dev/null +++ b/tests/baselines/reference/paramTagTypeResolution.types @@ -0,0 +1,31 @@ +=== tests/cases/conformance/jsdoc/main.js === +var f = require('./first'); +>f : (x: T, k: (t: T) => void) => void +>require('./first') : (x: T, k: (t: T) => void) => void +>require : any +>'./first' : "./first" + +f(1, n => { }) +>f(1, n => { }) : void +>f : (x: T, k: (t: T) => void) => void +>1 : 1 +>n => { } : (n: number) => void +>n : number + +=== tests/cases/conformance/jsdoc/first.js === +/** @template T + * @param {T} x + * @param {(t: T) => void} k + */ +module.exports = function (x, k) { return k(x) } +>module.exports = function (x, k) { return k(x) } : (x: T, k: (t: T) => void) => void +>module.exports : any +>module : any +>exports : any +>function (x, k) { return k(x) } : (x: T, k: (t: T) => void) => void +>x : T +>k : (t: T) => void +>k(x) : void +>k : (t: T) => void +>x : T + diff --git a/tests/baselines/reference/typedefTagTypeResolution.errors.txt b/tests/baselines/reference/typedefTagTypeResolution.errors.txt new file mode 100644 index 0000000000000..8d8c324beb32f --- /dev/null +++ b/tests/baselines/reference/typedefTagTypeResolution.errors.txt @@ -0,0 +1,37 @@ +tests/cases/conformance/jsdoc/github20832.js(2,15): error TS2304: Cannot find name 'U'. +tests/cases/conformance/jsdoc/github20832.js(17,12): error TS2304: Cannot find name 'V'. + + +==== tests/cases/conformance/jsdoc/github20832.js (2 errors) ==== + // #20832 + /** @typedef {U} T - should be "error, can't find type named 'U' */ + ~ +!!! error TS2304: Cannot find name 'U'. + /** + * @template U + * @param {U} x + * @return {T} + */ + function f(x) { + return x; + } + + /** @type T - should be fine, since T will be any */ + const x = 3; + + /** + * @callback Cb + * @param {V} firstParam + ~ +!!! error TS2304: Cannot find name 'V'. + */ + /** + * @template V + * @param {V} vvvvv + */ + function g(vvvvv) { + } + + /** @type {Cb} */ + const cb = x => {} + \ No newline at end of file diff --git a/tests/baselines/reference/typedefTagTypeResolution.symbols b/tests/baselines/reference/typedefTagTypeResolution.symbols new file mode 100644 index 0000000000000..9201ecb2cd855 --- /dev/null +++ b/tests/baselines/reference/typedefTagTypeResolution.symbols @@ -0,0 +1,38 @@ +=== tests/cases/conformance/jsdoc/github20832.js === +// #20832 +/** @typedef {U} T - should be "error, can't find type named 'U' */ +/** + * @template U + * @param {U} x + * @return {T} + */ +function f(x) { +>f : Symbol(f, Decl(github20832.js, 0, 0)) +>x : Symbol(x, Decl(github20832.js, 7, 11)) + + return x; +>x : Symbol(x, Decl(github20832.js, 7, 11)) +} + +/** @type T - should be fine, since T will be any */ +const x = 3; +>x : Symbol(x, Decl(github20832.js, 12, 5)) + +/** + * @callback Cb + * @param {V} firstParam + */ +/** + * @template V + * @param {V} vvvvv + */ +function g(vvvvv) { +>g : Symbol(g, Decl(github20832.js, 12, 12)) +>vvvvv : Symbol(vvvvv, Decl(github20832.js, 22, 11)) +} + +/** @type {Cb} */ +const cb = x => {} +>cb : Symbol(cb, Decl(github20832.js, 26, 5)) +>x : Symbol(x, Decl(github20832.js, 26, 10)) + diff --git a/tests/baselines/reference/typedefTagTypeResolution.types b/tests/baselines/reference/typedefTagTypeResolution.types new file mode 100644 index 0000000000000..20c9412832667 --- /dev/null +++ b/tests/baselines/reference/typedefTagTypeResolution.types @@ -0,0 +1,40 @@ +=== tests/cases/conformance/jsdoc/github20832.js === +// #20832 +/** @typedef {U} T - should be "error, can't find type named 'U' */ +/** + * @template U + * @param {U} x + * @return {T} + */ +function f(x) { +>f : (x: U) => any +>x : U + + return x; +>x : U +} + +/** @type T - should be fine, since T will be any */ +const x = 3; +>x : any +>3 : 3 + +/** + * @callback Cb + * @param {V} firstParam + */ +/** + * @template V + * @param {V} vvvvv + */ +function g(vvvvv) { +>g : (vvvvv: V) => void +>vvvvv : V +} + +/** @type {Cb} */ +const cb = x => {} +>cb : Cb +>x => {} : (x: any) => void +>x : any + diff --git a/tests/cases/conformance/jsdoc/paramTagTypeResolution.ts b/tests/cases/conformance/jsdoc/paramTagTypeResolution.ts new file mode 100644 index 0000000000000..0256651234d25 --- /dev/null +++ b/tests/cases/conformance/jsdoc/paramTagTypeResolution.ts @@ -0,0 +1,13 @@ +// @noEmit: true +// @allowJs: true +// @checkJs: true +// @Filename: first.js +/** @template T + * @param {T} x + * @param {(t: T) => void} k + */ +module.exports = function (x, k) { return k(x) } + +// @Filename: main.js +var f = require('./first'); +f(1, n => { }) diff --git a/tests/cases/conformance/jsdoc/typedefTagTypeResolution.ts b/tests/cases/conformance/jsdoc/typedefTagTypeResolution.ts new file mode 100644 index 0000000000000..c4c993d9c78e0 --- /dev/null +++ b/tests/cases/conformance/jsdoc/typedefTagTypeResolution.ts @@ -0,0 +1,32 @@ +// @noEmit: true +// @allowJs: true +// @checkJs: true +// @Filename: github20832.js + +// #20832 +/** @typedef {U} T - should be "error, can't find type named 'U' */ +/** + * @template U + * @param {U} x + * @return {T} + */ +function f(x) { + return x; +} + +/** @type T - should be fine, since T will be any */ +const x = 3; + +/** + * @callback Cb + * @param {V} firstParam + */ +/** + * @template V + * @param {V} vvvvv + */ +function g(vvvvv) { +} + +/** @type {Cb} */ +const cb = x => {}