Skip to content

Commit be488fa

Browse files
authored
Revert part of catch clause PR which broke other declarations (#52403)
1 parent 022516e commit be488fa

5 files changed

+74
-3
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27211,7 +27211,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2721127211
isFunctionLike(node) && !getImmediatelyInvokedFunctionExpression(node) ||
2721227212
node.kind === SyntaxKind.ModuleBlock ||
2721327213
node.kind === SyntaxKind.SourceFile ||
27214-
node.kind === SyntaxKind.CatchClause ||
2721527214
node.kind === SyntaxKind.PropertyDeclaration)!;
2721627215
}
2721727216

@@ -27587,7 +27586,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2758727586
const isParameter = getRootDeclaration(declaration).kind === SyntaxKind.Parameter;
2758827587
const declarationContainer = getControlFlowContainer(declaration);
2758927588
let flowContainer = getControlFlowContainer(node);
27590-
const isCatch = flowContainer.kind === SyntaxKind.CatchClause;
2759127589
const isOuterVariable = flowContainer !== declarationContainer;
2759227590
const isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent);
2759327591
const isModuleExports = symbol.flags & SymbolFlags.ModuleExports;
@@ -27602,7 +27600,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2760227600
// We only look for uninitialized variables in strict null checking mode, and only when we can analyze
2760327601
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
2760427602
// declaration container are the same).
27605-
const assumeInitialized = isParameter || isCatch || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isSameScopedBindingElement(node, declaration) ||
27603+
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isSameScopedBindingElement(node, declaration) ||
2760627604
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
2760727605
isInTypeQuery(node) || isInAmbientOrTypeNode(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
2760827606
node.parent.kind === SyntaxKind.NonNullExpression ||
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [potentiallyUnassignedVariableInCatch.ts]
2+
let foo;
3+
try {
4+
if (Math.random() > 0.5) {
5+
foo = 1234;
6+
}
7+
} catch {
8+
foo;
9+
}
10+
11+
12+
//// [potentiallyUnassignedVariableInCatch.js]
13+
"use strict";
14+
var foo;
15+
try {
16+
if (Math.random() > 0.5) {
17+
foo = 1234;
18+
}
19+
}
20+
catch (_a) {
21+
foo;
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts ===
2+
let foo;
3+
>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3))
4+
5+
try {
6+
if (Math.random() > 0.5) {
7+
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
8+
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
9+
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
10+
11+
foo = 1234;
12+
>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3))
13+
}
14+
} catch {
15+
foo;
16+
>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3))
17+
}
18+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts ===
2+
let foo;
3+
>foo : any
4+
5+
try {
6+
if (Math.random() > 0.5) {
7+
>Math.random() > 0.5 : boolean
8+
>Math.random() : number
9+
>Math.random : () => number
10+
>Math : Math
11+
>random : () => number
12+
>0.5 : 0.5
13+
14+
foo = 1234;
15+
>foo = 1234 : 1234
16+
>foo : any
17+
>1234 : 1234
18+
}
19+
} catch {
20+
foo;
21+
>foo : number | undefined
22+
}
23+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @strict: true
2+
3+
let foo;
4+
try {
5+
if (Math.random() > 0.5) {
6+
foo = 1234;
7+
}
8+
} catch {
9+
foo;
10+
}

0 commit comments

Comments
 (0)