|
| 1 | +/* @internal */ |
| 2 | +namespace ts.codefix { |
| 3 | + const fixId = "returnValueCorrect"; |
| 4 | + const fixIdAddReturnStatement = "fixAddReturnStatement"; |
| 5 | + const fixIdRemoveBlockBodyBrace = "fixRemoveBlockBodyBrace"; |
| 6 | + const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen"; |
| 7 | + const errorCodes = [ |
| 8 | + Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code, |
| 9 | + Diagnostics.Type_0_is_not_assignable_to_type_1.code, |
| 10 | + Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code |
| 11 | + ]; |
| 12 | + |
| 13 | + enum ProblemKind { |
| 14 | + MissingReturnStatement, |
| 15 | + MissingParentheses |
| 16 | + } |
| 17 | + |
| 18 | + interface MissingReturnInfo { |
| 19 | + kind: ProblemKind.MissingReturnStatement; |
| 20 | + declaration: FunctionLikeDeclaration; |
| 21 | + expression: Expression; |
| 22 | + statement: Statement; |
| 23 | + commentSource: Node; |
| 24 | + } |
| 25 | + |
| 26 | + interface MissingParenInfo { |
| 27 | + kind: ProblemKind.MissingParentheses; |
| 28 | + declaration: ArrowFunction; |
| 29 | + expression: Expression; |
| 30 | + statement: Statement; |
| 31 | + commentSource: Node; |
| 32 | + } |
| 33 | + |
| 34 | + type Info = MissingReturnInfo | MissingParenInfo; |
| 35 | + |
| 36 | + registerCodeFix({ |
| 37 | + errorCodes, |
| 38 | + fixIds: [fixIdAddReturnStatement, fixIdRemoveBlockBodyBrace, fixIdWrapTheBlockWithParen], |
| 39 | + getCodeActions: context => { |
| 40 | + const { program, sourceFile, span: { start }, errorCode } = context; |
| 41 | + const info = getInfo(program.getTypeChecker(), sourceFile, start, errorCode); |
| 42 | + if (!info) return undefined; |
| 43 | + |
| 44 | + if (info.kind === ProblemKind.MissingReturnStatement) { |
| 45 | + return append( |
| 46 | + [getActionForfixAddReturnStatement(context, info.expression, info.statement)], |
| 47 | + isArrowFunction(info.declaration) ? getActionForfixRemoveBlockBodyBrace(context, info.declaration, info.expression, info.commentSource): undefined); |
| 48 | + } |
| 49 | + else { |
| 50 | + return [getActionForfixWrapTheBlockWithParen(context, info.declaration, info.expression)]; |
| 51 | + } |
| 52 | + }, |
| 53 | + getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { |
| 54 | + const info = getInfo(context.program.getTypeChecker(), diag.file, diag.start, diag.code); |
| 55 | + if (!info) return undefined; |
| 56 | + |
| 57 | + switch (context.fixId) { |
| 58 | + case fixIdAddReturnStatement: |
| 59 | + addReturnStatement(changes, diag.file, info.expression, info.statement); |
| 60 | + break; |
| 61 | + case fixIdRemoveBlockBodyBrace: |
| 62 | + if (!isArrowFunction(info.declaration)) return undefined; |
| 63 | + removeBlockBodyBrace(changes, diag.file, info.declaration, info.expression, info.commentSource, /* withParen */ false); |
| 64 | + break; |
| 65 | + case fixIdWrapTheBlockWithParen: |
| 66 | + if (!isArrowFunction(info.declaration)) return undefined; |
| 67 | + wrapBlockWithParen(changes, diag.file, info.declaration, info.expression); |
| 68 | + break; |
| 69 | + default: |
| 70 | + Debug.fail(JSON.stringify(context.fixId)); |
| 71 | + } |
| 72 | + }), |
| 73 | + }); |
| 74 | + |
| 75 | + function getFixInfo(checker: TypeChecker, declaration: FunctionLikeDeclaration, expectType: Type, isFunctionType: boolean): Info | undefined { |
| 76 | + if (!declaration.body || !isBlock(declaration.body) || length(declaration.body.statements) !== 1) return undefined; |
| 77 | + |
| 78 | + const firstStatement = first(declaration.body.statements); |
| 79 | + if (isExpressionStatement(firstStatement) && checkFixedAssignableTo(checker, declaration, firstStatement.expression, expectType, isFunctionType)) { |
| 80 | + return { |
| 81 | + declaration, |
| 82 | + kind: ProblemKind.MissingReturnStatement, |
| 83 | + expression: firstStatement.expression, |
| 84 | + statement: firstStatement, |
| 85 | + commentSource: firstStatement.expression |
| 86 | + }; |
| 87 | + } |
| 88 | + else if (isLabeledStatement(firstStatement) && isExpressionStatement(firstStatement.statement)) { |
| 89 | + const node = createObjectLiteral([createPropertyAssignment(firstStatement.label, firstStatement.statement.expression)]); |
| 90 | + if (checkFixedAssignableTo(checker, declaration, node, expectType, isFunctionType)) { |
| 91 | + return isArrowFunction(declaration) ? { |
| 92 | + declaration, |
| 93 | + kind: ProblemKind.MissingParentheses, |
| 94 | + expression: node, |
| 95 | + statement: firstStatement, |
| 96 | + commentSource: firstStatement.statement.expression |
| 97 | + } : { |
| 98 | + declaration, |
| 99 | + kind: ProblemKind.MissingReturnStatement, |
| 100 | + expression: node, |
| 101 | + statement: firstStatement, |
| 102 | + commentSource: firstStatement.statement.expression |
| 103 | + }; |
| 104 | + } |
| 105 | + } |
| 106 | + else if (isBlock(firstStatement) && length(firstStatement.statements) === 1) { |
| 107 | + const firstBlockStatement = first(firstStatement.statements); |
| 108 | + if (isLabeledStatement(firstBlockStatement) && isExpressionStatement(firstBlockStatement.statement)) { |
| 109 | + const node = createObjectLiteral([createPropertyAssignment(firstBlockStatement.label, firstBlockStatement.statement.expression)]); |
| 110 | + if (checkFixedAssignableTo(checker, declaration, node, expectType, isFunctionType)) { |
| 111 | + return { |
| 112 | + declaration, |
| 113 | + kind: ProblemKind.MissingReturnStatement, |
| 114 | + expression: node, |
| 115 | + statement: firstStatement, |
| 116 | + commentSource: firstBlockStatement |
| 117 | + }; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return undefined; |
| 123 | + } |
| 124 | + |
| 125 | + function checkFixedAssignableTo(checker: TypeChecker, declaration: FunctionLikeDeclaration, expr: Expression, type: Type, isFunctionType: boolean) { |
| 126 | + return checker.isTypeAssignableTo(checker.getTypeAtLocation(isFunctionType ? updateFunctionLikeBody(declaration, createBlock([createReturn(expr)])) : expr), type); |
| 127 | + } |
| 128 | + |
| 129 | + function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number, errorCode: number): Info | undefined { |
| 130 | + const node = getTokenAtPosition(sourceFile, position); |
| 131 | + if (!node.parent) return undefined; |
| 132 | + |
| 133 | + const declaration = findAncestor(node.parent, isFunctionLikeDeclaration); |
| 134 | + switch (errorCode) { |
| 135 | + case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code: |
| 136 | + if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined; |
| 137 | + return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /* isFunctionType */ false); |
| 138 | + case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code: |
| 139 | + if (!declaration || !isCallExpression(declaration.parent) || !declaration.body) return undefined; |
| 140 | + const pos = declaration.parent.arguments.indexOf(<Expression>declaration); |
| 141 | + const type = checker.getContextualTypeForArgumentAtIndex(declaration.parent, pos); |
| 142 | + if (!type) return undefined; |
| 143 | + return getFixInfo(checker, declaration, type, /* isFunctionType */ true); |
| 144 | + case Diagnostics.Type_0_is_not_assignable_to_type_1.code: |
| 145 | + if (!isDeclarationName(node) || !isVariableLike(node.parent) && !isJsxAttribute(node.parent)) return undefined; |
| 146 | + const initializer = getVariableLikeInitializer(node.parent); |
| 147 | + if (!initializer || !isFunctionLikeDeclaration(initializer) || !initializer.body) return undefined; |
| 148 | + return getFixInfo(checker, initializer, checker.getTypeAtLocation(node.parent), /* isFunctionType */ true); |
| 149 | + } |
| 150 | + return undefined; |
| 151 | + } |
| 152 | + |
| 153 | + function getVariableLikeInitializer(declaration: VariableLikeDeclaration): Expression | undefined { |
| 154 | + switch (declaration.kind) { |
| 155 | + case SyntaxKind.VariableDeclaration: |
| 156 | + case SyntaxKind.Parameter: |
| 157 | + case SyntaxKind.BindingElement: |
| 158 | + case SyntaxKind.PropertyDeclaration: |
| 159 | + case SyntaxKind.PropertyAssignment: |
| 160 | + return declaration.initializer; |
| 161 | + case SyntaxKind.JsxAttribute: |
| 162 | + return declaration.initializer && (isJsxExpression(declaration.initializer) ? declaration.initializer.expression : undefined); |
| 163 | + case SyntaxKind.ShorthandPropertyAssignment: |
| 164 | + case SyntaxKind.PropertySignature: |
| 165 | + case SyntaxKind.EnumMember: |
| 166 | + case SyntaxKind.JSDocPropertyTag: |
| 167 | + case SyntaxKind.JSDocParameterTag: |
| 168 | + return undefined; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + function addReturnStatement(changes: textChanges.ChangeTracker, sourceFile: SourceFile, expression: Expression, statement: Statement) { |
| 173 | + suppressLeadingAndTrailingTrivia(expression); |
| 174 | + const probablyNeedSemi = probablyUsesSemicolons(sourceFile); |
| 175 | + changes.replaceNode(sourceFile, statement, createReturn(expression), { |
| 176 | + leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude, |
| 177 | + trailingTriviaOption: textChanges.TrailingTriviaOption.Exclude, |
| 178 | + suffix: probablyNeedSemi ? ";" : undefined |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + function removeBlockBodyBrace(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression, commentSource: Node, withParen: boolean) { |
| 183 | + const newBody = (withParen || needsParentheses(expression)) ? createParen(expression) : expression; |
| 184 | + suppressLeadingAndTrailingTrivia(commentSource); |
| 185 | + copyComments(commentSource, newBody); |
| 186 | + |
| 187 | + changes.replaceNode(sourceFile, declaration.body, newBody); |
| 188 | + } |
| 189 | + |
| 190 | + function wrapBlockWithParen(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression) { |
| 191 | + changes.replaceNode(sourceFile, declaration.body, createParen(expression)); |
| 192 | + } |
| 193 | + |
| 194 | + function getActionForfixAddReturnStatement(context: CodeFixContext, expression: Expression, statement: Statement) { |
| 195 | + const changes = textChanges.ChangeTracker.with(context, t => addReturnStatement(t, context.sourceFile, expression, statement)); |
| 196 | + return createCodeFixAction(fixId, changes, Diagnostics.Add_a_return_statement, fixIdAddReturnStatement, Diagnostics.Add_all_missing_return_statement); |
| 197 | + } |
| 198 | + |
| 199 | + function getActionForfixRemoveBlockBodyBrace(context: CodeFixContext, declaration: ArrowFunction, expression: Expression, commentSource: Node) { |
| 200 | + const changes = textChanges.ChangeTracker.with(context, t => removeBlockBodyBrace(t, context.sourceFile, declaration, expression, commentSource, /* withParen */ false)); |
| 201 | + return createCodeFixAction(fixId, changes, Diagnostics.Remove_block_body_braces, fixIdRemoveBlockBodyBrace, Diagnostics.Remove_all_incorrect_body_block_braces); |
| 202 | + } |
| 203 | + |
| 204 | + function getActionForfixWrapTheBlockWithParen(context: CodeFixContext, declaration: ArrowFunction, expression: Expression) { |
| 205 | + const changes = textChanges.ChangeTracker.with(context, t => wrapBlockWithParen(t, context.sourceFile, declaration, expression)); |
| 206 | + return createCodeFixAction(fixId, changes, Diagnostics.Wrap_the_following_body_with_parentheses_which_should_be_an_object_literal, fixIdWrapTheBlockWithParen, Diagnostics.Wrap_all_object_literal_with_parentheses); |
| 207 | + } |
| 208 | +} |
0 commit comments