@sun-asterisk/sunlint 1.3.37 → 1.3.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,486 @@
1
+ # Registry Generation Flow - SunLint Rules
2
+
3
+ > **Document này giải thích chi tiết cách SunLint generate rule registry từ markdown files**
4
+
5
+ ---
6
+
7
+ ## 🎯 Core Concept
8
+
9
+ **QUAN TRỌNG:** Rule names và descriptions trong SunLint được quản lý tập trung tại **markdown files**, KHÔNG phải JSON configs hay source code.
10
+
11
+ ```
12
+ Markdown Files = SOURCE OF TRUTH
13
+
14
+ Auto Generation
15
+
16
+ Registry JSON Files
17
+ ```
18
+
19
+ ---
20
+
21
+ ## 📊 Luồng Generation Chi Tiết
22
+
23
+ ### 1. Source Files (Markdown)
24
+
25
+ **Location:** `coding-quality/rules/`
26
+
27
+ ```
28
+ coding-quality/rules/
29
+ ├── common-en.md # Common rules (C001-C076)
30
+ ├── dart-en.md # Dart-specific rules (D001-D025)
31
+ ├── security-en.md # Security rules (S001-S060)
32
+ ├── typescript-en.md # TypeScript rules (T001-T021)
33
+ ├── java-en.md # Java rules (J001-J006)
34
+ ├── kotlin-mobile-en.md # Kotlin rules (K001-K034)
35
+ ├── reactjs-en.md # React rules (R001-R009)
36
+ └── swift-en.md # Swift rules (SW001-SW035)
37
+ ```
38
+
39
+ **Format trong markdown:**
40
+
41
+ ```markdown
42
+ ### 📘 Rule D001 – Recommended Lint Rules Should Be Enabled
43
+
44
+ - **Objective**: Ensure code quality through standard lint configurations
45
+ - **Details**: The `analysis_options.yaml` file should include recommended lint packages...
46
+ - **Applies to**: Flutter/Dart
47
+ - **Tools**: `dart lint` (flutter_lints, very_good_analysis, lints)
48
+ - **Principles**: CODE_QUALITY
49
+ - **Version**: 1.0
50
+ - **Status**: activated
51
+ - **Severity**: major
52
+ ```
53
+
54
+ ### 2. Copy Rules Script
55
+
56
+ **Script:** `coding-quality/extensions/sunlint/scripts/copy-rules.js`
57
+
58
+ **Chức năng:**
59
+ - Copy tất cả `*-en.md` files từ `coding-quality/rules/`
60
+ - Đến `coding-quality/extensions/sunlint/origin-rules/`
61
+ - Chỉ copy English versions (`*-en.md`)
62
+
63
+ **Command:**
64
+ ```bash
65
+ cd coding-quality/extensions/sunlint
66
+ node scripts/copy-rules.js
67
+ ```
68
+
69
+ **Output:**
70
+ ```
71
+ 📋 Copying all rules for packaging...
72
+ Source: /path/to/coding-quality/rules
73
+ Target: /path/to/coding-quality/extensions/sunlint/origin-rules
74
+ ✅ Copied: common-en.md
75
+ ✅ Copied: dart-en.md
76
+ ✅ Copied: security-en.md
77
+ ...
78
+ ✅ Successfully copied 8 rule files
79
+ ```
80
+
81
+ **Result:**
82
+ ```
83
+ coding-quality/extensions/sunlint/origin-rules/
84
+ ├── common-en.md # Copied from ../../../rules/
85
+ ├── dart-en.md # Copied from ../../../rules/
86
+ ├── security-en.md # Copied from ../../../rules/
87
+ └── ...
88
+ ```
89
+
90
+ ### 3. Generate Registry Script
91
+
92
+ **Script:** `coding-quality/extensions/sunlint/scripts/generate-rules-registry.js`
93
+
94
+ **Chức năng:**
95
+ - Parse tất cả files trong `origin-rules/`
96
+ - Extract rule information từ markdown format
97
+ - Generate JSON registry file
98
+
99
+ **Parser Logic:**
100
+
101
+ ```javascript
102
+ // Tìm rule header: ### 📘 Rule D001 – Rule Name
103
+ const ruleHeaderRegex = /###\s*📘\s*Rule\s+([A-Z]+\d+)\s*[–-]\s*(.+)/;
104
+
105
+ // Parse rule properties
106
+ - **Objective**: ${objective}
107
+ - **Details**: ${details}
108
+ - **Applies to**: ${appliesTo}
109
+ - **Tools**: ${tools}
110
+ - **Principles**: ${principles}
111
+ - **Version**: ${version}
112
+ - **Status**: ${status}
113
+ - **Severity**: ${severity}
114
+ ```
115
+
116
+ **Command:**
117
+ ```bash
118
+ node scripts/generate-rules-registry.js
119
+ ```
120
+
121
+ **Output:**
122
+ ```
123
+ 📋 Generating rules registry from origin-rules...
124
+ Found 8 English rule files
125
+ Parsed rule C001: 0 good examples, 0 bad examples, 0 configs
126
+ Parsed rule C002: 0 good examples, 0 bad examples, 0 configs
127
+ ...
128
+ Parsed rule D001: 0 good examples, 0 bad examples, 0 configs
129
+ ...
130
+ ✅ Generated registry with 257 rules
131
+ 📁 File: config/rules/rules-registry-generated.json (156.2 KB)
132
+ ```
133
+
134
+ ### 4. Generated Registry File
135
+
136
+ **Output File:** `config/rules/rules-registry-generated.json`
137
+
138
+ **Structure:**
139
+
140
+ ```json
141
+ {
142
+ "rules": {
143
+ "D001": {
144
+ "name": "Recommended Lint Rules Should Be Enabled",
145
+ "description": "Ensure code quality through standard lint configurations",
146
+ "category": "Common",
147
+ "severity": "major",
148
+ "languages": ["dart"],
149
+ "version": "1.0",
150
+ "status": "activated",
151
+ "tags": ["Common", "readability", "code-quality"],
152
+ "tools": ["`dart lint` (flutter_lints, very_good_analysis, lints)"],
153
+ "framework": "All",
154
+ "principles": ["CODE_QUALITY"]
155
+ },
156
+ "D002": {
157
+ ...
158
+ }
159
+ }
160
+ }
161
+ ```
162
+
163
+ ---
164
+
165
+ ## 🔄 Complete Flow Diagram
166
+
167
+ ```
168
+ ┌─────────────────────────────────────────────────────────────┐
169
+ │ Step 1: Markdown Files (SOURCE OF TRUTH) │
170
+ │ Location: coding-quality/rules/ │
171
+ │ │
172
+ │ Files: │
173
+ │ - common-en.md │
174
+ │ - dart-en.md ← EDIT HERE to change rule names │
175
+ │ - security-en.md │
176
+ │ - typescript-en.md │
177
+ │ - ... │
178
+ └──────────────────────┬───────────────────────────────────────┘
179
+
180
+
181
+ ┌──────────────────────────────────────────────────────────────┐
182
+ │ Step 2: Copy Rules Script │
183
+ │ Command: node scripts/copy-rules.js │
184
+ │ │
185
+ │ Action: │
186
+ │ - Copy *-en.md from rules/ to origin-rules/ │
187
+ │ - Only English versions │
188
+ └──────────────────────┬────────────────────────────────────────┘
189
+
190
+
191
+ ┌──────────────────────────────────────────────────────────────┐
192
+ │ Intermediate: origin-rules/ │
193
+ │ Location: coding-quality/extensions/sunlint/origin-rules/ │
194
+ │ │
195
+ │ Files: (Copies of source markdown files) │
196
+ │ - common-en.md │
197
+ │ - dart-en.md │
198
+ │ - ... │
199
+ └──────────────────────┬────────────────────────────────────────┘
200
+
201
+
202
+ ┌──────────────────────────────────────────────────────────────┐
203
+ │ Step 3: Generate Registry Script │
204
+ │ Command: node scripts/generate-rules-registry.js │
205
+ │ │
206
+ │ Action: │
207
+ │ - Parse all *-en.md in origin-rules/ │
208
+ │ - Extract rule ID, name, description, etc. │
209
+ │ - Generate JSON structure │
210
+ └──────────────────────┬────────────────────────────────────────┘
211
+
212
+
213
+ ┌──────────────────────────────────────────────────────────────┐
214
+ │ Step 4: Generated Registry (AUTO-GENERATED) │
215
+ │ File: config/rules/rules-registry-generated.json │
216
+ │ │
217
+ │ Content: │
218
+ │ { │
219
+ │ "rules": { │
220
+ │ "D001": { │
221
+ │ "name": "Recommended Lint Rules...", │
222
+ │ "description": "...", │
223
+ │ ... │
224
+ │ } │
225
+ │ } │
226
+ │ } │
227
+ └──────────────────────┬────────────────────────────────────────┘
228
+
229
+
230
+ ┌──────────────────────────────────────────────────────────────┐
231
+ │ Step 5: Used by SunLint CLI & VSCode Extension │
232
+ │ │
233
+ │ Usage: │
234
+ │ - UnifiedRuleRegistry loads this JSON │
235
+ │ - Displays rule names to users │
236
+ │ - Shows descriptions in error messages │
237
+ └──────────────────────────────────────────────────────────────┘
238
+ ```
239
+
240
+ ---
241
+
242
+ ## 🛠️ Practical Usage
243
+
244
+ ### When Creating a New Dart Rule
245
+
246
+ ```bash
247
+ # 1. Create rule implementation
248
+ vim dart_analyzer/lib/rules/dart/D026_new_rule.dart
249
+
250
+ # 2. Register in analyzer service
251
+ vim dart_analyzer/lib/analyzer_service.dart
252
+
253
+ # 3. UPDATE MARKDOWN FILES (CRITICAL!)
254
+ vim ../../../rules/dart-en.md # Add rule definition
255
+ vim ../../../rules/dart.md # Add Vietnamese version
256
+
257
+ # 4. Create examples
258
+ vim ../../../rules/examples/en/D026.md
259
+ vim ../../../rules/examples/vi/D026.md
260
+
261
+ # 5. GENERATE REGISTRY (CRITICAL!)
262
+ node scripts/copy-rules.js
263
+ node scripts/generate-rules-registry.js
264
+
265
+ # 6. Rebuild Dart binary
266
+ cd dart_analyzer
267
+ dart compile exe bin/sunlint_dart_analyzer.dart -o bin/sunlint-dart-macos
268
+ cp bin/sunlint-dart-macos ../sunlint-dart-macos
269
+ cd ..
270
+
271
+ # 7. Test
272
+ node cli.js --rule=D026 --input=/path/to/project --languages=dart
273
+ ```
274
+
275
+ ### When Updating an Existing Rule Name
276
+
277
+ ```bash
278
+ # 1. UPDATE MARKDOWN FIRST
279
+ vim ../../../rules/dart-en.md
280
+ # Change: ### 📘 Rule D001 – OLD NAME
281
+ # To: ### 📘 Rule D001 – NEW NAME
282
+
283
+ vim ../../../rules/dart.md
284
+ # Update Vietnamese version too
285
+
286
+ # 2. REGENERATE REGISTRY
287
+ node scripts/copy-rules.js
288
+ node scripts/generate-rules-registry.js
289
+
290
+ # 3. Verify the change
291
+ grep -A 5 '"D001"' config/rules/rules-registry-generated.json
292
+
293
+ # 4. Update Dart code comment (optional, for consistency)
294
+ vim dart_analyzer/lib/rules/dart/D001_*.dart
295
+ # Update: /// D001: NEW NAME
296
+
297
+ # 5. Test to see new name
298
+ node cli.js --rule=D001 --input=/path/to/project --languages=dart
299
+ ```
300
+
301
+ ---
302
+
303
+ ## 📋 Files Reference
304
+
305
+ ### Source Files (Edit These)
306
+
307
+ | File | Purpose | When to Edit |
308
+ |------|---------|--------------|
309
+ | `../../../rules/dart-en.md` | Rule definitions (English) | Adding/updating Dart rules |
310
+ | `../../../rules/dart.md` | Rule definitions (Vietnamese) | Adding/updating Dart rules |
311
+ | `../../../rules/common-en.md` | Common rules definitions | Adding/updating common rules |
312
+ | `../../../rules/security-en.md` | Security rules definitions | Adding/updating security rules |
313
+
314
+ ### Intermediate Files (Auto-copied)
315
+
316
+ | File | Purpose | Generated By |
317
+ |------|---------|--------------|
318
+ | `origin-rules/dart-en.md` | Copied source | `scripts/copy-rules.js` |
319
+ | `origin-rules/common-en.md` | Copied source | `scripts/copy-rules.js` |
320
+
321
+ ### Generated Files (DO NOT EDIT MANUALLY)
322
+
323
+ | File | Purpose | Generated By |
324
+ |------|---------|--------------|
325
+ | `config/rules/rules-registry-generated.json` | Main registry | `scripts/generate-rules-registry.js` |
326
+
327
+ ### Other Registry Files
328
+
329
+ | File | Purpose | Notes |
330
+ |------|---------|-------|
331
+ | `config/rules-summary.json` | Summary view | May need manual update if not generated |
332
+ | `config/rules/enhanced-rules-registry.json` | Enhanced registry | Different format, separate system |
333
+
334
+ ---
335
+
336
+ ## ⚠️ Common Mistakes
337
+
338
+ ### ❌ Mistake 1: Edit Registry JSON Directly
339
+
340
+ ```bash
341
+ # WRONG! Will be overwritten
342
+ vim config/rules/rules-registry-generated.json
343
+ # Change: "name": "Old Name"
344
+ # To: "name": "New Name"
345
+ ```
346
+
347
+ **Why it's wrong:** Next time someone runs `generate-rules-registry.js`, your changes will be lost.
348
+
349
+ **Correct approach:**
350
+ ```bash
351
+ # RIGHT! Edit source markdown
352
+ vim ../../../rules/dart-en.md
353
+ # Then regenerate
354
+ node scripts/copy-rules.js && node scripts/generate-rules-registry.js
355
+ ```
356
+
357
+ ### ❌ Mistake 2: Put Display Name in config.json
358
+
359
+ ```json
360
+ // WRONG! Not used for display
361
+ {
362
+ "id": "D001",
363
+ "name": "My Display Name", // ❌ Not read by registry
364
+ "category": "dart"
365
+ }
366
+ ```
367
+
368
+ **Why it's wrong:** `config.json` is only used for rule detection, not for display names.
369
+
370
+ **Correct approach:**
371
+ ```markdown
372
+ <!-- RIGHT! Put in markdown -->
373
+ ### 📘 Rule D001 – My Display Name
374
+ ```
375
+
376
+ ### ❌ Mistake 3: Forget to Regenerate Registry
377
+
378
+ ```bash
379
+ # Edit markdown
380
+ vim ../../../rules/dart-en.md
381
+
382
+ # Test immediately
383
+ node cli.js --rule=D001 --input=... # ❌ Still shows old name!
384
+ ```
385
+
386
+ **Why it's wrong:** Registry hasn't been regenerated yet.
387
+
388
+ **Correct approach:**
389
+ ```bash
390
+ # Edit markdown
391
+ vim ../../../rules/dart-en.md
392
+
393
+ # MUST regenerate
394
+ node scripts/copy-rules.js
395
+ node scripts/generate-rules-registry.js
396
+
397
+ # Now test
398
+ node cli.js --rule=D001 --input=... # ✅ Shows new name
399
+ ```
400
+
401
+ ---
402
+
403
+ ## 🔍 Debugging Registry Issues
404
+
405
+ ### Check if Rule Exists in Registry
406
+
407
+ ```bash
408
+ # Search for rule in generated registry
409
+ grep -A 10 '"D001"' config/rules/rules-registry-generated.json
410
+ ```
411
+
412
+ Expected output:
413
+ ```json
414
+ "D001": {
415
+ "name": "Recommended Lint Rules Should Be Enabled",
416
+ "description": "Ensure code quality through standard lint configurations",
417
+ ...
418
+ }
419
+ ```
420
+
421
+ ### Check Markdown Format
422
+
423
+ ```bash
424
+ # Check rule definition format in markdown
425
+ grep -A 10 "### 📘 Rule D001" ../../../rules/dart-en.md
426
+ ```
427
+
428
+ Expected format:
429
+ ```markdown
430
+ ### 📘 Rule D001 – Recommended Lint Rules Should Be Enabled
431
+
432
+ - **Objective**: ...
433
+ - **Details**: ...
434
+ ```
435
+
436
+ ### Verify Copy Step
437
+
438
+ ```bash
439
+ # Check if markdown was copied to origin-rules
440
+ ls -la origin-rules/dart-en.md
441
+
442
+ # Compare with source
443
+ diff ../../../rules/dart-en.md origin-rules/dart-en.md
444
+ ```
445
+
446
+ Should show no differences (or only whitespace/timestamps).
447
+
448
+ ### Test Registry Parser
449
+
450
+ ```bash
451
+ # Run generator with verbose output
452
+ node scripts/generate-rules-registry.js 2>&1 | grep D001
453
+ ```
454
+
455
+ Expected:
456
+ ```
457
+ Parsed rule D001: 0 good examples, 0 bad examples, 0 configs
458
+ ```
459
+
460
+ ---
461
+
462
+ ## 📚 Related Documentation
463
+
464
+ - [CREATE_NEW_DART_RULE.md](./skills/CREATE_NEW_DART_RULE.md) - How to create new Dart rules
465
+ - [PROJECT_STRUCTURE.md](./PROJECT_STRUCTURE.md) - Overall project structure
466
+ - [rules/parser/rule-parser-simple.js](../rules/parser/rule-parser-simple.js) - Parser implementation
467
+
468
+ ---
469
+
470
+ ## 🎓 Key Takeaways
471
+
472
+ 1. **Markdown files** are the single source of truth for rule names and descriptions
473
+ 2. **Always regenerate** registry after updating markdown files
474
+ 3. **Never edit** `rules-registry-generated.json` manually
475
+ 4. **Copy → Generate** is a two-step process that must be done together
476
+ 5. **Verify** changes in generated JSON before testing
477
+
478
+ ---
479
+
480
+ ## 📝 Version History
481
+
482
+ ### v1.0.0 (2025-01-23)
483
+ - Initial documentation of registry generation flow
484
+ - Added complete flow diagram
485
+ - Added common mistakes and debugging sections
486
+ - Added practical usage examples