(changes, sourceFile, position, checker)
| 151378 | SynthBindingNameKind[SynthBindingNameKind["BindingPattern"] = 1] = "BindingPattern"; |
| 151379 | })(SynthBindingNameKind || (SynthBindingNameKind = {})); |
| 151380 | function convertToAsyncFunction(changes, sourceFile, position, checker) { |
| 151381 | // get the function declaration - returns a promise |
| 151382 | var tokenAtPosition = ts.getTokenAtPosition(sourceFile, position); |
| 151383 | var functionToConvert; |
| 151384 | // if the parent of a FunctionLikeDeclaration is a variable declaration, the convertToAsync diagnostic will be reported on the variable name |
| 151385 | if (ts.isIdentifier(tokenAtPosition) && ts.isVariableDeclaration(tokenAtPosition.parent) && |
| 151386 | tokenAtPosition.parent.initializer && ts.isFunctionLikeDeclaration(tokenAtPosition.parent.initializer)) { |
| 151387 | functionToConvert = tokenAtPosition.parent.initializer; |
| 151388 | } |
| 151389 | else { |
| 151390 | functionToConvert = ts.tryCast(ts.getContainingFunction(ts.getTokenAtPosition(sourceFile, position)), ts.canBeConvertedToAsync); |
| 151391 | } |
| 151392 | if (!functionToConvert) { |
| 151393 | return; |
| 151394 | } |
| 151395 | var synthNamesMap = new ts.Map(); |
| 151396 | var isInJavascript = ts.isInJSFile(functionToConvert); |
| 151397 | var setOfExpressionsToReturn = getAllPromiseExpressionsToReturn(functionToConvert, checker); |
| 151398 | var functionToConvertRenamed = renameCollidingVarNames(functionToConvert, checker, synthNamesMap); |
| 151399 | if (!ts.returnsPromise(functionToConvertRenamed, checker)) { |
| 151400 | return; |
| 151401 | } |
| 151402 | var returnStatements = functionToConvertRenamed.body && ts.isBlock(functionToConvertRenamed.body) ? getReturnStatementsWithPromiseHandlers(functionToConvertRenamed.body, checker) : ts.emptyArray; |
| 151403 | var transformer = { checker: checker, synthNamesMap: synthNamesMap, setOfExpressionsToReturn: setOfExpressionsToReturn, isInJSFile: isInJavascript }; |
| 151404 | if (!returnStatements.length) { |
| 151405 | return; |
| 151406 | } |
| 151407 | var pos = functionToConvert.modifiers ? functionToConvert.modifiers.end : |
| 151408 | functionToConvert.decorators ? ts.skipTrivia(sourceFile.text, functionToConvert.decorators.end) : |
| 151409 | functionToConvert.getStart(sourceFile); |
| 151410 | var options = functionToConvert.modifiers ? { prefix: " " } : { suffix: " " }; |
| 151411 | changes.insertModifierAt(sourceFile, pos, 131 /* SyntaxKind.AsyncKeyword */, options); |
| 151412 | var _loop_14 = function (returnStatement) { |
| 151413 | ts.forEachChild(returnStatement, function visit(node) { |
| 151414 | if (ts.isCallExpression(node)) { |
| 151415 | var newNodes = transformExpression(node, node, transformer, /*hasContinuation*/ false); |
| 151416 | if (hasFailed()) { |
| 151417 | return true; // return something truthy to shortcut out of more work |
| 151418 | } |
| 151419 | changes.replaceNodeWithNodes(sourceFile, returnStatement, newNodes); |
| 151420 | } |
| 151421 | else if (!ts.isFunctionLike(node)) { |
| 151422 | ts.forEachChild(node, visit); |
| 151423 | if (hasFailed()) { |
| 151424 | return true; // return something truthy to shortcut out of more work |
| 151425 | } |
| 151426 | } |
| 151427 | }); |
| 151428 | if (hasFailed()) { |
| 151429 | return { value: void 0 }; |
| 151430 | } |
| 151431 | }; |
| 151432 | for (var _i = 0, returnStatements_1 = returnStatements; _i < returnStatements_1.length; _i++) { |
| 151433 | var returnStatement = returnStatements_1[_i]; |
| 151434 | var state_5 = _loop_14(returnStatement); |
| 151435 | if (typeof state_5 === "object") |
| 151436 | return state_5.value; |
| 151437 | } |
no test coverage detected