linthis 0.22.0

A fast, cross-platform multi-language linter and formatter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# 创建插件

本指南介绍如何创建和发布 linthis 插件。

## 什么是插件?

linthis 插件是一个包含以下内容的 Git 仓库:
- `linthis-plugin.toml` 清单文件
- 一个或多个语言配置文件(TOML、YAML 或 JSON)
- 可选的 `linthis-hook.toml`,用于捆绑 hook 覆盖配置
- 可选的自定义规则和预设

插件允许您在项目或团队之间共享 lint 配置**以及 git/agent hook 设置**。

## 插件结构

```
my-linthis-plugin/
├── linthis-plugin.toml           # 必需:插件清单
├── linthis-hook.toml           # 可选:Hook 来源覆盖(plugin add 时自动合并)
├── config.toml                   # 主 lint 配置
├── hooks/                        # 可选:自定义 hook 文件
│   ├── git/
│   │   └── pre-commit            # 自定义 pre-commit 脚本
│   └── agent/
│       └── plugins/
│           ├── _default/                     # 默认回退(所有 provider)
│           │   └── lt/                       # 插件包
│           │       ├── skills/lt-lint/SKILL.md
│           │       ├── commands/
│           │       ├── memories/TOPLEVEL.md
│           │       └── hooks/hooks.json
│           └── claude/                       # Provider 特定覆盖(可选)
│               └── lt/
│                   ├── skills/lt-lint/SKILL.md
│                   └── hooks/hooks.json      # Provider hooks(如 stop hook)
├── rules/                        # 可选:额外规则配置
│   ├── strict.toml
│   └── relaxed.toml
└── README.md                     # 可选:文档
```

## 通过 `linthis-hook.toml` 捆绑 Hook 配置

插件可以在插件根目录包含一个 `linthis-hook.toml`,与 lint 配置一起发布。该文件使用 `plugin = "self"` 声明指向插件自身的 `[hook.*]` 来源覆盖。

当用户运行 `linthis plugin add <alias> <url>` 时,linthis 自动:

1. 将所有 `plugin = "self"` 替换为 `plugin = "<alias>"`(用户选择的别名)
2.`[hook.*]` 条目以非覆盖方式合并到用户的 `.linthis/config.toml`
之后运行 `linthis hook install` 将自动使用插件的定制 hook 脚本和 agent 包——无需手动配置。

### `linthis-hook.toml` 示例

```toml
# 插件内置。引用使用 plugin = "self"。

[hook.git]
pre-commit = { source = { plugin = "self", file = "hooks/git/pre-commit" } }

[hook.agent.plugins._default]
"lt" = { source = { plugin = "self", file = "hooks/agent/plugins/_default/lt" } }

[hook.agent.plugins.claude]
"lt" = { source = { plugin = "self", file = "hooks/agent/plugins/claude/lt" } }
```

用户运行 `linthis plugin add myteam <url>` 后,`plugin = "self"` 变为 `plugin = "myteam"`,所有条目合并到用户的 `.linthis/config.toml` 中。

## 插件清单

`linthis-plugin.toml` 清单描述您的插件:

```toml
[plugin]
# 必填字段
name = "my-plugin"
version = "1.0.0"

# 可选字段
description = "Custom lint rules for my organization"
author = "Your Name <email@example.com>"
repository = "https://github.com/username/my-linthis-plugin"
license = "MIT"

# 最低 linthis 版本要求(可选)
min_linthis_version = "0.1.0"

[config]
# 主配置文件路径
path = "config.toml"

# 可选:可引用的额外配置文件
# [config.presets]
# strict = "rules/strict.toml"
# relaxed = "rules/relaxed.toml"
```

## 配置文件

主配置文件遵循标准 linthis 配置格式:

```toml
# config.toml

# 排除模式(将与项目配置合并)
excludes = ["vendor/**", "third_party/**"]

# 此插件风格指南的默认最大复杂度
max_complexity = 15

# 规则修改
[rules]
disable = ["E501"]  # 禁用行长度检查

[rules.severity]
"W0612" = "error"   # 将未使用变量视为错误

# 自定义规则
[[rules.custom]]
code = "org/no-fixme"
pattern = "FIXME|XXX"
message = "FIXME comments must be resolved before merge"
severity = "error"
suggestion = "Create a tracking issue or resolve the FIXME"

[[rules.custom]]
code = "org/copyright-header"
pattern = "^(?!// Copyright)"
message = "Missing copyright header"
severity = "warning"
extensions = ["rs", "go", "java"]

# 语言特定设置
[rust]
max_complexity = 12

[python]
excludes = ["*_test.py", "test_*.py"]
```

## 创建您的第一个插件

### 步骤 1:创建仓库

```bash
mkdir my-linthis-plugin
cd my-linthis-plugin
git init
```

### 步骤 2:创建清单

创建 `linthis-plugin.toml`:

```toml
[plugin]
name = "my-plugin"
version = "1.0.0"
description = "My custom lint configuration"

[config]
path = "config.toml"
```

### 步骤 3:创建配置

创建 `config.toml`:

```toml
# My organization's lint rules

excludes = ["generated/**"]
max_complexity = 20

[[rules.custom]]
code = "custom/no-console-log"
pattern = "console\\.log"
message = "Remove console.log before committing"
severity = "warning"
languages = ["typescript", "javascript"]
```

### 步骤 4:本地测试

发布前,本地测试您的插件:

```bash
# 在项目的 .linthis/config.toml 中
[plugins]
sources = [
    { name = "local-test", url = "file:///path/to/my-linthis-plugin" }
]
```

然后运行:

```bash
linthis plugin init
linthis -c
```

### 步骤 5:发布

推送到 GitHub 或任何 Git 托管:

```bash
git add .
git commit -m "Initial plugin release"
git tag v1.0.0
git push origin main --tags
```

## 使用您的插件

添加到项目的 `.linthis/config.toml`:

```toml
[plugins]
sources = [
    { name = "my-plugin", url = "https://github.com/username/my-linthis-plugin.git" }
]
```

或固定到特定版本:

```toml
[plugins]
sources = [
    { name = "my-plugin", url = "https://github.com/username/my-linthis-plugin.git", ref = "v1.0.0" }
]
```

## 插件命令

```bash
# 初始化/下载插件
linthis plugin init

# 列出已安装插件
linthis plugin list

# 更新所有插件到最新版
linthis plugin update

# 更新特定插件
linthis plugin update my-plugin

# 清理插件缓存
linthis plugin clean
```

## 配置合并

插件配置与项目配置合并。优先级为:

1. **CLI 参数**(最高)
2. **项目配置** (`.linthis/config.toml`)
3. **插件配置**(按列出顺序)
4. **用户配置** (`~/.linthis/config.toml`)
5. **内置默认值**(最低)

数组字段(`excludes`、`includes`、`rules.disable`、`rules.custom`)会被**扩展**(添加),而标量字段会被**覆盖**。

## 最佳实践

### 1. 版本化您的插件

使用语义化版本和 Git 标签:

```bash
git tag v1.0.0   # 初始发布
git tag v1.1.0   # 新功能(向后兼容)
git tag v2.0.0   # 破坏性更改
```

### 2. 记录您的规则

添加注释解释每个自定义规则:

```toml
# Prevent debug code from being committed
[[rules.custom]]
code = "org/no-debug"
pattern = "debugger|console\\.debug"
message = "Debug code should not be committed"
```

### 3. 设置最低版本

如果您的插件使用特定 linthis 版本的功能:

```toml
[plugin]
min_linthis_version = "0.2.0"
```

### 4. 提供预设

为灵活性提供多个配置级别:

```
my-plugin/
├── config.toml           # 默认(推荐)设置
├── presets/
│   ├── strict.toml       # CI 更严格的规则
│   └── relaxed.toml      # 原型开发更宽松
```

### 5. 彻底测试

发布前测试您的插件:
- 在您的规则针对的多种语言上
- 与现有项目配置一起
- linthis 更新后

## 示例插件

### 组织风格指南

```toml
# linthis-plugin.toml
[plugin]
name = "acme-style"
version = "2.0.0"
description = "ACME Corp coding standards"

[config]
path = "config.toml"
```

```toml
# config.toml
max_complexity = 15
preset = "google"

[rules]
disable = ["E501"]  # We use 120-char lines

[[rules.custom]]
code = "acme/ticket-ref"
pattern = "TODO(?!.*\\[ACME-\\d+\\])"
message = "TODO comments must reference a JIRA ticket [ACME-XXX]"
severity = "warning"

[rust]
max_complexity = 12

[python]
max_complexity = 10
```

### 安全规则插件

```toml
# linthis-plugin.toml
[plugin]
name = "security-rules"
version = "1.0.0"
description = "Security-focused lint rules"

[config]
path = "security.toml"
```

```toml
# security.toml
[[rules.custom]]
code = "sec/no-eval"
pattern = "\\beval\\s*\\("
message = "Avoid eval() - potential code injection vulnerability"
severity = "error"
languages = ["javascript", "typescript", "python"]

[[rules.custom]]
code = "sec/no-hardcoded-secret"
pattern = "(password|secret|api_key|token)\\s*=\\s*['\"][^'\"]+['\"]"
message = "Potential hardcoded secret detected"
severity = "error"

[[rules.custom]]
code = "sec/no-http"
pattern = "http://"
message = "Use HTTPS instead of HTTP"
severity = "warning"
```

## 观看演示

观看[插件系统视频教程](../getting-started/videos.md#3),20 秒了解插件使用。

## 故障排除

### 插件未加载

1. 检查 `linthis plugin list` 查看已安装插件
2. 运行 `linthis plugin init` 重新获取
3. 验证 Git URL 可访问
4. 检查 `linthis-plugin.toml` 中的清单错误

### 配置未应用

1. 检查合并顺序(后面的插件覆盖前面的)
2. 验证清单中的配置文件路径
3. 使用 `--verbose` 运行查看配置加载

### 版本冲突

如果看到版本兼容性错误:
1. 将 linthis 更新到所需版本
2. 或使用 `ref = "v1.0.0"` 使用旧版插件