SuperAPI AI 工具集

lint-format

编程与开发

查看项目

更新于 10-15 07:47

技能介绍

Lint & Format 旨在为项目搭建一致性代码质量所需的代码检查与格式化配置,涵盖 JavaScript/TypeScript 与 Python 生态的常用工具,并给出具体安装命令、配置示例与运行方式。

用途方面,该技能用于在项目中引入统一的 lint 与 format 规则,帮助保持代码风格一致、减少低级错误,并可通过提交钩子与 CI 环节自动执行检查。

主要能力按语言与工具划分:

ESLint(JavaScript/TypeScript):支持 ESLint 9+ 的 Flat Config 方式初始化,可通过 npm init @eslint/config@latest 或安装 eslint、@eslint/js、typescript-eslint 来配置。示例配置启用了推荐规则,覆盖 no-unused-vars、@typescript-eslint/no-unused-vars、@typescript-eslint/no-explicit-any、no-console 等,并可通过 ignores 忽略 dist、node_modules 与配置文件。针对 React 提供了安装 eslint-plugin-react 与 eslint-plugin-react-hooks 的配置方式,包括 react-hooks 推荐规则、关闭 react/react-in-jsx-scope 与 react/prop-types,以及 react version 自动检测。运行命令包括 npx eslint .、npx eslint --fix .、npx eslint src/ 以及按 glob 匹配运行。

Prettier:通过 npm install -D prettier eslint-config-prettier 安装,提供 .prettierrc 示例,包含 semi、singleQuote、tabWidth、trailingComma、printWidth、arrowParens、endOfLine 等选项,并用 .prettierignore 排除 dist、node_modules、coverage、压缩文件与锁文件。与 ESLint 集成时需将 eslint-config-prettier 放在配置最后以覆盖冲突规则。运行命令包括写入与检查模式,并支持按文件类型匹配。

Ruff(Python 的 Linter 与 Formatter):通过 pip install ruff 安装,在 pyproject.toml 中配置 target-version、line-length,选择 E、W、F、I、N、UP、B、SIM、TCH 等规则集,忽略 E501,并可配置 isort 的 known-first-party 与 format 的 quote-style、indent-style。运行命令区分 ruff check 与 ruff format,均支持检查与自动修复选项。

Black(Python 格式化工具):pip install black 后提供 black .、black --check .、black --diff . 等命令,并可在 pyproject.toml 中配置 line-length 与 target-version。

mypy(Python 类型检查):pip install mypy 后运行 mypy src/ 或 mypy --strict src/,并展示 python_version、strict、warn_return_any、warn_unused_configs、disallow_untyped_defs 等配置项。

EditorConfig:提供 .editorconfig 示例,统一设置 end_of_line、insert_final_newline、trim_trailing_whitespace、charset,并针对 js/ts/tsx/json/css/yml/yaml、py、md、Makefile 分别设置缩进方式与大小。

提交钩子集成:JavaScript 侧可使用 husky 与 lint-staged,通过 npx husky init 初始化,在 .husky/pre-commit 中执行 npx lint-staged,并在 package.json 中按文件类型配置 eslint --fix、prettier --write、ruff check --fix、ruff format。Python 侧可使用 pre-commit,通过 .pre-commit-config.yaml 引入 ruff-pre-commit 与 mirrors-mypy,并执行 pre-commit install 与 pre-commit run --all-files。

package.json 脚本示例:提供 lint、lint:fix、format、format:check、typecheck、check 等脚本,其中 check 依次执行 lint、format:check 与 typecheck。

适用场景:适用于需要在 JavaScript/TypeScript 或 Python 项目中统一代码检查、格式化、类型检查,并希望将检查流程接入提交前钩子或持续集成的团队与个人。资料最后提到可参考 references/configs.md 获取 CI 集成与配置配方。

限制:该技能主要提供配置示例与命令说明,不涉及具体版本兼容性保证或跨语言之外的其它工具;文中未提供收费、平台支持、性能效果等额外信息。