How to enable ESLint autofix on save in vscode settings?
Jul 02, 2025 am 12:16 AM要讓VSCode保存時自動運(yùn)行ESLint修復(fù),需完成以下步驟:1. 確保項(xiàng)目已安裝并配置ESLint;2. 在VSCode中啟用保存時自動修復(fù)功能;3. 設(shè)置ESLint為默認(rèn)格式化工具。首先通過npm或yarn安裝ESLint并創(chuàng)建.eslintrc文件;接著在VSCode設(shè)置中開啟eslint.autoFixOnSave和editor.formatOnSave選項(xiàng);最后將ESLint設(shè)為默認(rèn)格式化工具,可通過右鍵菜單或在settings.json中指定editor.defaultFormatter。正確配置后,保存文件即可自動修復(fù)大部分可糾正的代碼問題。
要讓 VSCode 在保存文件時自動運(yùn)行 ESLint 修復(fù)功能,其實(shí)只需要幾個簡單的設(shè)置步驟。核心在于配置好 .eslintrc
文件,并在 VSCode 的設(shè)置中啟用保存時自動修復(fù)的選項(xiàng)。

確保項(xiàng)目中已正確安裝并配置 ESLint
首先得確認(rèn)你的項(xiàng)目里已經(jīng)安裝了 ESLint,并且有基本的配置。如果還沒裝,可以通過 npm 或 yarn 安裝:

npm install eslint --save-dev # 或者 yarn add eslint --dev
然后創(chuàng)建或檢查項(xiàng)目根目錄下的 .eslintrc
文件,確保里面有一些基礎(chǔ)規(guī)則配置,比如:
{ "env": { "es2021": true, "node": true }, "extends": ["eslint:recommended"], "parserOptions": { "ecmaVersion": 12, "sourceType": "module" }, "rules": {} }
如果沒有這些內(nèi)容,VSCode 就不知道該用什么規(guī)則去修復(fù)代碼。

在 VSCode 中啟用保存時自動修復(fù)
接下來是 VSCode 的設(shè)置部分。打開 VSCode,在設(shè)置界面(可以通過 Ctrl + ,
快捷鍵)中搜索以下兩個關(guān)鍵詞,并勾選它們:
- Eslint: Auto Fix On Save
- Editor: Format On Save
也可以通過 settings.json
文件手動添加配置:
{ "eslint.enable": true, "eslint.autoFixOnSave": true, "editor.formatOnSave": true }
注意:如果你使用 Prettier 和 ESLint 一起工作,可能還需要額外配置
eslint.quiet
或調(diào)整擴(kuò)展優(yōu)先級,避免沖突。
檢查默認(rèn)格式化工具是否為 ESLint
有時候即使設(shè)置了自動修復(fù),也可能因?yàn)槟J(rèn)格式化工具不是 ESLint 而不起作用。可以右鍵點(diǎn)擊任意 JS/JSX 文件,選擇 “Format Document With…” 然后將默認(rèn)格式化工具設(shè)為 ESLint。
或者在 settings.json
中直接指定:
"[javascript]": { "editor.defaultFormatter": "dbaeumer.vscode-eslint" }
這樣就能確保每次保存時真正調(diào)用的是 ESLint 來處理代碼風(fēng)格問題。
基本上就這些。只要 ESLint 配置沒問題,VSCode 設(shè)置也正確,保存時就能自動修復(fù)大部分可糾正的問題了。不復(fù)雜但容易忽略的地方可能是格式化工具沒選對,或者 ESLint 沒有被正確識別為默認(rèn)處理器。
The above is the detailed content of How to enable ESLint autofix on save in vscode settings?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The steps to build a Laravel environment on different operating systems are as follows: 1.Windows: Use XAMPP to install PHP and Composer, configure environment variables, and install Laravel. 2.Mac: Use Homebrew to install PHP and Composer and install Laravel. 3.Linux: Use Ubuntu to update the system, install PHP and Composer, and install Laravel. The specific commands and paths of each system are different, but the core steps are consistent to ensure the smooth construction of the Laravel development environment.

Configuring the Ruby development environment in VSCode requires the following steps: 1. Install Ruby: Download and install from the official website or using RubyInstaller. 2. Install the plug-in: Install CodeRunner and Ruby plug-ins in VSCode. 3. Set up the debugging environment: Install the DebuggerforRuby plug-in and create a launch.json file in the .vscode folder for configuration. This way, you can write, run, and debug Ruby code efficiently in VSCode.

In VSCode, you can use Git for code version fallback. 1. Use gitreset--hardHEAD~1 to fall back to the previous version. 2. Use gitreset--hard to fall back to a specific commit. 3. Use gitrevert to safely fall back without changing history.

Configuring VSCode to synchronize code with GitHub can improve development efficiency and team collaboration. First, install the "GitHubPullRequestsandIssues" and "GitLens" plugins; second, configure the GitHub account; then clone or create a repository; finally, submit and push the code to GitHub.

The steps to manually install the plug-in package in VSCode are: 1. Download the .vsix file of the plug-in; 2. Open VSCode and press Ctrl Shift P (Windows/Linux) or Cmd Shift P (Mac) to call up the command panel; 3. Enter and select Extensions:InstallfromVSIX..., then select .vsix file and install. Manually installing plug-ins provides a flexible way to install, especially when the network is restricted or the plug-in market is unavailable, but attention needs to be paid to file security and possible dependencies.

Best practices for writing JavaScript code in VSCode include: 1) Install Prettier, ESLint, and JavaScript (ES6) codesnippets extensions, 2) Configure launch.json files for debugging, and 3) Use modern JavaScript features and optimization loops to improve performance. With these settings and tricks, you can develop JavaScript code more efficiently in VSCode.

VSCode solves the problems of multilingual project coding and garbled code including: 1. Ensure that the file is saved with correct encoding and use the "redetection encoding" function; 2. Set the file encoding to UTF-8 and automatically detect the encoding; 3. Control whether to add BOM; 4. Use the "EncodingConverter" plug-in to convert encoding; 5. Use the multiple workspace functions to set encoding for different sub-projects; 6. Optimize performance and ignore unnecessary file monitoring. Through these steps, the coding problem of multilingual projects can be effectively dealt with.

I have a lot of experience in participating in VSCode offline technology exchange activities, and my main gains include sharing of plug-in development, practical demonstrations and communication with other developers. 1. Sharing of plug-in development: I learned how to use VSCode's plug-in API to improve development efficiency, such as automatic formatting and static analysis plug-ins. 2. Practical demonstration: I learned how to use VSCode for remote development and realized its flexibility and scalability. 3. Communicate with developers: I have obtained skills to optimize VSCode startup speed, such as reducing the number of plug-ins loaded at startup and managing the plug-in loading order. In short, this event has benefited me a lot and I highly recommend those who are interested in VSCode to participate.
