当前位置:网站首页>In vscade, how to use eslint to lint and format
In vscade, how to use eslint to lint and format
2022-06-29 07:05:00 【ifredom】
stay Vscode in , How to use eslint Come on lint and format
This tutorial has been used Visual Studio Code v1.63.2、ESLint Extension v2.2.2、Node.js v17.2.0、npmv8.1.4 and eslintv8.3.0 It was verified .
The first 1 Step - establish JavaScript Starting code
1. Create a directory folder
$ mkdir vscode-eslint-example
- Enter the directory folder
$ cd vscode-eslint-example
- stay vscode-eslint-example Inside the catalog , Create a JavaScript file app.js:
$ touch app.js
4. stay Visual Studio Code Open in app.js. Write the following code in the file :
const name = 'James'
const person = {
first: name}
console.log(person)
const sayHelloLinting = (fName) => {
console.log(`Hello linting, ${
fName}`);
};
You may have noticed that there are formatting problems that can be improved :
- Inconsistent use of quotation marks
- Inconsistent semicolon use
- spacing
- Now you can initialize this project . Navigate back to... On the command line vscode-eslint-example Catalog , Then run the following command :
$ npm init --yes

Now? JavaScript The test item has been set correctly , You can start setting ESLint.
The first 2 Step - Set up ESLint
- Setting for the project ESLint Before , First, install in the project directory ESLint:
$ npm install eslint --save-dev
The --save-dev Signs are important , Because this saves the package as a development only dependency , Once the project enters the production environment ,eslint No longer need
- Now? ESLint already installed , You can use the following command to initialize your project ESLint To configure ( Navigate to... Under the project root directory node_modules/.bin In the middle ):
$ ./node_modules/.bin/eslint --init
Next , Different questions about the project will be prompted . These questions are asked to ensure that the configuration initialized for the project is best suited to your own needs .
- The first tip will be , choice
To check syntax, find problems, and enforce code styleOptions .
? How would you like to use ESLint? …
To check syntax only
To check syntax and find problems
* To check syntax, find problems, and enforce code style
- The second tip will be , choice
CommonJSUse CommonJS Options for global variables .
What type of modules does your project use? …
JavaScript modules (import/export)
* CommonJS (require/exports)
None of these
- The next tip will say , choice
None of theseOptions :
? Which framework does your project use? …
React
Vue.js
* None of these
- The next prompt will ask , choice
NoOptions :
? Does your project use TypeScript? › No / Yes
- The following prompt will say , choice
BrowserOptions :
? Where does your code run? … (Press <space> to select, <a> to toggle all, <i> to invert selection)
Browser
Node
- The next tip will say , choice
Use a popular style guideOptions :
( about **Which style guide do you want to follow?** Tips , choiceAirbnb: https://github.com/airbnb/javascriptOptions )
How would you like to define a style for your project? …
* Use a popular style guide
Answer questions about your style
Inspect your JavaScript file(s)
- The next prompt will ask , choice
JSONOptions .:
? What format do you want your config file to be in? …
JavaScript
YAML
* JSON
- Then you will see this message :
Checking peerDependencies of [email protected]
The config that you've selected requires the following dependencies:
[email protected] [email protected]^7.32.0 || ^6.8.0 || ^7.2.0 [email protected]^2.25.2
- The last tip will ask , choice
YesOptions for installing dependencies npm:
? Would you like to install them now with npm? › No / Yes
After all prompts are completed , You will notice a file named .eslintrc.json Has been added to your vscode-eslint-example Directory . Now installed ESLint. The code in app.js Not changed . This is because ESLint Need and Visual Studio Code Integrate .
The first 3 Step - install ESLint Expand
stay Visual Studio Code Install in ESLint after , Be careful app.js Colored underscores in the file highlight errors . These markings are color coded according to severity . If you hover over underlined code , You will see a message explaining the error to you . In this way ,ESLint It can help us find and remove code and syntax errors .

ESLint Can do more for you . You can modify ESLint To automatically fix errors each time a file is saved .
The first 4 Step - Format on save
To put ESLint It is configured to automatically fix syntax and formatting problems every time it is saved , You need to open the setup menu .
To be in Visual Studio Code Find settings in , Please use the command panel to open Preferences: Open Workspace Settings (JSON).
# .vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.validate": ["javascript"]
}
Okay , Now? ESLint The plug-in will now automatically correct the error and verify on save JavaScript.
return app.js File and save . You will see some changes , Include less colored underscores .ESLint Some formatting issues that have been fixed include :
- Use single quotation marks consistently
- Appropriate indentation inside the function
- Use semicolons consistently

ESLint Syntax errors will now be automatically resolved when you save app.js. There are some remaining error messages . These can be customized ESLint Configure to catch or ignore specific errors and formatting problems to fix .
The first 5 Step - Customize ESLint The rules
This step is no longer necessary .
In mode ,console.log Statements are usually required . You can customize ESLint To configure , To allow the use of console.log Statement without generating an error message .ESLint Configuration rules can be set in .eslintrc.json Modification in file .
open .eslintrc.json file . You will see in this file
These codes :
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"extends": ["airbnb-base"],
"parserOptions": {
"ecmaVersion": 12
},
"rules": {
}
}
stay .eslintrc.json At the bottom of the file , You will see a "rules" object . Here you can customize the trigger ESLint Error or disable ESLint Response to some code , You will "rules" Object to add key value pairs . This key will match the name of the rule you want to add or change . This value will match the severity level of the problem . You can choose three severity levels :
- error- Produce a red underline
- warn- A yellow underline will be generated
- off- It doesn't show anything .
such as : If you don't want to use console.log Statement generates any error messages , You can use no-console Rule name as key . Input off by Value no-console.
In the file .eslintrc.json Add :
"rules" : {
"no-console": "off"
}
app.js Will no longer produce console.log Error message :

边栏推荐
- 把多个ROC曲线画在一张图上
- Object detection - VIDEO reasoning using yolov6
- Effective methods for construction enterprises to select smart construction sites
- QT foreach keyword
- Idea common plug-ins
- 二叉树的迭代法前序遍历的两种方法
- Illustrate plug-in -- AI plug-in development -- creative plug-in -- astute graphics -- multi axis mirroring function
- Move disassembly of exclusive delivery of script (the first time)
- Method of changing host name (permanent)
- NoSQL数据库之Redis(二):Redis配置文件介绍
猜你喜欢

Delete tag

Chapter IV introduction to FPGA development platform

Draw multiple ROC curves on a graph

Qt 程序打包发布-windeployqt工具

Error: GPG check FAILED Once install MySQL

Suggestions on digital transformation of large chemical enterprises

package. Are you familiar with all configuration items and their usage of JSON

Message queue batch processing refund order through queue

RedisTemplate处理hash整数类型的问题解析

开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
随机推荐
Json对象和Json字符串的区别
Service grid ASM year end summary: how do end users use the service grid?
多线程工具类 CompletableFuture
QT qframe details
CI工具Jenkins之二:搭建一个简单的CI项目
CI工具Jenkins安装配置教程
NoSQL数据库介绍
VerilogA - dynamic comparator
Message queue batch processing refund order through queue
关于DDNS
消息队列之通过队列批处理退款订单
Redis de la base de données nosql (v): redis Jedis Tests
NoSQL数据库之Redis(一):安装 & 简介
转:侯宏:企业数字化转型的关键不是技术,而是战略
QT serial port programming
How to fix Error: Failed to download metadata for repo ‘appstream‘: Cannot prepare internal mirrorli
多模态 —— Learnable pooling with Context Gating for video classification
Go basic data type conversion
Ribbon service invocation and load balancing
Aging design guide for applets