Skip to content

Rule proposal: simple-condition-first #2853

@fisker

Description

@fisker

Description

When writing multiple conditions, the simple one should come first.

I believe this can improve performance. The direct boolean check and variable ===/!== comparison are obviously cheaper.

Examples

// ❌
if (check(foo) && bar);

// ✅
if (bar && check(foo));
// ❌
if (foo.bar.baz === 1 && bar === 2);

// ✅
if (bar === 2 && foo.bar.baz === 1);

Proposed rule name

simple-condition-first

Additional Info

There is an internal rule in Prettier codebase checking a particular case https://github.com/prettier/prettier/blob/46061a160f7a6726d11d047647d59036435a3279/scripts/tools/eslint-plugin-prettier-internal-rules/test.js#L39-L44 , but it will be great to have a generic rule.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions