Skip to content

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

Conversation

lcharlois-neotys
Copy link

@lcharlois-neotys lcharlois-neotys commented Oct 23, 2023

PR Checklist

Overview

Partially addresses #748.
Add a new rule that prevent spreading a function.

@typescript-eslint
Copy link
Contributor

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.

@netlify
Copy link

netlify bot commented Oct 23, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 4aed59b
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/655d9e8a567913000824c5de
😎 Deploy Preview https://deploy-preview-7822--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 8 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@lcharlois-neotys lcharlois-neotys changed the title feat(eslint-plugin): [no-spread-function] Add new rule no-spread-function feat(eslint-plugin): [no-spread-function] add new rule no-spread-function Oct 23, 2023
Copy link
Member

@Josh-Cena Josh-Cena left a 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.

@lcharlois-neotys lcharlois-neotys changed the title feat(eslint-plugin): [no-spread-function] add new rule no-spread-function feat(eslint-plugin): [no-misused-spread] add new rule no-misused-spread Oct 25, 2023
@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Nov 10, 2023
meta: {
docs: {
description: 'Disallow spread operator on function',
recommended: 'stylistic',
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm
is it stylistic?

Copy link
Author

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?

Copy link
Contributor

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

Copy link
Member

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',
Copy link
Contributor

@StyleShit StyleShit Nov 21, 2023

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?

Copy link
Author

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.

Copy link
Contributor

@StyleShit StyleShit Nov 22, 2023

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?

Copy link
Member

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.

Comment on lines 68 to 71
`
const a = () => ({ value: 33 });
const b = ({ value: number }) => ({ value: value });
`,
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

@StyleShit StyleShit Nov 21, 2023

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

Copy link
Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

yup

Copy link
Member

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',
Copy link
Contributor

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

Copy link
Author

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.

@StyleShit
Copy link
Contributor

StyleShit commented Nov 21, 2023

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>
@lcharlois-neotys
Copy link
Author

If you've used some of @OliverJAsh's code from this comment, consider adding him as a co-author 😄

I will be happy. Could you tell me how to add co-author?
Is adding something in the PR description enough?

@StyleShit
Copy link
Contributor

If you've used some of @OliverJAsh's code from this comment, consider adding him as a co-author 😄

I will be happy. Could you tell me how to add co-author? Is adding something in the PR description enough?

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.

See:
https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors

Copy link
Contributor

@StyleShit StyleShit left a 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',
Copy link
Contributor

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

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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",
Copy link
Member

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.

Suggested change
"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 => {
Copy link
Member

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 {
    // ...
  }
};

Comment on lines +27 to +30
const checker = services.program.getTypeChecker();

const tsNode = services.esTreeNodeToTSNodeMap.get(node.argument);
const type = checker.getTypeAtLocation(tsNode);
Copy link
Member

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

Suggested change
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?',
Copy link
Member

Choose a reason for hiding this comment

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

[Proofreading] Maybe...

Suggested change
'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:
Copy link
Member

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!

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Dec 5, 2023
@JoshuaKGoldberg
Copy link
Member

👋 Ping @lcharlois-neotys, is this still something you have time for? No worries if not - I just don't want to leave it hanging.

@JoshuaKGoldberg JoshuaKGoldberg added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Dec 29, 2023
@lcharlois-neotys
Copy link
Author

👋 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.
I will try next week.

@JoshuaKGoldberg JoshuaKGoldberg removed the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Jan 11, 2024
@bradzacher bradzacher added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label Jan 27, 2024
@JoshuaKGoldberg
Copy link
Member

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! 😊

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party enhancement: new plugin rule New rule request for eslint-plugin stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants