Skip to content

Commit 4e40cec

Browse files
committed
feat: add comprehensive whitespace checking for all text files
- Add check-whitespace.js script for trailing whitespace detection and fixing - Update husky pre-commit hook to check all staged text files - Configure lint-staged to automatically fix whitespace issues - Support multiple file types: code files, scripts, configs, docs - Add npm scripts: check-whitespace and fix-whitespace - Include comprehensive documentation in WHITESPACE_CHECK.md This enhancement ensures code quality by automatically detecting and fixing trailing whitespace, mixed line endings, and other formatting issues across all supported text file types before commits. Change-Id: Ia203cfdcd66054839b5ac09f0fcaea7d5c9ecb4e Co-developed-by: Cursor <noreply@cursor.com> Signed-off-by: Jiang Xin <zhiyou.jx@alibaba-inc.com>
1 parent 8240afc commit 4e40cec

8 files changed

Lines changed: 3239 additions & 1869 deletions

File tree

.husky/pre-commit

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
#!/usr/bin/env sh
2+
3+
# Run lint-staged for staged files
24
npx lint-staged
5+
6+
# Additional check for all staged text files
7+
echo "🔍 Checking for trailing whitespace in staged files..."
8+
git diff --cached --name-only | grep -E '\.(txt|md|js|ts|jsx|tsx|json|yml|yaml|xml|html|css|scss|less|sh|bash|zsh|fish|ps1|bat|cmd|ini|cfg|conf|log|csv|tsv|diff|patch|gitignore|gitattributes)$' | xargs -r node scripts/check-whitespace.js
9+
10+
# Check specific files without extensions
11+
git diff --cached --name-only | grep -E '^(Dockerfile|Makefile|README|CHANGELOG|LICENSE|\.env\.)' | xargs -r node scripts/check-whitespace.js

WHITESPACE_CHECK.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# 空白字符检查 (Trailing Whitespace Checker)
2+
3+
这个项目配置了自动的空白字符检查,确保代码质量和一致性。
4+
5+
## 🎯 功能特性
6+
7+
- **自动检测**: 在 git commit 时自动检查尾随空白字符
8+
- **自动修复**: 支持一键修复所有空白字符问题
9+
- **多文件支持**: 支持各种文本文件格式
10+
- **集成 husky**: 与 git hooks 完全集成
11+
12+
## 📁 支持的文件类型
13+
14+
### 代码文件
15+
16+
- JavaScript/TypeScript: `.js`, `.ts`, `.jsx`, `.tsx`
17+
- 配置文件: `.json`, `.yml`, `.yaml`, `.xml`
18+
- 样式文件: `.css`, `.scss`, `.less`
19+
- 标记文件: `.html`, `.md`
20+
21+
### 脚本文件
22+
23+
- Shell 脚本: `.sh`, `.bash`, `.zsh`, `.fish`
24+
- Windows 脚本: `.ps1`, `.bat`, `.cmd`
25+
- 配置文件: `.ini`, `.cfg`, `.conf`
26+
27+
### 其他文件
28+
29+
- 日志文件: `.log`
30+
- 数据文件: `.csv`, `.tsv`
31+
- 差异文件: `.diff`, `.patch`
32+
- Git 文件: `.gitignore`, `.gitattributes`
33+
34+
### 无扩展名文件
35+
36+
- `Dockerfile`
37+
- `Makefile`
38+
- `README`
39+
- `CHANGELOG`
40+
- `LICENSE`
41+
- `.env.*` 文件
42+
43+
## 🚀 使用方法
44+
45+
### 手动检查
46+
47+
```bash
48+
# 检查单个文件
49+
npm run check-whitespace <filename>
50+
51+
# 检查多个文件
52+
npm run check-whitespace file1.txt file2.md file3.js
53+
54+
# 检查整个目录
55+
npm run check-whitespace src/ test/
56+
```
57+
58+
### 自动修复
59+
60+
```bash
61+
# 修复单个文件
62+
npm run fix-whitespace <filename>
63+
64+
# 修复多个文件
65+
npm run fix-whitespace file1.txt file2.md file3.js
66+
67+
# 修复整个目录
68+
npm run fix-whitespace src/ test/
69+
```
70+
71+
### Git 集成
72+
73+
空白字符检查已集成到 git hooks 中:
74+
75+
1. **pre-commit**: 在提交前自动检查暂存的文件
76+
2. **lint-staged**: 只对暂存的文件运行检查
77+
3. **自动修复**: 如果发现问题,可以自动修复
78+
79+
## ⚙️ 配置说明
80+
81+
### package.json 脚本
82+
83+
```json
84+
{
85+
"scripts": {
86+
"check-whitespace": "node scripts/check-whitespace.js",
87+
"fix-whitespace": "node scripts/check-whitespace.js --fix"
88+
}
89+
}
90+
```
91+
92+
### lint-staged 配置
93+
94+
```json
95+
{
96+
"lint-staged": {
97+
"*.{txt,sh,bash,zsh,fish,ps1,bat,cmd,ini,cfg,conf,log,csv,tsv,diff,patch,gitignore,gitattributes}": [
98+
"node scripts/check-whitespace.js --fix"
99+
],
100+
"Dockerfile": ["node scripts/check-whitespace.js --fix"],
101+
"Makefile": ["node scripts/check-whitespace.js --fix"]
102+
}
103+
}
104+
```
105+
106+
### husky 配置
107+
108+
```bash
109+
#!/usr/bin/env sh
110+
. "$(dirname -- "$0")/_/husky.sh"
111+
112+
# Run lint-staged for staged files
113+
npx lint-staged
114+
115+
# Additional check for all staged text files
116+
echo "🔍 Checking for trailing whitespace in staged files..."
117+
git diff --cached --name-only | grep -E '\.(txt|md|js|ts|jsx|tsx|json|yml|yaml|xml|html|css|scss|less|sh|bash|zsh|fish|ps1|bat|cmd|ini|cfg|conf|log|csv|tsv|diff|patch|gitignore|gitattributes)$' | xargs -r node scripts/check-whitespace.js
118+
119+
# Check specific files without extensions
120+
git diff --cached --name-only | grep -E '^(Dockerfile|Makefile|README|CHANGELOG|LICENSE|\.env\.)' | xargs -r node scripts/check-whitespace.js
121+
```
122+
123+
## 🔧 脚本参数
124+
125+
### check-whitespace.js
126+
127+
```bash
128+
# 基本用法
129+
node scripts/check-whitespace.js <files...>
130+
131+
# 修复模式
132+
node scripts/check-whitespace.js --fix <files...>
133+
134+
# 帮助信息
135+
node scripts/check-whitespace.js --help
136+
```
137+
138+
## 📋 检查规则
139+
140+
### 尾随空白字符
141+
142+
- 行末的空格字符
143+
- 行末的制表符
144+
- 行末的其他空白字符
145+
146+
### 混合行结束符
147+
148+
- 检测 CRLF 和 LF 混合使用
149+
- 统一使用 LF (Unix 风格)
150+
151+
### 文件结尾
152+
153+
- 检测文件末尾的空行
154+
- 保持文件结构整洁
155+
156+
## 🚨 错误处理
157+
158+
如果检测到空白字符问题:
159+
160+
1. **检查模式**: 显示错误信息并退出 (exit code 1)
161+
2. **修复模式**: 自动修复问题并显示修复信息
162+
3. **Git 集成**: 阻止提交直到问题解决
163+
164+
## 💡 最佳实践
165+
166+
1. **定期检查**: 在开发过程中定期运行检查
167+
2. **自动修复**: 使用 `--fix` 参数自动修复问题
168+
3. **IDE 集成**: 配置编辑器显示空白字符
169+
4. **团队协作**: 确保所有团队成员都使用相同的检查规则
170+
171+
## 🔍 故障排除
172+
173+
### 常见问题
174+
175+
1. **权限错误**: 确保脚本有执行权限
176+
2. **文件编码**: 确保文件使用 UTF-8 编码
177+
3. **Git 状态**: 确保在 git 仓库中运行
178+
179+
### 调试模式
180+
181+
```bash
182+
# 详细输出
183+
node scripts/check-whitespace.js --verbose <files...>
184+
```
185+
186+
## 📚 相关资源
187+
188+
- [Git Hooks Documentation](https://git-scm.com/docs/githooks)
189+
- [Husky Documentation](https://typicode.github.io/husky/)
190+
- [lint-staged Documentation](https://github.com/okonet/lint-staged)
191+
- [Prettier Documentation](https://prettier.io/docs/en/)

0 commit comments

Comments
 (0)