Skip to content

Commit 0c6d23c

Browse files
Fix string literal preservation in pretty formatting
- Add containsMultilineStringLiteral helper method to detect multi-line string literals - Preserve string literal content in SelectStmt target lists, GROUP BY, HAVING, ORDER BY clauses - Fix WithClause CTE formatting to avoid corrupting string literals during indentation - Resolves all 6 failing kitchen sink tests while maintaining AST equivalence Co-Authored-By: Dan Lynch <[email protected]>
1 parent ab7f3bc commit 0c6d23c

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

packages/deparser/src/deparser.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,13 @@ export class Deparser implements DeparserVisitor {
304304
const targetList = ListUtils.unwrapList(node.targetList);
305305
if (this.formatter.isPretty()) {
306306
const targetStrings = targetList
307-
.map(e => this.formatter.indent(this.visit(e as Node, { ...context, select: true })));
307+
.map(e => {
308+
const targetStr = this.visit(e as Node, { ...context, select: true });
309+
if (this.containsMultilineStringLiteral(targetStr)) {
310+
return targetStr;
311+
}
312+
return this.formatter.indent(targetStr);
313+
});
308314
const formattedTargets = targetStrings.join(',' + this.formatter.newline());
309315
output.push('SELECT' + distinctPart);
310316
output.push(formattedTargets);
@@ -360,7 +366,13 @@ export class Deparser implements DeparserVisitor {
360366
const groupList = ListUtils.unwrapList(node.groupClause);
361367
if (this.formatter.isPretty()) {
362368
const groupItems = groupList
363-
.map(e => this.formatter.indent(this.visit(e as Node, { ...context, group: true })))
369+
.map(e => {
370+
const groupStr = this.visit(e as Node, { ...context, group: true });
371+
if (this.containsMultilineStringLiteral(groupStr)) {
372+
return groupStr;
373+
}
374+
return this.formatter.indent(groupStr);
375+
})
364376
.join(',' + this.formatter.newline());
365377
output.push('GROUP BY');
366378
output.push(groupItems);
@@ -376,7 +388,12 @@ export class Deparser implements DeparserVisitor {
376388
if (node.havingClause) {
377389
if (this.formatter.isPretty()) {
378390
output.push('HAVING');
379-
output.push(this.formatter.indent(this.visit(node.havingClause as Node, context)));
391+
const havingStr = this.visit(node.havingClause as Node, context);
392+
if (this.containsMultilineStringLiteral(havingStr)) {
393+
output.push(havingStr);
394+
} else {
395+
output.push(this.formatter.indent(havingStr));
396+
}
380397
} else {
381398
output.push('HAVING');
382399
output.push(this.visit(node.havingClause as Node, context));
@@ -396,7 +413,13 @@ export class Deparser implements DeparserVisitor {
396413
const sortList = ListUtils.unwrapList(node.sortClause);
397414
if (this.formatter.isPretty()) {
398415
const sortItems = sortList
399-
.map(e => this.formatter.indent(this.visit(e as Node, { ...context, sort: true })))
416+
.map(e => {
417+
const sortStr = this.visit(e as Node, { ...context, sort: true });
418+
if (this.containsMultilineStringLiteral(sortStr)) {
419+
return sortStr;
420+
}
421+
return this.formatter.indent(sortStr);
422+
})
400423
.join(',' + this.formatter.newline());
401424
output.push('ORDER BY');
402425
output.push(sortItems);
@@ -972,6 +995,9 @@ export class Deparser implements DeparserVisitor {
972995
if (this.formatter.isPretty()) {
973996
const cteStrings = ctes.map(cte => {
974997
const cteStr = this.visit(cte, context);
998+
if (this.containsMultilineStringLiteral(cteStr)) {
999+
return this.formatter.newline() + cteStr;
1000+
}
9751001
return this.formatter.newline() + this.formatter.indent(cteStr);
9761002
});
9771003
output.push(cteStrings.join(','));
@@ -10900,6 +10926,8 @@ export class Deparser implements DeparserVisitor {
1090010926
return output.join(' ');
1090110927
}
1090210928

10903-
10904-
10929+
private containsMultilineStringLiteral(content: string): boolean {
10930+
const stringLiteralRegex = /'[^']*\n[^']*'/g;
10931+
return stringLiteralRegex.test(content);
10932+
}
1090510933
}

0 commit comments

Comments
 (0)