diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0612c4d430780..7da5d74739105 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4406,7 +4406,7 @@ namespace ts { type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); } else { - Debug.fail("Unhandled declaration kind! " + (ts as any).SyntaxKind[declaration.kind]); + Debug.fail("Unhandled declaration kind! " + Debug.showSyntaxKind(declaration)); } if (!popTypeResolution()) { @@ -20682,7 +20682,7 @@ namespace ts { case SyntaxKind.ImportSpecifier: // https://github.com/Microsoft/TypeScript/pull/7591 return DeclarationSpaces.ExportValue; default: - Debug.fail((ts as any).SyntaxKind[d.kind]); + Debug.fail(Debug.showSyntaxKind(d)); } } } diff --git a/src/compiler/core.ts b/src/compiler/core.ts index fe8e63676d7d5..8479cf9418086 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -1466,7 +1466,7 @@ namespace ts { if (value !== undefined && test(value)) return value; if (value && typeof (value as any).kind === "number") { - Debug.fail(`Invalid cast. The supplied ${(ts as any).SyntaxKind[(value as any).kind]} did not pass the test '${Debug.getFunctionName(test)}'.`); + Debug.fail(`Invalid cast. The supplied ${Debug.showSyntaxKind(value as any as Node)} did not pass the test '${Debug.getFunctionName(test)}'.`); } else { Debug.fail(`Invalid cast. The supplied value did not pass the test '${Debug.getFunctionName(test)}'.`); @@ -2925,6 +2925,25 @@ namespace ts { return match ? match[1] : ""; } } + + export function showSymbol(symbol: Symbol): string { + return `{ flags: ${showFlags(symbol.flags, (ts as any).SymbolFlags)}; declarations: ${map(symbol.declarations, showSyntaxKind)} }`; + } + + function showFlags(flags: number, flagsEnum: { [flag: number]: string }): string { + const out = []; + for (let pow = 0; pow <= 30; pow++) { + const n = 1 << pow; + if (flags & n) { + out.push(flagsEnum[n]); + } + } + return out.join("|"); + } + + export function showSyntaxKind(node: Node): string { + return (ts as any).SyntaxKind[node.kind]; + } } /** Remove an item from an array, moving everything to its right one space left. */ diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index 8b9937394f10b..ba20b56ce88f4 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -416,10 +416,16 @@ namespace ts.FindAllReferences.Core { } // If the symbol is declared as part of a declaration like `{ type: "a" } | { type: "b" }`, use the property on the union type to get more references. - return firstDefined(symbol.declarations, decl => - isTypeLiteralNode(decl.parent) && isUnionTypeNode(decl.parent.parent) + return firstDefined(symbol.declarations, decl => { + if (!decl.parent) { + // Assertions for GH#21814. We should be handling SourceFile symbols in `getReferencedSymbolsForModule` instead of getting here. + Debug.assert(decl.kind === SyntaxKind.SourceFile); + Debug.fail(`Unexpected symbol at ${Debug.showSyntaxKind(node)}: ${Debug.showSymbol(symbol)}`); + } + return isTypeLiteralNode(decl.parent) && isUnionTypeNode(decl.parent.parent) ? checker.getPropertyOfType(checker.getTypeFromTypeNode(decl.parent.parent), symbol.name) - : undefined) || symbol; + : undefined; + }) || symbol; } /** diff --git a/src/services/importTracker.ts b/src/services/importTracker.ts index 02d0979ca1ed6..e76e21b2963ba 100644 --- a/src/services/importTracker.ts +++ b/src/services/importTracker.ts @@ -517,29 +517,12 @@ namespace ts.FindAllReferences { const sym = useLhsSymbol ? checker.getSymbolAtLocation(cast(node.left, isPropertyAccessExpression).name) : symbol; // Better detection for GH#20803 if (sym && !(checker.getMergedSymbol(sym.parent).flags & SymbolFlags.Module)) { - Debug.fail(`Special property assignment kind does not have a module as its parent. Assignment is ${showSymbol(sym)}, parent is ${showSymbol(sym.parent)}`); + Debug.fail(`Special property assignment kind does not have a module as its parent. Assignment is ${Debug.showSymbol(sym)}, parent is ${Debug.showSymbol(sym.parent)}`); } return sym && exportInfo(sym, kind); } } - function showSymbol(s: Symbol): string { - const decls = s.declarations.map(d => (ts as any).SyntaxKind[d.kind]).join(","); - const flags = showFlags(s.flags, (ts as any).SymbolFlags); - return `{ declarations: ${decls}, flags: ${flags} }`; - } - - function showFlags(f: number, flags: any) { - const out = []; - for (let pow = 0; pow <= 30; pow++) { - const n = 1 << pow; - if (f & n) { - out.push(flags[n]); - } - } - return out.join("|"); - } - function getImport(): ImportedSymbol | undefined { const isImport = isNodeImport(node); if (!isImport) return undefined;