Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)!;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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 ||
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/potentiallyUnassignedVariableInCatch.js
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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))
}

Original file line number Diff line number Diff line change
@@ -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
}

10 changes: 10 additions & 0 deletions tests/cases/compiler/potentiallyUnassignedVariableInCatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @strict: true

let foo;
try {
if (Math.random() > 0.5) {
foo = 1234;
}
} catch {
foo;
}