Skip to content

Commit 9e93eb2

Browse files
kibanamachinevadimkibanastratoula
authored
[8.19] [ES|QL] Print comments around functions (#229470) (#229559)
# Backport This will backport the following commits from `main` to `8.19`: - [[ES|QL] Print comments around functions (#229470)](#229470) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Vadim Kibana","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-28T07:39:41Z","message":"[ES|QL] Print comments around functions (#229470)\n\n## Summary\n\nFixes a bug where comments in `WrappingPrettyPrinter` were not printed\naround `function` AST nodes.\n\n\n### Checklist\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n---------\n\nCo-authored-by: Stratoula Kalafateli <[email protected]>","sha":"61adf45bea53b04ab6d896b6858f946da57aa85b","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","review","release_note:skip","Feature:ES|QL","Team:ESQL","backport:version","v9.2.0","v9.1.1","v8.19.1"],"title":"[ES|QL] Print comments around functions","number":229470,"url":"https://github.com/elastic/kibana/pull/229470","mergeCommit":{"message":"[ES|QL] Print comments around functions (#229470)\n\n## Summary\n\nFixes a bug where comments in `WrappingPrettyPrinter` were not printed\naround `function` AST nodes.\n\n\n### Checklist\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n---------\n\nCo-authored-by: Stratoula Kalafateli <[email protected]>","sha":"61adf45bea53b04ab6d896b6858f946da57aa85b"}},"sourceBranch":"main","suggestedTargetBranches":["9.1","8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/229470","number":229470,"mergeCommit":{"message":"[ES|QL] Print comments around functions (#229470)\n\n## Summary\n\nFixes a bug where comments in `WrappingPrettyPrinter` were not printed\naround `function` AST nodes.\n\n\n### Checklist\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n---------\n\nCo-authored-by: Stratoula Kalafateli <[email protected]>","sha":"61adf45bea53b04ab6d896b6858f946da57aa85b"}},{"branch":"9.1","label":"v9.1.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Vadim Kibana <[email protected]> Co-authored-by: Stratoula Kalafateli <[email protected]>
1 parent df5b2b6 commit 9e93eb2

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/platform/packages/shared/kbn-esql-ast/src/pretty_print/__tests__/wrapping_pretty_printer.comments.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import { WrappingPrettyPrinter, WrappingPrettyPrinterOptions } from '../wrapping
1313
const reprint = (src: string, opts?: WrappingPrettyPrinterOptions) => {
1414
const { root } = parse(src, { withFormatting: true });
1515
const text = WrappingPrettyPrinter.print(root, opts);
16+
// console.log(JSON.stringify(root, null, 2));
1617

1718
return { text };
1819
};
1920

2021
const assertReprint = (src: string, expected: string = src) => {
2122
const text = reprint(src).text;
23+
// console.log(text);
2224
expect(text).toBe(expected);
2325
};
2426

@@ -640,6 +642,39 @@ ROW
640642
);
641643
});
642644
});
645+
646+
describe('variadic call', () => {
647+
test('right from function call', () => {
648+
const query = `FROM logs-*-* | WHERE QSTR("term") /* Search all fields using QSTR – e.g. WHERE QSTR("""debug""") */ | LIMIT 10`;
649+
const text = reprint(query).text;
650+
651+
expect(text).toBe(
652+
`FROM logs-*-*
653+
| WHERE
654+
QSTR("term") /* Search all fields using QSTR – e.g. WHERE QSTR("""debug""") */
655+
| LIMIT 10`
656+
);
657+
});
658+
659+
test('can decorate a function call from all sides', () => {
660+
assertReprint(`FROM logs-*-*
661+
| WHERE
662+
// t1
663+
/* t2 */
664+
/* l1 */ /* l2 */ QSTR("term") /* r1 */ /* r2 */ // r3
665+
| LIMIT 10`);
666+
});
667+
668+
test('can decorate a function call outside and inside', () => {
669+
assertReprint(`FROM logs-*-*
670+
| WHERE
671+
/* t0 */
672+
// t1
673+
/* t2 */
674+
/* l1 */ /* l2 */ QSTR(/* i1 */ "term" /* i2 */ /* i3 */) /* r1 */ /* r2 */ // r3
675+
| LIMIT 10`);
676+
});
677+
});
643678
});
644679

645680
describe('map expressions', () => {

src/platform/packages/shared/kbn-esql-ast/src/pretty_print/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* License v3.0 only", or the "Server Side Public License, v 1".
88
*/
99

10-
import type { ESQLAstBaseItem, ESQLProperNode } from '../types';
11-
import { Walker } from '../walker';
10+
import type { ESQLAstBaseItem } from '../types';
11+
import { Walker, type WalkerAstNode } from '../walker';
1212

1313
export interface QueryPrettyPrintStats {
1414
/**
@@ -29,7 +29,7 @@ export interface QueryPrettyPrintStats {
2929
*
3030
* @param ast The part to compute the stats for.
3131
*/
32-
export const getPrettyPrintStats = (ast: ESQLProperNode): QueryPrettyPrintStats => {
32+
export const getPrettyPrintStats = (ast: WalkerAstNode): QueryPrettyPrintStats => {
3333
const stats: QueryPrettyPrintStats = {
3434
hasLineBreakingDecorations: false,
3535
hasRightSingleLineComments: false,

src/platform/packages/shared/kbn-esql-ast/src/pretty_print/wrapping_pretty_printer.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ import { commandOptionsWithEqualsSeparator, commandsWithNoCommaArgSeparator } fr
2525
import { getPrettyPrintStats } from './helpers';
2626
import { LeafPrinter } from './leaf_printer';
2727

28-
/**
29-
* @todo
30-
*
31-
* 1. Implement list literal pretty printing.
32-
*/
33-
3428
interface Input {
3529
indent: string;
3630
remaining: number;
@@ -480,6 +474,7 @@ export class WrappingPrettyPrinter {
480474
protected readonly visitor: Visitor<any> = new Visitor()
481475
.on('visitExpression', (ctx, inp: Input): Output => {
482476
const txt = ctx.node.text ?? '<EXPRESSION>';
477+
// TODO: decorate with comments
483478
return { txt };
484479
})
485480

@@ -601,7 +596,9 @@ export class WrappingPrettyPrinter {
601596
switch (node.subtype) {
602597
case 'unary-expression': {
603598
const separator = operator === '-' || operator === '+' ? '' : ' ';
604-
txt = `${operator}${separator}${ctx.visitArgument(0, inp).txt}`;
599+
const formatted = ctx.visitArgument(0, inp);
600+
601+
txt = `${operator}${separator}${formatted.txt}`;
605602
break;
606603
}
607604
case 'postfix-unary-expression': {
@@ -620,7 +617,7 @@ export class WrappingPrettyPrinter {
620617

621618
let breakClosingParenthesis = false;
622619

623-
if (getPrettyPrintStats(ctx.node).hasRightSingleLineComments) {
620+
if (getPrettyPrintStats(ctx.node.args).hasRightSingleLineComments) {
624621
breakClosingParenthesis = true;
625622
}
626623

@@ -634,7 +631,7 @@ export class WrappingPrettyPrinter {
634631
}
635632
}
636633

637-
return { txt };
634+
return this.decorateWithComments({ ...inp, suffix: '' }, ctx.node, txt);
638635
})
639636

640637
.on('visitCommandOption', (ctx, inp: Input): Output => {
@@ -647,6 +644,8 @@ export class WrappingPrettyPrinter {
647644
const separator = commandOptionsWithEqualsSeparator.has(ctx.node.name) ? ' =' : '';
648645
const txt = `${option}${separator}${argsFormatted}`;
649646

647+
// TODO: decorate with comments
648+
650649
return { txt, lines: args.lines };
651650
})
652651

0 commit comments

Comments
 (0)