Fix: TypeScript Code Organizer Deletes Commented-Out Code (#107)#119
Fix: TypeScript Code Organizer Deletes Commented-Out Code (#107)#119Christopher-C-Robinson wants to merge 2 commits intoaljazsim:masterfrom
Conversation
* Initial plan * Initial analysis: identify root cause of commented-out code deletion Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> * Fix: preserve trailing comments at end of file Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> * Address code review comments: add documentation and refactor logic Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> * Update src/tsco-cli/source-code/source-code-printer.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 <78235938+Christopher-C-Robinson@users.noreply.github.com> * Fix incomplete sanitization vulnerability by using replaceAll Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> * Update src/extension.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor: Extract duplicate pattern matching logic and fix event.waitUntil() timing Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Christopher-C-Robinson <78235938+Christopher-C-Robinson@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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