From eb804a9706bdd965bd7e806fded3fe33c79eda20 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Thu, 25 Mar 2021 03:27:24 -0400 Subject: [PATCH] `getSemanticDocumentHighlights`: don't fail... ...when missing `program.redirectTargetsMap` info. This assertion fails in the added test case -- looks like there is no entry in `program.redirectTargetsMap` when it comes from a file that is no in the project. So in this case don't follow the (missing) info, and instead drop the highlight. Fixes #33722. --- src/services/documentHighlights.ts | 6 ++++-- .../cases/fourslash/documentHighlights_33722.ts | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/documentHighlights_33722.ts diff --git a/src/services/documentHighlights.ts b/src/services/documentHighlights.ts index b0282393cce..a24ac6d02f0 100644 --- a/src/services/documentHighlights.ts +++ b/src/services/documentHighlights.ts @@ -32,9 +32,11 @@ namespace ts { const referenceEntries = FindAllReferences.getReferenceEntriesForNode(position, node, program, sourceFilesToSearch, cancellationToken, /*options*/ undefined, sourceFilesSet); if (!referenceEntries) return undefined; const map = arrayToMultiMap(referenceEntries.map(FindAllReferences.toHighlightSpan), e => e.fileName, e => e.span); - return arrayFrom(map.entries(), ([fileName, highlightSpans]) => { + return mapDefined(arrayFrom(map.entries()), ([fileName, highlightSpans]) => { if (!sourceFilesSet.has(fileName)) { - Debug.assert(program.redirectTargetsMap.has(fileName)); + if (!program.redirectTargetsMap.has(fileName)) { + return undefined; + } const redirectTarget = program.getSourceFile(fileName); const redirect = find(sourceFilesToSearch, f => !!f.redirectInfo && f.redirectInfo.redirectTarget === redirectTarget)!; fileName = redirect.fileName; diff --git a/tests/cases/fourslash/documentHighlights_33722.ts b/tests/cases/fourslash/documentHighlights_33722.ts new file mode 100644 index 00000000000..4374bb5970e --- /dev/null +++ b/tests/cases/fourslash/documentHighlights_33722.ts @@ -0,0 +1,17 @@ +/// + +// @Filename: /y.ts +////class Foo { +//// private foo() {} +////} +//// +////const f = () => new Foo(); +////export default f; + +// @Filename: /x.ts +////import y from "./y"; +//// +////y().[|foo|](); + +const [r] = test.ranges(); +verify.documentHighlightsOf(r, [], { filesToSearch: ["/x.ts"] });