n8n 节点代码检查器(n8n node linter)#
n8n 的节点代码检查器 eslint-plugin-n8n-nodes-base 会静态分析(“检查”)官方仓库和社区包中的 n8n 节点和凭证源代码。该检查器能够检测问题并自动修复,以帮助你遵循最佳实践。
🌐 n8n's node linter, eslint-plugin-n8n-nodes-base, statically analyzes ("lints") the source code of n8n nodes and credentials in the official repository and in community packages. The linter detects issues and automatically fixes them to help you follow best practices.
eslint-plugin-n8n-nodes-base 包含了节点文件(*.node.ts)、资源描述文件(*Description.ts)、凭证文件(*.credentials.ts)以及社区包的 package.json 的规则集合。
设置(Setup)#
如果使用 n8n 节点启动器:在启动器项目中运行 npm install 安装所有依赖。安装完成后,你就可以使用代码检查工具了。
🌐 If using the n8n node starter: Run npm install in the starter project to install all dependencies. Once the installation finishes, the linter is available to you.
如果使用 VS Code,请安装 ESLint VS Code 扩展。对于其他 IDE,请参考它们的 ESLint 集成。
🌐 If using VS Code, install the ESLint VS Code extension. For other IDEs, refer to their ESLint integrations.
不要编辑配置文件
.eslintrc.js 包含 eslint-plugin-n8n-nodes-base 的配置。请不要编辑此文件。
用法(Usage)#
你可以在社区包或 n8n 主仓库中使用代码检查器。
🌐 You can use the linter in a community package or in the main n8n repository.
代码检查(Linting)#
在社区包中,安装依赖后以及发布包到 npm 之前,会自动运行 linter。在主 n8n 仓库中,每当你向拉取请求推送代码时,linter 会使用 GitHub Actions 自动运行。
🌐 In a community package, the linter runs automatically after installing dependencies and before publishing the package to npm. In the main n8n repository, the linter runs automatically using GitHub Actions whenever you push to your pull request.
在这两种情况下,VS Code 会在你处理项目时在后台进行代码检查。将鼠标悬停在检测到的问题上,可以查看完整的检查说明以及进一步信息的链接。
🌐 In both cases, VS Code lints in the background as you work on your project. Hover over a detected issue to see a full description of the linting and a link to further information.
你也可以手动运行代码检查器:
🌐 You can also run the linter manually:
- 运行
npm run lint来进行代码检查,并在控制台查看检测到的问题。 - 运行
npm run lintfix以进行代码检查并自动修复问题。代码检查器会修复被标记为[可自动修复]的规则违规情况 (https://github.com/ivov/eslint-plugin-n8n-nodes-base#ruleset)。
这两个命令可以在你的社区包的根目录下运行,也可以在主仓库中的 /packages/nodes-base/ 目录下运行。
🌐 Both commands can run in the root directory of your community package, or in /packages/nodes-base/ in the main repository.
例外(Exceptions)#
除了修复规则违规之外,你还可以为其创建例外,这样代码检查器就不会标记它。
🌐 Instead of fixing a rule violation, you can also make an exception for it, so the linter doesn't flag it.
在 VS Code 中为 lint 创建例外:将鼠标悬停在问题上,点击 Quick fix(在 macOS 上为 cmd+.),然后选择 为此行禁用 {rule}。仅在有充分理由的情况下为某一行禁用规则。如果你认为 linter 错误地报告了一个问题,请 在 linter 仓库中报告。
🌐 To make a lint exception from VS Code: hover over the issue and click on Quick fix (or cmd+. in macOS) and select Disable {rule} for this line. Only disable rules for a line where you have good reason to. If you think the linter is incorrectly reporting an issue, please report it in the linter repository.
要为单个文件添加 lint 例外,请添加代码注释。特别是,TSLint 规则可能不会在 VS Code 中显示,可能需要通过代码注释将其关闭。有关更多指导,请参阅 TSLint 文档。
🌐 To add a lint exception to a single file, add a code comment. In particular, TSLint rules may not show up in VS Code and may need to be turned off using code comments. Refer to the TSLint documentation for more guidance.