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
Reported:
const headers = {
accept: "application/json",
...(apiKey ? { "x-api-key": apiKey } : {}),
};
const link = <a href={url} {...(external ? { target: "_blank" } : {})}>{label}</a>;
Not reported:
const headers = {
accept: "application/json",
...(apiKey && { "x-api-key": apiKey }),
};
An empty object in the first branch is reported too, without a suggestion:
const query = { page, ...(showAll ? {} : { limit: 20 }) };
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:
const routes = {
home,
...(feature("ADMIN") ? { admin } : {}),
};