Description
I have stumbled upon code a couple of times that first assigns an array and then pushes values unconditionally. That is not ideal, since it requires the array to potentially resize right away and change its internal shape, while also making it less readable and more verbose.
Instead, just assign the values directly.
The same applies to objects with properties.
Examples
// ❌
const foo = {}
foo.array = []
const random = Math.random()
foo.array.push(42)
// ✅
const foo = {
array: [42]
}
const random = Math.random()
Proposed rule name
prefer-direct-value-assignment
prefer-concise-values
Additional Info
No response
Description
I have stumbled upon code a couple of times that first assigns an array and then pushes values unconditionally. That is not ideal, since it requires the array to potentially resize right away and change its internal shape, while also making it less readable and more verbose.
Instead, just assign the values directly.
The same applies to objects with properties.
Examples
Proposed rule name
prefer-direct-value-assignment
prefer-concise-values
Additional Info
No response