From 79b1c230aa340af8889d21ae2266dc050416b821 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 15 Mar 2018 16:44:48 -0700 Subject: [PATCH] Fix await code action crashes when await is not in a function Fixes #22614 --- src/services/codefixes/fixAwaitInSyncFunction.ts | 4 ++++ .../fourslash/codeFixAwaitShouldNotCrashIfNotInFunction.ts | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 tests/cases/fourslash/codeFixAwaitShouldNotCrashIfNotInFunction.ts diff --git a/src/services/codefixes/fixAwaitInSyncFunction.ts b/src/services/codefixes/fixAwaitInSyncFunction.ts index 883993e7b5146..713a25456c054 100644 --- a/src/services/codefixes/fixAwaitInSyncFunction.ts +++ b/src/services/codefixes/fixAwaitInSyncFunction.ts @@ -36,6 +36,10 @@ namespace ts.codefix { function getNodes(sourceFile: SourceFile, start: number): { insertBefore: Node, returnType: TypeNode | undefined } | undefined { const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false); const containingFunction = getContainingFunction(token); + if (!containingFunction) { + return; + } + let insertBefore: Node | undefined; switch (containingFunction.kind) { case SyntaxKind.MethodDeclaration: diff --git a/tests/cases/fourslash/codeFixAwaitShouldNotCrashIfNotInFunction.ts b/tests/cases/fourslash/codeFixAwaitShouldNotCrashIfNotInFunction.ts new file mode 100644 index 0000000000000..1ce5c771ed40b --- /dev/null +++ b/tests/cases/fourslash/codeFixAwaitShouldNotCrashIfNotInFunction.ts @@ -0,0 +1,5 @@ +/// + +////await a + +verify.not.codeFixAvailable();