(variableType, initializer)
| 161017 | var renameLocation = ts.getRenameLocation(edits, renameFilename, localNameText, /*isDeclaredBeforeUse*/ true); |
| 161018 | return { renameFilename: renameFilename, renameLocation: renameLocation, edits: edits }; |
| 161019 | function transformFunctionInitializerAndType(variableType, initializer) { |
| 161020 | // If no contextual type exists there is nothing to transfer to the function signature |
| 161021 | if (variableType === undefined) |
| 161022 | return { variableType: variableType, initializer: initializer }; |
| 161023 | // Only do this for function expressions and arrow functions that are not generic |
| 161024 | if (!ts.isFunctionExpression(initializer) && !ts.isArrowFunction(initializer) || !!initializer.typeParameters) |
| 161025 | return { variableType: variableType, initializer: initializer }; |
| 161026 | var functionType = checker.getTypeAtLocation(node); |
| 161027 | var functionSignature = ts.singleOrUndefined(checker.getSignaturesOfType(functionType, 0 /* SignatureKind.Call */)); |
| 161028 | // If no function signature, maybe there was an error, do nothing |
| 161029 | if (!functionSignature) |
| 161030 | return { variableType: variableType, initializer: initializer }; |
| 161031 | // If the function signature has generic type parameters we don't attempt to move the parameters |
| 161032 | if (!!functionSignature.getTypeParameters()) |
| 161033 | return { variableType: variableType, initializer: initializer }; |
| 161034 | // We add parameter types if needed |
| 161035 | var parameters = []; |
| 161036 | var hasAny = false; |
| 161037 | for (var _i = 0, _a = initializer.parameters; _i < _a.length; _i++) { |
| 161038 | var p = _a[_i]; |
| 161039 | if (p.type) { |
| 161040 | parameters.push(p); |
| 161041 | } |
| 161042 | else { |
| 161043 | var paramType = checker.getTypeAtLocation(p); |
| 161044 | if (paramType === checker.getAnyType()) |
| 161045 | hasAny = true; |
| 161046 | parameters.push(ts.factory.updateParameterDeclaration(p, p.decorators, p.modifiers, p.dotDotDotToken, p.name, p.questionToken, p.type || checker.typeToTypeNode(paramType, scope, 1 /* NodeBuilderFlags.NoTruncation */), p.initializer)); |
| 161047 | } |
| 161048 | } |
| 161049 | // If a parameter was inferred as any we skip adding function parameters at all. |
| 161050 | // Turning an implicit any (which under common settings is a error) to an explicit |
| 161051 | // is probably actually a worse refactor outcome. |
| 161052 | if (hasAny) |
| 161053 | return { variableType: variableType, initializer: initializer }; |
| 161054 | variableType = undefined; |
| 161055 | if (ts.isArrowFunction(initializer)) { |
| 161056 | initializer = ts.factory.updateArrowFunction(initializer, node.modifiers, initializer.typeParameters, parameters, initializer.type || checker.typeToTypeNode(functionSignature.getReturnType(), scope, 1 /* NodeBuilderFlags.NoTruncation */), initializer.equalsGreaterThanToken, initializer.body); |
| 161057 | } |
| 161058 | else { |
| 161059 | if (functionSignature && !!functionSignature.thisParameter) { |
| 161060 | var firstParameter = ts.firstOrUndefined(parameters); |
| 161061 | // If the function signature has a this parameter and if the first defined parameter is not the this parameter, we must add it |
| 161062 | // Note: If this parameter was already there, it would have been previously updated with the type if not type was present |
| 161063 | if ((!firstParameter || (ts.isIdentifier(firstParameter.name) && firstParameter.name.escapedText !== "this"))) { |
| 161064 | var thisType = checker.getTypeOfSymbolAtLocation(functionSignature.thisParameter, node); |
| 161065 | parameters.splice(0, 0, ts.factory.createParameterDeclaration( |
| 161066 | /* decorators */ undefined, |
| 161067 | /* modifiers */ undefined, |
| 161068 | /* dotDotDotToken */ undefined, "this", |
| 161069 | /* questionToken */ undefined, checker.typeToTypeNode(thisType, scope, 1 /* NodeBuilderFlags.NoTruncation */))); |
| 161070 | } |
| 161071 | } |
| 161072 | initializer = ts.factory.updateFunctionExpression(initializer, node.modifiers, initializer.asteriskToken, initializer.name, initializer.typeParameters, parameters, initializer.type || checker.typeToTypeNode(functionSignature.getReturnType(), scope, 1 /* NodeBuilderFlags.NoTruncation */), initializer.body); |
| 161073 | } |
| 161074 | return { variableType: variableType, initializer: initializer }; |
| 161075 | } |
| 161076 | } |
no test coverage detected