Skip to content

Commit 44eff65

Browse files
authored
Formatter: Refactor Format Printer using withInlineMode (#1285)
Follow up on #1267, #1269, and #1270
1 parent ac2eb0a commit 44eff65

File tree

1 file changed

+55
-76
lines changed

1 file changed

+55
-76
lines changed

javascript/packages/formatter/src/format-printer.ts

Lines changed: 55 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,15 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
287287
return result
288288
}
289289

290+
private withInlineMode<T>(callback: () => T): T {
291+
const was = this.inlineMode
292+
this.inlineMode = true
293+
const result = callback()
294+
this.inlineMode = was
295+
296+
return result
297+
}
298+
290299
private withContentPreserving<T>(callback: () => T): T {
291300
const was = this.inContentPreservingContext
292301
this.inContentPreservingContext = true
@@ -353,11 +362,10 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
353362
if (herbDisableComments.length > 0) {
354363
const commentLines = this.capture(() => {
355364
herbDisableComments.forEach(comment => {
356-
const wasInlineMode = this.inlineMode
357-
this.inlineMode = true
358-
this.lines.push(" ")
359-
this.visit(comment)
360-
this.inlineMode = wasInlineMode
365+
this.withInlineMode(() => {
366+
this.lines.push(" ")
367+
this.visit(comment)
368+
})
361369
})
362370
})
363371

@@ -418,12 +426,9 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
418426
const hasTextFlow = this.textFlow.isInTextFlowContext(children)
419427

420428
if (hasTextFlow) {
421-
const wasInlineMode = this.inlineMode
422-
this.inlineMode = true
423-
424-
this.textFlow.visitTextFlowChildren(children)
425-
426-
this.inlineMode = wasInlineMode
429+
this.withInlineMode(() => {
430+
this.textFlow.visitTextFlowChildren(children)
431+
})
427432

428433
return
429434
}
@@ -574,13 +579,8 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
574579
this.withContentPreserving(() => {
575580
element.body.map(child => {
576581
if (isNode(child, HTMLElementNode)) {
577-
const wasInlineMode = this.inlineMode
578-
this.inlineMode = true
579-
580-
const formattedElement = this.capture(() => this.visit(child)).join("")
582+
const formattedElement = this.withInlineMode(() => this.capture(() => this.visit(child)).join(""))
581583
this.pushToLastLine(formattedElement)
582-
583-
this.inlineMode = wasInlineMode
584584
} else {
585585
this.pushToLastLine(IdentityPrinter.print(child))
586586
}
@@ -591,45 +591,44 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
591591
private visitInlineElementBody(body: Node[], tagName: string, hasTextFlow: boolean, children: Node[]) {
592592
if (children.length === 0) return
593593

594-
const oldInlineMode = this.inlineMode
595594
const nodesToRender = hasTextFlow ? body : children
596595

597596
const hasOnlyTextContent = nodesToRender.every(child => isNode(child, HTMLTextNode) || isNode(child, WhitespaceNode))
598597
const shouldPreserveSpaces = hasOnlyTextContent && isInlineElement(tagName)
599598

600-
this.inlineMode = true
601-
602-
const lines = this.capture(() => {
603-
nodesToRender.forEach(child => {
604-
if (isNode(child, HTMLTextNode)) {
605-
if (hasTextFlow) {
606-
const normalizedContent = child.content.replace(ASCII_WHITESPACE, ' ')
607-
608-
if (normalizedContent && normalizedContent !== ' ') {
609-
this.push(normalizedContent)
610-
} else if (normalizedContent === ' ') {
611-
this.push(' ')
612-
}
613-
} else {
614-
const normalizedContent = child.content.replace(ASCII_WHITESPACE, ' ')
615-
616-
if (shouldPreserveSpaces && normalizedContent) {
617-
this.push(normalizedContent)
618-
} else {
619-
const trimmedContent = normalizedContent.trim()
599+
const lines = this.withInlineMode(() => {
600+
return this.capture(() => {
601+
nodesToRender.forEach(child => {
602+
if (isNode(child, HTMLTextNode)) {
603+
if (hasTextFlow) {
604+
const normalizedContent = child.content.replace(ASCII_WHITESPACE, ' ')
620605

621-
if (trimmedContent) {
622-
this.push(trimmedContent)
606+
if (normalizedContent && normalizedContent !== ' ') {
607+
this.push(normalizedContent)
623608
} else if (normalizedContent === ' ') {
624609
this.push(' ')
625610
}
611+
} else {
612+
const normalizedContent = child.content.replace(ASCII_WHITESPACE, ' ')
613+
614+
if (shouldPreserveSpaces && normalizedContent) {
615+
this.push(normalizedContent)
616+
} else {
617+
const trimmedContent = normalizedContent.trim()
618+
619+
if (trimmedContent) {
620+
this.push(trimmedContent)
621+
} else if (normalizedContent === ' ') {
622+
this.push(' ')
623+
}
624+
}
626625
}
626+
} else if (isNode(child, WhitespaceNode)) {
627+
return
628+
} else {
629+
this.visit(child)
627630
}
628-
} else if (isNode(child, WhitespaceNode)) {
629-
return
630-
} else {
631-
this.visit(child)
632-
}
631+
})
633632
})
634633
})
635634

@@ -642,8 +641,6 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
642641
if (inlineContent) {
643642
this.pushToLastLine(inlineContent)
644643
}
645-
646-
this.inlineMode = oldInlineMode
647644
}
648645

649646
private stripLeadingHerbDisable(children: Node[], body: Node[]): {
@@ -1358,9 +1355,7 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
13581355
return this.capture(() => {
13591356
const savedIndentLevel = this.indentLevel
13601357
this.indentLevel = 0
1361-
this.inlineMode = true
1362-
this.visit(node)
1363-
this.inlineMode = false
1358+
this.withInlineMode(() => this.visit(node))
13641359
this.indentLevel = savedIndentLevel
13651360
}).join("")
13661361
}
@@ -1401,10 +1396,7 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
14011396
}
14021397
}
14031398

1404-
const oldInlineMode = this.inlineMode
1405-
this.inlineMode = true
1406-
const inlineContent = this.capture(() => this.visit(child)).join("")
1407-
this.inlineMode = oldInlineMode
1399+
const inlineContent = this.withInlineMode(() => this.capture(() => this.visit(child)).join(""))
14081400
this.pushToLastLine((hasSpaceBefore ? " " : "") + inlineContent)
14091401
}
14101402
}
@@ -1442,10 +1434,7 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
14421434
* Render an ERB node as a string
14431435
*/
14441436
renderERBAsString(node: ERBContentNode): string {
1445-
return this.capture(() => {
1446-
this.inlineMode = true
1447-
this.visit(node)
1448-
}).join("")
1437+
return this.withInlineMode(() => this.capture(() => this.visit(node)).join(""))
14491438
}
14501439

14511440
/**
@@ -1472,13 +1461,10 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
14721461
if (isNode(child, HTMLAttributeNode)) {
14731462
this.lines.push(" " + this.attributeRenderer.renderAttribute(child, name))
14741463
} else if (!(isNode(child, WhitespaceNode))) {
1475-
const wasInlineMode = this.inlineMode
1476-
1477-
this.inlineMode = true
1478-
1479-
this.lines.push(" ")
1480-
this.visit(child)
1481-
this.inlineMode = wasInlineMode
1464+
this.withInlineMode(() => {
1465+
this.lines.push(" ")
1466+
this.visit(child)
1467+
})
14821468
}
14831469
})
14841470
})
@@ -1491,15 +1477,11 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
14911477

14921478
const lines = this.capture(() => {
14931479
inlineNodes.forEach(node => {
1494-
const wasInlineMode = this.inlineMode
1495-
14961480
if (!isERBControlFlowNode(node)) {
1497-
this.inlineMode = true
1481+
this.withInlineMode(() => this.visit(node))
1482+
} else {
1483+
this.visit(node)
14981484
}
1499-
1500-
this.visit(node)
1501-
1502-
this.inlineMode = wasInlineMode
15031485
})
15041486
})
15051487

@@ -1597,10 +1579,7 @@ export class FormatPrinter extends Printer implements TextFlowDelegate, Attribut
15971579

15981580
result += childInline
15991581
} else if (!isNode(child, HTMLTextNode) && !isWhitespace) {
1600-
const wasInlineMode = this.inlineMode
1601-
this.inlineMode = true
1602-
const captured = this.capture(() => this.visit(child)).join("")
1603-
this.inlineMode = wasInlineMode
1582+
const captured = this.withInlineMode(() => this.capture(() => this.visit(child)).join(""))
16041583
result += captured
16051584
}
16061585
}

0 commit comments

Comments
 (0)