The target
Configure ESLint in the React project and enable save automatic formatting
Train of thought
- Install ESLint in your project
- Create an ESLint configuration file using the init command of esLint
- Set up automatic save formatting for vscode
steps
-
npm i eslint typescript -D
-
In the project root directory, run NPX eslint –init
-
Install plug-ins as prompted
- Choose whether to use TypeScript or not. Select Yes
-
It automatically generates esLint configuration files
-
-
Set up automatic save formatting for vscode
code
In the root directory of the project, add the configuration file.vscode\settings.json, which contains the following contents:
{
"eslint.run": "onType",
"eslint.options": {
"extensions": [".js", ".vue", ".jsx", ".tsx"]
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
Copy the code
Environment Configuration – Importing prettier-now
Eslint does not drill down into JSX code to format, so additional tools are required.
prettier-now
Prettier is an offshoot of the Prettier project and does just as Prettier does, but allows for more configuration items. Prettier-now prettier in vscode, prettier-now prettier
steps
- Install the vscode plug-in
prettier-now
- Added configuration
code
.vscode\settings.json
{ "eslint.run": "onType", "eslint.options": { "extensions": [".js", ".vue", ".jsx", ".tsx"] }, "editor.codeActionsOnSave": { "source.fixAll.eslint": // "editor.formatOnSave": True, // Editor Settings - default prettier now formatting // If prettier is used, DefaultFormatter :"remimarsal. Prettier -now", // indent "prettier. UseTabs ": False, // Indent "prettier. TabWidth "instead of TAB: // If you use Prettier, you cannot select it, causing it to conflict with the default configuration of ESLint // You can find many files in Baidu: https://www.baidu.com/s?wd=prettier%20%E5%87%BD%E6%95%B0%E7%A9%BA%E6%A0%BC "prettier.spaceBeforeFunctionParen": True, / / the react JSX let > with the end tag "prettier. JsxBracketSameLine" : true, "prettier. BracketSpacing" : False, // Remove the space "prettier.semi": false, // do not add; "Prettier.singlequote ": true, // Use single quotes" prettier.trailingcomma ": "None ", // Do not follow comma," prettier.printwidth ": 80, // wrap each row over 80 columns // in.js, write div press TAB to automatically complete "emmet.includeLanguages": {"javascript": "javascript"}}Copy the code
\