Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup self-closing when I save code on VSCode with prettier and ESLint?

I use React and VSCode, I wanna setup self-closing when code is saved but I don't know how...

What I wanna do is

<Hello></Hello>

after I save code

<Hello />

Where can I setup self closing?

Thank you.

like image 527
Pytan Avatar asked Sep 16 '19 18:09

Pytan


People also ask

How do I fix ESLint on Save VSCode?

If you have the ESLint extension installed you can use CTRL+SHIFT+P to open the Command Palette. Then search for ESLint: Fix all auto-fixable Problems and press ENTER (or RETURN ).

Can you use ESLint and prettier in VSCode?

Open the terminal in your project root folder and install eslint as a dev dependency. We also need to enable the eslint and prettier extension for the VSCode. So visit the extensions section of VSCode (ctrl + shift + x) and search for Eslint and Prettier — Code formatter and install it.

How do I change VSCode ESLint settings?

Configure VSCode Settings to use ESLint for Formatting Click that tiny icon in the top-right that looks like a piece of paper with a little arrow. The first one turns on ESLint for formatting, and the next 3 make it do the formatting when you hit save. That should do it! Save the settings file and close it, we're done.

Does prettier conflict with ESLint?

Prettier runs as a plugin of ESLint and thanks to the special configuration it won't conflict with it.


2 Answers

You need to enable the related rule and make sure VSCode is integrated to fix lint warnings/errors on saving.

  1. Enable react/self-closing-comp rule:
// eslint config file (package.json / eslintrc / settings.json etc.)
{
  ...
  "rules": {
    "react/self-closing-comp": "error"
  }
}
  1. Within settings.json at VSCode make sure you got auto-fix enabled (for example with vscode-eslint extension, it may be any other lint extension related):
// settings.json @ VSCode
{ 
...
  "eslint.autoFixOnSave": true,
  "eslint.run": "onSave",
}

Refer to eslint-plugin for vscode for integration.

Note that eslint-config-airbnb enables it by default (I suggest using any config).

like image 92
Dennis Vash Avatar answered Oct 27 '22 12:10

Dennis Vash


Add this ti vscode config:

vscode settings.json:

"editor.codeActionsOnSave": {
  "source.fixAll.eslint": true
},
like image 38
Serge Nikolaev Avatar answered Oct 27 '22 13:10

Serge Nikolaev