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();