From ea51fabb7c97dbf83fc230606f19a67fd375460a Mon Sep 17 00:00:00 2001 From: Ryan Cavanaugh Date: Fri, 11 Sep 2020 17:18:23 -0700 Subject: [PATCH] Don't crash when observing invalid 'export' in object literal (#40295) Fixes #32870 --- src/services/documentHighlights.ts | 7 ++++++- tests/cases/fourslash/exportInObjectLiteral.ts | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/cases/fourslash/exportInObjectLiteral.ts diff --git a/src/services/documentHighlights.ts b/src/services/documentHighlights.ts index 6ed27e2beee..b0282393cce 100644 --- a/src/services/documentHighlights.ts +++ b/src/services/documentHighlights.ts @@ -204,7 +204,7 @@ namespace ts { 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; + const container = declaration.parent as ModuleBlock | SourceFile | Block | CaseClause | DefaultClause | ConstructorDeclaration | MethodDeclaration | FunctionDeclaration | ObjectTypeDeclaration | ObjectLiteralExpression; switch (container.kind) { case SyntaxKind.ModuleBlock: case SyntaxKind.SourceFile: @@ -240,6 +240,11 @@ namespace ts { return [...nodes, container]; } return nodes; + + // Syntactically invalid positions that the parser might produce anyway + case SyntaxKind.ObjectLiteralExpression: + return undefined; + default: Debug.assertNever(container, "Invalid container kind."); } diff --git a/tests/cases/fourslash/exportInObjectLiteral.ts b/tests/cases/fourslash/exportInObjectLiteral.ts new file mode 100644 index 00000000000..9d95ff5ff51 --- /dev/null +++ b/tests/cases/fourslash/exportInObjectLiteral.ts @@ -0,0 +1,9 @@ +/// + +// @Filename: a.ts +//// const k = { +//// [|export|] f() { } +//// } + +verify.documentHighlightsOf(test.ranges()[0], [], { filesToSearch: [test.ranges()[0].fileName] }); +