Revert part of catch clause PR which broke other declarations (#52403)

This commit is contained in:
Jake Bailey 2023-01-25 10:24:25 -08:00 committed by GitHub
parent 022516e24d
commit be488fa075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 3 deletions

View File

@ -27211,7 +27211,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
isFunctionLike(node) && !getImmediatelyInvokedFunctionExpression(node) ||
node.kind === SyntaxKind.ModuleBlock ||
node.kind === SyntaxKind.SourceFile ||
node.kind === SyntaxKind.CatchClause ||
node.kind === SyntaxKind.PropertyDeclaration)!;
}
@ -27587,7 +27586,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const isParameter = getRootDeclaration(declaration).kind === SyntaxKind.Parameter;
const declarationContainer = getControlFlowContainer(declaration);
let flowContainer = getControlFlowContainer(node);
const isCatch = flowContainer.kind === SyntaxKind.CatchClause;
const isOuterVariable = flowContainer !== declarationContainer;
const isSpreadDestructuringAssignmentTarget = node.parent && node.parent.parent && isSpreadAssignment(node.parent) && isDestructuringAssignmentTarget(node.parent.parent);
const isModuleExports = symbol.flags & SymbolFlags.ModuleExports;
@ -27602,7 +27600,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// We only look for uninitialized variables in strict null checking mode, and only when we can analyze
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
// declaration container are the same).
const assumeInitialized = isParameter || isCatch || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isSameScopedBindingElement(node, declaration) ||
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isSameScopedBindingElement(node, declaration) ||
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
isInTypeQuery(node) || isInAmbientOrTypeNode(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
node.parent.kind === SyntaxKind.NonNullExpression ||

View File

@ -0,0 +1,22 @@
//// [potentiallyUnassignedVariableInCatch.ts]
let foo;
try {
if (Math.random() > 0.5) {
foo = 1234;
}
} catch {
foo;
}
//// [potentiallyUnassignedVariableInCatch.js]
"use strict";
var foo;
try {
if (Math.random() > 0.5) {
foo = 1234;
}
}
catch (_a) {
foo;
}

View File

@ -0,0 +1,18 @@
=== tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts ===
let foo;
>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3))
try {
if (Math.random() > 0.5) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
foo = 1234;
>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3))
}
} catch {
foo;
>foo : Symbol(foo, Decl(potentiallyUnassignedVariableInCatch.ts, 0, 3))
}

View File

@ -0,0 +1,23 @@
=== tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts ===
let foo;
>foo : any
try {
if (Math.random() > 0.5) {
>Math.random() > 0.5 : boolean
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>0.5 : 0.5
foo = 1234;
>foo = 1234 : 1234
>foo : any
>1234 : 1234
}
} catch {
foo;
>foo : number | undefined
}

View File

@ -0,0 +1,10 @@
// @strict: true
let foo;
try {
if (Math.random() > 0.5) {
foo = 1234;
}
} catch {
foo;
}