Skip to content

Rule proposal: prefer-direct-value-assignment #2675

Description

@BridgeAR

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions