bun/no-useless-spread-fallback
Disallow an empty object as the fallback in ...(condition ? value : {})
has suggestions
Spreading false, null or undefined into an object does nothing. So the {} in ...(condition ? { a } : {}) isn't needed, and ...(condition && { a }) does the same thing with less code.
The rule checks spreads in object literals and in JSX attributes.
--fix-suggestions rewrites condition ? value : {} to condition && value.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"bun/no-useless-spread-fallback" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"bun/no-useless-spread-fallback" = ["error", {
ignoreConditionsThatCall = [], # list of strings
}]Examples#
example.js1 problem
const headers = {
accept: "application/json",
...(apiKey ? { "x-api-key": apiKey } : {}This empty object is not needed. Spreading false, null or undefined into an object does nothing.),
};example.jsx1 problem
const link = <a href={url} {...(external ? { target: "_blank" } : {}This empty object is not needed. Spreading false, null or undefined into an object does nothing.)}>{label}</a>;example.jsNo problems
const headers = {
accept: "application/json",
...(apiKey && { "x-api-key": apiKey }),
};An empty object in the first branch is reported too, without a suggestion:
example.js1 problem
const query = { page, ...(showAll ? {}This empty object is not needed. Spreading false, null or undefined into an object does nothing. : { limit: 20 }) };example.jsNo problems
const query = { page, ...(!showAll && { limit: 20 }) };Options#
| Option | Default | What it does |
|---|---|---|
ignoreConditionsThatCall | [] | Names of functions. A conditional isn't reported if its condition calls one of them. |
Use it for a function that a build tool only understands as the condition of ? :, such as a feature flag:
example.jswith
{ "ignoreConditionsThatCall": ["feature"] }No problemsconst routes = {
home,
...(feature("ADMIN") ? { admin } : {}),
};