Skip to content

Conversation

@Christopher-C-Robinson
Copy link
Contributor

Summary of Changes

  • Added logic to detect and preserve trailing commented-out code or comments that appear after the final syntax node in a TypeScript file.
  • Introduced a new method getFileTrailer in SourceCodeAnalyzer to extract trailing comments from the EndOfFileToken.
  • Updated SourceCodeOrganizer to include the extracted file trailer in its processing pipeline.
  • Modified SourceCodePrinter.print() to correctly append the preserved file trailer after organized content.
  • Version bump from 2.0.7 to 2.0.15 in package-lock.json.

Purpose

  • Previously, any trailing comments such as commented-out code at the end of a file were being lost during source reorganization.
  • This update ensures that such comments are retained, preserving potentially useful context and preventing accidental deletion.

Details

  • SourceCodeAnalyzer.getFileTrailer(sourceFile) uses TypeScript's API to find comment ranges attached to the EndOfFileToken.
  • If such comments exist, their text is extracted and returned.
  • SourceCodeOrganizer.organizeSourceCode() collects this trailer and passes it to SourceCodePrinter.print().
  • SourceCodePrinter.print() now accepts a fileTrailer argument. It conditionally appends it to the output, adding appropriate newlines.
  • Ensures the final printed code includes all original, relevant trailing content.

Commit Summary

  • Implemented extraction of trailing comments from the AST.
  • Integrated file trailer support throughout the code organization process.
  • Updated source printer to output preserved trailer content.
  • Bumped version number to reflect enhancement.
  • Improved in-code documentation for maintainability.

* 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]>
Copilot AI review requested due to automatic review settings November 5, 2025 21:05
Copy link

Copilot AI left a 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 SourceCodeOrganizer to 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)
Copy link

Copilot AI Nov 5, 2025

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.

Suggested change
public static getFileTrailer(sourceFile: ts.SourceFile)
public static getFileTrailer(sourceFile: ts.SourceFile): string | null

Copilot uses AI. Check for mistakes.

if (printedSourceCode.length > 0)
const hasContent = printedSourceCode.length > 0;
const hasTrailer = fileTrailer && fileTrailer.length > 0;
Copy link

Copilot AI Nov 5, 2025

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;.

Suggested change
const hasTrailer = fileTrailer && fileTrailer.length > 0;
const hasTrailer = (fileTrailer?.length ?? 0) > 0;

Copilot uses AI. Check for mistakes.
@Christopher-C-Robinson
Copy link
Contributor Author

Christopher-C-Robinson commented Nov 5, 2025

@aljazsim

Fixes #107. Already created a .vsix file to verify with the last comment example on the issue. It doesn't delete it anymore.

* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant