|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixId = "fixAwaitInSyncFunction"; |
| 4 | + const errorCodes = [ |
| 5 | + Diagnostics.await_expression_is_only_allowed_within_an_async_function.code, |
| 6 | + Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator.code, |
| 7 | + ]; |
| 8 | + registerCodeFix({ |
| 9 | + errorCodes, |
| 10 | + getCodeActions(context) { |
| 11 | + const { sourceFile, span } = context; |
| 12 | + const nodes = getNodes(sourceFile, span.start); |
| 13 | + if (!nodes) return undefined; |
| 14 | + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, nodes)); |
| 15 | + return [{ description: getLocaleSpecificMessage(Diagnostics.Add_async_modifier_to_containing_function), changes, fixId }]; |
| 16 | + }, |
| 17 | + fixIds: [fixId], |
| 18 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { |
| 19 | + const nodes = getNodes(diag.file, diag.start); |
| 20 | + if (!nodes) return; |
| 21 | + doChange(changes, context.sourceFile, nodes); |
| 22 | + }), |
| 23 | + }); |
| 24 | + |
| 25 | + function getReturnType(expr: FunctionDeclaration | MethodDeclaration | FunctionExpression | ArrowFunction) { |
| 26 | + if (expr.type) { |
| 27 | + return expr.type; |
| 28 | + } |
| 29 | + if (isVariableDeclaration(expr.parent) && |
| 30 | + expr.parent.type && |
| 31 | + isFunctionTypeNode(expr.parent.type)) { |
| 32 | + return expr.parent.type.type; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + function getNodes(sourceFile: SourceFile, start: number): { insertBefore: Node, returnType: TypeNode | undefined } | undefined { |
| 37 | + const token = getTokenAtPosition(sourceFile, start, /*includeJsDocComment*/ false); |
| 38 | + const containingFunction = getContainingFunction(token); |
| 39 | + let insertBefore: Node | undefined; |
| 40 | + switch (containingFunction.kind) { |
| 41 | + case SyntaxKind.MethodDeclaration: |
| 42 | + insertBefore = containingFunction.name; |
| 43 | + break; |
| 44 | + case SyntaxKind.FunctionDeclaration: |
| 45 | + case SyntaxKind.FunctionExpression: |
| 46 | + insertBefore = findChildOfKind(containingFunction, SyntaxKind.FunctionKeyword, sourceFile); |
| 47 | + break; |
| 48 | + case SyntaxKind.ArrowFunction: |
| 49 | + insertBefore = findChildOfKind(containingFunction, SyntaxKind.OpenParenToken, sourceFile) || first(containingFunction.parameters); |
| 50 | + break; |
| 51 | + default: |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + return { |
| 56 | + insertBefore, |
| 57 | + returnType: getReturnType(containingFunction) |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + function doChange( |
| 62 | + changes: textChanges.ChangeTracker, |
| 63 | + sourceFile: SourceFile, |
| 64 | + { insertBefore, returnType }: { insertBefore: Node | undefined, returnType: TypeNode | undefined }): void { |
| 65 | + |
| 66 | + if (returnType) { |
| 67 | + const entityName = getEntityNameFromTypeNode(returnType); |
| 68 | + if (!entityName || entityName.kind !== SyntaxKind.Identifier || entityName.text !== "Promise") { |
| 69 | + changes.replaceNode(sourceFile, returnType, createTypeReferenceNode("Promise", createNodeArray([returnType]))); |
| 70 | + } |
| 71 | + } |
| 72 | + changes.insertModifierBefore(sourceFile, SyntaxKind.AsyncKeyword, insertBefore); |
| 73 | + } |
| 74 | +} |
0 commit comments