Skip to content

Commit 575563d

Browse files
authored
do not return array from option converter (#235363)
## Summary The `.toByOption()` does not unnecessarily box its return value in an array anymore. Returns `node`, instad of `[node]`. ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
1 parent 41695f2 commit 575563d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/platform/packages/shared/kbn-esql-ast/src/parser/cst_to_ast_converter.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,11 @@ export class CstToAstConverter {
583583
}
584584

585585
if (ctx._grouping) {
586-
const options = this.toByOption(ctx, ctx.fields());
586+
const option = this.toByOption(ctx, ctx.fields());
587587

588-
command.args.push(...options);
588+
if (option) {
589+
command.args.push(option);
590+
}
589591
}
590592

591593
return command;
@@ -620,17 +622,14 @@ export class CstToAstConverter {
620622
return aggField;
621623
}
622624

623-
/**
624-
* @todo Do not return array here.
625-
*/
626625
private toByOption(
627626
ctx: antlr.ParserRuleContext & Pick<cst.StatsCommandContext, 'BY'>,
628627
expr: cst.FieldsContext | undefined
629-
): ast.ESQLCommandOption[] {
628+
): ast.ESQLCommandOption | undefined {
630629
const byCtx = ctx.BY();
631630

632631
if (!byCtx || !expr) {
633-
return [];
632+
return;
634633
}
635634

636635
const option = this.toOption(byCtx.getText().toLowerCase(), ctx);
@@ -642,7 +641,7 @@ export class CstToAstConverter {
642641

643642
if (lastArg) option.location.max = lastArg.location.max;
644643

645-
return [option];
644+
return option;
646645
}
647646

648647
// --------------------------------------------------------------------- SORT
@@ -1675,15 +1674,6 @@ export class CstToAstConverter {
16751674
);
16761675
}
16771676

1678-
/**
1679-
* @todo Inline this method.
1680-
*/
1681-
private getComparisonName(ctx: cst.ComparisonOperatorContext) {
1682-
return (
1683-
(ctx.EQ() || ctx.NEQ() || ctx.LT() || ctx.LTE() || ctx.GT() || ctx.GTE()).getText() || ''
1684-
);
1685-
}
1686-
16871677
private visitValueExpression(ctx: cst.ValueExpressionContext) {
16881678
if (!textExistsAndIsValid(ctx.getText())) {
16891679
return [];
@@ -1692,10 +1682,19 @@ export class CstToAstConverter {
16921682
return this.visitOperatorExpression(ctx.operatorExpression());
16931683
}
16941684
if (ctx instanceof cst.ComparisonContext) {
1695-
const comparisonNode = ctx.comparisonOperator();
1685+
const operatorCtx = ctx.comparisonOperator();
1686+
const comparisonOperatorText =
1687+
(
1688+
operatorCtx.EQ() ||
1689+
operatorCtx.NEQ() ||
1690+
operatorCtx.LT() ||
1691+
operatorCtx.LTE() ||
1692+
operatorCtx.GT() ||
1693+
operatorCtx.GTE()
1694+
).getText() || '';
16961695
const comparisonFn = this.toFunction(
1697-
this.getComparisonName(comparisonNode),
1698-
comparisonNode,
1696+
comparisonOperatorText,
1697+
operatorCtx,
16991698
undefined,
17001699
'binary-expression'
17011700
);

0 commit comments

Comments
 (0)