From 3e58233b34c67c48c7ab8515805978dde005d12a Mon Sep 17 00:00:00 2001 From: JemmyIndex Date: Mon, 9 Nov 2026 13:05:46 +0800 Subject: [PATCH] fix(checker): avoid crashing on invalid async generator yields --- tsc/internal/checker/checker.go | 4 +- .../asyncGeneratorInvalidThenable.errors.txt | 66 ++++++++++ .../asyncGeneratorInvalidThenable.symbols | 80 ++++++++++++ .../asyncGeneratorInvalidThenable.types | 116 ++++++++++++++++++ .../compiler/asyncGeneratorInvalidThenable.ts | 45 +++++++ 5 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.types create mode 100644 tsc/testdata/tests/cases/compiler/asyncGeneratorInvalidThenable.ts diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index 950b9a8e30fb8..73504aa5215ce 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -20962,7 +20962,9 @@ func (c *Checker) checkAndAggregateYieldOperandTypes(fn *ast.Node, checkMode Che if yieldExpr.Expression() != nil && c.isConstContext(yieldExpr.Expression()) { yieldExprType = c.getRegularTypeOfLiteralType(yieldExprType) } - yieldTypes = core.AppendIfUnique(yieldTypes, c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync)) + if yieldedType := c.getYieldedTypeOfYieldExpression(yieldExpr, yieldExprType, c.anyType, isAsync); yieldedType != nil { + yieldTypes = core.AppendIfUnique(yieldTypes, yieldedType) + } var nextType *Type if yieldExpr.AsYieldExpression().AsteriskToken != nil { iterationTypes := c.getIterationTypesOfIterable(yieldExprType, core.IfElse(isAsync, IterationUseAsyncYieldStar, IterationUseYieldStar), yieldExpr.Expression()) diff --git a/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.errors.txt b/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.errors.txt new file mode 100644 index 0000000000000..c2e1f4fd00e08 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.errors.txt @@ -0,0 +1,66 @@ +asyncGeneratorInvalidThenable.ts(3,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +asyncGeneratorInvalidThenable.ts(9,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +asyncGeneratorInvalidThenable.ts(13,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +asyncGeneratorInvalidThenable.ts(19,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +asyncGeneratorInvalidThenable.ts(20,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +asyncGeneratorInvalidThenable.ts(24,11): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. +asyncGeneratorInvalidThenable.ts(28,33): error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + + +==== asyncGeneratorInvalidThenable.ts (7 errors) ==== + // https://github.com/microsoft/TypeScript/issues/65378 + async function* invalidThenableBeforeValue() { + yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + yield 1; + } + + async function* invalidThenableAfterValue() { + yield 1; + yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + } + + async function* invalidThenableWithMultipleValues() { + yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + yield 1; + yield "text"; + } + + async function* onlyInvalidThenables() { + yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + } + + async function* singleInvalidThenable() { + yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + } + + async function* invalidThenableWithNext() { + const next: boolean = yield { then() {} }; + ~~~~~~~~~~~~~ +!!! error TS1321: Type of 'yield' operand in an async generator must either be a valid promise or must not contain a callable 'then' member. + yield 1; + return next; + } + + async function* validYields() { + yield Promise.resolve(1); + yield "text"; + } + + function* synchronousThenable() { + yield { then() {} }; + yield 1; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.symbols b/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.symbols new file mode 100644 index 0000000000000..8229fd069fe2a --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.symbols @@ -0,0 +1,80 @@ +//// [tests/cases/compiler/asyncGeneratorInvalidThenable.ts] //// + +=== asyncGeneratorInvalidThenable.ts === +// https://github.com/microsoft/TypeScript/issues/65378 +async function* invalidThenableBeforeValue() { +>invalidThenableBeforeValue : Symbol(invalidThenableBeforeValue, Decl(asyncGeneratorInvalidThenable.ts, 0, 0)) + + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 2, 11)) + + yield 1; +} + +async function* invalidThenableAfterValue() { +>invalidThenableAfterValue : Symbol(invalidThenableAfterValue, Decl(asyncGeneratorInvalidThenable.ts, 4, 1)) + + yield 1; + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 8, 11)) +} + +async function* invalidThenableWithMultipleValues() { +>invalidThenableWithMultipleValues : Symbol(invalidThenableWithMultipleValues, Decl(asyncGeneratorInvalidThenable.ts, 9, 1)) + + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 12, 11)) + + yield 1; + yield "text"; +} + +async function* onlyInvalidThenables() { +>onlyInvalidThenables : Symbol(onlyInvalidThenables, Decl(asyncGeneratorInvalidThenable.ts, 15, 1)) + + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 18, 11)) + + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 19, 11)) +} + +async function* singleInvalidThenable() { +>singleInvalidThenable : Symbol(singleInvalidThenable, Decl(asyncGeneratorInvalidThenable.ts, 20, 1)) + + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 23, 11)) +} + +async function* invalidThenableWithNext() { +>invalidThenableWithNext : Symbol(invalidThenableWithNext, Decl(asyncGeneratorInvalidThenable.ts, 24, 1)) + + const next: boolean = yield { then() {} }; +>next : Symbol(next, Decl(asyncGeneratorInvalidThenable.ts, 27, 9)) +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 27, 33)) + + yield 1; + return next; +>next : Symbol(next, Decl(asyncGeneratorInvalidThenable.ts, 27, 9)) +} + +async function* validYields() { +>validYields : Symbol(validYields, Decl(asyncGeneratorInvalidThenable.ts, 30, 1)) + + yield Promise.resolve(1); +>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) +>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --)) + + yield "text"; +} + +function* synchronousThenable() { +>synchronousThenable : Symbol(synchronousThenable, Decl(asyncGeneratorInvalidThenable.ts, 35, 1)) + + yield { then() {} }; +>then : Symbol(then, Decl(asyncGeneratorInvalidThenable.ts, 38, 11)) + + yield 1; +} + diff --git a/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.types b/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.types new file mode 100644 index 0000000000000..23d85abc7a73e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/asyncGeneratorInvalidThenable.types @@ -0,0 +1,116 @@ +//// [tests/cases/compiler/asyncGeneratorInvalidThenable.ts] //// + +=== asyncGeneratorInvalidThenable.ts === +// https://github.com/microsoft/TypeScript/issues/65378 +async function* invalidThenableBeforeValue() { +>invalidThenableBeforeValue : () => AsyncGenerator + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void + + yield 1; +>yield 1 : any +>1 : 1 +} + +async function* invalidThenableAfterValue() { +>invalidThenableAfterValue : () => AsyncGenerator + + yield 1; +>yield 1 : any +>1 : 1 + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void +} + +async function* invalidThenableWithMultipleValues() { +>invalidThenableWithMultipleValues : () => AsyncGenerator<"text" | 1, void, unknown> + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void + + yield 1; +>yield 1 : any +>1 : 1 + + yield "text"; +>yield "text" : any +>"text" : "text" +} + +async function* onlyInvalidThenables() { +>onlyInvalidThenables : () => AsyncGenerator + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void +} + +async function* singleInvalidThenable() { +>singleInvalidThenable : () => AsyncGenerator + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void +} + +async function* invalidThenableWithNext() { +>invalidThenableWithNext : () => AsyncGenerator + + const next: boolean = yield { then() {} }; +>next : boolean +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void + + yield 1; +>yield 1 : any +>1 : 1 + + return next; +>next : boolean +} + +async function* validYields() { +>validYields : () => AsyncGenerator + + yield Promise.resolve(1); +>yield Promise.resolve(1) : any +>Promise.resolve(1) : Promise +>Promise.resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +>Promise : PromiseConstructor +>resolve : { (): Promise; (value: T): Promise>; (value: T | PromiseLike): Promise>; } +>1 : 1 + + yield "text"; +>yield "text" : any +>"text" : "text" +} + +function* synchronousThenable() { +>synchronousThenable : () => Generator<1 | { then(): void; }, void, unknown> + + yield { then() {} }; +>yield { then() {} } : any +>{ then() {} } : { then(): void; } +>then : () => void + + yield 1; +>yield 1 : any +>1 : 1 +} + diff --git a/tsc/testdata/tests/cases/compiler/asyncGeneratorInvalidThenable.ts b/tsc/testdata/tests/cases/compiler/asyncGeneratorInvalidThenable.ts new file mode 100644 index 0000000000000..325818ee2935d --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/asyncGeneratorInvalidThenable.ts @@ -0,0 +1,45 @@ +// @target: es2020 +// @strict: true +// @noEmit: true + +// https://github.com/microsoft/TypeScript/issues/65378 +async function* invalidThenableBeforeValue() { + yield { then() {} }; + yield 1; +} + +async function* invalidThenableAfterValue() { + yield 1; + yield { then() {} }; +} + +async function* invalidThenableWithMultipleValues() { + yield { then() {} }; + yield 1; + yield "text"; +} + +async function* onlyInvalidThenables() { + yield { then() {} }; + yield { then() {} }; +} + +async function* singleInvalidThenable() { + yield { then() {} }; +} + +async function* invalidThenableWithNext() { + const next: boolean = yield { then() {} }; + yield 1; + return next; +} + +async function* validYields() { + yield Promise.resolve(1); + yield "text"; +} + +function* synchronousThenable() { + yield { then() {} }; + yield 1; +}