Skip to content

Commit b32c5ae

Browse files
authored
Port "Narrow types by typeof operands with extra parenthesis" (#1045)
1 parent e280a8c commit b32c5ae

File tree

3 files changed

+7
-23
lines changed

3 files changed

+7
-23
lines changed

internal/binder/binder.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,9 +2686,11 @@ func isNarrowingBinaryExpression(expr *ast.BinaryExpression) bool {
26862686
case ast.KindEqualsToken, ast.KindBarBarEqualsToken, ast.KindAmpersandAmpersandEqualsToken, ast.KindQuestionQuestionEqualsToken:
26872687
return containsNarrowableReference(expr.Left)
26882688
case ast.KindEqualsEqualsToken, ast.KindExclamationEqualsToken, ast.KindEqualsEqualsEqualsToken, ast.KindExclamationEqualsEqualsToken:
2689-
return isNarrowableOperand(expr.Left) || isNarrowableOperand(expr.Right) ||
2690-
isNarrowingTypeOfOperands(expr.Right, expr.Left) || isNarrowingTypeOfOperands(expr.Left, expr.Right) ||
2691-
(ast.IsBooleanLiteral(expr.Right) && isNarrowingExpression(expr.Left) || ast.IsBooleanLiteral(expr.Left) && isNarrowingExpression(expr.Right))
2689+
left := ast.SkipParentheses(expr.Left)
2690+
right := ast.SkipParentheses(expr.Right)
2691+
return isNarrowableOperand(left) || isNarrowableOperand(right) ||
2692+
isNarrowingTypeOfOperands(right, left) || isNarrowingTypeOfOperands(left, right) ||
2693+
(ast.IsBooleanLiteral(right) && isNarrowingExpression(left) || ast.IsBooleanLiteral(left) && isNarrowingExpression(right))
26922694
case ast.KindInstanceOfKeyword:
26932695
return isNarrowableOperand(expr.Left)
26942696
case ast.KindInKeyword:

testdata/baselines/reference/submodule/compiler/narrowingTypeofParenthesized1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if ((typeof foo) === "string") {
1818

1919
} else {
2020
foo;
21-
>foo : string
21+
>foo : never
2222
}
2323

2424
if (typeof foo === ("string")) {
@@ -33,6 +33,6 @@ if (typeof foo === ("string")) {
3333

3434
} else {
3535
foo;
36-
>foo : string
36+
>foo : never
3737
}
3838

testdata/baselines/reference/submodule/compiler/narrowingTypeofParenthesized1.types.diff

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)