この記事の内容
実際に
ESLintと Biomeに よる JavaScript静的解析
JavaScriptを
まずは、
コード品質は、
// Convention of preferred code formatting and quality
function add(a, b){
return a + b;
}
// An example for what would be considered as
// poor code formatting, making it hard to read
// and error-prone
function add(c,d){return c+d;}JavaScriptエコシステムには、
BiomeとESLintの概要
ESLintは
ESLintは
JavaScriptプロジェクトを.eslintrc.jsファイルを
// .eslintrc.js
module.exports = {
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "double"]
}
};一方、biome.jsonファイルの
{
"javascript": {
"formatter": {
"indentWidth": 2,
"lineWidth": 100,
"trailingComma": "none",
"semicolons": "always",
}
}
}現在、
PrettierからBiomeへの移行と、コード品質に注力するESLint
これまで、

だからと
Biomeも
JavaScriptプロジェクトでBiomeを使う
Biomeは
JavaScriptで
# Install Biome as a development dependency
npm install --save-dev @biomejs/biomeインストール時には、
# Initialize a new Biome configuration file
npx @biomejs/biome initこれにbiome.json設定ファイルが
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}ご覧の.prettierrc.jsファイルは
module.exports = {
printWidth: 100,
tabWidth: 2,
singleQuote: true,
semi: false,
trailingComma: 'none',
useTabs: false,
bracketSpacing: false
}
Biomeをbiome.jsonファイルに
{
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 100,
"quoteStyle": "single",
"trailingComma": "none",
"semicolons": "asNeeded",
"bracketSpacing": true,
"arrowParentheses": "asNeeded"
}
}
これで、
npx @biomejs/biome format ./srcその
./src/Utils.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Formatter would have printed the following content:
2 2 │
3 3 │ module.exports = {
4 │ - ··parseUrl:·function(urlToScan)·{
4 │ + ··parseUrl:·function·(urlToScan)·{
5 5 │ if (urlToScan === undefined) return urlToScan
6 6 │
····· │
12 12 │ return urlToScan
13 13 │ },
14 │ - ··trimUtmParams:·function(urlToTrim)·{
14 │ + ··trimUtmParams:·function·(urlToTrim)·{別の
Biomeでフォーマットとlintを組み合わせる
Biomeにはbiome.json設定ファイルで
Biomeのbiome checkコマンドをbiome.jsonで
# Run Biome on your code
npx @biomejs/biome check ./srcBiomeを
./src/Audit.js:30:7 lint/complexity/useOptionalChain FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Change to an optional chain.
29 │ if (
> 30 │ vulnerableAudit.details &&
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
> 31 │ vulnerableAudit.details.items &&
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32 │ vulnerableAudit.details.items.length > 0
33 │ ) {
ℹ Unsafe fix: Change to an optional chain.
28 28 │
29 29 │ if (
30 │ - ······vulnerableAudit.details·&&
31 │ - ······vulnerableAudit.details.items·&&
30 │ + ······vulnerableAudit.details?.items·&&
32 31 │ vulnerableAudit.details.items.length > 0
33 32 │ ) {
./src/RenderJson.js:27:7 lint/complexity/noForEach ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Prefer for...of instead of forEach.
25 │ vulnerabilitiesResults.details.items.length > 0
26 │ ) {
> 27 │ vulnerabilitiesResults.details.items.forEach(vulnItem => {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 28 │ vulnerabilitiesCount += vulnItem.vulnCount
> 29 │ const vulnInfo = this.formatVulnerability(vulnItem)
> 30 │ vulnerabilities.push(vulnInfo)
> 31 │ })
│ ^^
32 │ }
33 │
ℹ forEach may lead to performance issues when working with large arrays. When combined with functions like filter or map, this causes multiple iterations over the same type.
The number of diagnostics exceeds the number allowed by Biome.
Diagnostics not shown: 15.
Checked 4 file(s) in 3ms
Found 35 error(s)
check ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Some errors were emitted while running checks.実際、
biome ciコマンドとbiome explainコマンド
biome ciコマンドは、biome.json設定ファイルで
biome ciコマンドの
# Check your code using biome ci
npx @biomejs/biome ci ./src一方、biome explainコマンドは、
たとえば、
# Understand a specific rule
npx @biomejs/biome explain useArrowFunctionこれに
# useArrowFunction
Fix is Safe.
This rule is recommended.
# Description
Use arrow functions over function expressions.
An arrow function expression is a compact alternative to a regular function expression,
with an important distinction:
this is not bound to the arrow function. It inherits this from its parent scope.
This rule proposes turning all function expressions that are not generators (function) and don't use this into arrow functions.
## Examples
### Invalid
const z = function() {
return 0;
}
const delegatedFetch = async function(url) {
return await fetch(url);
}
const f = function() {
return this.prop;
}
Named function expressions are ignored:
const z = function z() {
return 0;
}
Function expressions that declare the type of `this` are also ignored:
const z = function(this: A): number {
return 0;
}
VS CodeでBiomeを使う
Visual Studio Code
VS Codeで
VS Code Marketplaceから
Biome拡張機能を インストールします。 コマンドパレット
( [表示]または Ctrl/⌘+⇧+P)を 開き、 [フォーマットして 保存]を 選択します。 次に [既定の フォーマッターを 構成]を 選び、 Biomeを 選択します。
BiomeとNode.jsプロジェクト
Node.jsプロジェクトの
Biomeを
まとめ
これは
セキュアなコーディングプラクティスを実践しましょう
安全で
コードフォーマットや