Skip to content

快速入门

🌐 Quickstart

本页展示了推荐的Oxlint设置方法及最常见的工作流程,并配有复制粘贴命令。

🌐 This page shows the recommended setup for Oxlint and the most common workflows, with copy-paste commands.

安装

🌐 Install

oxlint 安装为开发依赖:

🌐 Install oxlint as a dev dependency:

sh
$ npm add -D oxlint
sh
$ pnpm add -D oxlint
sh
$ yarn add -D oxlint
sh
$ bun add -D oxlint

package.json 添加 lint 命令:

🌐 Add lint commands to package.json:

package.json
json
{
  "scripts": {
    "lint": "oxlint",
    "lint:fix": "oxlint --fix"
  }
}

运行它:

🌐 Run it:

sh
pnpm run lint

应用修复:

🌐 Apply fixes:

sh
pnpm run lint:fix

用法

🌐 Usage

有关完整的选项列表,请参阅 CLI 参考

🌐 For the complete list of options, see the CLI reference.

sh
oxlint [OPTIONS] [PATH]...

如果省略 PATH,Oxlint 将检查当前工作目录。

🌐 If PATH is omitted, Oxlint lints the current working directory.

常见工作流程

🌐 Common workflows

使用 lint-staged 的预提交

🌐 Pre-commit with lint-staged

package.json
json
{
  "lint-staged": {
    "*.{js,jsx,ts,tsx,mjs,cjs}": "pnpm run lint"
  }
}

创建配置文件

🌐 Create a config file

使用默认值初始化 .oxlintrc.json 配置:

🌐 Initialize the .oxlintrc.json config with default values:

sh
oxlint --init

然后运行 Oxlint:

🌐 Then run Oxlint:

sh
oxlint

TIP

如果你正在从 ESLint 迁移,请参阅 从 ESLint 迁移 页面,获取关于迁移的详细指导。

解决问题

🌐 Fix problems

应用安全修复:

🌐 Apply safe fixes:

sh
oxlint --fix

应用建议(可能会改变程序行为):

🌐 Apply suggestions (may change program behavior):

sh
oxlint --fix-suggestions

应用危险修复和建议:

🌐 Apply dangerous fixes and suggestions:

sh
oxlint --fix-dangerously

请参阅 自动修复 了解何时使用每种模式的指南。

🌐 See Automatic fixes for guidance on when to use each mode.

忽略文件

🌐 Ignore files

使用显式忽略文件:

🌐 Use an explicit ignore file:

sh
oxlint --ignore-path .oxlintignore

从命令行添加忽略模式:

🌐 Add ignore patterns from the command line:

sh
oxlint --ignore-pattern "dist/**" --ignore-pattern "*.min.js"

禁用忽略处理:

🌐 Disable ignore handling:

sh
oxlint --no-ignore

请参阅 忽略文件

🌐 See Ignore files.

可靠地使 CI 失败

🌐 Fail CI reliably

只报告错误:

🌐 Only report errors:

sh
oxlint --quiet

如果发现任何警告则失败:

🌐 Fail if any warnings are found:

sh
oxlint --deny-warnings

如果警告超过阈值则失败:

🌐 Fail if warnings exceed a threshold:

sh
oxlint --max-warnings 0

请参阅 CI 设置

🌐 See CI setup.

使用机器可读的输出

🌐 Use machine-readable output

选择输出格式:

🌐 Select an output format:

sh
oxlint -f json

可用的格式包括:defaultjsonunixcheckstylegithubgitlabjunitstylish

🌐 Available formats include: default, json, unix, checkstyle, github, gitlab, junit, stylish.

检查有效配置

🌐 Inspect the effective configuration

打印文件时将使用的配置:

🌐 Print the configuration that would be used for a file:

sh
oxlint --print-config path/to/file.ts

列出可用规则

🌐 List available rules

列出已注册的规则,包括当前 oxlint 配置启用的规则:

🌐 List registered rules, including those enabled by your current oxlint config:

sh
oxlint --rules

完整列表见规则参考

🌐 The full list is in the Rules reference.

下一步

🌐 Next steps