mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 01:33:15 -05:00
Merge pull request #24474 from Microsoft/fix21115.2
Do not await iterated value in for-await-of
This commit is contained in:
@@ -416,7 +416,7 @@ namespace ts {
|
||||
createLogicalNot(getDone)
|
||||
),
|
||||
/*incrementor*/ undefined,
|
||||
/*statement*/ convertForOfStatementHead(node, createDownlevelAwait(getValue))
|
||||
/*statement*/ convertForOfStatementHead(node, getValue)
|
||||
),
|
||||
/*location*/ node
|
||||
),
|
||||
|
||||
148
src/harness/unittests/forAwaitOfEvaluation.ts
Normal file
148
src/harness/unittests/forAwaitOfEvaluation.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
/// <reference path="..\harness.ts" />
|
||||
|
||||
namespace ts {
|
||||
declare var Symbol: SymbolConstructor;
|
||||
|
||||
describe("forAwaitOfEvaluation", () => {
|
||||
const sourceFile = vpath.combine(vfs.srcFolder, "source.ts");
|
||||
|
||||
function compile(sourceText: string, options?: CompilerOptions) {
|
||||
const fs = vfs.createFromFileSystem(Harness.IO, /*ignoreCase*/ false);
|
||||
fs.writeFileSync(sourceFile, sourceText);
|
||||
const compilerOptions: CompilerOptions = { target: ScriptTarget.ES5, module: ModuleKind.CommonJS, lib: ["lib.esnext.d.ts"], ...options };
|
||||
const host = new fakes.CompilerHost(fs, compilerOptions);
|
||||
return compiler.compileFiles(host, [sourceFile], compilerOptions);
|
||||
}
|
||||
|
||||
function noRequire(id: string) {
|
||||
throw new Error(`Module '${id}' could not be found.`);
|
||||
}
|
||||
|
||||
// Define a custom "Symbol" constructor to attach missing built-in symbols without
|
||||
// modifying the global "Symbol" constructor
|
||||
// tslint:disable-next-line:variable-name
|
||||
const FakeSymbol: SymbolConstructor = ((description?: string) => Symbol(description)) as any;
|
||||
(<any>FakeSymbol).prototype = Symbol.prototype;
|
||||
for (const key of Object.getOwnPropertyNames(Symbol)) {
|
||||
Object.defineProperty(FakeSymbol, key, Object.getOwnPropertyDescriptor(Symbol, key)!);
|
||||
}
|
||||
|
||||
// Add "asyncIterator" if missing
|
||||
if (!hasProperty(FakeSymbol, "asyncIterator")) Object.defineProperty(FakeSymbol, "asyncIterator", { value: Symbol.for("Symbol.asyncIterator"), configurable: true });
|
||||
|
||||
function evaluate(result: compiler.CompilationResult) {
|
||||
const output = result.getOutput(sourceFile, "js")!;
|
||||
assert.isDefined(output);
|
||||
|
||||
const evaluateText = `(function (module, exports, require, __dirname, __filename, Symbol) { ${output.text} })`;
|
||||
const evaluateThunk = eval(evaluateText) as (module: any, exports: any, require: (id: string) => any, dirname: string, filename: string, symbolConstructor: SymbolConstructor) => void;
|
||||
const module: { exports: any; } = { exports: {} };
|
||||
evaluateThunk(module, module.exports, noRequire, vpath.dirname(output.file), output.file, FakeSymbol);
|
||||
return module;
|
||||
}
|
||||
|
||||
it("sync (es5)", async () => {
|
||||
const module = evaluate(compile(`
|
||||
let i = 0;
|
||||
const iterator = {
|
||||
[Symbol.iterator]() { return this; },
|
||||
next() {
|
||||
switch (i++) {
|
||||
case 0: return { value: 1, done: false };
|
||||
case 1: return { value: Promise.resolve(2), done: false };
|
||||
case 2: return { value: new Promise<number>(resolve => setTimeout(resolve, 100, 3)), done: false };
|
||||
default: return { value: undefined: done: true };
|
||||
}
|
||||
}
|
||||
};
|
||||
export const output: any[] = [];
|
||||
export async function main() {
|
||||
for await (const item of iterator) {
|
||||
output.push(item);
|
||||
}
|
||||
}`));
|
||||
await module.exports.main();
|
||||
assert.strictEqual(module.exports.output[0], 1);
|
||||
assert.strictEqual(module.exports.output[1], 2);
|
||||
assert.strictEqual(module.exports.output[2], 3);
|
||||
});
|
||||
|
||||
it("sync (es2015)", async () => {
|
||||
const module = evaluate(compile(`
|
||||
let i = 0;
|
||||
const iterator = {
|
||||
[Symbol.iterator]() { return this; },
|
||||
next() {
|
||||
switch (i++) {
|
||||
case 0: return { value: 1, done: false };
|
||||
case 1: return { value: Promise.resolve(2), done: false };
|
||||
case 2: return { value: new Promise<number>(resolve => setTimeout(resolve, 100, 3)), done: false };
|
||||
default: return { value: undefined: done: true };
|
||||
}
|
||||
}
|
||||
};
|
||||
export const output: any[] = [];
|
||||
export async function main() {
|
||||
for await (const item of iterator) {
|
||||
output.push(item);
|
||||
}
|
||||
}`, { target: ScriptTarget.ES2015 }));
|
||||
await module.exports.main();
|
||||
assert.strictEqual(module.exports.output[0], 1);
|
||||
assert.strictEqual(module.exports.output[1], 2);
|
||||
assert.strictEqual(module.exports.output[2], 3);
|
||||
});
|
||||
|
||||
it("async (es5)", async () => {
|
||||
const module = evaluate(compile(`
|
||||
let i = 0;
|
||||
const iterator = {
|
||||
[Symbol.asyncIterator]() { return this; },
|
||||
async next() {
|
||||
switch (i++) {
|
||||
case 0: return { value: 1, done: false };
|
||||
case 1: return { value: Promise.resolve(2), done: false };
|
||||
case 2: return { value: new Promise<number>(resolve => setTimeout(resolve, 100, 3)), done: false };
|
||||
default: return { value: undefined: done: true };
|
||||
}
|
||||
}
|
||||
};
|
||||
export const output: any[] = [];
|
||||
export async function main() {
|
||||
for await (const item of iterator) {
|
||||
output.push(item);
|
||||
}
|
||||
}`));
|
||||
await module.exports.main();
|
||||
assert.strictEqual(module.exports.output[0], 1);
|
||||
assert.instanceOf(module.exports.output[1], Promise);
|
||||
assert.instanceOf(module.exports.output[2], Promise);
|
||||
});
|
||||
|
||||
it("async (es2015)", async () => {
|
||||
const module = evaluate(compile(`
|
||||
let i = 0;
|
||||
const iterator = {
|
||||
[Symbol.asyncIterator]() { return this; },
|
||||
async next() {
|
||||
switch (i++) {
|
||||
case 0: return { value: 1, done: false };
|
||||
case 1: return { value: Promise.resolve(2), done: false };
|
||||
case 2: return { value: new Promise<number>(resolve => setTimeout(resolve, 100, 3)), done: false };
|
||||
default: return { value: undefined: done: true };
|
||||
}
|
||||
}
|
||||
};
|
||||
export const output: any[] = [];
|
||||
export async function main() {
|
||||
for await (const item of iterator) {
|
||||
output.push(item);
|
||||
}
|
||||
}`, { target: ScriptTarget.ES2015 }));
|
||||
await module.exports.main();
|
||||
assert.strictEqual(module.exports.output[0], 1);
|
||||
assert.instanceOf(module.exports.output[1], Promise);
|
||||
assert.instanceOf(module.exports.output[2], Promise);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user