Skip to content

Commit 3af3ba8

Browse files
committed
fix: Correct comment extraction logic and remove console.log from tests
- Fix comment extraction to only check lines BETWEEN import declarations, not WITHIN multi-line imports - Remove all console.log statements from test files (tests should use assertions, not console output) - Prevents inline comments (e.g., /* mid */ B) from being incorrectly extracted as standalone comments - All 251 main tests + 162 comparison tests passing
1 parent 056225e commit 3af3ba8

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

comparison-test-harness/test-cases/09-demo-for-video.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ export class DemoComponent implements OnInit {
182182
2,
183183
'Should have exactly 2 blank lines after imports in legacy mode'
184184
);
185-
186-
console.log('✅ Demo file test PASSED - both extensions produce identical, expected output');
187185
});
188186

189187
test('129. Demo file - NEW extension with modern defaults (not legacy)', async () => {
@@ -309,8 +307,6 @@ export class DemoComponent implements OnInit {
309307
1,
310308
'Should have exactly 1 blank line after imports with modern defaults'
311309
);
312-
313-
console.log('✅ Demo file with modern defaults PASSED - 1 blank line as expected');
314310
});
315311

316312
});

comparison-test-harness/test-cases/999-manual-proof.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ const x = A + B + Z;
9797
`;
9898

9999
const oldResult = await organizeImportsOld(input, { disableImportRemovalOnOrganize: true });
100-
console.log('PROOF 1 - Old extension (no comments):', JSON.stringify(oldResult));
101100

102101
assert.strictEqual(oldResult, expected, 'Old extension sorts imports without comments');
103102
});
@@ -115,7 +114,6 @@ const x = A + B + Z;
115114
// Testing what old extension ACTUALLY does with trailing line comments
116115
// Disable removal to focus on comment handling
117116
const oldResult = await organizeImportsOld(input, { disableImportRemovalOnOrganize: true });
118-
console.log('PROOF 2 - Old extension (trailing comments):', JSON.stringify(oldResult));
119117

120118
// The old extension STRIPS trailing line comments
121119
const expected = `import { A, B, Z } from 'lib';
@@ -140,7 +138,6 @@ const x = A + B + Z;
140138
// Testing what old extension ACTUALLY does with leading block comments
141139
// Disable removal to focus on comment handling
142140
const oldResult = await organizeImportsOld(input, { disableImportRemovalOnOrganize: true });
143-
console.log('PROOF 3 - Old extension (leading block comment):', JSON.stringify(oldResult));
144141

145142
// The old extension STRIPS leading block comments
146143
const expected = `import { A, B, Z } from 'lib';

src/imports/import-manager.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -733,15 +733,29 @@ export class ImportManager {
733733
let importSectionEndLine = lastImport.getEndLineNumber() - 1;
734734

735735
// Extract comments between imports (old TypeScript Hero moves them after imports)
736+
// Only extract standalone comment lines that are BETWEEN import declarations,
737+
// not lines that are WITHIN a multi-line import declaration
736738
const commentsBetweenImports: string[] = [];
737739
for (let i = importSectionStartLine; i <= importSectionEndLine; i++) {
738-
const lineText = this.document.lineAt(i).text;
739-
const trimmedText = lineText.trim();
740-
// Check if line is a comment (and not an import statement)
741-
// Check the trimmed version but preserve the original with indentation
742-
if ((trimmedText.startsWith('//') || trimmedText.startsWith('/*') || trimmedText.startsWith('*')) &&
743-
!trimmedText.includes('import ')) {
744-
commentsBetweenImports.push(lineText);
740+
const lineNumber = i + 1; // Convert to 1-indexed for comparison with ts-morph
741+
742+
// Check if this line is within any import declaration
743+
const isWithinImport = allImports.some(imp => {
744+
const start = imp.getStartLineNumber();
745+
const end = imp.getEndLineNumber();
746+
return lineNumber >= start && lineNumber <= end;
747+
});
748+
749+
// Only check for standalone comments if the line is NOT within an import
750+
if (!isWithinImport) {
751+
const lineText = this.document.lineAt(i).text;
752+
const trimmedText = lineText.trim();
753+
const isStandaloneComment = (trimmedText.startsWith('//') || trimmedText.startsWith('/*') || trimmedText.startsWith('*')) &&
754+
!trimmedText.includes('import ');
755+
756+
if (isStandaloneComment) {
757+
commentsBetweenImports.push(lineText);
758+
}
745759
}
746760
}
747761

0 commit comments

Comments
 (0)