Skip to content

fix(core): keep a comment after an import, so Organize Imports works in .tsrx files with one (#1024) - #1032

Merged
leonidaz merged 4 commits into
mainfrom
fix/import-trailing-comment-1024
Oct 3, 2026
Merged

leonidaz merged 4 commits into
mainfrom
fix/import-trailing-comment-1024

Conversation

@leonidaz

@leonidaz leonidaz commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1024.

Organize Imports, Sort Imports and Remove Unused Imports now change a .tsrx file in which a comment follows an import on the same line. They work on TypeScript 5.9 and 6 (with @tsrx/typescript-plugin) and on TypeScript 7 (with @tsrx/content-mapper).

import { useState } from 'react';
import { label } from './label'; // keep this
import { App } from './App.tsrx';

export function Imports() @{
	const [count] = useState(0);
	<p>{count}{label}<App /></p>
}

Sort Imports, before: no change. After, as TypeScript sorts the same imports in a .ts file:

import { useState } from 'react';
import { App } from './App.tsrx';
import { label } from './label'; // keep this

When label is not used, Remove Unused Imports and Organize Imports now remove the import line with its comment, as in a .ts file.

Cause

The code that TypeScript checks did not contain the comment. TypeScript's edits did not contain it either, and the end of the import line was different text in the two files, so neither path could map an edit up to the next line.

Changes

Editor print (packages/tsrx/src/transform/jsx/helpers.js). After an import or a re-export, the type-only print writes the rest of its line as written, when the rest has only comments and spaces:

// source
import { label } from './label';	/* first */  // second
// generated, before
import { label } from './label';
// generated, after
import { label } from './label';	/* first */  // second

A comment over several lines, or code after the import, is not written, as before. Only type-only output changes; build output does not.

The spaces at the end of the line are written too, also when no comment follows the import. TypeScript 7 maps an edit up to the next line only over the same text, so import { label } from './label';␣␣ (two spaces at the end) also got no edits on TypeScript 7 before, on main too. As in a .ts file, Sort Imports drops those spaces from a line it moves.

Mappings (packages/tsrx/src/transform/segments.js):

  • TypeScript 5.9 and 6: the whole-import mapping gets its point at the start of the next line when the rest of the line is the same text in both files. Before, the rest of the line had to be spaces only.
  • TypeScript 7: the comments get one mapping with the same data as the import's tokens. The content mapper then joins the whole line into one span of identical text, which TypeScript 7 needs to apply an edit. A tooling comment there (such as // @ts-ignore) does not also get a verification-only mapping, which would split the span.

New exports in @tsrx/core: getLineCommentsAfter (the comments after a statement on its line) and isOrganizedImport (an import or a re-export). A compiler with its own type-only printer can use them to print the same comments.

The unreleased changeset of #1022 said that these actions still did nothing with such a comment. I removed that paragraph.

Notes

Tests

  • packages/tsrx/tests/shared/source-mappings.js (all targets): the generated line keeps the rest of the line, and the edit up to the next line maps to the source line with its comment: ; or no ;, a tab and two comments, a block comment, a block comment with spaces after it, only spaces, and a re-export. A comment over several lines, and code after the import, still leave the line unmapped.
  • packages/typescript-plugin/tests/plugin-imports.test.js (tsserver 5.9): Sort, Remove Unused and Organize Imports on a file with the comment, with and without ;. A .tsx copy of the file gives the same result.
  • packages/content-mapper/tests/native-lsp.test.js (TypeScript 7): the actions now give the expected file after a line comment, a block comment with spaces after it, and only spaces. Before, they gave no edits.
  • packages/tsrx/tests/utils/comment-utils.test.js: getLineCommentsAfter and isOrganizedImport.

Local runs: tsrx-react, tsrx-preact, tsrx-solid, tsrx-vue, tsrx-hono, typescript-plugin, content-mapper, language-server, tsrx-utils (5743 passed), pnpm typecheck, pnpm format:check.

🤖 Generated with Claude Code

…anize Imports works with it (#1024)

The type-only print now writes the comments that follow an import or a
re-export on its line after the statement, as written, with the spaces
before them. TypeScript sees the comment and moves it with its import,
as in a `.ts` file, and the rest of the line is the same text in both
files.

- TypeScript 5.9 and 6: the whole-import mapping gets its next-line
  point when the rest of the line is the same in both files, not only
  when it is whitespace.
- TypeScript 7: the comments map with the import's token data, so the
  content mapper joins the line into one span of identical text. They
  do not also get a verification-only mapping.

A comment over several lines, or code after the import, still leaves
the line unmapped. A printer that leaves the comment out (Ripple's)
keeps today's behavior. Core exports `getLineCommentsAfter` and
`isOrganizedImport` for such printers.

The unreleased changeset of #1022 no longer says that a comment after
an import blocks the import actions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Block comment trailing spaces break mapping
    • The type-only printer now copies spaces and tabs after the last same-line block comment on an import, so the generated line matches the source and TypeScript 7 can extend the verbatim span through the following newline.

Create PR

Or push these changes by commenting:

@cursor push d5a2f298c1
Preview (d5a2f298c1)
diff --git a/.changeset/import-block-comment-trailing-spaces.md b/.changeset/import-block-comment-trailing-spaces.md
new file mode 100644
--- /dev/null
+++ b/.changeset/import-block-comment-trailing-spaces.md
@@ -1,0 +1,5 @@
+---
+'@tsrx/core': patch
+---
+
+**Organize Imports**, **Sort Imports** and **Remove Unused Imports** now change a `.tsrx` file when spaces follow a block comment on an import's line, as in `import { a } from './a'; /* keep */   `. On TypeScript 7, these actions did not change such a file before.

diff --git a/packages/tsrx/src/transform/jsx/helpers.js b/packages/tsrx/src/transform/jsx/helpers.js
--- a/packages/tsrx/src/transform/jsx/helpers.js
+++ b/packages/tsrx/src/transform/jsx/helpers.js
@@ -150,17 +150,20 @@
 
 	/**
 	 * Write the comments after an import or a re-export on its line, with the
-	 * spaces before them, as written. TypeScript then keeps a comment with its
-	 * import when Organize Imports moves the import, and the rest of the line
-	 * is the same as in the source, so an edit up to the next line maps back
-	 * (see `add_line_point` in `segments.js`).
+	 * spaces before them, as written, and the spaces after the last block
+	 * comment (a line comment's span already includes those). TypeScript then
+	 * keeps a comment with its import when Organize Imports moves the import,
+	 * and the rest of the line is the same as in the source, so an edit up to
+	 * the next line maps back (see `add_line_point` in `segments.js`).
+	 * TypeScript 7 grows a verbatim span only over matching whitespace.
 	 * @param {AST.Node} node
 	 * @param {ESRap.Context} context
 	 */
 	const write_line_comments = (node, context) => {
 		if (!emitted_comments || source === undefined || !is_organized_import(node)) return;
 		let end = /** @type {number} */ (node.end);
-		for (const comment of get_line_comments_after(node, source)) {
+		const comments = get_line_comments_after(node, source);
+		for (const comment of comments) {
 			emitted_comments.add(`${comment.start}:${comment.end}:${comment.type}:${comment.value}`);
 			context.write(source.slice(end, comment.start));
 			context.location(comment.loc.start.line, comment.loc.start.column);
@@ -168,6 +171,10 @@
 			context.location(comment.loc.end.line, comment.loc.end.column);
 			end = comment.end;
 		}
+		if (comments.length === 0) return;
+		let trailing = end;
+		while (source[trailing] === ' ' || source[trailing] === '\t') trailing++;
+		if (trailing !== end) context.write(source.slice(end, trailing));
 	};
 
 	/**

diff --git a/packages/tsrx/tests/shared/source-mappings.js b/packages/tsrx/tests/shared/source-mappings.js
--- a/packages/tsrx/tests/shared/source-mappings.js
+++ b/packages/tsrx/tests/shared/source-mappings.js
@@ -1922,6 +1922,12 @@
 			["import { label } from './label'", ' // keep this'],
 			["import { label } from './label';", '\t/* first */  // second'],
 			["import { label } from './label';", ' /* keep this */'],
+			// Spaces or tabs after `*/` sit outside the comment. The print keeps
+			// them so the rest of the line matches and TypeScript 7 can extend
+			// the import's span over the newline an organize-imports edit runs to.
+			["import { label } from './label';", ' /* keep this */   '],
+			["import { label } from './label';", ' /* keep */\t'],
+			["import { label } from './label';", ' /* first */  /* second */   '],
 		])('keeps the comment after the import %j and maps its line (%j)', (statement, comment) => {
 			const source = `${statement}${comment}

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit cddec65. Configure here.

leonidaz and others added 3 commits October 3, 2026 02:40
…or print

TypeScript 7 maps an edit up to the next line only over the same text
in both files. The editor print now also writes the spaces after the
last comment, or directly after the import or re-export, when only
spaces follow on its line. Before, Organize, Sort and Remove Unused
Imports returned no edits on TypeScript 7 for such a line (a Bugbot
finding for block comments; the case without a comment fails on main
too). As in a `.ts` file, a moved import loses those spaces.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit d2f42e8 into main Oct 3, 2026
7 checks passed
@leonidaz
leonidaz deleted the fix/import-trailing-comment-1024 branch October 3, 2026 15:07
@github-actions github-actions Bot mentioned this pull request Oct 3, 2026
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.

[Bug]: tsrx-core: Organize, Sort and Remove Unused Imports do nothing in .tsrx files when a comment follows an import on its line

1 participant