Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give a more helpful error message for certain decorators with too many arguments #18811

Merged
merged 10 commits into from
Oct 5, 2017
18 changes: 18 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16381,6 +16381,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;
Expand All @@ -16393,6 +16399,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[]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should you also check that the return type is callable/any to ensure your suggestion of calling it is warranted.

Copy link
Member Author

Choose a reason for hiding this comment

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

any is accounted for - it's an uncalled function call. It also has no signatures.

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
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Empty file modified src/compiler/emitter.ts
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions tests/baselines/reference/decoratorOnClassMethod6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1241: Unable to resolve signature of method decorator when called as an expression.
tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5): error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'?


==== tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts (1 errors) ====
Expand All @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/method/decoratorOnClassMethod6.ts(4,5):
class C {
@dec ["method"]() {}
~~~~
!!! error TS1241: Unable to resolve signature of method decorator when called as an expression.
!!! error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'?
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1240: Unable to resolve signature of property decorator when called as an expression.
tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(4,5): error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'?


==== tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts (1 errors) ====
Expand All @@ -7,5 +7,5 @@ tests/cases/conformance/decorators/class/property/decoratorOnClassProperty11.ts(
class C {
@dec prop;
~~~~
!!! error TS1240: Unable to resolve signature of property decorator when called as an expression.
!!! error TS1329: 'dec' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@dec()'?
}
174 changes: 174 additions & 0 deletions tests/baselines/reference/potentiallyUncalledDecorators.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
tests/cases/compiler/potentiallyUncalledDecorators.ts(4,5): error TS1329: 'Input' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@Input()'?
tests/cases/compiler/potentiallyUncalledDecorators.ts(35,1): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
tests/cases/compiler/potentiallyUncalledDecorators.ts(37,5): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
tests/cases/compiler/potentiallyUncalledDecorators.ts(38,5): error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
tests/cases/compiler/potentiallyUncalledDecorators.ts(41,1): error TS1238: Unable to resolve signature of class decorator when called as an expression.
Type 'OmniDecorator' is not assignable to type 'typeof B'.
Type 'OmniDecorator' provides no match for the signature 'new (): B'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(43,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
Unable to resolve signature of property decorator when called as an expression.
tests/cases/compiler/potentiallyUncalledDecorators.ts(44,5): error TS1241: Unable to resolve signature of method decorator when called as an expression.
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(47,1): error TS1238: Unable to resolve signature of class decorator when called as an expression.
Type 'OmniDecorator' is not assignable to type 'typeof C'.
Type 'OmniDecorator' provides no match for the signature 'new (): C'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(49,5): error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'?
tests/cases/compiler/potentiallyUncalledDecorators.ts(50,5): error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'?
tests/cases/compiler/potentiallyUncalledDecorators.ts(53,1): error TS1238: Unable to resolve signature of class decorator when called as an expression.
Type 'OmniDecorator' is not assignable to type 'typeof D'.
Type 'OmniDecorator' provides no match for the signature 'new (): D'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(55,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
Unable to resolve signature of property decorator when called as an expression.
tests/cases/compiler/potentiallyUncalledDecorators.ts(56,5): error TS1241: Unable to resolve signature of method decorator when called as an expression.
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(59,1): error TS1238: Unable to resolve signature of class decorator when called as an expression.
Type 'OmniDecorator' is not assignable to type 'typeof E'.
Type 'OmniDecorator' provides no match for the signature 'new (): E'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(61,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
Unable to resolve signature of property decorator when called as an expression.
tests/cases/compiler/potentiallyUncalledDecorators.ts(62,5): error TS1241: Unable to resolve signature of method decorator when called as an expression.
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(65,1): error TS1238: Unable to resolve signature of class decorator when called as an expression.
Type 'OmniDecorator' is not assignable to type 'typeof F'.
Type 'OmniDecorator' provides no match for the signature 'new (): F'.
tests/cases/compiler/potentiallyUncalledDecorators.ts(67,5): error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
Unable to resolve signature of property decorator when called as an expression.
tests/cases/compiler/potentiallyUncalledDecorators.ts(68,5): error TS1241: Unable to resolve signature of method decorator when called as an expression.
Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.


==== tests/cases/compiler/potentiallyUncalledDecorators.ts (19 errors) ====
// Angular-style Input/Output API:
declare function Input(bindingPropertyName?: string): any;
class FooComponent {
@Input foo: string;
~~~~~~
!!! error TS1329: 'Input' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@Input()'?
}

// Glimmer-style tracked API:
declare const tracked: PropertyDecorator & { (...watchedProperties: string[]): any; }

class Person {
@tracked person; any;
}

class MultiplyByTwo {
args: any;
@tracked('args')
get multiplied() {
return this.args.number * 2;
}
}

// Other fun stuff.

interface OmniDecorator extends MethodDecorator, ClassDecorator, PropertyDecorator {
}

declare function noArgs(): OmniDecorator;
declare function allRest(...args: any[]): OmniDecorator;
declare function oneOptional(x?: any): OmniDecorator;
declare function twoOptional(x?: any, y?: any): OmniDecorator;
declare function threeOptional(x?: any, y?: any, z?: any): OmniDecorator;
declare function oneOptionalWithRest(x?: any, ...args: any[]): OmniDecorator;
declare const anyDec: any;

@noArgs
~~~~~~~
!!! error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
class A {
@noArgs foo: any;
~~~~~~~
!!! error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
@noArgs bar() { }
~~~~~~~
!!! error TS1329: 'noArgs' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@noArgs()'?
}

@allRest
~~~~~~~~
!!! error TS1238: Unable to resolve signature of class decorator when called as an expression.
!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof B'.
!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): B'.
class B {
@allRest foo: any;
~~~~~~~~
!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
!!! error TS1236: Unable to resolve signature of property decorator when called as an expression.
@allRest bar() { }
~~~~~~~~
!!! error TS1241: Unable to resolve signature of method decorator when called as an expression.
!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
}

@oneOptional
~~~~~~~~~~~~
!!! error TS1238: Unable to resolve signature of class decorator when called as an expression.
!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof C'.
!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): C'.
class C {
@oneOptional foo: any;
~~~~~~~~~~~~
!!! error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'?
@oneOptional bar() { }
~~~~~~~~~~~~
!!! error TS1329: 'oneOptional' accepts too few arguments to be used as a decorator here. Did you mean to call it first and write '@oneOptional()'?
}

@twoOptional
~~~~~~~~~~~~
!!! error TS1238: Unable to resolve signature of class decorator when called as an expression.
!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof D'.
!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): D'.
class D {
@twoOptional foo: any;
~~~~~~~~~~~~
!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
!!! error TS1236: Unable to resolve signature of property decorator when called as an expression.
@twoOptional bar() { }
~~~~~~~~~~~~
!!! error TS1241: Unable to resolve signature of method decorator when called as an expression.
!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
}

@threeOptional
~~~~~~~~~~~~~~
!!! error TS1238: Unable to resolve signature of class decorator when called as an expression.
!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof E'.
!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): E'.
class E {
@threeOptional foo: any;
~~~~~~~~~~~~~~
!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
!!! error TS1236: Unable to resolve signature of property decorator when called as an expression.
@threeOptional bar() { }
~~~~~~~~~~~~~~
!!! error TS1241: Unable to resolve signature of method decorator when called as an expression.
!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
}

@oneOptionalWithRest
~~~~~~~~~~~~~~~~~~~~
!!! error TS1238: Unable to resolve signature of class decorator when called as an expression.
!!! error TS1238: Type 'OmniDecorator' is not assignable to type 'typeof F'.
!!! error TS1238: Type 'OmniDecorator' provides no match for the signature 'new (): F'.
class F {
@oneOptionalWithRest foo: any;
~~~~~~~~~~~~~~~~~~~~
!!! error TS1236: The return type of a property decorator function must be either 'void' or 'any'.
!!! error TS1236: Unable to resolve signature of property decorator when called as an expression.
@oneOptionalWithRest bar() { }
~~~~~~~~~~~~~~~~~~~~
!!! error TS1241: Unable to resolve signature of method decorator when called as an expression.
!!! error TS1241: Type 'OmniDecorator' has no properties in common with type 'TypedPropertyDescriptor<() => void>'.
}

@anyDec
class G {
@anyDec foo: any;
@anyDec bar() { }
}

export { };

Loading