From e5f8287e5cd695f646ac7e0500836bce6cb956bf Mon Sep 17 00:00:00 2001 From: Andy Date: Wed, 18 Oct 2017 15:46:39 -0700 Subject: [PATCH] findAllRefs: Support anonymous default export (#19302) --- src/services/findAllReferences.ts | 27 ++++++++++++------- ...indAllRefsDefaultImportThroughNamespace.ts | 2 +- .../findAllRefsForDefaultExport_anonymous.ts | 13 +++++++++ .../findAllRefsForDefaultExport_reExport.ts | 2 -- 4 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 tests/cases/fourslash/findAllRefsForDefaultExport_anonymous.ts diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index e43a2cbfd8..0f64321f1b 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -377,19 +377,26 @@ namespace ts.FindAllReferences.Core { const result: SymbolAndEntries[] = []; const state = new State(sourceFiles, /*isForConstructor*/ node.kind === SyntaxKind.ConstructorKeyword, checker, cancellationToken, searchMeaning, options, result); - const search = state.createSearch(node, symbol, /*comingFrom*/ undefined, { allSearchSymbols: populateSearchSymbolSet(symbol, node, checker, options.implementations) }); - // Try to get the smallest valid scope that we can limit our search to; - // otherwise we'll need to search globally (i.e. include each file). - const scope = getSymbolScope(symbol); - if (scope) { - getReferencesInContainer(scope, scope.getSourceFile(), search, state); + if (node.kind === SyntaxKind.DefaultKeyword) { + addReference(node, symbol, node, state); + searchForImportsOfExport(node, symbol, { exportingModuleSymbol: symbol.parent, exportKind: ExportKind.Default }, state); } else { - // Global search - for (const sourceFile of state.sourceFiles) { - state.cancellationToken.throwIfCancellationRequested(); - searchForName(sourceFile, search, state); + const search = state.createSearch(node, symbol, /*comingFrom*/ undefined, { allSearchSymbols: populateSearchSymbolSet(symbol, node, checker, options.implementations) }); + + // Try to get the smallest valid scope that we can limit our search to; + // otherwise we'll need to search globally (i.e. include each file). + const scope = getSymbolScope(symbol); + if (scope) { + getReferencesInContainer(scope, scope.getSourceFile(), search, state); + } + else { + // Global search + for (const sourceFile of state.sourceFiles) { + state.cancellationToken.throwIfCancellationRequested(); + searchForName(sourceFile, search, state); + } } } diff --git a/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts b/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts index 1411a106c8..722403f45c 100644 --- a/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts +++ b/tests/cases/fourslash/findAllRefsDefaultImportThroughNamespace.ts @@ -15,7 +15,7 @@ const [r0, r1, r2, r3, r4] = test.ranges(); -verify.referenceGroups([r0], [{ definition: "function f(): void", ranges: [r1, r2] }]); +verify.referenceGroups([r0], [{ definition: "function f(): void", ranges: [r0, r2] }]); verify.singleReferenceGroup("function f(): void", [r1, r2]); verify.singleReferenceGroup("(property) default: number", [r3, r4]); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport_anonymous.ts b/tests/cases/fourslash/findAllRefsForDefaultExport_anonymous.ts new file mode 100644 index 0000000000..058bc41998 --- /dev/null +++ b/tests/cases/fourslash/findAllRefsForDefaultExport_anonymous.ts @@ -0,0 +1,13 @@ +/// + +// @Filename: /a.ts +////export [|{| "isDefinition": true, "isWriteAccess": true |}default|] 1; + +// @Filename: /b.ts +////import [|{| "isDefinition": true, "isWriteAccess": true |}a|] from "./a"; + +const [r0, r1] = test.ranges(); +verify.referenceGroups(r0, [ + { definition: "(property) default: 1", ranges: [r0] }, + { definition: "import a", ranges: [r1] }, +]); diff --git a/tests/cases/fourslash/findAllRefsForDefaultExport_reExport.ts b/tests/cases/fourslash/findAllRefsForDefaultExport_reExport.ts index 401db1a803..4c2427639f 100644 --- a/tests/cases/fourslash/findAllRefsForDefaultExport_reExport.ts +++ b/tests/cases/fourslash/findAllRefsForDefaultExport_reExport.ts @@ -10,8 +10,6 @@ // @Filename: /re-export-dep.ts ////import [|{| "isWriteAccess": true, "isDefinition": true |}fooDefault|] from "./re-export"; -verify.noErrors(); - const [r0, r1, r2, r3] = test.ranges(); verify.referenceGroups([r0, r1], [ { definition: "const foo: 1", ranges: [r0, r1] },