Repository navigation
fix(core): keep a comment after an import, so Organize Imports works in .tsrx files with one (#1024) - #1032
Merged
Conversation
…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]>
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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.
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.
…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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Fixes #1024.
Organize Imports, Sort Imports and Remove Unused Imports now change a
.tsrxfile 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).Sort Imports, before: no change. After, as TypeScript sorts the same imports in a
.tsfile:When
labelis not used, Remove Unused Imports and Organize Imports now remove the import line with its comment, as in a.tsfile.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: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.tsfile, Sort Imports drops those spaces from a line it moves.Mappings (
packages/tsrx/src/transform/segments.js):// @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) andisOrganizedImport(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
;. That is [Bug]: content-mapper: Organize, Sort and Remove Unused Imports do nothing in .tsrx files when an import does not end with a semicolon (TypeScript 7) #1023, and this PR does not change it. On TypeScript 5.9 and 6, imports without;work, with or without the comment.// @ts-expect-errorat the end of an import line. It then applies to the next line, as in a.tsfile. Before, the print dropped it.createJsxTransform, so it gets this fix with the next@tsrx/coreupdate.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.tsxcopy 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:getLineCommentsAfterandisOrganizedImport.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