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.

Rule proposal: Forbid `...(foo || {})` and `...(foo ?? {})` in object literal

See original GitHub issue

Unlike spread in array literals, spread falsy values in object won’t throw error, and works as expected. There is no need to add empty object {} as fallback.

Fail

const object = {
	foo,
	...(bar || {}),
};
const object = {
	foo,
	...(bar ?? {}),
};

Pass

const object = {
	foo,
	...bar,
};
const array = [
	foo,
	...(bar || []),
];

Not sure about the rule name, prefer-directly-spread? no-useless-empty-object?

Spread falsy value
[
	  0,
	  false,
	  undefined,
      '',
	  NaN,
	  0n,
	  null,
	  document.all, // The only exception, but who care?
].map(value => Object.keys(({...value})).length)

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
sindresorhuscommented, Aug 9, 2021

no-useless-fallback-in-spread?

1reaction
will-stonecommented, Jan 15, 2022

Thank you @papb, that makes sense 🙂 However, I’m sure I noticed it from a real world example of my issue. I’ll turn the rule back on in my shared eslint config and see if I can find the project it affected. If not, then happy days!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to rewrite code to avoid TSLint "object access via string ...
You have a couple options here: 1) Just disable the rule /* tslint:disable:no-string-literal */ whatever.
Read more >
Airbnb JavaScript Style Guide()
1.2 Complex: When you access a complex type you work on a reference to its value. object; array; function. const foo = [ ......
Read more >
Google JavaScript Style Guide
1 Introduction. This document serves as the complete definition of Google's coding standards for source code in the JavaScript programming language.
Read more >
typescript get all properties of object - Lena's Italian Restaurant
Say you have simple object like the following: 1 2 3 4 const obj = { foo: ... if we use object literals...
Read more >
prefer-optional-chain | typescript-eslint
optional chain expressions provide undefined if an object is null or undefined ... this rule also supports converting chained strict nullish checks: foo...
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