-
Notifications
You must be signed in to change notification settings - Fork 13
Fix: TypeScript Code Organizer Deletes Commented-Out Code (#107) #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix: TypeScript Code Organizer Deletes Commented-Out Code (#107) #119
Conversation
* Initial plan * Initial analysis: identify root cause of commented-out code deletion Co-authored-by: Christopher-C-Robinson <[email protected]> * Fix: preserve trailing comments at end of file Co-authored-by: Christopher-C-Robinson <[email protected]> * Address code review comments: add documentation and refactor logic Co-authored-by: Christopher-C-Robinson <[email protected]> * Update src/tsco-cli/source-code/source-code-printer.ts Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Christopher-C-Robinson <[email protected]> Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds support for preserving trailing comments at the end of TypeScript files during code reorganization. This ensures that commented-out code or other comments appearing after the last syntax node are not lost when the tool reorganizes the file.
Key changes:
- Added
getFileTrailer()method to extract trailing comments from the end of files - Updated
SourceCodePrinter.print()to accept and properly format file trailers - Modified
SourceCodeOrganizerto extract and pass file trailers through the printing pipeline
Reviewed Changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/tsco-cli/source-code/source-code-analyzer.ts | Implements new getFileTrailer() method to extract trailing comments attached to the EndOfFileToken |
| src/tsco-cli/source-code/source-code-printer.ts | Updates print() method signature to accept fileTrailer parameter and adds logic to append trailers with proper spacing |
| src/tsco-cli/source-code/source-code-organizer.ts | Integrates file trailer extraction into the source code organization workflow |
| package-lock.json | Version bump from 2.0.7 to 2.0.15 |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| * @param sourceFile The TypeScript source file to analyze | ||
| * @returns The trailing comments text, or null if there are no trailing comments | ||
| */ | ||
| public static getFileTrailer(sourceFile: ts.SourceFile) |
Copilot
AI
Nov 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method should have an explicit return type annotation. Add : string | null to match the documented return type in the JSDoc.
| public static getFileTrailer(sourceFile: ts.SourceFile) | |
| public static getFileTrailer(sourceFile: ts.SourceFile): string | null |
|
|
||
| if (printedSourceCode.length > 0) | ||
| const hasContent = printedSourceCode.length > 0; | ||
| const hasTrailer = fileTrailer && fileTrailer.length > 0; |
Copilot
AI
Nov 5, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hasTrailer check is redundant because fileTrailer.length > 0 already returns false for null/undefined values due to short-circuit evaluation. Consider simplifying to const hasTrailer = !!fileTrailer && fileTrailer.length > 0; for clarity, or use optional chaining: const hasTrailer = (fileTrailer?.length ?? 0) > 0;.
| const hasTrailer = fileTrailer && fileTrailer.length > 0; | |
| const hasTrailer = (fileTrailer?.length ?? 0) > 0; |
* Initial plan * Fix auto-save on focus change by using event.waitUntil() API * Fix pre-commit hook to use correct commands Co-authored-by: Christopher-C-Robinson <[email protected]> * Fix incomplete sanitization vulnerability by using replaceAll Co-authored-by: Christopher-C-Robinson <[email protected]> * Update src/extension.ts Co-authored-by: Copilot <[email protected]> * Refactor: Extract duplicate pattern matching logic and fix event.waitUntil() timing Co-authored-by: Christopher-C-Robinson <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Christopher-C-Robinson <[email protected]> Co-authored-by: Copilot <[email protected]>
Summary of Changes
getFileTrailerinSourceCodeAnalyzerto extract trailing comments from theEndOfFileToken.SourceCodeOrganizerto include the extracted file trailer in its processing pipeline.SourceCodePrinter.print()to correctly append the preserved file trailer after organized content.2.0.7to2.0.15inpackage-lock.json.Purpose
Details
SourceCodeAnalyzer.getFileTrailer(sourceFile)uses TypeScript's API to find comment ranges attached to theEndOfFileToken.SourceCodeOrganizer.organizeSourceCode()collects this trailer and passes it toSourceCodePrinter.print().SourceCodePrinter.print()now accepts afileTrailerargument. It conditionally appends it to the output, adding appropriate newlines.Commit Summary