Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] Comments inside lists #195858

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 255 additions & 0 deletions packages/kbn-esql-ast/src/parser/__tests__/comments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,76 @@ FROM index`;
],
});
});

it('to a list literal', () => {
const text = `
ROW
// comment
[1, 2, 3]`;
const { root } = parse(text, { withFormatting: true });

expect(root.commands[0]).toMatchObject({
type: 'command',
name: 'row',
args: [
{
type: 'list',
formatting: {
top: [
{
type: 'comment',
subtype: 'single-line',
text: ' comment',
},
],
},
},
],
});
});

it('to a list literal member', () => {
const text = `
ROW
[
// comment
1,
2, 3]`;
const { root } = parse(text, { withFormatting: true });

expect(root.commands[0]).toMatchObject({
type: 'command',
name: 'row',
args: [
{
type: 'list',
values: [
{
type: 'literal',
value: 1,
formatting: {
top: [
{
type: 'comment',
subtype: 'single-line',
text: ' comment',
},
],
},
},
{
type: 'literal',
value: 2,
},
{
type: 'literal',
value: 3,
},
],
},
],
});
});
});

describe('can attach "left" comment(s)', () => {
Expand Down Expand Up @@ -549,6 +619,92 @@ FROM index`;
},
]);
});

it('to a list literal', () => {
const text = `
ROW
/* left */ [1, 2, 3]`;
const { root } = parse(text, { withFormatting: true });

expect(root.commands[0]).toMatchObject({
type: 'command',
name: 'row',
args: [
{
type: 'list',
formatting: {
left: [
{
type: 'comment',
subtype: 'multi-line',
text: ' left ',
},
],
},
},
],
});
});

it('to a list literal member', () => {
const text = `
ROW
[
/* 1 */ 1,
/* 2 */ 2, /* 3 */ 3]`;
const { root } = parse(text, { withFormatting: true });

expect(root.commands[0]).toMatchObject({
type: 'command',
name: 'row',
args: [
{
type: 'list',
values: [
{
type: 'literal',
value: 1,
formatting: {
left: [
{
type: 'comment',
subtype: 'multi-line',
text: ' 1 ',
},
],
},
},
{
type: 'literal',
value: 2,
formatting: {
left: [
{
type: 'comment',
subtype: 'multi-line',
text: ' 2 ',
},
],
},
},
{
type: 'literal',
value: 3,
formatting: {
left: [
{
type: 'comment',
subtype: 'multi-line',
text: ' 3 ',
},
],
},
},
],
},
],
});
});
});

describe('can attach "right" comment(s)', () => {
Expand Down Expand Up @@ -776,6 +932,66 @@ FROM index`;
],
});
});

it('to a list literal member after comma', () => {
const text = `
ROW
[
1, /* 1 */
2 /* 2 */, 3 /* 3 */]`;
const { root } = parse(text, { withFormatting: true });

expect(root.commands[0]).toMatchObject({
type: 'command',
name: 'row',
args: [
{
type: 'list',
values: [
{
type: 'literal',
value: 1,
formatting: {
right: [
{
type: 'comment',
subtype: 'multi-line',
text: ' 1 ',
},
],
},
},
{
type: 'literal',
value: 2,
formatting: {
right: [
{
type: 'comment',
subtype: 'multi-line',
text: ' 2 ',
},
],
},
},
{
type: 'literal',
value: 3,
formatting: {
right: [
{
type: 'comment',
subtype: 'multi-line',
text: ' 3 ',
},
],
},
},
],
},
],
});
});
});

describe('can attach "right end" comments', () => {
Expand Down Expand Up @@ -908,6 +1124,45 @@ FROM index`;
},
]);
});
it('to a list literal member after comma', () => {
const text = `
ROW
[
1, // 1
2, 3]`;
const { root } = parse(text, { withFormatting: true });

expect(root.commands[0]).toMatchObject({
type: 'command',
name: 'row',
args: [
{
type: 'list',
values: [
{
type: 'literal',
value: 1,
formatting: {
rightSingleLine: {
type: 'comment',
subtype: 'single-line',
text: ' 1',
},
},
},
{
type: 'literal',
value: 2,
},
{
type: 'literal',
value: 3,
},
],
},
],
});
});
});

describe('can attach "bottom" comment(s)', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const reprint = (src: string, opts?: WrappingPrettyPrinterOptions) => {
const { root } = parse(src, { withFormatting: true });
const text = WrappingPrettyPrinter.print(root, opts);

// console.log(JSON.stringify(root, null, 2));

return { text };
};

Expand Down Expand Up @@ -422,6 +424,52 @@ ROW
// 4
/* 5 */ /* 6 */ [1, 2, 3] /* 7 */ /* 8 */ // 9`);
});

describe('members', () => {
test('string list literal members, surrounded from three sides', () => {
const query = `
ROW
// list
[
/* 1.1 */
// 1.2
/* 1.3 */
// 1.4
/* 1.5 */ /* 1.6 */ 1, /* 1.7 */ /* 1.8 */ // 1.9
/* 2.1 */
// 2.2
/* 2.3 */
// 2.4
/* 2.5 */ /* 2.6 */ 2, /* 2.7 */ /* 2.8 */ // 2.9
/* 3.1 */
// 3.2
/* 3.3 */
// 3.4
/* 3.5 */ /* 3.6 */ 3 /* 3.7 */ /* 3.8 */ // 3.9
]`;
const text = reprint(query).text;

expect('\n' + text).toBe(`
ROW
// list
[
/* 1.1 */
// 1.2
/* 1.3 */
// 1.4
/* 1.5 */ /* 1.6 */ 1, /* 1.7 */ /* 1.8 */ // 1.9
/* 2.1 */
// 2.2
/* 2.3 */
// 2.4
/* 2.5 */ /* 2.6 */ 2, /* 2.7 */ /* 2.8 */ // 2.9
/* 3.1 */
// 3.2
/* 3.3 */
// 3.4
/* 3.5 */ /* 3.6 */ 3 /* 3.7 */ /* 3.8 */ // 3.9]`);
});
});
});

describe('rename expressions', () => {
Expand Down
13 changes: 13 additions & 0 deletions packages/kbn-esql-ast/src/visitor/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const isNodeWithChildren = (x: unknown): x is ESQLAstNodeWithChildren =>
typeof x === 'object' &&
(Array.isArray((x as any).args) || Array.isArray((x as any).values));

const isNodeWithValues = (x: unknown): x is Pick<ESQLList, 'values'> =>
!!x && typeof x === 'object' && Array.isArray((x as any).values);

export class VisitorContext<
Methods extends VisitorMethods = VisitorMethods,
Data extends SharedData = SharedData,
Expand Down Expand Up @@ -118,6 +121,16 @@ export class VisitorContext<
return args;
}

public children(): ESQLAstExpressionNode[] {
const node = this.node;

if (isNodeWithValues(node)) {
return node.values;
}

return this.arguments();
}

public visitArgument(
index: number,
input: VisitorInput<Methods, 'visitExpression'>
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-esql-ast/src/visitor/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class Visitor<
if (!location) return null;
const isBefore = location.min > pos;
let isFirstChild = true;
for (const child of ctx.arguments()) {
for (const child of ctx.children()) {
const { location: childLocation } = child;
if (!childLocation) continue;
if (isFirstChild) {
Expand Down Expand Up @@ -116,7 +116,7 @@ export class Visitor<
return new Visitor()
.on('visitExpression', (ctx): ESQLProperNode | null => {
const nodeLocation = ctx.node.location;
const nodes = [...ctx.arguments()];
const nodes = [...ctx.children()];

if (nodeLocation && nodeLocation.max < pos) {
const last = nodes[nodes.length - 1];
Expand Down