Skip to content

Commit f5d512e

Browse files
committed
fix
1 parent 45cdb79 commit f5d512e

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

src/languages/ts.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,27 @@ export default (options = {}) => {
136136

137137
/**
138138
* @param {Context} context
139-
* @param {{ line: number, column: number }} loc
139+
* @param {{ line: number, column: number } | null} from
140+
* @param {{ line: number, column: number }} to
140141
* @param {boolean} pad
141142
*/
142-
function flush_comments_until(context, loc, pad) {
143+
function flush_comments_until(context, from, to, pad) {
144+
let first = true;
145+
143146
while (comment_index < comments.length) {
144147
const comment = comments[comment_index];
145148

146-
if (comment && before(comment.loc.start, loc)) {
149+
if (comment && before(comment.loc.start, to)) {
150+
if (first && from !== null && comment.loc.start.line > from.line) {
151+
context.margin();
152+
context.newline();
153+
}
154+
155+
first = false;
156+
147157
write_comment(comment, context);
148158

149-
if (comment.loc.end.line < loc.line) {
159+
if (comment.loc.end.line < to.line) {
150160
context.newline();
151161
} else if (pad) {
152162
context.write(' ');
@@ -227,7 +237,7 @@ export default (options = {}) => {
227237
prev = child;
228238
}
229239

230-
flush_comments_until(context, until);
240+
flush_comments_until(context, nodes[nodes.length - 1]?.loc.end ?? null, until, false);
231241

232242
if (multiline) {
233243
context.dedent();
@@ -270,7 +280,12 @@ export default (options = {}) => {
270280

271281
if (node.loc) {
272282
context.newline();
273-
flush_comments_until(context, node.loc.end);
283+
flush_comments_until(
284+
context,
285+
node.body[node.body.length - 1]?.loc.end ?? null,
286+
node.loc.end,
287+
false
288+
);
274289
}
275290
}
276291

@@ -529,7 +544,7 @@ export default (options = {}) => {
529544
const is_statement = /(Statement|Declaration)$/.test(node.type);
530545

531546
if (node.loc) {
532-
flush_comments_until(context, node.loc.start, true);
547+
flush_comments_until(context, null, node.loc.start, true);
533548
}
534549

535550
visit(node);

0 commit comments

Comments
 (0)