Skip to content

Commit fd197fe

Browse files
bartvenemanclaude
andauthored
perf: shorten some print functons && use string concat directly in some places (#179)
## Summary This PR refactors multiple string-building operations throughout the CSS formatter to use direct string concatenation instead of building arrays and joining them. This simplifies the code and improves readability while maintaining the same functionality. ## Key Changes - **`print_operator()`**: Replaced array-based building with direct concatenation using ternary operators to determine spacing rules - **`print_declaration()`**: Changed `important` from array to string, simplified the logic for extracting and formatting the `!important` flag - **`print_nth()`**: Replaced parts array with direct string concatenation and template literals - **`print_nth_of()`**: Converted from array building to direct string concatenation - **`print_compound_selector()`**: Simplified to use `map().join()` instead of manual forEach loop - **`print_rule()`**: Moved `block_has_content` calculation to the top to avoid duplication, simplified block rendering logic - **`print_atrule()`**: Refactored name building from array to string concatenation, consolidated `block_has_content` logic to avoid recalculation, simplified return statements ## Notable Implementation Details - All changes maintain the exact same output formatting and CSS specification compliance - The refactoring reduces intermediate array allocations and simplifies control flow - Comments explaining CSS specification requirements (e.g., spacing around `+` and `-` operators) are preserved https://claude.ai/code/session_01P6rhwrSj4m7rKSqqsPUqgz Co-authored-by: Claude <noreply@anthropic.com>
1 parent be22838 commit fd197fe

File tree

1 file changed

+33
-79
lines changed

1 file changed

+33
-79
lines changed

src/lib/index.ts

Lines changed: 33 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -101,37 +101,14 @@ export function format(
101101
}
102102

103103
function print_operator(node: CSSNode): string {
104-
let parts = []
105104
// https://developer.mozilla.org/en-US/docs/Web/CSS/calc#notes
106105
// The + and - operators must be surrounded by whitespace
107106
// Whitespace around other operators is optional
108-
109107
let operator = node.text
110108
let code = operator.charCodeAt(0)
111-
112-
if (code === 43 || code === 45) {
113-
// + or -
114-
// Add required space before + and - operators
115-
parts.push(SPACE)
116-
} else if (code !== 44) {
117-
// ,
118-
// Add optional space before operator
119-
parts.push(OPTIONAL_SPACE)
120-
}
121-
122-
// FINALLY, render the operator
123-
parts.push(operator)
124-
125-
if (code === 43 || code === 45) {
126-
// + or -
127-
// Add required space after + and - operators
128-
parts.push(SPACE)
129-
} else {
130-
// Add optional space after other operators (like *, /, and ,)
131-
parts.push(OPTIONAL_SPACE)
132-
}
133-
134-
return parts.join(EMPTY_STRING)
109+
// + or - require spaces; comma has no leading space; others use optional space
110+
let space = code === 43 || code === 45 ? SPACE : OPTIONAL_SPACE
111+
return (code === 44 ? EMPTY_STRING : space) + operator + space
135112
}
136113

137114
function print_list(nodes: CSSNode[]): string {
@@ -182,13 +159,12 @@ export function format(
182159
}
183160

184161
function print_declaration(node: CSSNode): string {
185-
let important = []
162+
let important = EMPTY_STRING
186163
if (node.is_important) {
187164
let text = node.text
188-
let has_semicolon = text.endsWith(SEMICOLON)
189165
let start = text.lastIndexOf('!')
190-
let end = has_semicolon ? -1 : undefined
191-
important.push(OPTIONAL_SPACE, text.slice(start, end).toLowerCase())
166+
important =
167+
OPTIONAL_SPACE + text.slice(start, text.endsWith(SEMICOLON) ? -1 : undefined).toLowerCase()
192168
}
193169
let value = print_value(node.value as CSSNode[] | null)
194170
let property = node.property!
@@ -206,40 +182,32 @@ export function format(
206182
if (!property.startsWith('--')) {
207183
property = property.toLowerCase()
208184
}
209-
return property + COLON + OPTIONAL_SPACE + value + important.join(EMPTY_STRING)
185+
return property + COLON + OPTIONAL_SPACE + value + important
210186
}
211187

212188
function print_nth(node: CSSNode): string {
213-
let parts = []
214189
let a = node.nth_a
215190
let b = node.nth_b
216-
217-
if (a) {
218-
parts.push(a)
219-
}
220-
if (a && b) {
221-
parts.push(OPTIONAL_SPACE)
222-
}
191+
let result = a ? `${a}` : EMPTY_STRING
223192
if (b) {
224-
if (a && !b.startsWith('-')) {
225-
parts.push('+', OPTIONAL_SPACE)
193+
if (a) {
194+
result += OPTIONAL_SPACE
195+
if (!b.startsWith('-')) result += '+' + OPTIONAL_SPACE
226196
}
227-
parts.push(parseFloat(b))
197+
result += parseFloat(b)
228198
}
229-
230-
return parts.join(EMPTY_STRING)
199+
return result
231200
}
232201

233202
function print_nth_of(node: CSSNode): string {
234-
let parts = []
203+
let result = EMPTY_STRING
235204
if (node.children[0]?.type === NODE.NTH_SELECTOR) {
236-
parts.push(print_nth(node.children[0]))
237-
parts.push(SPACE, 'of', SPACE)
205+
result = print_nth(node.children[0]) + SPACE + 'of' + SPACE
238206
}
239207
if (node.children[1]?.type === NODE.SELECTOR_LIST) {
240-
parts.push(print_inline_selector_list(node.children[1]))
208+
result += print_inline_selector_list(node.children[1])
241209
}
242-
return parts.join(EMPTY_STRING)
210+
return result
243211
}
244212

245213
function print_simple_selector(node: CSSNode, is_first: boolean = false): string {
@@ -330,12 +298,7 @@ export function format(
330298
}
331299

332300
// Handle compound selector (combination of simple selectors)
333-
let parts: string[] = []
334-
node.children.forEach((child, index) => {
335-
const part = print_simple_selector(child, index === 0)
336-
parts.push(part)
337-
})
338-
return parts.join(EMPTY_STRING)
301+
return node.children.map((child, i) => print_simple_selector(child, i === 0)).join(EMPTY_STRING)
339302
}
340303

341304
function print_inline_selector_list(node: CSSNode): string {
@@ -433,6 +396,8 @@ export function format(
433396
}
434397

435398
function print_rule(node: CSSNode): string {
399+
let block_has_content =
400+
node.block && (node.block.has_children || get_comment(node.block.start, node.block.end))
436401
let lines = []
437402

438403
if (node.first_child?.type === NODE.SELECTOR_LIST) {
@@ -444,21 +409,14 @@ export function format(
444409
}
445410

446411
list += OPTIONAL_SPACE + OPEN_BRACE
447-
448-
let block_has_content =
449-
node.block && (node.block.has_children || get_comment(node.block.start, node.block.end))
450412
if (!block_has_content) {
451413
list += CLOSE_BRACE
452414
}
453415
lines.push(list)
454416
}
455417

456-
if (node.block) {
457-
let block_has_content =
458-
node.block.has_children || get_comment(node.block.start, node.block.end)
459-
if (block_has_content) {
460-
lines.push(print_block(node.block))
461-
}
418+
if (block_has_content) {
419+
lines.push(print_block(node.block!))
462420
}
463421

464422
return lines.join(NEWLINE)
@@ -490,31 +448,27 @@ export function format(
490448
}
491449

492450
function print_atrule(node: CSSNode): string {
493-
let lines = []
494-
let name = [`@`, node.name!.toLowerCase()]
451+
let name = '@' + node.name!.toLowerCase()
495452
if (node.prelude) {
496-
name.push(SPACE, print_atrule_prelude(node.prelude.text))
453+
name += SPACE + print_atrule_prelude(node.prelude.text)
497454
}
498455

456+
let block_has_content =
457+
node.block !== null &&
458+
(!node.block.is_empty || !!get_comment(node.block.start, node.block.end))
499459
if (node.block === null) {
500-
name.push(SEMICOLON)
460+
name += SEMICOLON
501461
} else {
502-
name.push(OPTIONAL_SPACE, OPEN_BRACE)
503-
let block_has_content = !node.block.is_empty || get_comment(node.block.start, node.block.end)
462+
name += OPTIONAL_SPACE + OPEN_BRACE
504463
if (!block_has_content) {
505-
name.push(CLOSE_BRACE)
464+
name += CLOSE_BRACE
506465
}
507466
}
508-
lines.push(name.join(EMPTY_STRING))
509467

510-
if (node.block !== null) {
511-
let block_has_content = !node.block.is_empty || get_comment(node.block.start, node.block.end)
512-
if (block_has_content) {
513-
lines.push(print_block(node.block))
514-
}
468+
if (block_has_content) {
469+
return name + NEWLINE + print_block(node.block!)
515470
}
516-
517-
return lines.join(NEWLINE)
471+
return name
518472
}
519473

520474
function print_stylesheet(node: CSSNode): string {

0 commit comments

Comments
 (0)