Skip to content

Commit 7d5d41e

Browse files
authored
Formatter: Support formatting case/in statements (#345)
This pull request adds support for formatting `case/in` statements. Previously it would only format the `case` line and omit all `in` nodes. Now an input like: ```erb <% case { hash: { nested: '4' } } %> <% in { hash: { nested: } } %> <span>there</span> <% in { another: } %> <span>there</span> <% in { yet_another: { nested: } } %> <span>hi</span> <% else %> <span>there</span> <% end %> ``` Will now format as: ```erb <% case { hash: { nested: '4' } } %> <% in { hash: { nested: } } %> <span>there</span> <% in { another: } %> <span>there</span> <% in { yet_another: { nested: } } %> <span>hi</span> <% else %> <span>there</span> <% end %> ```
1 parent 99a86f5 commit 7d5d41e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

javascript/packages/formatter/src/printer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export class Printer extends Visitor {
137137
node instanceof ERBUnlessNode || (node as any).type === 'AST_ERB_UNLESS_NODE' ||
138138
node instanceof ERBBlockNode || (node as any).type === 'AST_ERB_BLOCK_NODE' ||
139139
node instanceof ERBCaseNode || (node as any).type === 'AST_ERB_CASE_NODE' ||
140+
node instanceof ERBCaseMatchNode || (node as any).type === 'AST_ERB_CASE_MATCH_NODE' ||
140141
node instanceof ERBWhileNode || (node as any).type === 'AST_ERB_WHILE_NODE' ||
141142
node instanceof ERBForNode || (node as any).type === 'AST_ERB_FOR_NODE'
142143
}
@@ -895,10 +896,19 @@ export class Printer extends Visitor {
895896

896897
visitERBInNode(node: ERBInNode): void {
897898
this.printERBNode(node)
899+
900+
this.withIndent(() => {
901+
node.statements.forEach(stmt => this.visit(stmt))
902+
})
898903
}
899904

900905
visitERBCaseMatchNode(node: ERBCaseMatchNode): void {
901906
this.printERBNode(node)
907+
908+
node.conditions.forEach(condition => this.visit(condition))
909+
910+
if (node.else_clause) this.visit(node.else_clause)
911+
if (node.end_node) this.visit(node.end_node)
902912
}
903913

904914
visitERBBlockNode(node: ERBBlockNode): void {

javascript/packages/formatter/test/erb/case.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,53 @@ describe("@herb-tools/formatter", () => {
5151
</div>
5252
`)
5353
})
54+
55+
test("formats ERB case/in statements", () => {
56+
const source = dedent`
57+
<% case { hash: { nested: '4' } } %>
58+
<% in { hash: { nested: } } %>
59+
<span>there</span>
60+
<% in { another: } %>
61+
<span>there</span>
62+
<% in { yet_another: { nested: } } %>
63+
<span>hi</span>
64+
<% else %>
65+
<span>there</span>
66+
<% end %>
67+
`
68+
const result = formatter.format(source)
69+
expect(result).toEqual(dedent`
70+
<% case { hash: { nested: '4' } } %>
71+
<% in { hash: { nested: } } %>
72+
<span>there</span>
73+
<% in { another: } %>
74+
<span>there</span>
75+
<% in { yet_another: { nested: } } %>
76+
<span>hi</span>
77+
<% else %>
78+
<span>there</span>
79+
<% end %>
80+
`)
81+
})
82+
83+
test("formats ERB case/in/else statements", () => {
84+
const source = dedent`
85+
<% case { hash: { nested: '4' } } %>
86+
<% in { hash: { nested: } } %>
87+
2
88+
<% else %>
89+
3
90+
91+
<% end %>
92+
`
93+
const result = formatter.format(source)
94+
expect(result).toEqual(dedent`
95+
<% case { hash: { nested: '4' } } %>
96+
<% in { hash: { nested: } } %>
97+
2
98+
<% else %>
99+
3
100+
<% end %>
101+
`)
102+
})
54103
})

0 commit comments

Comments
 (0)