Skip to content

Commit 9849938

Browse files
fix: update SelectStmt target list and WHERE clause to use indentToCurrentLevel for proper systematic indentation
Co-Authored-By: Dan Lynch <[email protected]>
1 parent b15b55b commit 9849938

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

packages/deparser/src/deparser.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ export class Deparser implements DeparserVisitor {
199199
if (node == null) {
200200
return null;
201201
}
202-
202+
203203
if (!context) {
204204
const formatter = new SqlFormatter(this.options.newline, this.options.tab, this.options.pretty);
205205
context = new DeparserContext({ formatter, prettyMode: this.options.pretty });
@@ -222,23 +222,23 @@ export class Deparser implements DeparserVisitor {
222222
const formatter = new SqlFormatter(this.options.newline, this.options.tab, this.options.pretty);
223223
context = new DeparserContext({ formatter, prettyMode: this.options.pretty });
224224
}
225-
225+
226226
const nodeType = this.getNodeType(node);
227-
227+
228228
// Handle empty objects
229229
if (!nodeType) {
230230
return '';
231231
}
232-
232+
233233
const nodeData = this.getNodeData(node);
234234

235235
const methodName = nodeType as keyof this;
236236
if (typeof this[methodName] === 'function') {
237237
const result = (this[methodName] as any)(nodeData, context);
238-
238+
239239
return result;
240240
}
241-
241+
242242
throw new Error(`Deparser does not handle node type: ${nodeType}`);
243243
}
244244

@@ -378,7 +378,7 @@ export class Deparser implements DeparserVisitor {
378378
if (context.isPretty()) {
379379
if (targetList.length === 1) {
380380
const targetNode = targetList[0] as Node;
381-
const target = this.visit(targetNode, context.spawn('SelectStmt', { select: true }));
381+
const target = this.visit(targetNode, context.spawn('SelectStmt', { select: true, indentLevel: context.indentLevel + 1 }));
382382

383383
// Check if single target is complex - if so, use multiline format
384384
if (this.isComplexSelectTarget(targetNode)) {
@@ -394,11 +394,11 @@ export class Deparser implements DeparserVisitor {
394394
} else {
395395
const targetStrings = targetList
396396
.map(e => {
397-
const targetStr = this.visit(e as Node, context.spawn('SelectStmt', { select: true }));
397+
const targetStr = this.visit(e as Node, context.spawn('SelectStmt', { select: true, indentLevel: context.indentLevel + 1 }));
398398
if (this.containsMultilineStringLiteral(targetStr)) {
399399
return targetStr;
400400
}
401-
return context.indent(targetStr);
401+
return context.indentToCurrentLevel(targetStr);
402402
});
403403
const formattedTargets = targetStrings.join(',' + context.newline());
404404
output.push('SELECT' + distinctPart);
@@ -428,11 +428,12 @@ export class Deparser implements DeparserVisitor {
428428
if (node.whereClause) {
429429
if (context.isPretty()) {
430430
output.push('WHERE');
431-
const whereExpr = this.visit(node.whereClause as Node, context);
431+
const whereContext = context.spawn('SelectStmt', { indentLevel: context.indentLevel + 1 });
432+
const whereExpr = this.visit(node.whereClause as Node, whereContext);
432433
const lines = whereExpr.split(context.newline());
433434
const indentedLines = lines.map((line, index) => {
434435
if (index === 0) {
435-
return context.indent(line);
436+
return context.indentToCurrentLevel(line);
436437
}
437438
return line;
438439
});
@@ -1282,15 +1283,15 @@ export class Deparser implements DeparserVisitor {
12821283
switch (boolop) {
12831284
case 'AND_EXPR':
12841285
if (context.isPretty() && args.length > 1) {
1285-
const andArgs = args.map(arg => this.visit(arg, boolContext)).join(context.newline() + context.indent('AND '));
1286+
const andArgs = args.map(arg => this.visit(arg, boolContext)).join(context.newline() + context.indentToCurrentLevel('AND '));
12861287
return formatStr.replace('%s', () => andArgs);
12871288
} else {
12881289
const andArgs = args.map(arg => this.visit(arg, boolContext)).join(' AND ');
12891290
return formatStr.replace('%s', () => andArgs);
12901291
}
12911292
case 'OR_EXPR':
12921293
if (context.isPretty() && args.length > 1) {
1293-
const orArgs = args.map(arg => this.visit(arg, boolContext)).join(context.newline() + context.indent('OR '));
1294+
const orArgs = args.map(arg => this.visit(arg, boolContext)).join(context.newline() + context.indentToCurrentLevel('OR '));
12941295
return formatStr.replace('%s', () => orArgs);
12951296
} else {
12961297
const orArgs = args.map(arg => this.visit(arg, boolContext)).join(' OR ');
@@ -2176,27 +2177,28 @@ export class Deparser implements DeparserVisitor {
21762177
const args = ListUtils.unwrapList(node.args);
21772178

21782179
if (context.isPretty() && args.length > 0) {
2180+
const caseContext = context.spawn('CaseExpr', { indentLevel: context.indentLevel + 1 });
21792181
for (const arg of args) {
2180-
const whenClause = this.visit(arg, context);
2182+
const whenClause = this.visit(arg, caseContext);
21812183
if (this.containsMultilineStringLiteral(whenClause)) {
21822184
output.push(context.newline() + whenClause);
21832185
} else {
2184-
output.push(context.newline() + context.indent(whenClause));
2186+
output.push(context.newline() + context.indentToCurrentLevel(whenClause));
21852187
}
21862188
}
21872189

21882190
if (node.defresult) {
2189-
const elseResult = this.visit(node.defresult, context);
2191+
const elseResult = this.visit(node.defresult, caseContext);
21902192
if (this.containsMultilineStringLiteral(elseResult)) {
21912193
output.push(context.newline() + 'ELSE ' + elseResult);
21922194
} else {
2193-
output.push(context.newline() + context.indent('ELSE ' + elseResult));
2195+
output.push(context.newline() + context.indentToCurrentLevel('ELSE ' + elseResult));
21942196
}
21952197
}
21962198

21972199
output.push(context.newline() + 'END');
21982200
return output.join(' ');
2199-
} else {
2201+
}else {
22002202
for (const arg of args) {
22012203
output.push(this.visit(arg, context));
22022204
}

0 commit comments

Comments
 (0)