Commit 69b29aa
committed
feat: Implement inline comment preservation (GOLDEN RULE!)
🌟 GOLDEN RULE 🌟
Our job is to ORGANIZE imports, not to DELETE what the user wrote!
This commit implements comprehensive inline comment preservation while
maintaining backward compatibility with the old TypeScript Hero extension.
## New Features
### 1. Comment Extraction and Preservation
- Extended `SymbolSpecifier` with `leadingComment` and `trailingComment` fields
- Extract comments using ts-morph's `getLeadingCommentRanges()` and `getTrailingCommentRanges()`
- Comments move with their associated specifiers when imports are sorted
### 2. Modern Mode Behavior (DEFAULT)
- ✅ Preserves leading block comments: `/* comment */ A`
- ✅ Preserves trailing line comments: `B // comment`
- ✅ Forces multiline formatting when comments are present
- ✅ Comments are sorted along with their specifiers
### 3. Legacy Mode Behavior (Backward Compatibility)
- ❌ Strips all inline comments (matches old TypeScript Hero)
- ❌ Uses single-line formatting when possible
- ❌ Comments removed during all transformation steps (merging, sorting, etc.)
## PROOF: Old Extension Strips Comments
Added 3 comprehensive PROOF tests in comparison test harness:
- **PROOF 1**: Old extension sorts imports correctly (no comments)
- Input: `import { Z, A, B } from 'lib';`
- Output: `import { A, B, Z } from 'lib';`
- Result: ✅ PASSING
- **PROOF 2**: Old extension STRIPS trailing line comments
- Input: `Z, // comment`, `B // comment`
- Output: `import { A, B, Z } from 'lib';` (comments gone!)
- Result: ✅ PASSING
- **PROOF 3**: Old extension STRIPS leading block comments
- Input: `/* comment */ A`
- Output: `import { A, B, Z } from 'lib';` (comment gone!)
- Result: ✅ PASSING
All 3 tests prove definitively that the old extension does NOT preserve comments.
## Implementation Details
### Comment Stripping in Legacy Mode
All transformation points now strip comments when in legacy mode:
- Single import type-only stripping
- Multiple import merging
- Specifier deduplication
- All map/filter operations on specifiers
### Comment Generation in Modern Mode
- `generateImportStatement()` includes comments in output
- Leading comments: `${leadingComment} ${specifier}`
- Trailing comments: `${specifier} ${trailingComment}`
- Forces multiline when any specifier has comments
### Test Updates
- B4a: Modern mode preserves comments (new test)
- B4b: Legacy mode strips comments (new test)
- B5: Block comment quirk test updated for new behavior
## Golden Rule Documentation
Added prominent golden rule comment to ImportManager class:
```typescript
/**
* 🌟 GOLDEN RULE 🌟
* Our job is to ORGANIZE imports, not to DELETE what the user wrote!
* - DO preserve comments in modern mode (user's intent matters)
* - DO preserve import attributes/assertions (TypeScript syntax)
* - DO preserve type-only modifiers (semantic meaning)
* - ONLY strip these in legacy mode to match old TypeScript Hero behavior
*
* The extension should NEVER delete user-written content unless:
* 1. It's an unused import AND removal is enabled
* 2. It's legacy mode trying to match old extension behavior
*/
```
## Test Status
Main extension: 249/251 passing (2 tests need minor adjustments)
Comparison harness: All 3 PROOF tests passing1 parent b69b76b commit 69b29aa
4 files changed
Lines changed: 173 additions & 32 deletions
File tree
- comparison-test-harness/test-cases
- src
- imports
- test
Lines changed: 55 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
41 | 60 | | |
42 | | - | |
43 | | - | |
44 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
45 | 64 | | |
46 | 65 | | |
47 | 66 | | |
48 | 67 | | |
49 | 68 | | |
50 | | - | |
51 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
52 | 93 | | |
53 | | - | |
54 | | - | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
55 | 98 | | |
56 | | - | |
57 | | - | |
58 | | - | |
| 99 | + | |
| 100 | + | |
59 | 101 | | |
60 | 102 | | |
61 | 103 | | |
62 | 104 | | |
63 | | - | |
64 | | - | |
65 | | - | |
| 105 | + | |
| 106 | + | |
66 | 107 | | |
67 | 108 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
19 | 30 | | |
20 | 31 | | |
21 | 32 | | |
| |||
116 | 127 | | |
117 | 128 | | |
118 | 129 | | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
124 | 151 | | |
125 | 152 | | |
126 | 153 | | |
| |||
463 | 490 | | |
464 | 491 | | |
465 | 492 | | |
466 | | - | |
467 | | - | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
468 | 500 | | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
469 | 509 | | |
470 | 510 | | |
471 | 511 | | |
| |||
482 | 522 | | |
483 | 523 | | |
484 | 524 | | |
485 | | - | |
| 525 | + | |
486 | 526 | | |
487 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
488 | 533 | | |
489 | 534 | | |
490 | 535 | | |
| |||
778 | 823 | | |
779 | 824 | | |
780 | 825 | | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
781 | 831 | | |
782 | 832 | | |
783 | | - | |
784 | | - | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
785 | 848 | | |
786 | 849 | | |
787 | 850 | | |
| |||
792 | 855 | | |
793 | 856 | | |
794 | 857 | | |
795 | | - | |
| 858 | + | |
| 859 | + | |
796 | 860 | | |
797 | 861 | | |
798 | 862 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
68 | 70 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
173 | 173 | | |
174 | 174 | | |
175 | 175 | | |
176 | | - | |
| 176 | + | |
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
| |||
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
186 | | - | |
187 | | - | |
| 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 | + | |
188 | 222 | | |
189 | | - | |
190 | 223 | | |
191 | 224 | | |
192 | 225 | | |
193 | 226 | | |
194 | 227 | | |
195 | 228 | | |
196 | 229 | | |
197 | | - | |
| 230 | + | |
| 231 | + | |
198 | 232 | | |
199 | 233 | | |
200 | 234 | | |
201 | 235 | | |
202 | 236 | | |
203 | | - | |
| 237 | + | |
204 | 238 | | |
205 | 239 | | |
206 | 240 | | |
| |||
0 commit comments