Skip to content

Commit

Permalink
Use a better check.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser committed Sep 29, 2017
1 parent fbbf3d2 commit 966f370
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/compiler/checker.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16381,8 +16381,8 @@ namespace ts {
return resolveUntypedCall(node);
}

if (callSignatures.length === 1 && callSignatures[0].parameters.length === 0) {
error(node, Diagnostics.A_decorator_function_must_accept_some_number_of_arguments_but_this_expression_takes_none_Did_you_mean_to_call_it_first);
if (isPotentiallyUncalledDecorator(node, callSignatures)) {
error(node, Diagnostics.This_function_cannot_be_used_as_a_decorator_Did_you_mean_to_call_it_first);
return resolveErrorCall(node);
}

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

/**
* Sometimes, we have a decorator that could accept zero arguments,
* but is receiving too many arguments as part of the decorator invocation.
* In those cases, a user may have meant to *call* the expression before using it as a decorator.
*/
function isPotentiallyUncalledDecorator(decorator: Decorator, signatures: Signature[]) {
return signatures.length && every(signatures, signature =>
signature.minArgumentCount === 0 &&
signature.parameters.length < getEffectiveArgumentCount(decorator, /*args*/ undefined, signature))
}

/**
* This function is similar to getResolvedSignature but is exclusively for trying to resolve JSX stateless-function component.
* 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
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@
"category": "Error",
"code": 1328
},
"A decorator function must accept some number of arguments, but this expression takes none. Did you mean to call it first?": {
"This function cannot be used as a decorator. Did you mean to call it first?": {
"category": "Error",
"code": 1329
},
Expand Down

0 comments on commit 966f370

Please sign in to comment.