Skip to content

Commit e332a42

Browse files
authored
Formatter: Keep herb:disable on same line as tag-opening (marcoroth#946)
Resolves marcoroth#913
1 parent 1429d15 commit e332a42

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,37 @@ export class FormatPrinter extends Printer {
709709
* Render multiline attributes for a tag
710710
*/
711711
private renderMultilineAttributes(tagName: string, allChildren: Node[] = [], isSelfClosing: boolean = false,) {
712-
this.pushWithIndent(`<${tagName}`)
712+
const herbDisableComments = allChildren.filter(child =>
713+
isNode(child, ERBContentNode) && isHerbDisableComment(child)
714+
)
715+
716+
let openingLine = `<${tagName}`
717+
718+
if (herbDisableComments.length > 0) {
719+
const commentLines = this.capture(() => {
720+
herbDisableComments.forEach(comment => {
721+
const wasInlineMode = this.inlineMode
722+
this.inlineMode = true
723+
this.lines.push(" ")
724+
this.visit(comment)
725+
this.inlineMode = wasInlineMode
726+
})
727+
})
728+
729+
openingLine += commentLines.join("")
730+
}
731+
732+
this.pushWithIndent(openingLine)
713733

714734
this.withIndent(() => {
715735
allChildren.forEach(child => {
716736
if (isNode(child, HTMLAttributeNode)) {
717737
this.pushWithIndent(this.renderAttribute(child))
718738
} else if (!isNode(child, WhitespaceNode)) {
739+
if (isNode(child, ERBContentNode) && isHerbDisableComment(child)) {
740+
return
741+
}
742+
719743
this.visit(child)
720744
}
721745
})

javascript/packages/formatter/test/herb-disable-comment-formatting.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,20 @@ describe("herb:disable comment formatting", () => {
324324

325325
expect(result).toBe(source)
326326
})
327+
328+
test("keeps herb:disable comment on same line as tag name in multiline opening tag", () => {
329+
const source = dedent`
330+
<a <%# herb:disable html-anchor-require-href %>
331+
class="btn btn-secondary no-donate-btn"
332+
aria-label="Close"
333+
data-dismiss="modal"
334+
>
335+
Close
336+
</a>
337+
`
338+
339+
const result = formatter.format(source)
340+
341+
expect(result).toBe(source)
342+
})
327343
})

0 commit comments

Comments
 (0)