-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-misused-spread] add new rule no-misused-spread
#7822
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): [no-misused-spread] add new rule no-misused-spread
#7822
Conversation
Thanks for the PR, @lcharlois-neotys! 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. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
no-spread-function
no-spread-function
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 rule's scope is too narrow. We should probably rename it to something like no-misused-spread
, so it can be easily expanded to support spreading arrays and objects without any data properties.
no-spread-function
no-misused-spread
meta: { | ||
docs: { | ||
description: 'Disallow spread operator on function', | ||
recommended: 'stylistic', |
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.
hmm
is it stylistic?
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.
What will be your recommendation?
Should it be strict
?
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.
TBH I'm not sure what the guidelines are, let's wait for a maintainer
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.
https://typescript-eslint.io/linting/configs - this is a functional, bug-catching rule so it wouldn't be 'stylistic'
. It would be 'strict'
for now to avoid a breaking change. In each new next major version we look at which rules might be promoted to 'recommended'
.
'Spreading a function is almost always a mistake. Did you forgot to call the function?', | ||
}, | ||
schema: [], | ||
type: 'suggestion', |
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 think this is a "problem" rather than a "suggestion", WDYT?
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.
IMO this is most of the time a problem, but untill today there is nothing warning the user that it is one.
So I guess this is an expected usage and I choose suggestion
rather than problem
.
I can change it if needed.
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.
Not sure...
let's wait for a maintainer I guess?
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.
It's a 'problem'
. Anything that catches directly incorrect logic is a problem.
` | ||
const a = () => ({ value: 33 }); | ||
const b = ({ value: number }) => ({ value: value }); | ||
`, |
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.
where is the spread here?
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 think it is a copy/paste from the previous in order to test another case, but honestly I don't remember since last time I worke on this PR.
I remove it.
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.
maybe also add tests for spreading Map
, Set
, and a class instance?
EDIT: hmm, should we include a custom iterable as well? might be overkill, IDK
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.
Do you mean in the valid part?
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.
yup
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.
+1 on adding a test for a custom iterable :)
name: 'no-misused-spread', | ||
meta: { | ||
docs: { | ||
description: 'Disallow spread operator on function', |
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.
should probably be updated to a more generic "no-spread" description
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've proposed a change.
If you've used some of @OliverJAsh's code from this comment, consider adding him as a co-author 😄 |
Co-authored-by: StyleShit <32631382+StyleShit@users.noreply.github.com>
I will be happy. Could you tell me how to add co-author? |
Co-authored-by: StyleShit <32631382+StyleShit@users.noreply.github.com>
Co-authored-by: StyleShit <32631382+StyleShit@users.noreply.github.com>
Co-authored-by: StyleShit <32631382+StyleShit@users.noreply.github.com>
You need to add "Co-authored-by: {{userName}}<{{userEmail}}>" in the commit description. You can either do it on merge (but you're not gonna be the merger, so that's not an option), or add a commit with this description and it'll automatically be appended when the PR is merged. |
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.
looks good to me overall except for a small nitpicking, and maybe add the tests we've discussed earlier
anyway, I'm not a maintainer, just stumbled across this PR, so let's wait for one 😄
@@ -97,6 +97,7 @@ export = { | |||
'@typescript-eslint/no-misused-new': 'error', | |||
'@typescript-eslint/no-misused-promises': 'error', | |||
'@typescript-eslint/no-mixed-enums': 'error', | |||
'@typescript-eslint/no-misused-spread': 'error', |
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.
[Nit]
This line should probably need to be one line above
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.
Looking good! Requesting expanding and a few small cleanups, but this is a great start 🙂
meta: { | ||
docs: { | ||
description: | ||
"Disallow spread operator that shouldn't be spread most of the time", |
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.
[Proofreading] "most of the time" is kind of an imprecise, vague statement. Users may not understand it.
Instead, let's speak to what this rule is specifically trying to avoid: spreads that don't do anything.
"Disallow spread operator that shouldn't be spread most of the time", | |
"Disallow using the spread operator on values that can't be spread", |
type: 'suggestion', | ||
}, | ||
create: (context): TSESLint.RuleListener => { | ||
const listener = (node: TSESTree.SpreadElement): void => { |
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.
[Style] Is there a reason you'd wanted to split this out instead of having it inline at the bottom of the function? Inline would mean an inferrable type for node
:
return {
SpreadElement(node): void {
// ...
}
};
const checker = services.program.getTypeChecker(); | ||
|
||
const tsNode = services.esTreeNodeToTSNodeMap.get(node.argument); | ||
const type = checker.getTypeAtLocation(tsNode); |
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.
[Cleanup] We have a new thing for this 😄 https://typescript-eslint.io/blog/announcing-typescript-eslint-v6#type-checker-wrapper-apis
const checker = services.program.getTypeChecker(); | |
const tsNode = services.esTreeNodeToTSNodeMap.get(node.argument); | |
const type = checker.getTypeAtLocation(tsNode); | |
const type = services.getTypeAtLocation(node.argument); |
}, | ||
messages: { | ||
forbiddenFunctionSpread: | ||
'Spreading a function is almost always a mistake. Did you forget to call the function?', |
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.
[Proofreading] Maybe...
'Spreading a function is almost always a mistake. Did you forget to call the function?', | |
'Spreading a function with no custom properties does nothing. Did you forget to call the function?', |
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
forbiddenFunctionSpread: |
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.
[Feature] If we add the rule as-is (only looking at functions), it'd be a breaking change to later add in Maps, Sets, etc. I think the rule should include all of what's requested in the issue to start.
LMK if any of them give you grief, happy to help!
👋 Ping @lcharlois-neotys, is this still something you have time for? No worries if not - I just don't want to leave it hanging. |
Yes, I just need a bit of time to reread all the comments and do the changes. |
Closing this PR as it's been stale for a while without activity. cc @lcharlois-neotys - if you want to keep working on it, that'd be swell. But no worries if you don't have time. If anybody wants to drive it forward, please do post your own PR - and if you use this as a start, consider adding a co-author attribution at the end of your PR description. Thanks! 😊 |
PR Checklist
Overview
Partially addresses #748.
Add a new rule that prevent spreading a function.