Skip to content

Commit f1d4a81

Browse files
committed
remove uncalled function checks with negations
1 parent 745a34c commit f1d4a81

10 files changed

+18
-188
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34538,6 +34538,7 @@ namespace ts {
3453834538

3453934539
function checkTestingKnownTruthyCallableOrAwaitableType(condExpr: Expression, type: Type, body?: Statement | Expression) {
3454034540
if (!strictNullChecks) return;
34541+
if (getFalsyFlags(type)) return;
3454134542

3454234543
if (getAwaitedTypeOfPromise(type)) {
3454334544
errorAndMaybeSuggestAwait(
@@ -34548,9 +34549,7 @@ namespace ts {
3454834549
return;
3454934550
}
3455034551

34551-
const location = isBinaryExpression(condExpr) ? condExpr.right
34552-
: isPrefixUnaryExpression(condExpr) ? condExpr.operand
34553-
: condExpr;
34552+
const location = isBinaryExpression(condExpr) ? condExpr.right : condExpr;
3455434553
if (isBinaryExpression(condExpr) && condExpr.operatorToken.kind === SyntaxKind.BarBarToken) {
3455534554
checkTestingKnownTruthyCallableOrAwaitableType(condExpr.left, type, body);
3455634555
}
@@ -34564,17 +34563,12 @@ namespace ts {
3456434563
return;
3456534564
}
3456634565

34567-
const testedType = isPrefixUnaryExpression(condExpr) ? checkExpression(location) : type;
34568-
if (typeToString(testedType).includes("undefined")) { // maybeTypeOfKind(testedType, TypeFlags.Undefined)
34569-
return;
34570-
}
34571-
3457234566
// While it technically should be invalid for any known-truthy value
3457334567
// to be tested, we de-scope to functions unrefenced in the block as a
3457434568
// heuristic to identify the most common bugs. There are too many
3457534569
// false positives for values sourced from type definitions without
3457634570
// strictNullChecks otherwise.
34577-
const callSignatures = getSignaturesOfType(testedType, SignatureKind.Call);
34571+
const callSignatures = getSignaturesOfType(type, SignatureKind.Call);
3457834572
if (callSignatures.length === 0) {
3457934573
return;
3458034574
}
@@ -34587,12 +34581,7 @@ namespace ts {
3458734581
const isUsed = isBinaryExpression(condExpr.parent) && isFunctionUsedInBinaryExpressionChain(condExpr.parent, testedSymbol)
3458834582
|| body && isFunctionUsedInConditionBody(condExpr, body, testedNode, testedSymbol);
3458934583
if (!isUsed) {
34590-
if (getFalsyFlags(type)) {
34591-
error(location, Diagnostics.This_condition_will_always_return_false_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
34592-
}
34593-
else {
34594-
error(location, Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
34595-
}
34584+
error(location, Diagnostics.This_condition_will_always_return_true_since_the_function_is_always_defined_Did_you_mean_to_call_it_instead);
3459634585
}
3459734586
}
3459834587

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,10 +3260,6 @@
32603260
"category": "Error",
32613261
"code": 2801
32623262
},
3263-
"This condition will always return false since the function is always defined. Did you mean to call it instead?": {
3264-
"category": "Error",
3265-
"code": 2802
3266-
},
32673263

32683264
"Import declaration '{0}' is using private name '{1}'.": {
32693265
"category": "Error",

src/compiler/moduleSpecifiers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ namespace ts.moduleSpecifiers {
463463
}
464464

465465
function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCanonicalFileName, sourceDirectory }: Info, host: ModuleSpecifierResolutionHost, options: CompilerOptions, packageNameOnly?: boolean): string | undefined {
466-
if (!host.readFile) {
466+
if (!host.fileExists || !host.readFile) {
467467
return undefined;
468468
}
469469
const parts: NodeModulePathParts = getNodeModulePathParts(path)!;
@@ -563,6 +563,7 @@ namespace ts.moduleSpecifiers {
563563
}
564564

565565
function tryGetAnyFileFromPath(host: ModuleSpecifierResolutionHost, path: string) {
566+
if (!host.fileExists) return;
566567
// We check all js, `node` and `json` extensions in addition to TS, since node module resolution would also choose those over the directory
567568
const extensions = getSupportedExtensions({ allowJs: true }, [{ extension: "node", isMixedContent: false }, { extension: "json", isMixedContent: false, scriptKind: ScriptKind.JSON }]);
568569
for (const e of extensions) {

src/compiler/sys.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ namespace ts {
7878

7979
/* @internal */
8080
export function setCustomPollingValues(system: System) {
81+
if (!system.getEnvironmentVariable) {
82+
return;
83+
}
8184
const pollingIntervalChanged = setCustomLevels("TSC_WATCH_POLLINGINTERVAL", PollingInterval);
8285
pollingChunkSize = getCustomPollingBasedLevels("TSC_WATCH_POLLINGCHUNKSIZE", defaultChunkLevels) || pollingChunkSize;
8386
unchangedPollThresholds = getCustomPollingBasedLevels("TSC_WATCH_UNCHANGEDPOLLTHRESHOLDS", defaultChunkLevels) || unchangedPollThresholds;

tests/baselines/reference/truthinessCallExpressionCoercion2.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ tests/cases/compiler/truthinessCallExpressionCoercion2.ts(14,10): error TS2774:
33
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(41,18): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
44
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(44,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
55
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(48,11): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
6+
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(65,46): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
67
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(76,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
78
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(79,10): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
89
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(99,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
910
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(109,9): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
1011
tests/cases/compiler/truthinessCallExpressionCoercion2.ts(112,14): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
1112

1213

13-
==== tests/cases/compiler/truthinessCallExpressionCoercion2.ts (10 errors) ====
14+
==== tests/cases/compiler/truthinessCallExpressionCoercion2.ts (11 errors) ====
1415
declare class A {
1516
static from(): string;
1617
}
@@ -86,6 +87,8 @@ tests/cases/compiler/truthinessCallExpressionCoercion2.ts(112,14): error TS2774:
8687
// error
8788
typeof window !== 'undefined' && window.console &&
8889
((window.console as any).firebug || (window.console.exception && window.console.table));
90+
~~~~~~~~~~~~~~~~~~~~~~~~
91+
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
8992
}
9093

9194
function checksPropertyAccess() {
Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
1-
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(5,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
2-
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(13,6): error TS2802: This condition will always return false since the function is always defined. Did you mean to call it instead?
3-
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(21,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
4-
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(21,14): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
5-
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(25,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
6-
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(33,6): error TS2802: This condition will always return false since the function is always defined. Did you mean to call it instead?
1+
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(4,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
2+
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(8,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
3+
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(8,14): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
4+
tests/cases/compiler/uncalledFunctionChecksInConditional.ts(12,5): error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
75

86

9-
==== tests/cases/compiler/uncalledFunctionChecksInConditional.ts (6 errors) ====
7+
==== tests/cases/compiler/uncalledFunctionChecksInConditional.ts (4 errors) ====
108
declare function isFoo(): boolean;
119
declare function isBar(): boolean;
12-
declare const isUndefinedFoo: () => boolean | undefined;
1310

1411
if (isFoo) {
1512
~~~~~
1613
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
1714
// error
1815
}
1916

20-
if (isUndefinedFoo) {
21-
// no error
22-
}
23-
24-
if (!isFoo) {
25-
~~~~~
26-
!!! error TS2802: This condition will always return false since the function is always defined. Did you mean to call it instead?
27-
// error
28-
}
29-
30-
if (!isUndefinedFoo) {
31-
// no error
32-
}
33-
3417
if (isFoo || isBar) {
3518
~~~~~
3619
!!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead?
@@ -48,14 +31,4 @@ tests/cases/compiler/uncalledFunctionChecksInConditional.ts(33,6): error TS2802:
4831
if (isFoo && isFoo()) {
4932
// no error
5033
}
51-
52-
if (!isFoo || isFoo()) {
53-
~~~~~
54-
!!! error TS2802: This condition will always return false since the function is always defined. Did you mean to call it instead?
55-
// error
56-
}
57-
58-
if (!isUndefinedFoo || isFoo()) {
59-
// no error
60-
}
6134

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
//// [uncalledFunctionChecksInConditional.ts]
22
declare function isFoo(): boolean;
33
declare function isBar(): boolean;
4-
declare const isUndefinedFoo: () => boolean | undefined;
54

65
if (isFoo) {
76
// error
87
}
98

10-
if (isUndefinedFoo) {
11-
// no error
12-
}
13-
14-
if (!isFoo) {
15-
// error
16-
}
17-
18-
if (!isUndefinedFoo) {
19-
// no error
20-
}
21-
229
if (isFoo || isBar) {
2310
// error
2411
}
@@ -30,29 +17,12 @@ if (isFoo || isFoo()) {
3017
if (isFoo && isFoo()) {
3118
// no error
3219
}
33-
34-
if (!isFoo || isFoo()) {
35-
// error
36-
}
37-
38-
if (!isUndefinedFoo || isFoo()) {
39-
// no error
40-
}
4120

4221

4322
//// [uncalledFunctionChecksInConditional.js]
4423
if (isFoo) {
4524
// error
4625
}
47-
if (isUndefinedFoo) {
48-
// no error
49-
}
50-
if (!isFoo) {
51-
// error
52-
}
53-
if (!isUndefinedFoo) {
54-
// no error
55-
}
5626
if (isFoo || isBar) {
5727
// error
5828
}
@@ -62,9 +32,3 @@ if (isFoo || isFoo()) {
6232
if (isFoo && isFoo()) {
6333
// no error
6434
}
65-
if (!isFoo || isFoo()) {
66-
// error
67-
}
68-
if (!isUndefinedFoo || isFoo()) {
69-
// no error
70-
}

tests/baselines/reference/uncalledFunctionChecksInConditional.symbols

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,12 @@ declare function isFoo(): boolean;
55
declare function isBar(): boolean;
66
>isBar : Symbol(isBar, Decl(uncalledFunctionChecksInConditional.ts, 0, 34))
77

8-
declare const isUndefinedFoo: () => boolean | undefined;
9-
>isUndefinedFoo : Symbol(isUndefinedFoo, Decl(uncalledFunctionChecksInConditional.ts, 2, 13))
10-
118
if (isFoo) {
129
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksInConditional.ts, 0, 0))
1310

1411
// error
1512
}
1613

17-
if (isUndefinedFoo) {
18-
>isUndefinedFoo : Symbol(isUndefinedFoo, Decl(uncalledFunctionChecksInConditional.ts, 2, 13))
19-
20-
// no error
21-
}
22-
23-
if (!isFoo) {
24-
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksInConditional.ts, 0, 0))
25-
26-
// error
27-
}
28-
29-
if (!isUndefinedFoo) {
30-
>isUndefinedFoo : Symbol(isUndefinedFoo, Decl(uncalledFunctionChecksInConditional.ts, 2, 13))
31-
32-
// no error
33-
}
34-
3514
if (isFoo || isBar) {
3615
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksInConditional.ts, 0, 0))
3716
>isBar : Symbol(isBar, Decl(uncalledFunctionChecksInConditional.ts, 0, 34))
@@ -53,17 +32,3 @@ if (isFoo && isFoo()) {
5332
// no error
5433
}
5534

56-
if (!isFoo || isFoo()) {
57-
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksInConditional.ts, 0, 0))
58-
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksInConditional.ts, 0, 0))
59-
60-
// error
61-
}
62-
63-
if (!isUndefinedFoo || isFoo()) {
64-
>isUndefinedFoo : Symbol(isUndefinedFoo, Decl(uncalledFunctionChecksInConditional.ts, 2, 13))
65-
>isFoo : Symbol(isFoo, Decl(uncalledFunctionChecksInConditional.ts, 0, 0))
66-
67-
// no error
68-
}
69-

tests/baselines/reference/uncalledFunctionChecksInConditional.types

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,12 @@ declare function isFoo(): boolean;
55
declare function isBar(): boolean;
66
>isBar : () => boolean
77

8-
declare const isUndefinedFoo: () => boolean | undefined;
9-
>isUndefinedFoo : () => boolean | undefined
10-
118
if (isFoo) {
129
>isFoo : () => boolean
1310

1411
// error
1512
}
1613

17-
if (isUndefinedFoo) {
18-
>isUndefinedFoo : () => boolean | undefined
19-
20-
// no error
21-
}
22-
23-
if (!isFoo) {
24-
>!isFoo : false
25-
>isFoo : () => boolean
26-
27-
// error
28-
}
29-
30-
if (!isUndefinedFoo) {
31-
>!isUndefinedFoo : false
32-
>isUndefinedFoo : () => boolean | undefined
33-
34-
// no error
35-
}
36-
3714
if (isFoo || isBar) {
3815
>isFoo || isBar : () => boolean
3916
>isFoo : () => boolean
@@ -60,23 +37,3 @@ if (isFoo && isFoo()) {
6037
// no error
6138
}
6239

63-
if (!isFoo || isFoo()) {
64-
>!isFoo || isFoo() : boolean
65-
>!isFoo : false
66-
>isFoo : () => boolean
67-
>isFoo() : boolean
68-
>isFoo : () => boolean
69-
70-
// error
71-
}
72-
73-
if (!isUndefinedFoo || isFoo()) {
74-
>!isUndefinedFoo || isFoo() : boolean
75-
>!isUndefinedFoo : false
76-
>isUndefinedFoo : () => boolean | undefined
77-
>isFoo() : boolean
78-
>isFoo : () => boolean
79-
80-
// no error
81-
}
82-

tests/cases/compiler/uncalledFunctionChecksInConditional.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,11 @@
22

33
declare function isFoo(): boolean;
44
declare function isBar(): boolean;
5-
declare const isUndefinedFoo: () => boolean | undefined;
65

76
if (isFoo) {
87
// error
98
}
109

11-
if (isUndefinedFoo) {
12-
// no error
13-
}
14-
15-
if (!isFoo) {
16-
// error
17-
}
18-
19-
if (!isUndefinedFoo) {
20-
// no error
21-
}
22-
2310
if (isFoo || isBar) {
2411
// error
2512
}
@@ -31,11 +18,3 @@ if (isFoo || isFoo()) {
3118
if (isFoo && isFoo()) {
3219
// no error
3320
}
34-
35-
if (!isFoo || isFoo()) {
36-
// error
37-
}
38-
39-
if (!isUndefinedFoo || isFoo()) {
40-
// no error
41-
}

0 commit comments

Comments
 (0)