Skip to content

Commit 171b68c

Browse files
author
Andy
authored
Add assertions for bad symbol declaration (#21837)
* Add assertions for bad symbol declaration * Fix lint
1 parent 31ec5e7 commit 171b68c

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4406,7 +4406,7 @@ namespace ts {
44064406
type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true);
44074407
}
44084408
else {
4409-
Debug.fail("Unhandled declaration kind! " + (ts as any).SyntaxKind[declaration.kind]);
4409+
Debug.fail("Unhandled declaration kind! " + Debug.showSyntaxKind(declaration));
44104410
}
44114411

44124412
if (!popTypeResolution()) {
@@ -20682,7 +20682,7 @@ namespace ts {
2068220682
case SyntaxKind.ImportSpecifier: // https://github.com/Microsoft/TypeScript/pull/7591
2068320683
return DeclarationSpaces.ExportValue;
2068420684
default:
20685-
Debug.fail((ts as any).SyntaxKind[d.kind]);
20685+
Debug.fail(Debug.showSyntaxKind(d));
2068620686
}
2068720687
}
2068820688
}

src/compiler/core.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ namespace ts {
14661466
if (value !== undefined && test(value)) return value;
14671467

14681468
if (value && typeof (value as any).kind === "number") {
1469-
Debug.fail(`Invalid cast. The supplied ${(ts as any).SyntaxKind[(value as any).kind]} did not pass the test '${Debug.getFunctionName(test)}'.`);
1469+
Debug.fail(`Invalid cast. The supplied ${Debug.showSyntaxKind(value as any as Node)} did not pass the test '${Debug.getFunctionName(test)}'.`);
14701470
}
14711471
else {
14721472
Debug.fail(`Invalid cast. The supplied value did not pass the test '${Debug.getFunctionName(test)}'.`);
@@ -2925,6 +2925,25 @@ namespace ts {
29252925
return match ? match[1] : "";
29262926
}
29272927
}
2928+
2929+
export function showSymbol(symbol: Symbol): string {
2930+
return `{ flags: ${showFlags(symbol.flags, (ts as any).SymbolFlags)}; declarations: ${map(symbol.declarations, showSyntaxKind)} }`;
2931+
}
2932+
2933+
function showFlags(flags: number, flagsEnum: { [flag: number]: string }): string {
2934+
const out = [];
2935+
for (let pow = 0; pow <= 30; pow++) {
2936+
const n = 1 << pow;
2937+
if (flags & n) {
2938+
out.push(flagsEnum[n]);
2939+
}
2940+
}
2941+
return out.join("|");
2942+
}
2943+
2944+
export function showSyntaxKind(node: Node): string {
2945+
return (ts as any).SyntaxKind[node.kind];
2946+
}
29282947
}
29292948

29302949
/** Remove an item from an array, moving everything to its right one space left. */

src/services/findAllReferences.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,16 @@ namespace ts.FindAllReferences.Core {
416416
}
417417

418418
// 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.
419-
return firstDefined(symbol.declarations, decl =>
420-
isTypeLiteralNode(decl.parent) && isUnionTypeNode(decl.parent.parent)
419+
return firstDefined(symbol.declarations, decl => {
420+
if (!decl.parent) {
421+
// Assertions for GH#21814. We should be handling SourceFile symbols in `getReferencedSymbolsForModule` instead of getting here.
422+
Debug.assert(decl.kind === SyntaxKind.SourceFile);
423+
Debug.fail(`Unexpected symbol at ${Debug.showSyntaxKind(node)}: ${Debug.showSymbol(symbol)}`);
424+
}
425+
return isTypeLiteralNode(decl.parent) && isUnionTypeNode(decl.parent.parent)
421426
? checker.getPropertyOfType(checker.getTypeFromTypeNode(decl.parent.parent), symbol.name)
422-
: undefined) || symbol;
427+
: undefined;
428+
}) || symbol;
423429
}
424430

425431
/**

src/services/importTracker.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -517,29 +517,12 @@ namespace ts.FindAllReferences {
517517
const sym = useLhsSymbol ? checker.getSymbolAtLocation(cast(node.left, isPropertyAccessExpression).name) : symbol;
518518
// Better detection for GH#20803
519519
if (sym && !(checker.getMergedSymbol(sym.parent).flags & SymbolFlags.Module)) {
520-
Debug.fail(`Special property assignment kind does not have a module as its parent. Assignment is ${showSymbol(sym)}, parent is ${showSymbol(sym.parent)}`);
520+
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)}`);
521521
}
522522
return sym && exportInfo(sym, kind);
523523
}
524524
}
525525

526-
function showSymbol(s: Symbol): string {
527-
const decls = s.declarations.map(d => (ts as any).SyntaxKind[d.kind]).join(",");
528-
const flags = showFlags(s.flags, (ts as any).SymbolFlags);
529-
return `{ declarations: ${decls}, flags: ${flags} }`;
530-
}
531-
532-
function showFlags(f: number, flags: any) {
533-
const out = [];
534-
for (let pow = 0; pow <= 30; pow++) {
535-
const n = 1 << pow;
536-
if (f & n) {
537-
out.push(flags[n]);
538-
}
539-
}
540-
return out.join("|");
541-
}
542-
543526
function getImport(): ImportedSymbol | undefined {
544527
const isImport = isNodeImport(node);
545528
if (!isImport) return undefined;

0 commit comments

Comments
 (0)