-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): new rule method-signature-style #1685
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
feat(eslint-plugin): new rule method-signature-style #1685
Conversation
Thanks for the PR, @phaux! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
haven't reviewed the code, but a few quick comments on the documentation
``` | ||
|
||
A method and a function property of the same type behave differently. | ||
Methods are always bivariant in their argument, while function properties are contravariant in their argument under `strictFunctionTypes`. See the reasoning behind that [here](https://github.com/microsoft/TypeScript/pull/18654). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to clarify the functionality with/without the flag, and better define bivariance and contravariance, as most users wouldn't know what those mean (I always forget and have to google it 😅).
With strictFunctionTypes
turned off, both method signatures and function types are considered bivariant in their argument types (meaning that all arguments can be the same, supertypes, or subtypes).
However, with strictFunctionTypes
turned on, method signatures remain bivariant, but function types become contravariant in their argument types (meaning that all arguments must be the same type, or a supertype).
See the reasoning behind that in the typescript PR for the compiler option.
This difference creates a possible safety hole when using method types instead of function types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to overwhelm the user with too much information. I changed this part to be more concise and only nudge the user towards using the more correct option. I think your explanation goes too much into detail. Tell me what you think.
That original paragraph was from TypeScript's FAQ. If the user wants to learn more they still can click the link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mostly LGTM right now.
One change for the fixer.
Thanks!
function getMethodParams( | ||
node: TSESTree.TSMethodSignature | TSESTree.TSFunctionType, | ||
): string { | ||
let params = node.params.map(node => sourceCode.getText(node)).join(', '); | ||
params = `(${params})`; | ||
if (node.typeParameters != null) { | ||
const typeParams = sourceCode.getText(node.typeParameters); | ||
params = `${typeParams}${params}`; | ||
} | ||
return params; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will remove all comments inside the type.
For the most part I don't have problems with a fixer removing comments in weird places if it makes the fixer simpler and easier to maintain.
But one place we should accept and support comments is around parameters, eg:
method(/* leading comment */arg: string): void
So I would suggest converting this to something like:
- grab first open paren token
- grab last end paren token
- grab source code between the two
That way we preserve the "expected" comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just did that, but it only works if there's at least one argument. If the argument list is (/* comment */)
then the comment is lost. The problem is that there's no way to find the paren tokens because the method node's "params" property is just an empty array. This is contrary to "typeParams" which is a separate AST node so I can just getText
it even if it's empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - thanks for doing this
Codecov Report
|
Note - there was a linting change recently which has caused CI failures for your PR. |
The problem is that the rule
|
Ahh fooey. This is an unhandled case in prettier that's fixed in v2.0, but we haven't upgraded to v2 yet (https://prettier.io/blog/2020/03/21/2.0.0.html#fix-printing-of-comments-in-empty-type-parameters-7729httpsgithubcomprettierprettierpull7729-by-sosukesuzukihttpsgithubcomsosukesuzuki) You can get around this for now by adding the example: typescript-eslint/packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts Lines 77 to 82 in 188b689
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
Thanks for this nice check but how are we supposed to handle overloaded methods? I.e.:
This is not valid (duplicate key error by TypeScript). |
You can do it via an intersection type:
Under the hood this is essentially how TS does overloads. |
Closes #1598