Skip to content

Commit e5c0562

Browse files
committed
Format FOREACH loop
1 parent 0068c7b commit e5c0562

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/syntax/procedural_language.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ export const proceduralLanguageMap: Partial<CstToDocMap<AllProceduralNodes>> = {
162162
print.spaced(["to", "by"]),
163163
]),
164164
for_by_clause: (print) => group(print.spaced(["byKw", "expr"])),
165+
// FOREACH
166+
foreach_stmt: (print) =>
167+
group([
168+
print.spaced([
169+
"foreachKw",
170+
"left",
171+
"slice",
172+
"inArrayKw",
173+
"right",
174+
"loop",
175+
]),
176+
]),
177+
foreach_slice: (print) => group(print.spaced(["sliceKw", "count"])),
165178
// BREAK/CONTINUE
166179
break_stmt: (print) => group(print.spaced(["breakKw", "label", "when"])),
167180
continue_stmt: (print) =>

test/proc/loops.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,20 @@ describe("loops", () => {
8181
END LOOP
8282
`);
8383
});
84+
85+
it(`formats FOREACH .. LOOP`, async () => {
86+
await testPlpgsql(dedent`
87+
FOREACH n IN ARRAY arr LOOP
88+
x = x + n;
89+
END LOOP
90+
`);
91+
});
92+
93+
it(`formats FOREACH .. SLICE .. LOOP`, async () => {
94+
await testPlpgsql(dedent`
95+
FOREACH n SLICE 8 IN ARRAY arr LOOP
96+
x = x + n;
97+
END LOOP
98+
`);
99+
});
84100
});

0 commit comments

Comments
 (0)