From a3b877bbf54dcc8d6fdeec963e548dd877ed0d12 Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Wed, 24 Jul 2024 10:43:55 -0600 Subject: [PATCH 1/4] capitalize function and operator signatures --- .../src/autocomplete/__tests__/helpers.ts | 25 ++++++++++++++----- .../src/autocomplete/autocomplete.test.ts | 6 ++--- .../src/autocomplete/factories.ts | 4 +-- .../src/definitions/helpers.ts | 7 ++++-- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts index 99d8e3e961317..ac2a6d9a85154 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts @@ -17,6 +17,7 @@ import * as autocomplete from '../autocomplete'; import type { ESQLCallbacks } from '../../shared/types'; import type { EditorContext, SuggestionRawDefinition } from '../types'; import { TIME_SYSTEM_PARAMS } from '../factories'; +import { getFunctionSignatures } from '../../definitions/helpers'; export interface Integration { name: string; @@ -142,7 +143,7 @@ export function getFunctionSignaturesByReturnType( paramsTypes?: string[], ignored?: string[], option?: string -) { +): PartialSuggestionWithText[] { const expectedReturnType = Array.isArray(_expectedReturnType) ? _expectedReturnType : [_expectedReturnType]; @@ -203,13 +204,25 @@ export function getFunctionSignaturesByReturnType( return true; }) .sort(({ name: a }, { name: b }) => a.localeCompare(b)) - .map(({ type, name, signatures }) => { + .map((definition) => { + const { type, name, signatures } = definition; + if (type === 'builtin') { - return signatures.some(({ params }) => params.length > 1) - ? `${name.toUpperCase()} $0` - : name.toUpperCase(); + return { + text: signatures.some(({ params }) => params.length > 1) + ? `${name.toUpperCase()} $0` + : name.toUpperCase(), + label: name.toUpperCase(), + }; } - return `${name.toUpperCase()}($0)`; + const printedSignatures = getFunctionSignatures(definition, { + withTypes: true, + capitalize: true, + }); + return { + text: `${name.toUpperCase()}($0)`, + label: printedSignatures[0].declaration, + }; }); } diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts index a87d8a6e37dc3..eceb0d87fe075 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.test.ts @@ -668,7 +668,7 @@ describe('autocomplete', () => { ...getFieldNamesByType('string').map((v) => `${v},`), ...getFunctionSignaturesByReturnType('eval', 'string', { scalar: true }, undefined, [ 'concat', - ]).map((v) => `${v},`), + ]).map((v) => ({ ...v, text: `${v.text},` })), ]); testSuggestions( 'from a | eval a=concat(stringField, ', @@ -696,7 +696,7 @@ describe('autocomplete', () => { ...getFieldNamesByType('ip').map((v) => `${v},`), ...getFunctionSignaturesByReturnType('eval', 'ip', { scalar: true }, undefined, [ 'cidr_match', - ]).map((v) => `${v},`), + ]).map((v) => ({ ...v, text: `${v.text},` })), ]); testSuggestions( 'from a | eval a=cidr_match(ipField, ', @@ -884,7 +884,7 @@ describe('autocomplete', () => { ...getLiteralsByType('time_literal').map((t) => `${t},`), ...getFunctionSignaturesByReturnType('eval', 'date', { scalar: true }, undefined, [ 'date_trunc', - ]).map((t) => `${t},`), + ]).map((t) => ({ ...t, text: `${t.text},` })), ...getFieldNamesByType('date').map((t) => `${t},`), TIME_PICKER_SUGGESTION, ], diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts index fa4afe148f003..f54511c8e304d 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/factories.ts @@ -49,7 +49,7 @@ function getSafeInsertSourceText(text: string) { } export function getSuggestionFunctionDefinition(fn: FunctionDefinition): SuggestionRawDefinition { - const fullSignatures = getFunctionSignatures(fn); + const fullSignatures = getFunctionSignatures(fn, { capitalize: true, withTypes: true }); return { label: fullSignatures[0].declaration, text: `${fn.name.toUpperCase()}($0)`, @@ -69,7 +69,7 @@ export function getSuggestionFunctionDefinition(fn: FunctionDefinition): Suggest export function getSuggestionBuiltinDefinition(fn: FunctionDefinition): SuggestionRawDefinition { const hasArgs = fn.signatures.some(({ params }) => params.length > 1); return { - label: fn.name, + label: fn.name.toUpperCase(), text: hasArgs ? `${fn.name.toUpperCase()} $0` : fn.name.toUpperCase(), asSnippet: hasArgs, kind: 'Operator', diff --git a/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts index 0ccb7855b284b..60c3bb4725eba 100644 --- a/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts @@ -17,7 +17,10 @@ import type { CommandDefinition, FunctionDefinition } from './types'; */ export function getFunctionSignatures( { name, signatures }: FunctionDefinition, - { withTypes }: { withTypes: boolean } = { withTypes: true } + { withTypes, capitalize }: { withTypes: boolean; capitalize: boolean } = { + withTypes: true, + capitalize: false, + } ) { return signatures.map(({ params, returnType, minParams }) => { // for functions with a minimum number of args, repeat the last arg multiple times @@ -25,7 +28,7 @@ export function getFunctionSignatures( const minParamsToAdd = Math.max((minParams || 0) - params.length, 0); const extraArg = Array(minParamsToAdd || 1).fill(params[Math.max(params.length - 1, 0)]); return { - declaration: `${name}(${params + declaration: `${capitalize ? name.toUpperCase() : name}(${params .map((arg) => printArguments(arg, withTypes)) .join(', ')}${handleAdditionalArgs(minParamsToAdd > 0, extraArg, withTypes)})${ withTypes ? `: ${returnType}` : '' From b63ce082f8f795506045763936925d4ba60910b9 Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Wed, 24 Jul 2024 11:35:16 -0600 Subject: [PATCH 2/4] actually show what is written in validation error messages --- .../src/shared/helpers.ts | 5 +- .../esql_validation_meta_tests.json | 115 +++++++++++++++--- .../src/validation/validation.test.ts | 30 ++--- .../src/validation/validation.ts | 3 +- 4 files changed, 118 insertions(+), 35 deletions(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts index 6696664d8e126..68658b29251b5 100644 --- a/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/shared/helpers.ts @@ -287,13 +287,12 @@ export function areFieldAndVariableTypesCompatible( return fieldType === variableType; } -export function printFunctionSignature(arg: ESQLFunction, useCaps = true): string { +export function printFunctionSignature(arg: ESQLFunction): string { const fnDef = getFunctionDefinition(arg.name); if (fnDef) { const signature = getFunctionSignatures( { ...fnDef, - name: useCaps ? fnDef.name.toUpperCase() : fnDef.name, signatures: [ { ...fnDef?.signatures[0], @@ -310,7 +309,7 @@ export function printFunctionSignature(arg: ESQLFunction, useCaps = true): strin }, ], }, - { withTypes: false } + { withTypes: false, capitalize: true } ); return signature[0].declaration; } diff --git a/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json b/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json index 65dc30e79064b..0e30da959f82e 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json +++ b/packages/kbn-esql-validation-autocomplete/src/validation/esql_validation_meta_tests.json @@ -9187,8 +9187,8 @@ { "query": "from a_index | eval date_diff(\"year\", concat(\"20\", \"22\"), concat(\"20\", \"22\"))", "error": [ - "Argument of [date_diff] must be [date], found value [concat(\"20\", \"22\")] type [string]", - "Argument of [date_diff] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [date_diff] must be [date], found value [concat(\"20\",\"22\")] type [string]", + "Argument of [date_diff] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -11086,7 +11086,7 @@ { "query": "from a_index | eval date_extract(\"ALIGNED_DAY_OF_WEEK_IN_MONTH\", concat(\"20\", \"22\"))", "error": [ - "Argument of [date_extract] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [date_extract] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -11173,7 +11173,7 @@ { "query": "from a_index | eval date_format(stringField, concat(\"20\", \"22\"))", "error": [ - "Argument of [date_format] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [date_format] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -11390,7 +11390,7 @@ { "query": "from a_index | eval date_trunc(1 year, concat(\"20\", \"22\"))", "error": [ - "Argument of [date_trunc] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [date_trunc] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -11402,8 +11402,8 @@ { "query": "from a_index | eval date_trunc(concat(\"20\", \"22\"), concat(\"20\", \"22\"))", "error": [ - "Argument of [date_trunc] must be [time_literal], found value [concat(\"20\", \"22\")] type [string]", - "Argument of [date_trunc] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [date_trunc] must be [time_literal], found value [concat(\"20\",\"22\")] type [string]", + "Argument of [date_trunc] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -24143,7 +24143,7 @@ { "query": "from a_index | stats max(concat(\"20\", \"22\"))", "error": [ - "Argument of [max] must be [number], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [max] must be [number], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -24520,7 +24520,7 @@ { "query": "from a_index | stats min(concat(\"20\", \"22\"))", "error": [ - "Argument of [min] must be [number], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [min] must be [number], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -25184,14 +25184,14 @@ { "query": "from a_index | stats bucket(concat(\"20\", \"22\"), 1 year)", "error": [ - "Argument of [bucket] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [bucket] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, { "query": "from a_index | stats by bucket(concat(\"20\", \"22\"), 1 year)", "error": [ - "Argument of [bucket] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [bucket] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -25203,7 +25203,7 @@ { "query": "from a_index | stats bucket(concat(\"20\", \"22\"), 5, \"a\", \"a\")", "error": [ - "Argument of [bucket] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [bucket] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -25215,7 +25215,7 @@ { "query": "from a_index | stats bucket(concat(\"20\", \"22\"), 5, concat(\"20\", \"22\"), concat(\"20\", \"22\"))", "error": [ - "Argument of [bucket] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [bucket] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -25227,7 +25227,7 @@ { "query": "from a_index | stats bucket(concat(\"20\", \"22\"), 5, \"a\", concat(\"20\", \"22\"))", "error": [ - "Argument of [bucket] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [bucket] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -25239,7 +25239,7 @@ { "query": "from a_index | stats bucket(concat(\"20\", \"22\"), 5, concat(\"20\", \"22\"), \"a\")", "error": [ - "Argument of [bucket] must be [date], found value [concat(\"20\", \"22\")] type [string]" + "Argument of [bucket] must be [date], found value [concat(\"20\",\"22\")] type [string]" ], "warning": [] }, @@ -26582,6 +26582,91 @@ "error": [], "warning": [] }, + { + "query": "row var = exp(5)", + "error": [], + "warning": [] + }, + { + "query": "row exp(5)", + "error": [], + "warning": [] + }, + { + "query": "row var = exp(to_integer(true))", + "error": [], + "warning": [] + }, + { + "query": "row var = exp(true)", + "error": [ + "Argument of [exp] must be [number], found value [true] type [boolean]" + ], + "warning": [] + }, + { + "query": "from a_index | where exp(numberField) > 0", + "error": [], + "warning": [] + }, + { + "query": "from a_index | where exp(booleanField) > 0", + "error": [ + "Argument of [exp] must be [number], found value [booleanField] type [boolean]" + ], + "warning": [] + }, + { + "query": "from a_index | eval var = exp(numberField)", + "error": [], + "warning": [] + }, + { + "query": "from a_index | eval exp(numberField)", + "error": [], + "warning": [] + }, + { + "query": "from a_index | eval var = exp(to_integer(booleanField))", + "error": [], + "warning": [] + }, + { + "query": "from a_index | eval exp(booleanField)", + "error": [ + "Argument of [exp] must be [number], found value [booleanField] type [boolean]" + ], + "warning": [] + }, + { + "query": "from a_index | eval var = exp(*)", + "error": [ + "Using wildcards (*) in exp is not allowed" + ], + "warning": [] + }, + { + "query": "from a_index | eval exp(numberField, extraArg)", + "error": [ + "Error: [exp] function expects exactly one argument, got 2." + ], + "warning": [] + }, + { + "query": "from a_index | sort exp(numberField)", + "error": [], + "warning": [] + }, + { + "query": "from a_index | eval exp(null)", + "error": [], + "warning": [] + }, + { + "query": "row nullVar = null | eval exp(nullVar)", + "error": [], + "warning": [] + }, { "query": "f", "error": [ diff --git a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts index e9a707fd85e98..67c8472cd53e8 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts @@ -1780,8 +1780,8 @@ describe('validation logic', () => { testErrorsAndWarnings( 'from a_index | eval date_diff("year", concat("20", "22"), concat("20", "22"))', [ - 'Argument of [date_diff] must be [date], found value [concat("20", "22")] type [string]', - 'Argument of [date_diff] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [date_diff] must be [date], found value [concat("20","22")] type [string]', + 'Argument of [date_diff] must be [date], found value [concat("20","22")] type [string]', ] ); }); @@ -2668,7 +2668,7 @@ describe('validation logic', () => { testErrorsAndWarnings( 'from a_index | eval date_extract("ALIGNED_DAY_OF_WEEK_IN_MONTH", concat("20", "22"))', [ - 'Argument of [date_extract] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [date_extract] must be [date], found value [concat("20","22")] type [string]', ] ); }); @@ -2712,7 +2712,7 @@ describe('validation logic', () => { testErrorsAndWarnings('row nullVar = null | eval date_format(nullVar, nullVar)', []); testErrorsAndWarnings('from a_index | eval date_format(stringField, "2022")', []); testErrorsAndWarnings('from a_index | eval date_format(stringField, concat("20", "22"))', [ - 'Argument of [date_format] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [date_format] must be [date], found value [concat("20","22")] type [string]', ]); }); @@ -2817,15 +2817,15 @@ describe('validation logic', () => { testErrorsAndWarnings('row nullVar = null | eval date_trunc(nullVar, nullVar)', []); testErrorsAndWarnings('from a_index | eval date_trunc(1 year, "2022")', []); testErrorsAndWarnings('from a_index | eval date_trunc(1 year, concat("20", "22"))', [ - 'Argument of [date_trunc] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [date_trunc] must be [date], found value [concat("20","22")] type [string]', ]); testErrorsAndWarnings('from a_index | eval date_trunc("2022", "2022")', []); testErrorsAndWarnings( 'from a_index | eval date_trunc(concat("20", "22"), concat("20", "22"))', [ - 'Argument of [date_trunc] must be [time_literal], found value [concat("20", "22")] type [string]', - 'Argument of [date_trunc] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [date_trunc] must be [time_literal], found value [concat("20","22")] type [string]', + 'Argument of [date_trunc] must be [date], found value [concat("20","22")] type [string]', ] ); }); @@ -9170,7 +9170,7 @@ describe('validation logic', () => { testErrorsAndWarnings('row nullVar = null | stats max(nullVar)', []); testErrorsAndWarnings('from a_index | stats max("2022")', []); testErrorsAndWarnings('from a_index | stats max(concat("20", "22"))', [ - 'Argument of [max] must be [number], found value [concat("20", "22")] type [string]', + 'Argument of [max] must be [number], found value [concat("20","22")] type [string]', ]); testErrorsAndWarnings('from a_index | stats max(cartesianPointField)', [ @@ -9368,7 +9368,7 @@ describe('validation logic', () => { testErrorsAndWarnings('row nullVar = null | stats min(nullVar)', []); testErrorsAndWarnings('from a_index | stats min("2022")', []); testErrorsAndWarnings('from a_index | stats min(concat("20", "22"))', [ - 'Argument of [min] must be [number], found value [concat("20", "22")] type [string]', + 'Argument of [min] must be [number], found value [concat("20","22")] type [string]', ]); testErrorsAndWarnings('from a_index | stats min(cartesianPointField)', [ @@ -9742,34 +9742,34 @@ describe('validation logic', () => { ); testErrorsAndWarnings('from a_index | stats bucket("2022", 1 year)', []); testErrorsAndWarnings('from a_index | stats bucket(concat("20", "22"), 1 year)', [ - 'Argument of [bucket] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [bucket] must be [date], found value [concat("20","22")] type [string]', ]); testErrorsAndWarnings('from a_index | stats by bucket(concat("20", "22"), 1 year)', [ - 'Argument of [bucket] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [bucket] must be [date], found value [concat("20","22")] type [string]', ]); testErrorsAndWarnings('from a_index | stats bucket("2022", 5, "a", "a")', []); testErrorsAndWarnings('from a_index | stats bucket(concat("20", "22"), 5, "a", "a")', [ - 'Argument of [bucket] must be [date], found value [concat("20", "22")] type [string]', + 'Argument of [bucket] must be [date], found value [concat("20","22")] type [string]', ]); testErrorsAndWarnings('from a_index | stats bucket("2022", 5, "2022", "2022")', []); testErrorsAndWarnings( 'from a_index | stats bucket(concat("20", "22"), 5, concat("20", "22"), concat("20", "22"))', - ['Argument of [bucket] must be [date], found value [concat("20", "22")] type [string]'] + ['Argument of [bucket] must be [date], found value [concat("20","22")] type [string]'] ); testErrorsAndWarnings('from a_index | stats bucket("2022", 5, "a", "2022")', []); testErrorsAndWarnings( 'from a_index | stats bucket(concat("20", "22"), 5, "a", concat("20", "22"))', - ['Argument of [bucket] must be [date], found value [concat("20", "22")] type [string]'] + ['Argument of [bucket] must be [date], found value [concat("20","22")] type [string]'] ); testErrorsAndWarnings('from a_index | stats bucket("2022", 5, "2022", "a")', []); testErrorsAndWarnings( 'from a_index | stats bucket(concat("20", "22"), 5, concat("20", "22"), "a")', - ['Argument of [bucket] must be [date], found value [concat("20", "22")] type [string]'] + ['Argument of [bucket] must be [date], found value [concat("20","22")] type [string]'] ); }); diff --git a/packages/kbn-esql-validation-autocomplete/src/validation/validation.ts b/packages/kbn-esql-validation-autocomplete/src/validation/validation.ts index e3c94aee3f482..cea9dccbd8d97 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/validation.ts +++ b/packages/kbn-esql-validation-autocomplete/src/validation/validation.ts @@ -43,7 +43,6 @@ import { isSupportedFunction, isTimeIntervalItem, inKnownTimeInterval, - printFunctionSignature, sourceExists, getColumnExists, hasWildcard, @@ -220,7 +219,7 @@ function validateNestedFunctionArg( values: { name: astFunction.name, argType: parameterDefinition.type, - value: printFunctionSignature(actualArg, false) || actualArg.name, + value: actualArg.text, givenType: argFn.signatures[0].returnType, }, locations: actualArg.location, From 82d324484c5829bfce7c4cd07177b3081fee7d06 Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Wed, 24 Jul 2024 11:57:25 -0600 Subject: [PATCH 3/4] extend support for non-string suggestions in tests --- .../autocomplete.command.stats.test.ts | 13 ++++++------ .../src/autocomplete/__tests__/helpers.ts | 21 +++++++++++++++++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts index e439be607c35f..7378760781d97 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.stats.test.ts @@ -73,13 +73,12 @@ describe('autocomplete.suggest', () => { test('on function left paren', async () => { const { assertSuggestions } = await setup(); - await assertSuggestions( - 'from a | stats by bucket(/', - [ - ...getFieldNamesByType(['number', 'date']), - ...getFunctionSignaturesByReturnType('eval', ['date', 'number'], { scalar: true }), - ].map((field) => `${field},`) - ); + await assertSuggestions('from a | stats by bucket(/', [ + ...getFieldNamesByType(['number', 'date']).map((field) => `${field},`), + ...getFunctionSignaturesByReturnType('eval', ['date', 'number'], { scalar: true }).map( + (s) => ({ ...s, text: `${s.text},` }) + ), + ]); await assertSuggestions('from a | stats round(/', [ ...getFunctionSignaturesByReturnType('stats', 'number', { agg: true, grouping: true }), diff --git a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts index ac2a6d9a85154..3d9ed7058a4a8 100644 --- a/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/autocomplete/__tests__/helpers.ts @@ -313,11 +313,28 @@ export const setup = async (caret = '/') => { ); }; - const assertSuggestions = async (query: string, expected: string[], opts?: SuggestOptions) => { + const assertSuggestions = async ( + query: string, + expected: Array, + opts?: SuggestOptions + ) => { const result = await suggest(query, opts); const resultTexts = [...result.map((suggestion) => suggestion.text)].sort(); - expect(resultTexts).toEqual([...expected].sort()); + const expectedTexts = expected + .map((suggestion) => (typeof suggestion === 'string' ? suggestion : suggestion.text ?? '')) + .sort(); + + expect(resultTexts).toEqual(expectedTexts); + + const expectedNonStringSuggestions = expected.filter( + (suggestion) => typeof suggestion !== 'string' + ) as PartialSuggestionWithText[]; + + for (const expectedSuggestion of expectedNonStringSuggestions) { + const suggestion = result.find((s) => s.text === expectedSuggestion.text); + expect(suggestion).toEqual(expect.objectContaining(expectedSuggestion)); + } }; return { From c74cf1e63ae20fb4f0cd2308067aaa08addd0442 Mon Sep 17 00:00:00 2001 From: Drew Tate Date: Wed, 24 Jul 2024 12:57:28 -0600 Subject: [PATCH 4/4] fix types --- .../kbn-esql-validation-autocomplete/src/definitions/helpers.ts | 2 +- .../src/validation/validation.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts b/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts index 60c3bb4725eba..a44db712ab230 100644 --- a/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts +++ b/packages/kbn-esql-validation-autocomplete/src/definitions/helpers.ts @@ -17,7 +17,7 @@ import type { CommandDefinition, FunctionDefinition } from './types'; */ export function getFunctionSignatures( { name, signatures }: FunctionDefinition, - { withTypes, capitalize }: { withTypes: boolean; capitalize: boolean } = { + { withTypes, capitalize }: { withTypes: boolean; capitalize?: boolean } = { withTypes: true, capitalize: false, } diff --git a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts index 67c8472cd53e8..73e66a5388939 100644 --- a/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts +++ b/packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts @@ -99,7 +99,7 @@ function prepareNestedFunction(fnSignature: FunctionDefinition): string { }, ], }, - { withTypes: false } + { withTypes: false, capitalize: false } )[0].declaration; } function getFieldMapping(