From a4ae3c46b65fe0224613c5d8bf3c258746d9bf16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 23 Jul 2024 00:58:39 +0200 Subject: [PATCH] Don't crash when observing invalid `export`s in any kind of container (#59376) --- src/services/documentHighlights.ts | 8 ++------ .../reference/exportInLabeledStatement.baseline.jsonc | 7 +++++++ tests/cases/fourslash/exportInLabeledStatement.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/exportInLabeledStatement.baseline.jsonc create mode 100644 tests/cases/fourslash/exportInLabeledStatement.ts diff --git a/src/services/documentHighlights.ts b/src/services/documentHighlights.ts index 0cb7654459c..644f0a12565 100644 --- a/src/services/documentHighlights.ts +++ b/src/services/documentHighlights.ts @@ -69,7 +69,6 @@ import { modifierToFlag, ModuleBlock, Node, - ObjectLiteralExpression, ObjectTypeDeclaration, Program, ReturnStatement, @@ -294,7 +293,7 @@ export namespace DocumentHighlights { function getNodesToSearchForModifier(declaration: Node, modifierFlag: ModifierFlags): readonly Node[] | undefined { // Types of node whose children might have modifiers. - const container = declaration.parent as ModuleBlock | SourceFile | Block | CaseClause | DefaultClause | ConstructorDeclaration | MethodDeclaration | FunctionDeclaration | ObjectTypeDeclaration | ObjectLiteralExpression; + const container = declaration.parent as ModuleBlock | SourceFile | Block | CaseClause | DefaultClause | ConstructorDeclaration | MethodDeclaration | FunctionDeclaration | ObjectTypeDeclaration; switch (container.kind) { case SyntaxKind.ModuleBlock: case SyntaxKind.SourceFile: @@ -332,11 +331,8 @@ export namespace DocumentHighlights { return nodes; // Syntactically invalid positions that the parser might produce anyway - case SyntaxKind.ObjectLiteralExpression: - return undefined; - default: - Debug.assertNever(container, "Invalid container kind."); + return undefined; } } diff --git a/tests/baselines/reference/exportInLabeledStatement.baseline.jsonc b/tests/baselines/reference/exportInLabeledStatement.baseline.jsonc new file mode 100644 index 00000000000..1d354ce77c8 --- /dev/null +++ b/tests/baselines/reference/exportInLabeledStatement.baseline.jsonc @@ -0,0 +1,7 @@ +// === documentHighlights === +// filesToSearch: +// /tests/cases/fourslash/a.ts + +// === /tests/cases/fourslash/a.ts === +// subTitle: +// /*HIGHLIGHTS*/export const title: string \ No newline at end of file diff --git a/tests/cases/fourslash/exportInLabeledStatement.ts b/tests/cases/fourslash/exportInLabeledStatement.ts new file mode 100644 index 00000000000..f9bde8c4922 --- /dev/null +++ b/tests/cases/fourslash/exportInLabeledStatement.ts @@ -0,0 +1,8 @@ +/// + +// @Filename: a.ts +//// subTitle: +//// [|export|] const title: string + +verify.baselineDocumentHighlights(test.ranges()[0], { filesToSearch: [test.ranges()[0].fileName] }); +