Skip to content

Commit 966f370

Browse files
Use a better check.
1 parent fbbf3d2 commit 966f370

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/compiler/checker.ts

100755100644
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16381,8 +16381,8 @@ namespace ts {
1638116381
return resolveUntypedCall(node);
1638216382
}
1638316383

16384-
if (callSignatures.length === 1 && callSignatures[0].parameters.length === 0) {
16385-
error(node, Diagnostics.A_decorator_function_must_accept_some_number_of_arguments_but_this_expression_takes_none_Did_you_mean_to_call_it_first);
16384+
if (isPotentiallyUncalledDecorator(node, callSignatures)) {
16385+
error(node, Diagnostics.This_function_cannot_be_used_as_a_decorator_Did_you_mean_to_call_it_first);
1638616386
return resolveErrorCall(node);
1638716387
}
1638816388

@@ -16398,6 +16398,17 @@ namespace ts {
1639816398
return resolveCall(node, callSignatures, candidatesOutArray, headMessage);
1639916399
}
1640016400

16401+
/**
16402+
* Sometimes, we have a decorator that could accept zero arguments,
16403+
* but is receiving too many arguments as part of the decorator invocation.
16404+
* In those cases, a user may have meant to *call* the expression before using it as a decorator.
16405+
*/
16406+
function isPotentiallyUncalledDecorator(decorator: Decorator, signatures: Signature[]) {
16407+
return signatures.length && every(signatures, signature =>
16408+
signature.minArgumentCount === 0 &&
16409+
signature.parameters.length < getEffectiveArgumentCount(decorator, /*args*/ undefined, signature))
16410+
}
16411+
1640116412
/**
1640216413
* This function is similar to getResolvedSignature but is exclusively for trying to resolve JSX stateless-function component.
1640316414
* The main reason we have to use this function instead of getResolvedSignature because, the caller of this function will already check the type of openingLikeElement's tagName

src/compiler/diagnosticMessages.json

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@
907907
"category": "Error",
908908
"code": 1328
909909
},
910-
"A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first?": {
910+
"This function cannot be used as a decorator. Did you mean to call it first?": {
911911
"category": "Error",
912912
"code": 1329
913913
},

0 commit comments

Comments
 (0)