Empty Destructuring Pattern
ID |
javascript.no_empty_pattern |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
reliability, suspicious-destructuring |
Description
Reports destructuring patterns that bind no names — const {} = foo;, const [] = bar;, or function parameters such as function f({}) {} and function g([]) {}. An empty pattern declares nothing and is almost always a typo: the author usually meant to provide a default value (= {} / = []) rather than an empty pattern ({} / []).
An empty object or array literal used as a default value, e.g. const {a = {}} = obj;, is parsed as an ObjectLiteral / ArrayLiteral, not as a binding pattern, and is not reported.
Rationale
-
const {} = expr;runsexprfor its side effects (and throws ifexprisnullorundefined) but binds nothing — the developer’s intent is unclear. -
function f({}) {}enforces only that the argument is destructurable, with no names extracted; this is rarely what the author wanted. -
The most common cause is a misplaced
=— e.g. writingfunction f({} = {})when the intended default object had property defaults.
// Bad
const {} = foo; // declares nothing
const [] = bar; // declares nothing
function f({}) {} // parameter pattern declares nothing
function g([]) {} // same
let {a: {}} = nested; // outer binds 'a' but inner pattern is empty
Remediation
If a default value was intended, write the literal on the right of an initializer instead of inside the pattern. If the pattern was meant to bind something, add the missing binding identifiers.
// Good
const {a = {}} = obj; // 'a' defaults to {} when missing
const [b = []] = arr; // 'b' defaults to [] when missing
const {c} = obj; // bind 'c'
const [d] = arr; // bind 'd'
function h({a, b}) {} // bind 'a' and 'b'