簡介
ESLint 和 Prettier
ESLint:代碼質量檢查工具,確保代碼風格一致與無錯誤
Prettier:代碼格式化工具,自動美化代碼布局
所以:ESLint + Prettier == 能自動美化代碼、自動檢查代碼錯誤的工具
Biome
Biome:集代碼檢查、代碼美化于一體的”高性能“的工具
所以:Biome > ESLint + Prettier
對比
ESLint 和 Prettier 配置復雜,但生態成熟、對應資料多
Biome 配置相對簡單、性能好,但生態尚未成熟、對應資料少
截至2025年2月19日,Biome 最新版本為 1.9.4
官網
Biome 官網鏈接:https://biomejs.dev/
簡單教程
注:這里只是簡單演示,如果你的項目和下面不匹配,請前往官網查看詳細的文檔教程
- 在你的項目運行下面命令,安裝 biome
npm install --save-dev --save-exact @biomejs/biome
- 安裝 VS Code 插件
- 在你的項目的根目錄下,找到配置文件 biome.json,根據你的需求修改文件內容即可(怎么修改?建議去官網查看配置屬性。而對于比較懶的朋友,可參考我的個人開發規范,基本上常用的配置都在這里了,復制粘貼到你項目修改即可。另外,下面配有它的注釋版,有看不懂的朋友,可以看看其注釋)
無注釋版
{"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json","vcs": {"enabled": false,"clientKind": "git","useIgnoreFile": false},"files": {"ignore": ["dist/*", "node_modules/*", ".vscode/*"],"ignoreUnknown": true},"organizeImports": {"enabled": false},"formatter": {"enabled": true,"indentStyle": "space","indentWidth": 2,"lineWidth": 80,"lineEnding": "lf","bracketSpacing": true},"javascript": {"formatter": {"semicolons": "always","quoteStyle": "single","trailingCommas": "none","arrowParentheses": "always"}},"linter": {"enabled": true,"rules": {"style": {"noVar": "error","useBlockStatements": "error","useConst": "error","useFilenamingConvention": {"level": "error","options": {"strictCase": true,"requireAscii": true,"filenameCases": ["PascalCase"]}},"useNamingConvention": {"level": "error","options": {"strictCase": true,"requireAscii": true,"conventions": [{"selector": { "kind": "const", "scope": "global" },"formats": ["CONSTANT_CASE"]}]}}},"performance": {"noReExportAll": "warn"},"suspicious": {"noDoubleEquals": "error","noDuplicateAtImportRules": "error"},"complexity": {"noExcessiveCognitiveComplexity": "error"},"correctness": {"noUnusedImports": "warn"}}}
}
含注釋版
{"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json", // 指定 Biome 配置文件的 JSON Schema,用于驗證配置文件的結構和內容"vcs": {"enabled": false, // 禁用版本控制系統(VCS)集成"clientKind": "git", // 設置 VCS 客戶端類型為 Git"useIgnoreFile": false // 禁用使用 Git 忽略文件},"files": {"ignore": ["dist/*", "node_modules/*", ".vscode/*"], // 忽略指定的文件和文件夾"ignoreUnknown": true // 忽略未知文件類型},"organizeImports": {"enabled": false // 禁用自動導入排序功能},"formatter": {"enabled": true, // 啟用代碼格式化功能"indentStyle": "space", // 設置縮進樣式為空格"indentWidth": 2, // 設置縮進寬度為 2 個空格"lineWidth": 80, // 設置每行最大寬度為 80 個字符"lineEnding": "lf", // 設置行結束符為 LF(換行符)"bracketSpacing": true // 在對象字面量的大括號之間添加空格},"javascript": {"formatter": {"semicolons": "always", // 始終在語句末尾添加分號"quoteStyle": "single", // 使用單引號表示字符串"trailingCommas": "none", // 不添加尾隨逗號"arrowParentheses": "always" // 始終在箭頭函數的參數周圍添加括號}},"linter": {"enabled": true, // 啟用代碼檢查功能"rules": {"style": {"noVar": "error", // 禁止使用 var 聲明變量"useBlockStatements": "error", // 強制使用塊級語句(即:不能省略花括號,比如if只有一句)"useConst": "error", // 強制使用 const 聲明常量(針對代碼中只用了一次的變量)"useFilenamingConvention": {"level": "error", // 設置文件命名約定規則的診斷級別為錯誤"options": {"strictCase": true, // 強制嚴格的大小寫規則,true是禁止連續大寫,反之"requireAscii": true, // 強制文件名使用 ASCII 字符,比如:無法使用中文命名"filenameCases": ["PascalCase"] // 強制文件名使用 PascalCase 命名風格}},"useNamingConvention": {"level": "error", // 設置命名約定規則的診斷級別為錯誤"options": {"strictCase": true, // 強制嚴格的大小寫規則,true是禁止連續大寫,反之"requireAscii": true, // 強制使用 ASCII 字符,比如:無法使用中文命名"conventions": [{"selector": { "kind": "const", "scope": "global" }, // 對于 const,在全局范圍內(強制全局常量使用 CONSTANT_CASE 命名風格)"formats": ["CONSTANT_CASE"] // 強制全局常量使用 CONSTANT_CASE 命名風格}]}}},"performance": {"noReExportAll": "warn" // 禁止導出所有內容,降低資源消耗,比如 import *},"suspicious": {"noDoubleEquals": "error", // 禁止使用雙等號(==)進行比較,只能用三等號 (===),null除外"noDuplicateAtImportRules": "error" // 禁止在導入規則中出現重復的 import 語句},"complexity": {"noExcessiveCognitiveComplexity": "error" // 禁止過高的認知復雜度,比如嵌套超過15個if-else語句},"correctness": {"noUnusedImports": "warn" // 禁止存在未使用的導入,即:不能導入后不用}}}
}