Skip to content

Commit f4e371c

Browse files
Harden findAllReferences.ts against symbol.declarations = undefined cases (microsoft#37088)
Fixes microsoft#37086
1 parent 241b3b9 commit f4e371c

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

src/services/findAllReferences.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,21 @@ namespace ts.FindAllReferences {
700700
}
701701
});
702702

703-
for (const decl of symbol.declarations) {
704-
switch (decl.kind) {
705-
case SyntaxKind.SourceFile:
706-
// Don't include the source file itself. (This may not be ideal behavior, but awkward to include an entire file as a reference.)
707-
break;
708-
case SyntaxKind.ModuleDeclaration:
709-
if (sourceFilesSet.has(decl.getSourceFile().fileName)) {
710-
references.push(nodeEntry((decl as ModuleDeclaration).name));
711-
}
712-
break;
713-
default:
714-
// This may be merged with something.
715-
Debug.assert(!!(symbol.flags & SymbolFlags.Transient), "Expected a module symbol to be declared by a SourceFile or ModuleDeclaration.");
703+
if (symbol.declarations) {
704+
for (const decl of symbol.declarations) {
705+
switch (decl.kind) {
706+
case SyntaxKind.SourceFile:
707+
// Don't include the source file itself. (This may not be ideal behavior, but awkward to include an entire file as a reference.)
708+
break;
709+
case SyntaxKind.ModuleDeclaration:
710+
if (sourceFilesSet.has(decl.getSourceFile().fileName)) {
711+
references.push(nodeEntry((decl as ModuleDeclaration).name));
712+
}
713+
break;
714+
default:
715+
// This may be merged with something.
716+
Debug.assert(!!(symbol.flags & SymbolFlags.Transient), "Expected a module symbol to be declared by a SourceFile or ModuleDeclaration.");
717+
}
716718
}
717719
}
718720

@@ -1075,6 +1077,8 @@ namespace ts.FindAllReferences {
10751077

10761078
// Go to the symbol we imported from and find references for it.
10771079
function searchForImportedSymbol(symbol: Symbol, state: State): void {
1080+
if (!symbol.declarations) return;
1081+
10781082
for (const declaration of symbol.declarations) {
10791083
const exportingFile = declaration.getSourceFile();
10801084
// Need to search in the file even if it's not in the search-file set, because it might export the symbol.
@@ -1554,7 +1558,7 @@ namespace ts.FindAllReferences {
15541558
*/
15551559
function findOwnConstructorReferences(classSymbol: Symbol, sourceFile: SourceFile, addNode: (node: Node) => void): void {
15561560
const constructorSymbol = getClassConstructorSymbol(classSymbol);
1557-
if (constructorSymbol) {
1561+
if (constructorSymbol && constructorSymbol.declarations) {
15581562
for (const decl of constructorSymbol.declarations) {
15591563
const ctrKeyword = findChildOfKind(decl, SyntaxKind.ConstructorKeyword, sourceFile)!;
15601564
Debug.assert(decl.kind === SyntaxKind.Constructor && !!ctrKeyword);
@@ -1586,7 +1590,7 @@ namespace ts.FindAllReferences {
15861590
/** Find references to `super` in the constructor of an extending class. */
15871591
function findSuperConstructorAccesses(classDeclaration: ClassLikeDeclaration, addNode: (node: Node) => void): void {
15881592
const constructor = getClassConstructorSymbol(classDeclaration.symbol);
1589-
if (!constructor) {
1593+
if (!(constructor && constructor.declarations)) {
15901594
return;
15911595
}
15921596

@@ -1722,7 +1726,7 @@ namespace ts.FindAllReferences {
17221726
// Set the key so that we don't infinitely recurse
17231727
cachedResults.set(key, false);
17241728

1725-
const inherits = symbol.declarations.some(declaration =>
1729+
const inherits = !!symbol.declarations && symbol.declarations.some(declaration =>
17261730
getAllSuperTypeNodes(declaration).some(typeReference => {
17271731
const type = checker.getTypeAtLocation(typeReference);
17281732
return !!type && !!type.symbol && explicitlyInheritsFrom(type.symbol, parent, cachedResults, checker);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*====== /tests/cases/fourslash/b.js ======*/
2+
3+
export {
4+
[|RENAME|]
5+
} from './a';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
// @allowJs: true
4+
5+
// @Filename: a.js
6+
//// import foo from 'unfound';
7+
//// export {
8+
//// foo,
9+
//// };
10+
11+
// @Filename: b.js
12+
//// export {
13+
//// /**/foo
14+
//// } from './a';
15+
16+
goTo.marker();
17+
verify.baselineRename("", { });

0 commit comments

Comments
 (0)