noUselessFallbackInSpread (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/noUselessFallbackInSpread - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noUselessFallbackInSpread": "error" } } }}Description
Section titled “Description”Disallow empty object fallbacks when spreading a value into an object.
Spreading null, undefined, or another falsy value (a value treated as false in a
condition, such as false, 0, or "") into an object literal adds no properties.
Writing {...(value || {})} or {...(value ?? {})} therefore gives the same result as
{...value}, and the {} fallback only adds noise.
This doesn’t apply to arrays: spreading null or undefined into an array or a function
call throws an error, so [...(value || [])] is allowed.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const object = { ...(options || {}) };code-block.js:1:30 lint/nursery/noUselessFallbackInSpread FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This empty object fallback is unnecessary.
> 1 │ const object = { ...(options || {}) };
│ ^^^^^
2 │
ℹ Spreading null, undefined, or another falsy value such as false, 0, or "" into an object adds no properties, so the fallback has no effect.
ℹ Remove the fallback and spread the value directly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Remove the empty object fallback.
1 │ const·object·=·{·...(options·||·{})·};
│ - -------
const object = { ...(options ?? {}) };code-block.js:1:30 lint/nursery/noUselessFallbackInSpread FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This empty object fallback is unnecessary.
> 1 │ const object = { ...(options ?? {}) };
│ ^^^^^
2 │
ℹ Spreading null, undefined, or another falsy value such as false, 0, or "" into an object adds no properties, so the fallback has no effect.
ℹ Remove the fallback and spread the value directly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Remove the empty object fallback.
1 │ const·object·=·{·...(options·??·{})·};
│ - -------
const object = { ...options };const withDefaults = { ...(options || { verbose: true }) };const conditional = { ...(enabled && {}) };const array = [...(items || [])];Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.