question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule proposal: no-undef-default

See original GitHub issue

Please describe what the rule should do:

Disallows using global undefined as a default value in destructuring patterns.

Optionally, disallows the same for function parameters.

Has autofix for both (with some exceptions for params).

What category of rule is this? (place an “X” next to just one item)

[X] Suggests an alternate way of doing something (suggestion)

Provide 2-3 code examples that this rule will warn about:

Destructuring:

/* eslint no-undef-default: ["error"]*/

var { a = undefined } = foo;

let { bar: b = undefined } = foo;

const [c = undefined] = foo;

({ d = undefined } = foo);

[e = undefined] = foo;

Auto-fixed to:

/* eslint no-undef-default: ["error"]*/

var { a } = foo;

let { bar: b } = foo;

const [c] = foo;

({ d } = foo);

[e] = foo;

Function params:

/* eslint no-undef-default: ["error", { enforceForParams: true }]*/

function foo(a, b = undefined, c = undefined) {
}

const bar = (d, e = 1, f = undefined) => {
}

Auto-fixed to:

/* eslint no-undef-default: ["error", { enforceForParams: true }]*/

function foo(a, b = undefined, c) { // just warning without autofix for `b`
}

const bar = (d, e = 1, f) => {
}

Autofix never removes a param default when it’s the first default in the list because it would automatically change function’s length. Also, it could turn a non-simple param list into a simple param list, I’m not sure could that also change some behavior.

Why should this rule be included in ESLint (instead of a plugin)?

It’s a best practice to avoid unnecessary use of undefined, and it’s completely unnecessary in destructuring patterns. Removing undefined improves readability and understanding on how destructuring works.

It could be used in functions to control length, that’s why it’s behind an option (default is false).

There is already a similar rule, no-undef-init.

I’m not sure about the name, should it be no-undef-default or no-undef-defaults.

Are you willing to submit a pull request to implement this rule?

Yes.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ilyavolodincommented, Oct 4, 2019

Not sure this rises to the level of inclusion into the core. I haven’t seen a code like that before. While I think the rule is valid, it seems to me that it only covers an extreme edge case.

0reactions
ljharbcommented, May 25, 2020

What a really unfortunate decision 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Proposed Rules
SEC Proposed Rules · Regulation NMS: Minimum Pricing Increments, Access Fees, and Transparency of Better Priced Orders · File No: S7-30-22 · Comments...
Read more >
Proposed Rules - GovInfo
Administration (FDA or Agency) is proposing to amend its regulations to modernize, simplify, and enhance the current system for oversight of ...
Read more >
Self-Regulatory Organizations; The Nasdaq Stock Market LLC
The Exchange proposes to extend Nasdaq's program providing Eligible Companies with complimentary board recruiting services.
Read more >
Proposed Amendments Published for Public Comment
Appellate Rules 32, 35, 40, and Appendix on Length Limits · Bankruptcy Restyled Rules Parts VII to IX; Rules 1007, 4004, 5009, 7001,...
Read more >
SEC Proposes Changes to the Fund Names Rule
These and other aspects of the Proposals are discussed in detail below. I. New 80% Investment Policy Requirement – Names Suggesting an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found