Skip to content

Commit 15ed016

Browse files
authored
[ES|QL] Cleanup of the autocomplete and validation package (#244720)
## Summary Part of #243935 This is the first step: - I am removing duplicates between ast and validation packages - Moved functions and files around in order to make more sense - Cleanup of unused functions - Folders restructure ### Checklist - [ ] [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 424c2d6 commit 15ed016

40 files changed

+469
-752
lines changed

src/platform/packages/shared/kbn-esql-ast/src/__tests__/autocomplete.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
inOperators,
3434
nullCheckOperators,
3535
} from '../definitions/all_operators';
36-
import { parse } from '../parser';
36+
import { Parser } from '../parser';
3737
import type { ESQLAstAllCommands } from '../types';
3838
import type {
3939
FieldType,
@@ -83,12 +83,12 @@ export const suggest = (
8383
): Promise<ISuggestionItem[]> => {
8484
const innerText = query.substring(0, offset ?? query.length);
8585
const correctedQuery = correctQuerySyntax(innerText);
86-
const { ast, root } = parse(correctedQuery, { withFormatting: true });
86+
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
8787
const headerConstruction = root?.header?.find((cmd) => cmd.name === commandName);
8888

8989
const cursorPosition = offset ?? query.length;
9090

91-
const command = headerConstruction ?? findAstPosition(ast, cursorPosition).command;
91+
const command = headerConstruction ?? findAstPosition(root, cursorPosition).command;
9292

9393
if (!command) {
9494
throw new Error(`${commandName.toUpperCase()} command not found in the parsed query`);

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/dissect/autocomplete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function autocomplete(
4141
// If cursor is inside a string literal, don't suggest anything
4242
const correctedQuery = correctQuerySyntax(innerText);
4343
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
44-
const { node } = findAstPosition(root.commands, innerText.length);
44+
const { node } = findAstPosition(root, innerText.length);
4545

4646
if (node?.type === 'literal' && node.literalType === 'keyword') {
4747
return [];

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/fork/autocomplete.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type { ICommandCallbacks } from '../../types';
2525
import type { FunctionReturnType, FieldType } from '../../../definitions/types';
2626
import { ESQL_STRING_TYPES, ESQL_NUMBER_TYPES } from '../../../definitions/types';
2727
import { correctQuerySyntax, findAstPosition } from '../../../definitions/utils/ast';
28-
import { parse } from '../../../parser';
28+
import { Parser } from '../../../parser';
2929

3030
const allEvalFnsForWhere = getFunctionSignaturesByReturnType(Location.WHERE, 'any', {
3131
scalar: true,
@@ -415,9 +415,10 @@ describe('FORK Autocomplete', () => {
415415
it('suggests pipe after complete subcommands', async () => {
416416
const assertSuggestsPipe = async (query: string) => {
417417
const correctedQuery = correctQuerySyntax(query);
418-
const { ast } = parse(correctedQuery, { withFormatting: true });
418+
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
419+
419420
const cursorPosition = query.length;
420-
const { command } = findAstPosition(ast, cursorPosition);
421+
const { command } = findAstPosition(root, cursorPosition);
421422
if (!command) {
422423
throw new Error('Command not found in the parsed query');
423424
}

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/from/autocomplete.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ describe('FROM Autocomplete', () => {
9595
test('suggests comma or pipe after complete index name', async () => {
9696
const suggest = async (query: string) => {
9797
const correctedQuery = correctQuerySyntax(query);
98-
const { ast } = Parser.parse(correctedQuery, { withFormatting: true });
98+
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
99+
99100
const cursorPosition = query.length;
100-
const { command } = findAstPosition(ast, cursorPosition);
101+
const { command } = findAstPosition(root, cursorPosition);
101102

102103
return autocomplete(query, command!, mockCallbacks, mockContext, cursorPosition);
103104
};

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/grok/autocomplete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export async function autocomplete(
3030
// If cursor is inside a string literal, don't suggest anything
3131
const correctedQuery = correctQuerySyntax(innerText);
3232
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
33-
const { node } = findAstPosition(root.commands, innerText.length);
33+
const { node } = findAstPosition(root, innerText.length);
3434

3535
if (node?.type === 'literal' && node.literalType === 'keyword') {
3636
return [];

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/stats/autocomplete.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
ESQL_COMMON_NUMERIC_TYPES,
3535
} from '../../../definitions/types';
3636
import { correctQuerySyntax, findAstPosition } from '../../../definitions/utils/ast';
37-
import { parse } from '../../../parser';
37+
import { Parser } from '../../../parser';
3838
import { setTestFunctions } from '../../../definitions/utils/test_functions';
3939
import { getDateHistogramCompletionItem } from '../../../..';
4040

@@ -126,9 +126,9 @@ describe('STATS Autocomplete', () => {
126126

127127
const suggest = async (query: string) => {
128128
const correctedQuery = correctQuerySyntax(query);
129-
const { ast } = parse(correctedQuery, { withFormatting: true });
129+
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
130130
const cursorPosition = query.length;
131-
const { command } = findAstPosition(ast, cursorPosition);
131+
const { command } = findAstPosition(root, cursorPosition);
132132
if (!command) {
133133
throw new Error('Command not found in the parsed query');
134134
}

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/stats/autocomplete.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
ESQLAstItem,
1919
ESQLSingleAstItem,
2020
ESQLAstAllCommands,
21+
ESQLAstQueryExpression,
2122
} from '../../../types';
2223
import { type ISuggestionItem, type ICommandContext } from '../../types';
2324
import {
@@ -469,7 +470,13 @@ function findFunctionForSuggestions(
469470
command: ESQLAstAllCommands,
470471
cursorPosition: number
471472
): ESQLFunction | null {
472-
const { node, containingFunction } = findAstPosition([command], cursorPosition);
473+
const { node, containingFunction } = findAstPosition(
474+
{
475+
type: 'query' as const,
476+
commands: [command],
477+
} as unknown as ESQLAstQueryExpression,
478+
cursorPosition
479+
);
473480

474481
if (node && node.type === 'function') {
475482
const fn = node as ESQLFunction;

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/timeseries/autocomplete.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { autocomplete } from './autocomplete';
1111
import { expectSuggestions } from '../../../__tests__/autocomplete';
1212
import type { ICommandCallbacks } from '../../types';
1313
import { correctQuerySyntax, findAstPosition } from '../../../definitions/utils/ast';
14-
import { parse } from '../../../parser';
14+
import { Parser } from '../../../parser';
1515
import { METADATA_FIELDS } from '../../options/metadata';
1616
import { getRecommendedQueriesTemplatesFromExtensions } from '../../options/recommended_queries';
1717

@@ -60,9 +60,10 @@ describe('TS Autocomplete', () => {
6060
describe('... <sources> ...', () => {
6161
const suggest = async (query: string) => {
6262
const correctedQuery = correctQuerySyntax(query);
63-
const { ast } = parse(correctedQuery, { withFormatting: true });
63+
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
64+
6465
const cursorPosition = query.length;
65-
const { command } = findAstPosition(ast, cursorPosition);
66+
const { command } = findAstPosition(root, cursorPosition);
6667
if (!command) {
6768
throw new Error('Command not found in the parsed query');
6869
}

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/commands/where/autocomplete.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type { ICommandCallbacks } from '../../types';
2727
import { ESQL_COMMON_NUMERIC_TYPES } from '../../../definitions/types';
2828
import { getDateLiterals } from '../../../definitions/utils';
2929
import { correctQuerySyntax, findAstPosition } from '../../../definitions/utils/ast';
30-
import { parse } from '../../../parser';
30+
import { Parser } from '../../../parser';
3131

3232
const allEvalFns = getFunctionSignaturesByReturnType(Location.WHERE, 'any', {
3333
scalar: true,
@@ -79,9 +79,10 @@ describe('WHERE Autocomplete', () => {
7979
describe('within the expression', () => {
8080
const suggest = async (query: string) => {
8181
const correctedQuery = correctQuerySyntax(query);
82-
const { ast } = parse(correctedQuery, { withFormatting: true });
82+
const { root } = Parser.parse(correctedQuery, { withFormatting: true });
83+
8384
const cursorPosition = query.length;
84-
const { command } = findAstPosition(ast, cursorPosition);
85+
const { command } = findAstPosition(root, cursorPosition);
8586
if (!command) {
8687
throw new Error('Command not found in the parsed query');
8788
}

src/platform/packages/shared/kbn-esql-ast/src/commands_registry/location.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ const functionBasedLocations: Record<
5555
*
5656
* This is primarily around for backwards compatibility with the old system of command and option names.
5757
*/
58-
const getLocationFromCommandOrOptionName = (name: string) => commandOptionNameToLocation[name];
58+
export const getLocationFromCommandOrOptionName = (name: string) =>
59+
commandOptionNameToLocation[name];
5960

6061
/**
6162
* Identifies the location ID at the given position

0 commit comments

Comments
 (0)