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
This commit is contained in:
parent
d0f669e956
commit
574a64dec9
|
@ -17794,6 +17794,8 @@ namespace ts {
|
|||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
case SyntaxKind.ExportDeclaration:
|
||||
case SyntaxKind.ExportAssignment:
|
||||
case SyntaxKind.FunctionExpression:
|
||||
case SyntaxKind.ArrowFunction:
|
||||
case SyntaxKind.Parameter:
|
||||
break;
|
||||
case SyntaxKind.FunctionDeclaration:
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
error TS2318: Cannot find global type 'Promise'.
|
||||
tests/cases/compiler/disallowAsyncModifierInES5.ts(2,1): error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
|
||||
tests/cases/compiler/disallowAsyncModifierInES5.ts(2,16): error TS1057: An async function or method must have a valid awaitable return type.
|
||||
tests/cases/compiler/disallowAsyncModifierInES5.ts(3,11): error TS1057: An async function or method must have a valid awaitable return type.
|
||||
tests/cases/compiler/disallowAsyncModifierInES5.ts(3,11): error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
|
||||
tests/cases/compiler/disallowAsyncModifierInES5.ts(4,11): error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
|
||||
tests/cases/compiler/disallowAsyncModifierInES5.ts(4,11): error TS1057: An async function or method must have a valid awaitable return type.
|
||||
|
||||
|
||||
!!! error TS2318: Cannot find global type 'Promise'.
|
||||
==== tests/cases/compiler/disallowAsyncModifierInES5.ts (6 errors) ====
|
||||
|
||||
async function foo() { return 42; } // ERROR: Async functions are only available in ES6+
|
||||
~~~~~
|
||||
!!! error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
|
||||
~~~
|
||||
!!! error TS1057: An async function or method must have a valid awaitable return type.
|
||||
let bar = async function () { return 42; } // OK, but should be an error
|
||||
~~~~~
|
||||
!!! error TS1057: An async function or method must have a valid awaitable return type.
|
||||
~~~~~
|
||||
!!! error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
|
||||
let baz = async () => 42; // OK, but should be an error
|
||||
~~~~~
|
||||
!!! error TS1311: Async functions are only available when targeting ECMAScript 6 and higher.
|
||||
~~~~~~~~~~~~~~
|
||||
!!! error TS1057: An async function or method must have a valid awaitable return type.
|
22
tests/baselines/reference/disallowAsyncModifierInES5.js
Normal file
22
tests/baselines/reference/disallowAsyncModifierInES5.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
//// [disallowAsyncModifierInES5.ts]
|
||||
|
||||
async function foo() { return 42; } // ERROR: Async functions are only available in ES6+
|
||||
let bar = async function () { return 42; } // OK, but should be an error
|
||||
let baz = async () => 42; // OK, but should be an error
|
||||
|
||||
//// [disallowAsyncModifierInES5.js]
|
||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments)).next());
|
||||
});
|
||||
};
|
||||
function foo() {
|
||||
return __awaiter(this, void 0, void 0, function* () { return 42; });
|
||||
} // ERROR: Async functions are only available in ES6+
|
||||
var bar = function () {
|
||||
return __awaiter(this, void 0, void 0, function* () { return 42; });
|
||||
}; // OK, but should be an error
|
||||
var baz = function () __awaiter(this, void 0, void 0, function* () { return 42; }); // OK, but should be an error
|
5
tests/cases/compiler/disallowAsyncModifierInES5.ts
Normal file
5
tests/cases/compiler/disallowAsyncModifierInES5.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
// @target: es5
|
||||
|
||||
async function foo() { return 42; } // ERROR: Async functions are only available in ES6+
|
||||
let bar = async function () { return 42; } // OK, but should be an error
|
||||
let baz = async () => 42; // OK, but should be an error
|
Loading…
Reference in a new issue