Skip to content

从 ESLint 和 Prettier 迁移

Biome 提供专用命令来简化从 ESLint 和 Prettier 的迁移。

¥Biome provides dedicated commands to ease the migration from ESLint and Prettier.

如果你不想知道详细信息,只需运行以下命令:

¥If you don’t want to know the details, just run the following commands:

Terminal window
biome migrate eslint --write
biome migrate prettier --write

¥Migrate from ESLint

许多 Biome linter 规则受到 ESLint 规则或 ESLint 插件规则的启发或与之相同。我们处理一些 ESLint 插件,例如 TypeScript ESLintESLint JSX A11yESLint ReactESLint Unicorn。但是,Biome 对其规则有自己的命名约定。Biome 使用 camelCaseRuleName,而 ESLint 使用 kebab-case-rule-name。此外,Biome 经常选择使用不同的名称来更好地传达其规则的意图。规则的来源可在描述规则的页面上找到。你还可以使用 专用页面 从 ESLint 规则中找到等效的 Biome 规则。

¥Many Biome linter rules are inspired by or identical to the ESLint rules or the rules of an ESLint plugin. We handle some ESLint plugins such as TypeScript ESLint, ESLint JSX A11y, ESLint React, and ESLint Unicorn. However, Biome has its own naming convention for its rules. Biome uses camelCaseRuleName while ESLint uses kebab-case-rule-name. Moreover, Biome has often chosen to use different names to better convey the intent of its rules. The source of a rule can be found on the page describing the rule. You can also find the equivalent Biome rule from an ESLint rule using the dedicated page.

为了简化迁移,Biome 提供了 biome migrate eslint 子命令。此子命令将读取你的 ESLint 配置并尝试将其设置移植到 Biome。子命令能够处理旧配置文件和平面配置文件。它支持旧配置的 extends 字段,并加载共享和插件配置。子命令还迁移了 .eslintignore

¥To ease the migration, Biome provides the biome migrate eslint subcommand. This subcommand will read your ESLint configuration and attempt to port its settings to Biome. The subcommand is able to handle both the legacy and the flat configuration files. It supports the extends field of the legacy configuration and loads both shared and plugin configurations. The subcommand also migrates .eslintignore.

给定以下 ESLint 配置:

¥Given the following ESLint configuration:

.eslintrc.json
{
"extends": ["plugin:unicorn/recommended"],
"plugins": ["unicorn"],
"ignore_patterns": ["dist/**"],
"globals": {
"Global1": "readonly"
},
"rules": {
"eqeqeq": "error"
},
"overrides": [
{
"files": ["tests/**"],
"rules": {
"eqeqeq": "off"
}
}
]
}

以下 Biome 配置:

¥And the following Biome configuration:

biome.json
{
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}

运行以下命令将你的 ESLint 配置迁移到 Biome。

¥Run the following command to migrate your ESLint configuration to Biome.

Terminal window
npx @biomejs/biome migrate eslint --write

子命令会覆盖你的初始 Biome 配置。例如,它禁用了 recommended。这会导致以下 Biome 配置:

¥The subcommand overwrites your initial Biome configuration. For example, it disables recommended. This results in the following Biome configuration:

biome.json
{
"organizeImports": { "enabled": true },
"linter": {
"enabled": true,
"rules": {
"recommended": false,
"complexity": {
"noForEach": "error",
"noStaticOnlyClass": "error",
"noUselessSwitchCase": "error",
"useFlatMap": "error"
},
"style": {
"noNegationElse": "off",
"useForOf": "error",
"useNodejsImportProtocol": "error",
"useNumberNamespace": "error"
},
"suspicious": {
"noDoubleEquals": "error",
"noThenProperty": "error",
"useIsArray": "error"
}
}
},
"javascript": { "globals": ["Global1"] },
"overrides": [
{
"include": ["tests/**"],
"linter": { "rules": { "suspicious": { "noDoubleEquals": "off" } } }
}
]
}

子命令需要 Node.js 来加载和解析 ESLint 配置文件中配置的所有插件和 extends。目前,biome migrate eslint 不支持以 YAML 编写的配置。

¥The subcommand needs Node.js to load and resolve all the plugins and extends configured in the ESLint configuration file. For now, biome migrate eslint doesn’t support configuration written in YAML.

默认情况下,Biome 不会迁移启发规则。你可以使用 CLI 标志 --include-inspired 来迁移它们。

¥By default, Biome doesn’t migrate inspired rules. You can use the CLI flag --include-inspired to migrate them.

Terminal window
npx @biomejs/biome migrate eslint --write --include-inspired

请注意,你不太可能获得与 ESLint 完全相同的行为,因为 Biome 选择不实现某些规则选项或与原始实现略有偏差。

¥Note that you are unlikely to get exactly the same behavior as ESLint because Biome has chosen not to implement some rule options or to deviate slightly from the original implementation.

由于 ESLint 考虑了 VCS 忽略文件,我们建议你启用 Biome 的 VCS 集成

¥Since ESLint takes VCS ignore files into account, we recommend that you enable Biome’s VCS integration.

¥Migrate from Prettier

Biome 尝试尽可能接近 Prettier 格式化程序。但是,Biome 对其格式化程序使用不同的默认值。例如,它使用制表符而不是空格进行缩进。你可以通过运行 biome migrate prettier --write 轻松迁移到 Biome。

¥Biome tries to match the Prettier formatter as closely as possible. However, Biome uses different defaults for its formatter. For example, it uses tabs for indentation instead of spaces. You can easily migrate to Biome by running biome migrate prettier --write.

给定以下 Prettier 配置:

¥Given the following Prettier configuration:

.prettierrc.json
{
"useTabs": false,
"singleQuote": true,
"overrides": [
{
"files": ["*.json"],
"options": { "tabWidth": 2 }
}
]
}

运行以下命令将你的 Prettier 配置迁移到 Biome。

¥Run the following command to migrate your Prettier configuration to Biome.

Terminal window
npx @biomejs/biome migrate prettier --write

这会导致以下 Biome 配置:

¥This results in the following Biome configuration:

biome.json
{
"formatter": {
"enabled": true,
"formatWithErrors": false,
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 80,
"attributePosition": "auto"
},
"organizeImports": { "enabled": true },
"linter": { "enabled": true, "rules": { "recommended": true } },
"javascript": {
"formatter": {
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded",
"trailingCommas": "all",
"semicolons": "asNeeded",
"arrowParentheses": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"quoteStyle": "single",
"attributePosition": "auto"
}
},
"overrides": [
{
"include": ["*.json"],
"formatter": {
"indentWidth": 2
}
}
]
}

子命令需要 Node.js 来加载 JavaScript 配置,例如 .prettierrc.jsbiome migrate prettier 不支持用 JSON5、TOML 或 YAML 编写的配置。

¥The subcommand needs Node.js to load JavaScript configurations such as .prettierrc.js. biome migrate prettier doesn’t support configuration written in JSON5, TOML, or YAML.

由于 Prettier 考虑了 VCS 忽略文件,我们建议你启用 Biome 的 VCS 集成

¥Since Prettier takes VCS ignore files into account, we recommend that you enable Biome’s VCS integration.