Skip to content

Commit d5a2f29

Browse files
committed
fix(core): keep spaces after a block comment on an import line
The type-only print copied a same-line import comment and the spaces before it, and stopped at the end of the comment. A line comment's span already includes the spaces after it. A block comment ends at `*/`, so `import { a } from './a'; /* keep */ ` was printed without those spaces. TypeScript 5.9 and 6 still mapped the line, because the rest-of-line check trims trailing spaces. TypeScript 7 grows a verbatim span only over whitespace that is the same in both files, so it could not walk from the generated newline across the source-only spaces. Organize Imports, Sort Imports, and Remove Unused Imports then applied no edits. The print now copies the spaces and tabs after the last block comment. The rest of the line matches, and the import's span covers the newline the edit runs to.
1 parent cddec65 commit d5a2f29

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tsrx/core': patch
3+
---
4+
5+
**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.

‎packages/tsrx/src/transform/jsx/helpers.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,31 @@ export function tsx_with_ts_locations(
150150

151151
/**
152152
* Write the comments after an import or a re-export on its line, with the
153-
* spaces before them, as written. TypeScript then keeps a comment with its
154-
* import when Organize Imports moves the import, and the rest of the line
155-
* is the same as in the source, so an edit up to the next line maps back
156-
* (see `add_line_point` in `segments.js`).
153+
* spaces before them, as written, and the spaces after the last block
154+
* comment (a line comment's span already includes those). TypeScript then
155+
* keeps a comment with its import when Organize Imports moves the import,
156+
* and the rest of the line is the same as in the source, so an edit up to
157+
* the next line maps back (see `add_line_point` in `segments.js`).
158+
* TypeScript 7 grows a verbatim span only over matching whitespace.
157159
* @param {AST.Node} node
158160
* @param {ESRap.Context} context
159161
*/
160162
const write_line_comments = (node, context) => {
161163
if (!emitted_comments || source === undefined || !is_organized_import(node)) return;
162164
let end = /** @type {number} */ (node.end);
163-
for (const comment of get_line_comments_after(node, source)) {
165+
const comments = get_line_comments_after(node, source);
166+
for (const comment of comments) {
164167
emitted_comments.add(`${comment.start}:${comment.end}:${comment.type}:${comment.value}`);
165168
context.write(source.slice(end, comment.start));
166169
context.location(comment.loc.start.line, comment.loc.start.column);
167170
context.write(source.slice(comment.start, comment.end));
168171
context.location(comment.loc.end.line, comment.loc.end.column);
169172
end = comment.end;
170173
}
174+
if (comments.length === 0) return;
175+
let trailing = end;
176+
while (source[trailing] === ' ' || source[trailing] === '\t') trailing++;
177+
if (trailing !== end) context.write(source.slice(end, trailing));
171178
};
172179

173180
/**

‎packages/tsrx/tests/shared/source-mappings.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,12 @@ export function App() @{
19221922
["import { label } from './label'", ' // keep this'],
19231923
["import { label } from './label';", '\t/* first */ // second'],
19241924
["import { label } from './label';", ' /* keep this */'],
1925+
// Spaces or tabs after `*/` sit outside the comment. The print keeps
1926+
// them so the rest of the line matches and TypeScript 7 can extend
1927+
// the import's span over the newline an organize-imports edit runs to.
1928+
["import { label } from './label';", ' /* keep this */ '],
1929+
["import { label } from './label';", ' /* keep */\t'],
1930+
["import { label } from './label';", ' /* first */ /* second */ '],
19251931
])('keeps the comment after the import %j and maps its line (%j)', (statement, comment) => {
19261932
const source = `${statement}${comment}
19271933

0 commit comments

Comments
 (0)