Skip to content

Commit 87bcc61

Browse files
committed
clean up comments, remove double parse() call
1 parent bb2a52f commit 87bcc61

File tree

1 file changed

+39
-76
lines changed

1 file changed

+39
-76
lines changed

index.ts

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,12 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
4646

4747
// First pass: collect all comments
4848
let comments: number[] = []
49-
parse(css, {
49+
let ast = parse(css, {
5050
on_comment: ({ start, end }) => {
5151
comments.push(start, end)
5252
},
5353
})
5454

55-
// Now parse the CSS without inline comments
56-
let ast = parse(css, {
57-
on_comment: () => {
58-
// Comments already collected in first pass
59-
},
60-
})
61-
6255
let depth = 0
6356

6457
function indent(size: number) {
@@ -72,55 +65,32 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
7265
}
7366

7467
/**
75-
* Get a comment from the CSS string after the first offset and before the second offset
68+
* Get and format comments from the CSS string within a range
7669
* @param after After which offset to look for comments
7770
* @param before Before which offset to look for comments
78-
* @param skip_inline Skip comments that are on the same line as other content
79-
* @returns The comment string, if found
71+
* @param level Indentation level (uses current depth if not specified)
72+
* @returns The formatted comment string, or empty string if no comment found
8073
*/
81-
function print_comment(after?: number, before?: number): string | undefined {
74+
function get_comment(after?: number, before?: number, level: number = depth): string {
8275
if (minify || after === undefined || before === undefined) {
83-
return undefined
76+
return EMPTY_STRING
8477
}
8578

8679
let buffer = EMPTY_STRING
8780
for (let i = 0; i < comments.length; i += 2) {
88-
// Check that the comment is within the range
8981
let start = comments[i]
9082
if (start === undefined || start < after) continue
9183
let end = comments[i + 1]
9284
if (end === undefined || end > before) break
9385

94-
// Special case for comments that follow another comment:
9586
if (buffer.length > 0) {
96-
buffer += NEWLINE + indent(depth)
87+
buffer += NEWLINE + indent(level)
9788
}
9889
buffer += css.slice(start, end)
9990
}
10091
return buffer
10192
}
10293

103-
/**
104-
* Format a comment with indentation and optional prefix/suffix
105-
* @param after After which offset to look for comments
106-
* @param before Before which offset to look for comments
107-
* @param level Indentation level (default: 0)
108-
* @param prefix String to prepend (default: '')
109-
* @param suffix String to append (default: '')
110-
* @returns Formatted comment string or empty string if no comment
111-
*/
112-
function format_comment(
113-
after?: number,
114-
before?: number,
115-
level: number = 0,
116-
prefix: string = EMPTY_STRING,
117-
suffix: string = EMPTY_STRING,
118-
): string {
119-
let comment = print_comment(after, before)
120-
if (!comment) return EMPTY_STRING
121-
return prefix + indent(level) + comment + suffix
122-
}
123-
12494
function unquote(str: string): string {
12595
return str.replace(/(?:^['"])|(?:['"]$)/g, EMPTY_STRING)
12696
}
@@ -397,11 +367,10 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
397367
let count = 0
398368
for (let selector of node) {
399369
count++
400-
// Add comment between selectors
401370
if (prev_end !== undefined) {
402-
let comment = format_comment(prev_end, selector.start, depth)
371+
let comment = get_comment(prev_end, selector.start)
403372
if (comment) {
404-
lines.push(comment)
373+
lines.push(indent(depth) + comment)
405374
}
406375
}
407376

@@ -421,31 +390,28 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
421390

422391
let children = node.children
423392
if (children.length === 0) {
424-
// Check if the block maybe contains comments
425-
let comment = format_comment(node.start, node.end, depth)
393+
let comment = get_comment(node.start, node.end)
426394
if (comment) {
427-
lines.push(comment)
395+
lines.push(indent(depth) + comment)
428396
depth--
429397
lines.push(indent(depth) + CLOSE_BRACE)
430398
return lines.join(NEWLINE)
431399
}
432400
}
433401

434-
// Add comment before first child
435402
let first_child = children[0]
436-
let comment_before_first = format_comment(node.start, first_child?.start, depth)
403+
let comment_before_first = get_comment(node.start, first_child?.start)
437404
if (comment_before_first) {
438-
lines.push(comment_before_first)
405+
lines.push(indent(depth) + comment_before_first)
439406
}
440407

441408
let prev_end: number | undefined
442409

443410
for (let child of children) {
444-
// Add comment between children
445411
if (prev_end !== undefined) {
446-
let comment = format_comment(prev_end, child.start, depth)
412+
let comment = get_comment(prev_end, child.start)
447413
if (comment) {
448-
lines.push(comment)
414+
lines.push(indent(depth) + comment)
449415
}
450416
}
451417

@@ -470,10 +436,9 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
470436
prev_end = child.end
471437
}
472438

473-
// Add comment after last child
474-
let comment_after_last_text = print_comment(prev_end, node.end)
475-
if (comment_after_last_text) {
476-
lines.push(indent(depth) + comment_after_last_text)
439+
let comment_after_last = get_comment(prev_end, node.end)
440+
if (comment_after_last) {
441+
lines.push(indent(depth) + comment_after_last)
477442
}
478443

479444
depth--
@@ -487,24 +452,25 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
487452
if (node.first_child?.type === NODE.SELECTOR_LIST) {
488453
let list = print_selector_list(node.first_child)
489454

490-
// Add comment after selectors and before opening brace
491-
let comment = format_comment(node.first_child.end, node.block?.start, depth)
455+
let comment = get_comment(node.first_child.end, node.block?.start)
492456
if (comment) {
493-
list += NEWLINE + comment
457+
list += NEWLINE + indent(depth) + comment
494458
}
495459

496460
list += OPTIONAL_SPACE + OPEN_BRACE
497461

498-
// Check if block is truly empty (no children and no comments)
499-
let block_has_comments = node.block && print_comment(node.block.start, node.block.end)
500-
if (!node.block?.has_children && !block_has_comments) {
462+
let block_has_content = node.block && (node.block.has_children || get_comment(node.block.start, node.block.end))
463+
if (!block_has_content) {
501464
list += CLOSE_BRACE
502465
}
503466
lines.push(list)
504467
}
505468

506-
if (node.block && (node.block.has_children || print_comment(node.block.start, node.block.end))) {
507-
lines.push(print_block(node.block))
469+
if (node.block) {
470+
let block_has_content = node.block.has_children || get_comment(node.block.start, node.block.end)
471+
if (block_has_content) {
472+
lines.push(print_block(node.block))
473+
}
508474
}
509475

510476
return lines.join(NEWLINE)
@@ -542,16 +508,18 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
542508
name.push(SEMICOLON)
543509
} else {
544510
name.push(OPTIONAL_SPACE, OPEN_BRACE)
545-
// Check if block is truly empty (no children and no comments)
546-
let block_has_comments = print_comment(node.block.start, node.block.end)
547-
if (node.block.is_empty && !block_has_comments) {
511+
let block_has_content = !node.block.is_empty || get_comment(node.block.start, node.block.end)
512+
if (!block_has_content) {
548513
name.push(CLOSE_BRACE)
549514
}
550515
}
551516
lines.push(name.join(EMPTY_STRING))
552517

553-
if (node.block !== null && (!node.block.is_empty || print_comment(node.block.start, node.block.end))) {
554-
lines.push(print_block(node.block))
518+
if (node.block !== null) {
519+
let block_has_content = !node.block.is_empty || get_comment(node.block.start, node.block.end)
520+
if (block_has_content) {
521+
lines.push(print_block(node.block))
522+
}
555523
}
556524

557525
return lines.join(NEWLINE)
@@ -562,14 +530,12 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
562530
let children = node.children
563531

564532
if (children.length === 0) {
565-
// Stylesheet with only comments
566-
return format_comment(0, node.end) ?? EMPTY_STRING
533+
return get_comment(0, node.end, 0)
567534
}
568535

569-
// Add comment before first child
570536
let first_child = children[0]
571537
if (first_child) {
572-
let comment_before_first = format_comment(0, first_child.start, 0)
538+
let comment_before_first = get_comment(0, first_child.start, 0)
573539
if (comment_before_first) {
574540
lines.push(comment_before_first)
575541
}
@@ -578,9 +544,8 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
578544
let prev_end: number | undefined
579545

580546
for (let child of node) {
581-
// Add comment between children
582547
if (prev_end !== undefined) {
583-
let comment = format_comment(prev_end, child.start, 0)
548+
let comment = get_comment(prev_end, child.start, 0)
584549
if (comment) {
585550
lines.push(comment)
586551
}
@@ -594,17 +559,15 @@ export function format(css: string, { minify = false, tab_size = undefined }: Fo
594559

595560
prev_end = child.end
596561

597-
// Only add blank line if there's a next child and no comment before it
598562
if (child.has_next) {
599-
let next_has_comment = child.next_sibling && print_comment(child.end, child.next_sibling.start)
563+
let next_has_comment = child.next_sibling && get_comment(child.end, child.next_sibling.start, 0)
600564
if (!next_has_comment) {
601565
lines.push(EMPTY_STRING)
602566
}
603567
}
604568
}
605569

606-
// Add comment after last child
607-
let comment_after_last = format_comment(prev_end, node.end, 0)
570+
let comment_after_last = get_comment(prev_end, node.end, 0)
608571
if (comment_after_last) {
609572
lines.push(comment_after_last)
610573
}

0 commit comments

Comments
 (0)