Skip to content

Commit 16f2556

Browse files
TypeScript Botsandersn
TypeScript Bot
andauthored
Cherry-pick PR #42779 into release-4.2 (#42820)
Component commits: 214ef0c No did-you-mean-to-call error on casts I chose to do the ad-hoc check rather than yet another tree walk. 1. It's faster to run and easier to read. 2. This error came from looking at real code. It happened twice, so I think the best estimate for other uses that happened zero times is in fact zero. 3. I couldn't think of other places to put the cast, given the restrictions on `testedNode` just before the new code. 1d34778 Merge branch 'master' into no-did-you-mean-to-call-error-on-casts b3be79a Skip parentheses 2a28812 Merge branch 'master' into no-did-you-mean-to-call-error-on-casts Co-authored-by: Nathan Shively-Sanders <nathansa@microsoft.com>
1 parent 7f64b3a commit 16f2556

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34543,9 +34543,7 @@ namespace ts {
3454334543
: isBinaryExpression(location) && isIdentifier(location.right) ? location.right
3454434544
: undefined;
3454534545
const isPropertyExpressionCast = isPropertyAccessExpression(location)
34546-
&& isParenthesizedExpression(location.expression)
34547-
&& isAssertionExpression(location.expression.expression);
34548-
34546+
&& isAssertionExpression(skipParentheses(location.expression));
3454934547
if (!testedNode || isPropertyExpressionCast) {
3455034548
return;
3455134549
}

tests/baselines/reference/truthinessCallExpressionCoercion3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ function f(result: unknown) {
99
return result
1010
}
1111
}
12+
function g(result: unknown) {
13+
if (((result as I)).always) {
14+
return result
15+
}
16+
}
1217

1318

1419
//// [truthinessCallExpressionCoercion3.js]
@@ -17,3 +22,8 @@ function f(result) {
1722
return result;
1823
}
1924
}
25+
function g(result) {
26+
if (result.always) {
27+
return result;
28+
}
29+
}

tests/baselines/reference/truthinessCallExpressionCoercion3.symbols

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@ function f(result: unknown) {
2121
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 5, 11))
2222
}
2323
}
24+
function g(result: unknown) {
25+
>g : Symbol(g, Decl(truthinessCallExpressionCoercion3.ts, 9, 1))
26+
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 10, 11))
27+
28+
if (((result as I)).always) {
29+
>((result as I)).always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
30+
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 10, 11))
31+
>I : Symbol(I, Decl(truthinessCallExpressionCoercion3.ts, 0, 0))
32+
>always : Symbol(I.always, Decl(truthinessCallExpressionCoercion3.ts, 1, 13))
33+
34+
return result
35+
>result : Symbol(result, Decl(truthinessCallExpressionCoercion3.ts, 10, 11))
36+
}
37+
}
2438

tests/baselines/reference/truthinessCallExpressionCoercion3.types

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,20 @@ function f(result: unknown) {
2020
>result : unknown
2121
}
2222
}
23+
function g(result: unknown) {
24+
>g : (result: unknown) => unknown
25+
>result : unknown
26+
27+
if (((result as I)).always) {
28+
>((result as I)).always : () => void
29+
>((result as I)) : I
30+
>(result as I) : I
31+
>result as I : I
32+
>result : unknown
33+
>always : () => void
34+
35+
return result
36+
>result : unknown
37+
}
38+
}
2339

tests/cases/compiler/truthinessCallExpressionCoercion3.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ function f(result: unknown) {
1111
return result
1212
}
1313
}
14+
function g(result: unknown) {
15+
if (((result as I)).always) {
16+
return result
17+
}
18+
}

0 commit comments

Comments
 (0)