Merge pull request #18811 from Microsoft/uncalledDecorator

Give a more helpful error message for certain decorators with too many arguments
This commit is contained in:
Daniel Rosenwasser
2017-10-05 11:27:46 -07:00
committed by GitHub
10 changed files with 876 additions and 4 deletions

View File

@@ -16548,6 +16548,12 @@ namespace ts {
return resolveUntypedCall(node);
}
if (isPotentiallyUncalledDecorator(node, callSignatures)) {
const nodeStr = getTextOfNode(node.expression, /*includeTrivia*/ false);
error(node, Diagnostics._0_accepts_too_few_arguments_to_be_used_as_a_decorator_here_Did_you_mean_to_call_it_first_and_write_0, nodeStr);
return resolveErrorCall(node);
}
const headMessage = getDiagnosticHeadMessageForDecoratorResolution(node);
if (!callSignatures.length) {
let errorInfo: DiagnosticMessageChain;
@@ -16560,6 +16566,18 @@ 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.hasRestParameter &&
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

View File

@@ -907,6 +907,10 @@
"category": "Error",
"code": 1328
},
"'{0}' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@{0}()'?": {
"category": "Error",
"code": 1329
},
"Duplicate identifier '{0}'.": {
"category": "Error",

0
src/compiler/emitter.ts Executable file → Normal file
View File