Skip to content

Fix7334 Disallow async in functionExpression and ArrowFunction #9062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17782,6 +17782,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:
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/disallowAsyncModifierInES5.errors.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions tests/baselines/reference/disallowAsyncModifierInES5.js
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions tests/cases/compiler/disallowAsyncModifierInES5.ts
Original file line number Diff line number Diff line change
@@ -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