mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 21:36:50 -05:00
feat(36048): handle uncalled function checks in ternaries (#36402)
This commit is contained in:
@@ -27929,7 +27929,8 @@ namespace ts {
|
||||
}
|
||||
|
||||
function checkConditionalExpression(node: ConditionalExpression, checkMode?: CheckMode): Type {
|
||||
checkTruthinessExpression(node.condition);
|
||||
const type = checkTruthinessExpression(node.condition);
|
||||
checkTestingKnownTruthyCallableType(node.condition, node.whenTrue, type);
|
||||
const type1 = checkExpression(node.whenTrue, checkMode);
|
||||
const type2 = checkExpression(node.whenFalse, checkMode);
|
||||
return getUnionType([type1, type2], UnionReduction.Subtype);
|
||||
@@ -31051,9 +31052,8 @@ namespace ts {
|
||||
function checkIfStatement(node: IfStatement) {
|
||||
// Grammar checking
|
||||
checkGrammarStatementInAmbientContext(node);
|
||||
|
||||
const type = checkTruthinessExpression(node.expression);
|
||||
checkTestingKnownTruthyCallableType(node, type);
|
||||
checkTestingKnownTruthyCallableType(node.expression, node.thenStatement, type);
|
||||
checkSourceElement(node.thenStatement);
|
||||
|
||||
if (node.thenStatement.kind === SyntaxKind.EmptyStatement) {
|
||||
@@ -31063,15 +31063,15 @@ namespace ts {
|
||||
checkSourceElement(node.elseStatement);
|
||||
}
|
||||
|
||||
function checkTestingKnownTruthyCallableType(ifStatement: IfStatement, type: Type) {
|
||||
function checkTestingKnownTruthyCallableType(condExpr: Expression, body: Statement | Expression, type: Type) {
|
||||
if (!strictNullChecks) {
|
||||
return;
|
||||
}
|
||||
|
||||
const testedNode = isIdentifier(ifStatement.expression)
|
||||
? ifStatement.expression
|
||||
: isPropertyAccessExpression(ifStatement.expression)
|
||||
? ifStatement.expression.name
|
||||
const testedNode = isIdentifier(condExpr)
|
||||
? condExpr
|
||||
: isPropertyAccessExpression(condExpr)
|
||||
? condExpr.name
|
||||
: undefined;
|
||||
|
||||
if (!testedNode) {
|
||||
@@ -31098,7 +31098,7 @@ namespace ts {
|
||||
return;
|
||||
}
|
||||
|
||||
const functionIsUsedInBody = forEachChild(ifStatement.thenStatement, function check(childNode): boolean | undefined {
|
||||
const functionIsUsedInBody = forEachChild(body, function check(childNode): boolean | undefined {
|
||||
if (isIdentifier(childNode)) {
|
||||
const childSymbol = getSymbolAtLocation(childNode);
|
||||
if (childSymbol && childSymbol.id === testedFunctionSymbol.id) {
|
||||
@@ -31110,7 +31110,7 @@ namespace ts {
|
||||
});
|
||||
|
||||
if (!functionIsUsedInBody) {
|
||||
error(ifStatement.expression, Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
|
||||
error(condExpr, Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user