8000 Rule proposal: Detect useless default in function parameters and object destructuring · Issue #6106 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

Rule proposal: Detect useless default in function parameters and object destructuring #6106

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

Open
6 tasks done
RunDevelopment opened this issue Nov 27, 2022 · 9 comments
Open
6 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@RunDevelopment
Copy link

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

Default parameters and default values are only used if the parameter or property is undefined (either because its value is undefined or because it is missing). So if we know that a function parameter or property can never be undefined, then we also know that the default is never actually used. This can be used to detect useless defaults.

Fail Cases

// React props destructuring
function Bar({ foo = "" }: { foo: string }) { /* ... */ }

// destructuring assignment
const { foo = "" } = { foo: "bar" }

// default parameter
[1, 2, 3].map((a = 42) => a + 1)

Pass Cases

// React props destructuring
function Bar({ foo }: { foo: string }) { /* ... */ }
function Bar({ foo = "" }: { foo?: string }) { /* ... */ }

// destructuring assignment
const { foo } = { foo: "bar" }

// default parameter
[1, 2, 3].map((a) => a + 1)
[1, 2, 3, undefined].map((a = 42) => a + 1)

Additional Info

This rule requires strict null checks to be enabled.

@RunDevelopment RunDevelopment added enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Nov 27, 2022
@JoshuaKGoldberg

This comment was marked as resolved.

@Josh-Cena

This comment was marked as resolved.

@JoshuaKGoldberg

This comment was marked as resolved.

@RunDevelopment

This comment was marked as resolved.

@bradzacher

This comment was marked as resolved.

@JoshuaKGoldberg

This comment was marked as resolved.

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Nov 27, 2022
@JoshuaKGoldberg
Copy link
Member

Yeah this would be useful, +1. Thanks for filing!

Note that the considerations mentioned in #5748 around making sure users enable strictNullChecks should be followed in this rule.

@ronami
Copy link
Member
ronami commented Oct 4, 2024

Would it make sense for this to be split into two rules (e.g. no-useless-default-parameters and no-useless-default-values)?

These two examples check for unnecessary destructuring default values:

function Bar({ foo = "" }: { foo: string }) { /* ... */ }

const { foo = "" } = { foo: "bar" }

While this is for redundant default function parameters:

[1, 2, 3].map((a = 42) => a + 1)

@bradzacher
Copy link
Member

I don't see a reason you'd want one check but not the other.
I'd probably name it something like no-useless-default-assignment and cover both cases in one rule.
Similarly as I mentioned here

could just expand this to an extension rule for no-useless-assignment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

5 participants
0