Skip to content

Commit 574a64d

Browse files
authored
Fix7334 Disallow async in functionExpression and ArrowFunction (#9062)
* Error when using async modifier in function-expression and arrow-function when target es5 * Add tests and baselines
1 parent d0f669e commit 574a64d

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17794,6 +17794,8 @@ namespace ts {
1779417794
case SyntaxKind.ImportEqualsDeclaration:
1779517795
case SyntaxKind.ExportDeclaration:
1779617796
case SyntaxKind.ExportAssignment:
17797+
case SyntaxKind.FunctionExpression:
17798+
case SyntaxKind.ArrowFunction:
1779717799
case SyntaxKind.Parameter:
1779817800
break;
1779917801
case SyntaxKind.FunctionDeclaration:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error TS2318: Cannot find global type 'Promise'.
2+
tests/cases/compiler/disallowAsyncModifierInES5.ts(2,1): error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
3+
tests/cases/compiler/disallowAsyncModifierInES5.ts(2,16): error TS1057: An async function or method must have a valid awaitable return type.
4+
tests/cases/compiler/disallowAsyncModifierInES5.ts(3,11): error TS1057: An async function or method must have a valid awaitable return type.
5+
tests/cases/compiler/disallowAsyncModifierInES5.ts(3,11): error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
6+
tests/cases/compiler/disallowAsyncModifierInES5.ts(4,11): error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
7+
tests/cases/compiler/disallowAsyncModifierInES5.ts(4,11): error TS1057: An async function or method must have a valid awaitable return type.
8+
9+
10+
!!! error TS2318: Cannot find global type 'Promise'.
11+
==== tests/cases/compiler/disallowAsyncModifierInES5.ts (6 errors) ====
12+
13+
async function foo() { return 42; } // ERROR: Async functions are only available in ES6+
14+
~~~~~
15+
!!! error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
16+
~~~
17+
!!! error TS1057: An async function or method must have a valid awaitable return type.
18+
let bar = async function () { return 42; } // OK, but should be an error
19+
~~~~~
20+
!!! error TS1057: An async function or method must have a valid awaitable return type.
21+
~~~~~
22+
!!! error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
23+
let baz = async () => 42; // OK, but should be an error
24+
~~~~~
25+
!!! error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
26+
~~~~~~~~~~~~~~
27+
!!! error TS1057: An async function or method must have a valid awaitable return type.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [disallowAsyncModifierInES5.ts]
2+
3+
async function foo() { return 42; } // ERROR: Async functions are only available in ES6+
4+
let bar = async function () { return 42; } // OK, but should be an error
5+
let baz = async () => 42; // OK, but should be an error
6+
7+
//// [disallowAsyncModifierInES5.js]
8+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
9+
return new (P || (P = Promise))(function (resolve, reject) {
10+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
11+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
12+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
13+
step((generator = generator.apply(thisArg, _arguments)).next());
14+
});
15+
};
16+
function foo() {
17+
return __awaiter(this, void 0, void 0, function* () { return 42; });
18+
} // ERROR: Async functions are only available in ES6+
19+
var bar = function () {
20+
return __awaiter(this, void 0, void 0, function* () { return 42; });
21+
}; // OK, but should be an error
22+
var baz = function () __awaiter(this, void 0, void 0, function* () { return 42; }); // OK, but should be an error
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @target: es5
2+
3+
async function foo() { return 42; } // ERROR: Async functions are only available in ES6+
4+
let bar = async function () { return 42; } // OK, but should be an error
5+
let baz = async () => 42; // OK, but should be an error

0 commit comments

Comments
 (0)