eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Add `switch-braces` rule

Open Richienb opened this issue 2 years ago • 9 comments

Fixes #1702

Richienb avatar Jan 30 '22 08:01 Richienb

The fixer will require some tweaks to get it working nicer with both multiline and single-line expressions. Also some more tests may help.

Richienb avatar Jan 30 '22 08:01 Richienb

The files for no-array-for-each and no-useless-spread are both triggering the rule so that part seems to work

Richienb avatar Jan 30 '22 08:01 Richienb

You may misunderstood, the braces creates a new scope, when there are variable declare, braces are required. Correct me if I'm wrong.

fisker avatar Jan 31 '22 01:01 fisker

Yes, you are correct. The way MDN describes it makes it sound like more of an inconvenience but this will probably make automatic fixing improbable.

Richienb avatar Jan 31 '22 01:01 Richienb

My personal opinion, we should only report those cases that braces can be removed. No option alway and never.

fisker avatar Jan 31 '22 06:01 fisker

Stylistically speaking, we'd only want for all blocks to either have braces or blocks.

Richienb avatar Jan 31 '22 06:01 Richienb

I personally think braces should always be used for consistency and safety.

sindresorhus avatar Feb 04 '22 18:02 sindresorhus

The rule is built to allow for either always braces (default) or never braces to allow for differing opinions. The whole context thing adds extra steps since variables defined outside of break statements can't be re-defined within them unless they are block statements which makes fixing a manual thing.

Richienb avatar Feb 05 '22 03:02 Richienb

@Richienb Still interested in finishing this?

sindresorhus avatar Jun 30 '22 22:06 sindresorhus