Skip to content

Commit 411e90d

Browse files
committed
Improve decorator on overload error message (fixes microsoft#6064)
1 parent 55d4f0f commit 411e90d

5 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/compiler/checker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15544,7 +15544,12 @@ namespace ts {
1554415544
return false;
1554515545
}
1554615546
if (!nodeCanBeDecorated(node)) {
15547-
return grammarErrorOnFirstToken(node, Diagnostics.Decorators_are_not_valid_here);
15547+
if (node.kind === SyntaxKind.MethodDeclaration && !ts.nodeIsPresent((<MethodDeclaration>node).body)) {
15548+
return grammarErrorOnFirstToken(node, Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload);
15549+
}
15550+
else {
15551+
return grammarErrorOnFirstToken(node, Diagnostics.Decorators_are_not_valid_here);
15552+
}
1554815553
}
1554915554
else if (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor) {
1555015555
const accessors = getAllAccessorDeclarations((<ClassDeclaration>node.parent).members, <AccessorDeclaration>node);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,10 @@
795795
"category": "Error",
796796
"code": 1248
797797
},
798+
"A decorator can only decorate a method implementation, not an overload.": {
799+
"category": "Error",
800+
"code": 1249
801+
},
798802
"'with' statements are not allowed in an async function block.": {
799803
"category": "Error",
800804
"code": 1300
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts(4,5): error TS1249: A decorator can only decorate a method implementation, not an overload.
2+
3+
4+
==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethodOverload1.ts (1 errors) ====
5+
declare function dec<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
6+
7+
class C {
8+
@dec
9+
~
10+
!!! error TS1249: A decorator can only decorate a method implementation, not an overload.
11+
method()
12+
method() { }
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [decoratorOnClassMethodOverload1.ts]
2+
declare function dec<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
3+
4+
class C {
5+
@dec
6+
method()
7+
method() { }
8+
}
9+
10+
//// [decoratorOnClassMethodOverload1.js]
11+
var C = (function () {
12+
function C() {
13+
}
14+
C.prototype.method = function () { };
15+
return C;
16+
}());
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @target: ES5
2+
// @experimentaldecorators: true
3+
declare function dec<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
4+
5+
class C {
6+
@dec
7+
method()
8+
method() { }
9+
}

0 commit comments

Comments
 (0)