From be6e15391f3b2a1f0a9c90d1430958a78972f2a5 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 13 Jan 2022 05:37:06 +0000 Subject: [PATCH] Cherry-pick PR #47395 into release-4.5 Component commits: f230a5af47 Add failing test. 3ba84692c6 Guard against undefined module bodies in navbar/navtree. --- src/services/navigationBar.ts | 5 +++- .../navbarForDoubleAmbientModules01.ts | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/navbarForDoubleAmbientModules01.ts diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 6f9cdd55950c3..6b0e4869eb1a1 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -650,7 +650,10 @@ namespace ts.NavigationBar { // We use 1 NavNode to represent 'A.B.C', but there are multiple source nodes. // Only merge module nodes that have the same chain. Don't merge 'A.B.C' with 'A'! function areSameModule(a: ModuleDeclaration, b: ModuleDeclaration): boolean { - return a.body!.kind === b.body!.kind && (a.body!.kind !== SyntaxKind.ModuleDeclaration || areSameModule(a.body as ModuleDeclaration, b.body as ModuleDeclaration)); + if (!a.body || !b.body) { + return a.body === b.body; + } + return a.body.kind === b.body.kind && (a.body.kind !== SyntaxKind.ModuleDeclaration || areSameModule(a.body as ModuleDeclaration, b.body as ModuleDeclaration)); } /** Merge source into target. Source should be thrown away after this is called. */ diff --git a/tests/cases/fourslash/navbarForDoubleAmbientModules01.ts b/tests/cases/fourslash/navbarForDoubleAmbientModules01.ts new file mode 100644 index 0000000000000..2841f3e548d8d --- /dev/null +++ b/tests/cases/fourslash/navbarForDoubleAmbientModules01.ts @@ -0,0 +1,24 @@ +/// + +//// declare module "foo"; +//// declare module "foo"; + +verify.navigationBar([ + { + "text": "", + "kind": "script", + "childItems": [ + { + "text": "\"foo\"", + "kind": "module", + "kindModifiers": "declare" + } + ] + }, + { + "text": "\"foo\"", + "kind": "module", + "kindModifiers": "declare", + "indent": 1 + } +]); \ No newline at end of file