Prettier
强约定的代码格式化工具
简介
Prettier 是一款强约定(opinionated)的代码格式化工具。它通过解析您的代码并按照自身规则重新输出代码,强制执行一致的代码风格。这些规则会考虑最大行长度限制,并在必要时自动换行。
输入示例
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
输出示例
foo(
reallyLongArg(),
omgSoManyParameters(),
IShouldRefactorThis(),
isThereSeriouslyAnotherOne(),
);
Prettier 可在以下场景运行:
- 编辑器保存时自动格式化
- 预提交钩子(pre-commit hook)
- CI 持续集成环境
确保代码库保持统一的代码风格,开发者无需在代码评审中再提出琐碎的格式意见!
文档资源
- https://prettier.io/docs/en/install.html
- https://prettier.io/docs/en/options.html
- https://prettier.io/docs/en/cli.html
- https://prettier.io/docs/en/api.html
在线体验
通过 https://prettier.io/playground/ 实时体验代码格式化效果
项目徽章
在您的项目中展示使用 Prettier 的标识 → 代码风格:prettier
https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square](https://github.com/prettier/prettier)
https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square](https://github.com/prettier/prettier)
贡献指南
欢迎参与项目贡献 → https://github.com/prettier/prettier/blob/main/CONTRIBUTING.md
核心总结
- 自动化格式化:通过解析-重打印机制强制执行统一代码风格
- 智能换行:根据最大行长度自动调整代码布局
- 多场景集成:支持编辑器/预提交钩子/CI环境无缝集成
- 消除风格争议:从根本上解决团队代码风格分歧问题
- 生态完善:提供详细文档、在线演练场和项目标识系统
- 开源协作:通过标准化贡献流程推动工具持续进化