fix(43160): improve error location for functions without explicit return (#43367)

* fix(43160): improve error location for functions without explicit return

* handle functions returning never
This commit is contained in:
Zen
2021-04-06 22:21:02 +08:00
committed by GitHub
parent 3b06ef1a73
commit 3cf26e44ee
5 changed files with 303 additions and 5 deletions

View File

@@ -30574,18 +30574,18 @@ namespace ts {
}
const hasExplicitReturn = func.flags & NodeFlags.HasExplicitReturn;
const errorNode = getEffectiveReturnTypeNode(func) || func;
if (type && type.flags & TypeFlags.Never) {
error(getEffectiveReturnTypeNode(func), Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point);
error(errorNode, Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point);
}
else if (type && !hasExplicitReturn) {
// minimal check: function has syntactic return type annotation and no explicit return statements in the body
// this function does not conform to the specification.
// NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present
error(getEffectiveReturnTypeNode(func), Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
error(errorNode, Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value);
}
else if (type && strictNullChecks && !isTypeAssignableTo(undefinedType, type)) {
error(getEffectiveReturnTypeNode(func) || func, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
error(errorNode, Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined);
}
else if (compilerOptions.noImplicitReturns) {
if (!type) {
@@ -30600,7 +30600,7 @@ namespace ts {
return;
}
}
error(getEffectiveReturnTypeNode(func) || func, Diagnostics.Not_all_code_paths_return_a_value);
error(errorNode, Diagnostics.Not_all_code_paths_return_a_value);
}
}