Skip to content
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
9 changes: 6 additions & 3 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,11 +1338,14 @@ namespace ts {
let decorators: (ReadonlyArray<Decorator> | undefined)[] | undefined;
if (node) {
const parameters = node.parameters;
for (let i = 0; i < parameters.length; i++) {
const parameter = parameters[i];
const firstParameterIsThis = parameters.length > 0 && parameterIsThisKeyword(parameters[0]);
const firstParameterOffset = firstParameterIsThis ? 1 : 0;
const numParameters = firstParameterIsThis ? parameters.length - 1 : parameters.length;
for (let i = 0; i < numParameters; i++) {
const parameter = parameters[i + firstParameterOffset];
if (decorators || parameter.decorators) {
if (!decorators) {
decorators = new Array(parameters.length);
decorators = new Array(numParameters);
}

decorators[i] = parameter.decorators;
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/decoratorOnClassMethodParameter2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [decoratorOnClassMethodParameter2.ts]
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;

class C {
method(this: C, @dec p: number) {}
}

//// [decoratorOnClassMethodParameter2.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var C = /** @class */ (function () {
function C() {
}
C.prototype.method = function (p) { };
__decorate([
__param(0, dec)
], C.prototype, "method", null);
return C;
}());
18 changes: 18 additions & 0 deletions tests/baselines/reference/decoratorOnClassMethodParameter2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodParameter2.ts ===
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;
>dec : Symbol(dec, Decl(decoratorOnClassMethodParameter2.ts, 0, 0))
>target : Symbol(target, Decl(decoratorOnClassMethodParameter2.ts, 0, 21))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>propertyKey : Symbol(propertyKey, Decl(decoratorOnClassMethodParameter2.ts, 0, 36))
>parameterIndex : Symbol(parameterIndex, Decl(decoratorOnClassMethodParameter2.ts, 0, 66))

class C {
>C : Symbol(C, Decl(decoratorOnClassMethodParameter2.ts, 0, 97))

method(this: C, @dec p: number) {}
>method : Symbol(C.method, Decl(decoratorOnClassMethodParameter2.ts, 2, 9))
>this : Symbol(this, Decl(decoratorOnClassMethodParameter2.ts, 3, 11))
>C : Symbol(C, Decl(decoratorOnClassMethodParameter2.ts, 0, 97))
>dec : Symbol(dec, Decl(decoratorOnClassMethodParameter2.ts, 0, 0))
>p : Symbol(p, Decl(decoratorOnClassMethodParameter2.ts, 3, 19))
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/decoratorOnClassMethodParameter2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodParameter2.ts ===
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;
>dec : (target: Object, propertyKey: string | symbol, parameterIndex: number) => void
>target : Object
>propertyKey : string | symbol
>parameterIndex : number

class C {
>C : C

method(this: C, @dec p: number) {}
>method : (this: C, p: number) => void
>this : C
>dec : (target: Object, propertyKey: string | symbol, parameterIndex: number) => void
>p : number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts(4,17): error TS1003: Identifier expected.
tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts(4,17): error TS2680: A 'this' parameter must be the first parameter.


==== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts (2 errors) ====
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;

class C {
method(@dec this: C) {}
~~~~
!!! error TS1003: Identifier expected.
~~~~~~~
!!! error TS2680: A 'this' parameter must be the first parameter.
}
26 changes: 26 additions & 0 deletions tests/baselines/reference/decoratorOnClassMethodThisParameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [decoratorOnClassMethodThisParameter.ts]
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;

class C {
method(@dec this: C) {}
}

//// [decoratorOnClassMethodThisParameter.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var C = /** @class */ (function () {
function C() {
}
C.prototype.method = function () { };
__decorate([
__param(0, dec)
], C.prototype, "method", null);
return C;
}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts ===
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;
>dec : Symbol(dec, Decl(decoratorOnClassMethodThisParameter.ts, 0, 0))
>target : Symbol(target, Decl(decoratorOnClassMethodThisParameter.ts, 0, 21))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>propertyKey : Symbol(propertyKey, Decl(decoratorOnClassMethodThisParameter.ts, 0, 36))
>parameterIndex : Symbol(parameterIndex, Decl(decoratorOnClassMethodThisParameter.ts, 0, 66))

class C {
>C : Symbol(C, Decl(decoratorOnClassMethodThisParameter.ts, 0, 97))

method(@dec this: C) {}
>method : Symbol(C.method, Decl(decoratorOnClassMethodThisParameter.ts, 2, 9))
>dec : Symbol(dec, Decl(decoratorOnClassMethodThisParameter.ts, 0, 0))
> : Symbol((Missing), Decl(decoratorOnClassMethodThisParameter.ts, 3, 11))
>this : Symbol(this, Decl(decoratorOnClassMethodThisParameter.ts, 3, 15))
>C : Symbol(C, Decl(decoratorOnClassMethodThisParameter.ts, 0, 97))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/decorators/class/method/parameter/decoratorOnClassMethodThisParameter.ts ===
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;
>dec : (target: Object, propertyKey: string | symbol, parameterIndex: number) => void
>target : Object
>propertyKey : string | symbol
>parameterIndex : number

class C {
>C : C

method(@dec this: C) {}
>method : (: any, this: C) => void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type seems incorrectly printed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are decorators on this parameters even a thing? O.o

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's because this is a parse error (i.e. @dec this fails because this isn't an identifier), so you end up with a missing identifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, decorators on this parameters were never valid, but we did not have a test that reflects this. I opted to add one while investigating this fix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. Neat.

>dec : (target: Object, propertyKey: string | symbol, parameterIndex: number) => void
> : any
>this : C
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @target:es5
// @experimentaldecorators: true
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;

class C {
method(this: C, @dec p: number) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// @target:es5
// @experimentaldecorators: true
declare function dec(target: Object, propertyKey: string | symbol, parameterIndex: number): void;

class C {
method(@dec this: C) {}
}