Skip to content

Commit 1256352

Browse files
committed
Merge pull request #6967 from Microsoft/revertNoCustomPromise
Revert restrictions on custom Promise in async function return type
2 parents 06d62ed + b0e37ab commit 1256352

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+240
-134
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12623,7 +12623,7 @@ namespace ts {
1262312623
* callable `then` signature.
1262412624
*/
1262512625
function checkAsyncFunctionReturnType(node: FunctionLikeDeclaration): Type {
12626-
if (languageVersion >= ScriptTarget.ES6) {
12626+
if (compilerOptions.noCustomAsyncPromise && languageVersion >= ScriptTarget.ES6) {
1262712627
const returnType = getTypeFromTypeNode(node.type);
1262812628
return checkCorrectPromiseType(returnType, node.type);
1262912629
}
@@ -13029,6 +13029,10 @@ namespace ts {
1302913029
}
1303013030

1303113031
function checkCollisionWithGlobalPromiseInGeneratedCode(node: Node, name: Identifier): void {
13032+
if (!compilerOptions.noCustomAsyncPromise) {
13033+
return;
13034+
}
13035+
1303213036
if (!needCollisionCheckForIdentifier(node, name, "Promise")) {
1303313037
return;
1303413038
}

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ namespace ts {
299299
name: "noImplicitUseStrict",
300300
type: "boolean",
301301
description: Diagnostics.Do_not_emit_use_strict_directives_in_module_output
302+
},
303+
{
304+
name: "noCustomAsyncPromise",
305+
type: "boolean",
306+
experimental: true
302307
}
303308
];
304309

src/compiler/emitter.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,21 +296,21 @@ namespace ts {
296296
* If loop contains block scoped binding captured in some function then loop body is converted to a function.
297297
* Lexical bindings declared in loop initializer will be passed into the loop body function as parameters,
298298
* however if this binding is modified inside the body - this new value should be propagated back to the original binding.
299-
* This is done by declaring new variable (out parameter holder) outside of the loop for every binding that is reassigned inside the body.
299+
* This is done by declaring new variable (out parameter holder) outside of the loop for every binding that is reassigned inside the body.
300300
* On every iteration this variable is initialized with value of corresponding binding.
301301
* At every point where control flow leaves the loop either explicitly (break/continue) or implicitly (at the end of loop body)
302302
* we copy the value inside the loop to the out parameter holder.
303-
*
303+
*
304304
* for (let x;;) {
305305
* let a = 1;
306306
* let b = () => a;
307307
* x++
308308
* if (...) break;
309309
* ...
310310
* }
311-
*
311+
*
312312
* will be converted to
313-
*
313+
*
314314
* var out_x;
315315
* var loop = function(x) {
316316
* var a = 1;
@@ -326,7 +326,7 @@ namespace ts {
326326
* x = out_x;
327327
* if (state === "break") break;
328328
* }
329-
*
329+
*
330330
* NOTE: values to out parameters are not copies if loop is abrupted with 'return' - in this case this will end the entire enclosing function
331331
* so nobody can observe this new value.
332332
*/
@@ -3049,7 +3049,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
30493049
}
30503050

30513051
writeLine();
3052-
// end of loop body -> copy out parameter
3052+
// end of loop body -> copy out parameter
30533053
copyLoopOutParameters(convertedLoopState, CopyDirection.ToOutParameter, /*emitAsStatements*/true);
30543054

30553055
decreaseIndent();
@@ -4685,7 +4685,7 @@ const _super = (function (geti, seti) {
46854685
write(", void 0, ");
46864686
}
46874687

4688-
if (languageVersion >= ScriptTarget.ES6 || !promiseConstructor) {
4688+
if (!promiseConstructor || (compilerOptions.noCustomAsyncPromise && languageVersion >= ScriptTarget.ES6)) {
46894689
write("void 0");
46904690
}
46914691
else {
@@ -7245,7 +7245,7 @@ const _super = (function (geti, seti) {
72457245
}
72467246

72477247
// text should be quoted string
7248-
// for deduplication purposes in key remove leading and trailing quotes so 'a' and "a" will be considered the same
7248+
// for deduplication purposes in key remove leading and trailing quotes so 'a' and "a" will be considered the same
72497249
const key = text.substr(1, text.length - 2);
72507250

72517251
if (hasProperty(groupIndices, key)) {

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,8 @@ namespace ts {
24422442
/* @internal */ skipDefaultLibCheck?: boolean;
24432443
// Do not perform validation of output file name in transpile scenarios
24442444
/* @internal */ suppressOutputPathCheck?: boolean;
2445+
// Disallow custom promise return types in async functions.
2446+
/* @internal */ noCustomAsyncPromise?: boolean;
24452447

24462448
[option: string]: string | number | boolean;
24472449
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/async/es6/asyncAliasReturnType_es6.ts(3,16): error TS1055: Type 'PromiseAlias' is not a valid async function return type.
2+
3+
4+
==== tests/cases/conformance/async/es6/asyncAliasReturnType_es6.ts (1 errors) ====
5+
type PromiseAlias<T> = Promise<T>;
6+
7+
async function f(): PromiseAlias<void> {
8+
~
9+
!!! error TS1055: Type 'PromiseAlias' is not a valid async function return type.
10+
}

tests/baselines/reference/asyncAliasReturnType_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ async function f(): PromiseAlias<void> {
66

77
//// [asyncAliasReturnType_es6.js]
88
function f() {
9-
return __awaiter(this, void 0, void 0, function* () {
9+
return __awaiter(this, void 0, PromiseAlias, function* () {
1010
});
1111
}

tests/baselines/reference/asyncAliasReturnType_es6.symbols

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/baselines/reference/asyncAliasReturnType_es6.types

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/baselines/reference/asyncArrowFunction1_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ var foo = async (): Promise<void> => {
44
};
55

66
//// [asyncArrowFunction1_es6.js]
7-
var foo = () => __awaiter(this, void 0, void 0, function* () {
7+
var foo = () => __awaiter(this, void 0, Promise, function* () {
88
});

tests/baselines/reference/asyncArrowFunction6_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ var foo = async (a = await): Promise<void> => {
44
}
55

66
//// [asyncArrowFunction6_es6.js]
7-
var foo = (a = yield ) => __awaiter(this, void 0, void 0, function* () {
7+
var foo = (a = yield ) => __awaiter(this, void 0, Promise, function* () {
88
});

tests/baselines/reference/asyncArrowFunction7_es6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ var bar = async (): Promise<void> => {
77
}
88

99
//// [asyncArrowFunction7_es6.js]
10-
var bar = () => __awaiter(this, void 0, void 0, function* () {
10+
var bar = () => __awaiter(this, void 0, Promise, function* () {
1111
// 'await' here is an identifier, and not an await expression.
12-
var foo = (a = yield ) => __awaiter(this, void 0, void 0, function* () {
12+
var foo = (a = yield ) => __awaiter(this, void 0, Promise, function* () {
1313
});
1414
});

tests/baselines/reference/asyncArrowFunction8_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ var foo = async (): Promise<void> => {
55
}
66

77
//// [asyncArrowFunction8_es6.js]
8-
var foo = () => __awaiter(this, void 0, void 0, function* () {
8+
var foo = () => __awaiter(this, void 0, Promise, function* () {
99
var v = { [yield ]: foo };
1010
});

tests/baselines/reference/asyncAwaitIsolatedModules_es6.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,56 +52,56 @@ function f0() {
5252
return __awaiter(this, void 0, void 0, function* () { });
5353
}
5454
function f1() {
55-
return __awaiter(this, void 0, void 0, function* () { });
55+
return __awaiter(this, void 0, Promise, function* () { });
5656
}
5757
function f3() {
58-
return __awaiter(this, void 0, void 0, function* () { });
58+
return __awaiter(this, void 0, MyPromise, function* () { });
5959
}
6060
let f4 = function () {
6161
return __awaiter(this, void 0, void 0, function* () { });
6262
};
6363
let f5 = function () {
64-
return __awaiter(this, void 0, void 0, function* () { });
64+
return __awaiter(this, void 0, Promise, function* () { });
6565
};
6666
let f6 = function () {
67-
return __awaiter(this, void 0, void 0, function* () { });
67+
return __awaiter(this, void 0, MyPromise, function* () { });
6868
};
6969
let f7 = () => __awaiter(this, void 0, void 0, function* () { });
70-
let f8 = () => __awaiter(this, void 0, void 0, function* () { });
71-
let f9 = () => __awaiter(this, void 0, void 0, function* () { });
70+
let f8 = () => __awaiter(this, void 0, Promise, function* () { });
71+
let f9 = () => __awaiter(this, void 0, MyPromise, function* () { });
7272
let f10 = () => __awaiter(this, void 0, void 0, function* () { return p; });
7373
let f11 = () => __awaiter(this, void 0, void 0, function* () { return mp; });
74-
let f12 = () => __awaiter(this, void 0, void 0, function* () { return mp; });
75-
let f13 = () => __awaiter(this, void 0, void 0, function* () { return p; });
74+
let f12 = () => __awaiter(this, void 0, Promise, function* () { return mp; });
75+
let f13 = () => __awaiter(this, void 0, MyPromise, function* () { return p; });
7676
let o = {
7777
m1() {
7878
return __awaiter(this, void 0, void 0, function* () { });
7979
},
8080
m2() {
81-
return __awaiter(this, void 0, void 0, function* () { });
81+
return __awaiter(this, void 0, Promise, function* () { });
8282
},
8383
m3() {
84-
return __awaiter(this, void 0, void 0, function* () { });
84+
return __awaiter(this, void 0, MyPromise, function* () { });
8585
}
8686
};
8787
class C {
8888
m1() {
8989
return __awaiter(this, void 0, void 0, function* () { });
9090
}
9191
m2() {
92-
return __awaiter(this, void 0, void 0, function* () { });
92+
return __awaiter(this, void 0, Promise, function* () { });
9393
}
9494
m3() {
95-
return __awaiter(this, void 0, void 0, function* () { });
95+
return __awaiter(this, void 0, MyPromise, function* () { });
9696
}
9797
static m4() {
9898
return __awaiter(this, void 0, void 0, function* () { });
9999
}
100100
static m5() {
101-
return __awaiter(this, void 0, void 0, function* () { });
101+
return __awaiter(this, void 0, Promise, function* () { });
102102
}
103103
static m6() {
104-
return __awaiter(this, void 0, void 0, function* () { });
104+
return __awaiter(this, void 0, MyPromise, function* () { });
105105
}
106106
}
107107
var M;

tests/baselines/reference/asyncAwait_es6.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,56 +52,56 @@ function f0() {
5252
return __awaiter(this, void 0, void 0, function* () { });
5353
}
5454
function f1() {
55-
return __awaiter(this, void 0, void 0, function* () { });
55+
return __awaiter(this, void 0, Promise, function* () { });
5656
}
5757
function f3() {
58-
return __awaiter(this, void 0, void 0, function* () { });
58+
return __awaiter(this, void 0, MyPromise, function* () { });
5959
}
6060
let f4 = function () {
6161
return __awaiter(this, void 0, void 0, function* () { });
6262
};
6363
let f5 = function () {
64-
return __awaiter(this, void 0, void 0, function* () { });
64+
return __awaiter(this, void 0, Promise, function* () { });
6565
};
6666
let f6 = function () {
67-
return __awaiter(this, void 0, void 0, function* () { });
67+
return __awaiter(this, void 0, MyPromise, function* () { });
6868
};
6969
let f7 = () => __awaiter(this, void 0, void 0, function* () { });
70-
let f8 = () => __awaiter(this, void 0, void 0, function* () { });
71-
let f9 = () => __awaiter(this, void 0, void 0, function* () { });
70+
let f8 = () => __awaiter(this, void 0, Promise, function* () { });
71+
let f9 = () => __awaiter(this, void 0, MyPromise, function* () { });
7272
let f10 = () => __awaiter(this, void 0, void 0, function* () { return p; });
7373
let f11 = () => __awaiter(this, void 0, void 0, function* () { return mp; });
74-
let f12 = () => __awaiter(this, void 0, void 0, function* () { return mp; });
75-
let f13 = () => __awaiter(this, void 0, void 0, function* () { return p; });
74+
let f12 = () => __awaiter(this, void 0, Promise, function* () { return mp; });
75+
let f13 = () => __awaiter(this, void 0, MyPromise, function* () { return p; });
7676
let o = {
7777
m1() {
7878
return __awaiter(this, void 0, void 0, function* () { });
7979
},
8080
m2() {
81-
return __awaiter(this, void 0, void 0, function* () { });
81+
return __awaiter(this, void 0, Promise, function* () { });
8282
},
8383
m3() {
84-
return __awaiter(this, void 0, void 0, function* () { });
84+
return __awaiter(this, void 0, MyPromise, function* () { });
8585
}
8686
};
8787
class C {
8888
m1() {
8989
return __awaiter(this, void 0, void 0, function* () { });
9090
}
9191
m2() {
92-
return __awaiter(this, void 0, void 0, function* () { });
92+
return __awaiter(this, void 0, Promise, function* () { });
9393
}
9494
m3() {
95-
return __awaiter(this, void 0, void 0, function* () { });
95+
return __awaiter(this, void 0, MyPromise, function* () { });
9696
}
9797
static m4() {
9898
return __awaiter(this, void 0, void 0, function* () { });
9999
}
100100
static m5() {
101-
return __awaiter(this, void 0, void 0, function* () { });
101+
return __awaiter(this, void 0, Promise, function* () { });
102102
}
103103
static m6() {
104-
return __awaiter(this, void 0, void 0, function* () { });
104+
return __awaiter(this, void 0, MyPromise, function* () { });
105105
}
106106
}
107107
var M;

tests/baselines/reference/asyncFunctionDeclaration11_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ async function await(): Promise<void> {
44

55
//// [asyncFunctionDeclaration11_es6.js]
66
function await() {
7-
return __awaiter(this, void 0, void 0, function* () {
7+
return __awaiter(this, void 0, Promise, function* () {
88
});
99
}

tests/baselines/reference/asyncFunctionDeclaration13_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function foo(): Promise<void> {
77

88
//// [asyncFunctionDeclaration13_es6.js]
99
function foo() {
10-
return __awaiter(this, void 0, void 0, function* () {
10+
return __awaiter(this, void 0, Promise, function* () {
1111
// Legal to use 'await' in a type context.
1212
var v;
1313
});

tests/baselines/reference/asyncFunctionDeclaration14_es6.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function foo(): Promise<void> {
55

66
//// [asyncFunctionDeclaration14_es6.js]
77
function foo() {
8-
return __awaiter(this, void 0, void 0, function* () {
8+
return __awaiter(this, void 0, Promise, function* () {
99
return;
1010
});
1111
}

0 commit comments

Comments
 (0)