Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove curly "Unnecessary { after if/for-of" autofixes from on-save formatter #449

Open
3 tasks done
Dimava opened this issue Apr 12, 2024 · 2 comments
Open
3 tasks done
Labels
enhancement New feature or request

Comments

@Dimava
Copy link
Contributor

Dimava commented Apr 12, 2024

Clear and concise description of the problem

Pretty often I find myself writing

if (foo) {
   bar()
   void 0
}

to avoid ESLint collapsing that to

if (foo)
  bar()

when I save the file

Suggested solution

Move "Unnecessary { after if/for-of" autofixes to on-explicit-format (or on-command-line) formatting so it doesn't autofix while I'm in the middle of writing it

Alternative

Autosaving may sometimes bypass linting, but I prefer my files not to autosave ever in some cases

Additional context

No response

Validations

@Dimava Dimava added the enhancement New feature or request label Apr 12, 2024
@nowo
Copy link

nowo commented Apr 22, 2024

You can add 'curly' : [ 'error' , 'multi-line' , 'consistent' ] ,//unified curly braces to the eslint configuration file,or configure your own rules https://eslint.org/docs/latest/rules/curly

@zanfee
Copy link
Contributor

zanfee commented May 14, 2024

Curly rules are actually the only rules we have adapted for our ESLint config at Bechtle. I'm interested in whether there are any objective advantages to using the short form or if this is a personal preference, as Anthony usually has a good reason for these rules.

Our custom rules are as follows:

rules: {
  'curly': ['error', 'all'],
  'style/brace-style': ['error', 'stroustrup'],
},

And these are the current rules of @antfu/eslint-config (allowSingleLine of the 'style/brace-style' rule is part of eslint-stylistic defaults)

rules: {
  'curly': ['error', 'multi-or-nest', 'consistent'],
  'style/brace-style': ['error', 'stroustrup', { allowSingleLine: true }],
},

Here are the docs for curly and style/brace-style rules.

Basically, our rules ensure that there are always curly braces, even for single-line blocks. In my opinion, this has the following advantages:

  • No need to manually add curly braces when adding a second line to the block
  • Minimal git diff when adding a second line
  • Blocks are easier to identify, less room for error

One drawback of our rules is that the code takes up more space. But you can still write blocks without braces and they will be added automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants