Skip to content

Commit 12dbdf0

Browse files
authored
Parse parameter decorators outside of Await context when appropriate (#50040)
1 parent a179e91 commit 12dbdf0

File tree

6 files changed

+117
-1
lines changed

6 files changed

+117
-1
lines changed

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3337,7 +3337,7 @@ namespace ts {
33373337
// BindingElement[?Yield,?Await]
33383338

33393339
// Decorators are parsed in the outer [Await] context, the rest of the parameter is parsed in the function's [Await] context.
3340-
const decorators = inOuterAwaitContext ? doInAwaitContext(parseDecorators) : parseDecorators();
3340+
const decorators = inOuterAwaitContext ? doInAwaitContext(parseDecorators) : doOutsideOfAwaitContext(parseDecorators);
33413341

33423342
if (token() === SyntaxKind.ThisKeyword) {
33433343
const node = factory.createParameterDeclaration(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodParameter3.ts(5,23): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
2+
3+
4+
==== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodParameter3.ts (1 errors) ====
5+
// https://github.com/microsoft/TypeScript/issues/48509
6+
declare function dec(a: any): any;
7+
function fn(value: Promise<number>): any {
8+
class Class {
9+
async method(@dec(await value) arg: number) {}
10+
~~~~~
11+
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
12+
}
13+
return Class
14+
}
15+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [decoratorOnClassMethodParameter3.ts]
2+
// https://github.com/microsoft/TypeScript/issues/48509
3+
declare function dec(a: any): any;
4+
function fn(value: Promise<number>): any {
5+
class Class {
6+
async method(@dec(await value) arg: number) {}
7+
}
8+
return Class
9+
}
10+
11+
12+
//// [decoratorOnClassMethodParameter3.js]
13+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17+
return c > 3 && r && Object.defineProperty(target, key, r), r;
18+
};
19+
var __param = (this && this.__param) || function (paramIndex, decorator) {
20+
return function (target, key) { decorator(target, key, paramIndex); }
21+
};
22+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
23+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24+
return new (P || (P = Promise))(function (resolve, reject) {
25+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28+
step((generator = generator.apply(thisArg, _arguments || [])).next());
29+
});
30+
};
31+
function fn(value) {
32+
class Class {
33+
method(arg) {
34+
return __awaiter(this, void 0, void 0, function* () { });
35+
}
36+
}
37+
__decorate([
38+
__param(0, dec(yield value))
39+
], Class.prototype, "method", null);
40+
return Class;
41+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodParameter3.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/48509
3+
declare function dec(a: any): any;
4+
>dec : Symbol(dec, Decl(decoratorOnClassMethodParameter3.ts, 0, 0))
5+
>a : Symbol(a, Decl(decoratorOnClassMethodParameter3.ts, 1, 21))
6+
7+
function fn(value: Promise<number>): any {
8+
>fn : Symbol(fn, Decl(decoratorOnClassMethodParameter3.ts, 1, 34))
9+
>value : Symbol(value, Decl(decoratorOnClassMethodParameter3.ts, 2, 12))
10+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
11+
12+
class Class {
13+
>Class : Symbol(Class, Decl(decoratorOnClassMethodParameter3.ts, 2, 42))
14+
15+
async method(@dec(await value) arg: number) {}
16+
>method : Symbol(Class.method, Decl(decoratorOnClassMethodParameter3.ts, 3, 15))
17+
>dec : Symbol(dec, Decl(decoratorOnClassMethodParameter3.ts, 0, 0))
18+
>value : Symbol(value, Decl(decoratorOnClassMethodParameter3.ts, 2, 12))
19+
>arg : Symbol(arg, Decl(decoratorOnClassMethodParameter3.ts, 4, 17))
20+
}
21+
return Class
22+
>Class : Symbol(Class, Decl(decoratorOnClassMethodParameter3.ts, 2, 42))
23+
}
24+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodParameter3.ts ===
2+
// https://github.com/microsoft/TypeScript/issues/48509
3+
declare function dec(a: any): any;
4+
>dec : (a: any) => any
5+
>a : any
6+
7+
function fn(value: Promise<number>): any {
8+
>fn : (value: Promise<number>) => any
9+
>value : Promise<number>
10+
11+
class Class {
12+
>Class : Class
13+
14+
async method(@dec(await value) arg: number) {}
15+
>method : (arg: number) => Promise<void>
16+
>dec(await value) : any
17+
>dec : (a: any) => any
18+
>await value : number
19+
>value : Promise<number>
20+
>arg : number
21+
}
22+
return Class
23+
>Class : typeof Class
24+
}
25+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @target: es2015
2+
// @experimentaldecorators: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/48509
5+
declare function dec(a: any): any;
6+
function fn(value: Promise<number>): any {
7+
class Class {
8+
async method(@dec(await value) arg: number) {}
9+
}
10+
return Class
11+
}

0 commit comments

Comments
 (0)