|
| 1 | +import { |
| 2 | + append, |
| 3 | + declarationNameToString, |
| 4 | + Diagnostics, |
| 5 | + factory, |
| 6 | + findAncestor, |
| 7 | + firstOrUndefined, |
| 8 | + forEach, |
| 9 | + FunctionLikeDeclaration, |
| 10 | + getNameOfAccessExpression, |
| 11 | + getNameOfDeclaration, |
| 12 | + getTokenAtPosition, |
| 13 | + isAccessExpression, |
| 14 | + isCallExpression, |
| 15 | + isFunctionLikeDeclaration, |
| 16 | + isIdentifier, |
| 17 | + isParameter, |
| 18 | + isPropertyDeclaration, |
| 19 | + isVariableDeclaration, |
| 20 | + length, |
| 21 | + Node, |
| 22 | + NodeBuilderFlags, |
| 23 | + SignatureDeclaration, |
| 24 | + SignatureKind, |
| 25 | + SourceFile, |
| 26 | + SyntaxKind, |
| 27 | + textChanges, |
| 28 | + TypeChecker, |
| 29 | + TypeNode, |
| 30 | +} from "../_namespaces/ts"; |
| 31 | +import { |
| 32 | + codeFixAll, |
| 33 | + createCodeFixAction, |
| 34 | + registerCodeFix, |
| 35 | +} from "../_namespaces/ts.codefix"; |
| 36 | + |
| 37 | +const fixId = "addMissingParam"; |
| 38 | +const errorCodes = [Diagnostics.Expected_0_arguments_but_got_1.code]; |
| 39 | + |
| 40 | +registerCodeFix({ |
| 41 | + errorCodes, |
| 42 | + fixIds: [fixId], |
| 43 | + getCodeActions(context) { |
| 44 | + const checker = context.program.getTypeChecker(); |
| 45 | + const info = getInfo(context.sourceFile, checker, context.span.start); |
| 46 | + if (info === undefined) { |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, info)); |
| 50 | + return [createCodeFixAction(fixId, changes, [length(info.parameters) === 1 ? Diagnostics.Add_missing_parameter_to_0 : Diagnostics.Add_missing_parameters_to_0, info.name], fixId, Diagnostics.Add_all_missing_parameters)]; |
| 51 | + }, |
| 52 | + getAllCodeActions: context => |
| 53 | + codeFixAll(context, errorCodes, (changes, diag) => { |
| 54 | + const checker = context.program.getTypeChecker(); |
| 55 | + const info = getInfo(context.sourceFile, checker, diag.start); |
| 56 | + if (info) { |
| 57 | + doChange(changes, context.sourceFile, info); |
| 58 | + } |
| 59 | + }), |
| 60 | +}); |
| 61 | + |
| 62 | +interface Parameter { |
| 63 | + readonly name: string; |
| 64 | + readonly type: TypeNode; |
| 65 | +} |
| 66 | + |
| 67 | +interface Info { |
| 68 | + readonly signature: SignatureDeclaration; |
| 69 | + readonly name: string; |
| 70 | + readonly parameters: Parameter[]; |
| 71 | +} |
| 72 | + |
| 73 | +function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number): Info | undefined { |
| 74 | + const token = getTokenAtPosition(sourceFile, pos); |
| 75 | + const callExpression = findAncestor(token, isCallExpression); |
| 76 | + if (callExpression === undefined || length(callExpression.arguments) === 0) { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + |
| 80 | + const type = checker.getTypeAtLocation(callExpression.expression); |
| 81 | + const signature = firstOrUndefined(checker.getSignaturesOfType(type, SignatureKind.Call)); |
| 82 | + if (signature && signature.declaration && isFunctionLikeDeclaration(signature.declaration)) { |
| 83 | + const name = tryGetName(signature.declaration); |
| 84 | + if (name === undefined) { |
| 85 | + return undefined; |
| 86 | + } |
| 87 | + |
| 88 | + const parameters: Parameter[] = []; |
| 89 | + const parametersLength = length(signature.declaration.parameters); |
| 90 | + for (let i = parametersLength, index = 0; i < length(callExpression.arguments); i++) { |
| 91 | + const arg = callExpression.arguments[i]; |
| 92 | + const expr = isAccessExpression(arg) ? getNameOfAccessExpression(arg) : arg; |
| 93 | + append(parameters, { |
| 94 | + name: expr && isIdentifier(expr) ? expr.text : `param${index++}`, |
| 95 | + type: getArgumentType(checker, expr, signature.declaration), |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + return { |
| 100 | + signature: signature.declaration, |
| 101 | + name: declarationNameToString(name), |
| 102 | + parameters, |
| 103 | + }; |
| 104 | + } |
| 105 | + return undefined; |
| 106 | +} |
| 107 | + |
| 108 | +function tryGetName(node: FunctionLikeDeclaration) { |
| 109 | + const name = getNameOfDeclaration(node); |
| 110 | + if (name) { |
| 111 | + return name; |
| 112 | + } |
| 113 | + |
| 114 | + if ( |
| 115 | + isVariableDeclaration(node.parent) && isIdentifier(node.parent.name) || |
| 116 | + isPropertyDeclaration(node.parent) || |
| 117 | + isParameter(node.parent) |
| 118 | + ) { |
| 119 | + return node.parent.name; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function getArgumentType(checker: TypeChecker, node: Node, enclosingDeclaration: Node) { |
| 124 | + if (node) { |
| 125 | + const typeNode = checker.typeToTypeNode(checker.getWidenedType(checker.getBaseTypeOfLiteralType(checker.getTypeAtLocation(node))), enclosingDeclaration, NodeBuilderFlags.NoTruncation); |
| 126 | + if (typeNode) { |
| 127 | + return typeNode; |
| 128 | + } |
| 129 | + } |
| 130 | + return factory.createKeywordTypeNode(SyntaxKind.UnknownKeyword); |
| 131 | +} |
| 132 | + |
| 133 | +function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, { signature, parameters }: Info) { |
| 134 | + forEach(parameters, ({ name, type }, index) => { |
| 135 | + const param = factory.createParameterDeclaration( |
| 136 | + /*modifiers*/ undefined, |
| 137 | + /*dotDotDotToken*/ undefined, |
| 138 | + name, |
| 139 | + /*questionToken*/ undefined, |
| 140 | + type, |
| 141 | + /*initializer*/ undefined, |
| 142 | + ); |
| 143 | + if (length(signature.parameters) === 0 && index === 0) { |
| 144 | + changes.insertNodeAt(sourceFile, signature.parameters.end, param); |
| 145 | + } |
| 146 | + else { |
| 147 | + changes.insertNodeAtEndOfList(sourceFile, signature.parameters, param); |
| 148 | + } |
| 149 | + }); |
| 150 | +} |
0 commit comments