From 8ae9c7de21d3ec9a5e41c0e154947ca437fd98b8 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Fri, 16 Dec 2016 09:49:29 -0800 Subject: [PATCH] JSDoc functions are now in scope for instantiation Previously, `isSymbolInScopeOfMappedTypeParameter` did not understand JSDoc, so it would cause generic function declarations in JSDoc not be instantiated. --- src/compiler/checker.ts | 12 ++++++++++-- tests/cases/fourslash/jsDocGenerics2.ts | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/jsDocGenerics2.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 26164a528f3..5bcb5f90d55 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6634,7 +6634,7 @@ namespace ts { // Starting with the parent of the symbol's declaration, check if the mapper maps any of // the type parameters introduced by enclosing declarations. We just pick the first // declaration since multiple declarations will all have the same parent anyway. - let node = symbol.declarations[0].parent; + let node: Node = symbol.declarations[0]; while (node) { switch (node.kind) { case SyntaxKind.FunctionType: @@ -6654,7 +6654,7 @@ namespace ts { case SyntaxKind.ClassExpression: case SyntaxKind.InterfaceDeclaration: case SyntaxKind.TypeAliasDeclaration: - const declaration = node; + const declaration = node as DeclarationWithTypeParameters; if (declaration.typeParameters) { for (const d of declaration.typeParameters) { if (contains(mappedTypes, getDeclaredTypeOfTypeParameter(getSymbolOfNode(d)))) { @@ -6669,6 +6669,14 @@ namespace ts { } } break; + case SyntaxKind.JSDocFunctionType: + const func = node as JSDocFunctionType; + for (const p of func.parameters) { + if (contains(mappedTypes, getTypeOfNode(p))) { + return true; + } + } + break; case SyntaxKind.ModuleDeclaration: case SyntaxKind.SourceFile: return false; diff --git a/tests/cases/fourslash/jsDocGenerics2.ts b/tests/cases/fourslash/jsDocGenerics2.ts new file mode 100644 index 00000000000..23deb1a6502 --- /dev/null +++ b/tests/cases/fourslash/jsDocGenerics2.ts @@ -0,0 +1,19 @@ +/// +// @allowNonTsExtensions: true +// @Filename: Foo.js + +/////** +//// * @param {T[]} arr +//// * @param {(function(T):T)} valuator +//// * @template T +//// */ +////function SortFilter(arr,valuator) +////{ +//// return arr; +////} +////var a/*1*/ = SortFilter([0, 1, 2], q/*2*/ => q); +////var b/*3*/ = SortFilter([0, 1, 2], undefined); + +verify.quickInfoAt('1', "var a: number[]"); +verify.quickInfoAt('2', '(parameter) q: number'); +verify.quickInfoAt('3', "var b: number[]");