Skip to content

Commit

Permalink
move generic expression listeners to top
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimkibana committed Aug 19, 2024
1 parent f71385e commit 57f7a71
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/kbn-esql-ast/src/visitor/__tests__/scenarios.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ test('can remove a specific WHERE command', () => {

const print = () =>
new Visitor()
.on('visitExpression', (ctx) => '<expr>')
.on('visitColumnExpression', (ctx) => ctx.node.name)
.on(
'visitFunctionCallExpression',
(ctx) => `${ctx.node.name}(${[...ctx.visitArguments()].join(', ')})`
)
.on('visitExpression', (ctx) => '<expr>')
.on('visitCommand', (ctx) => {
if (ctx.node.name === 'where') {
const args = [...ctx.visitArguments()].join(', ');
Expand All @@ -84,12 +84,12 @@ test('can remove a specific WHERE command', () => {

const removeFilter = (field: string) => {
query.ast = new Visitor()
.on('visitExpression', (ctx) => ctx.node)
.on('visitColumnExpression', (ctx) => (ctx.node.name === field ? null : ctx.node))
.on('visitFunctionCallExpression', (ctx) => {
const args = [...ctx.visitArguments()];
return args.some((arg) => arg === null) ? null : ctx.node;
})
.on('visitExpression', (ctx) => ctx.node)
.on('visitCommand', (ctx) => {
if (ctx.node.name === 'where') {
ctx.node.args = [...ctx.visitArguments()].filter(Boolean);
Expand All @@ -116,6 +116,9 @@ test('can remove a specific WHERE command', () => {

export const prettyPrint = (ast: ESQLAstQueryNode) =>
new Visitor()
.on('visitExpression', (ctx) => {
return '<EXPRESSION>';
})
.on('visitSourceExpression', (ctx) => {
return ctx.node.name;
})
Expand All @@ -141,9 +144,6 @@ export const prettyPrint = (ast: ESQLAstQueryNode) =>
.on('visitInlineCastExpression', (ctx) => {
return '<CAST>';
})
.on('visitExpression', (ctx) => {
return '<EXPRESSION>';
})
.on('visitCommandOption', (ctx) => {
let args = '';
for (const arg of ctx.visitArguments()) {
Expand Down

0 comments on commit 57f7a71

Please sign in to comment.