From f3f0ca81a6c7479daf5fe28da847387a9ebefd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Tue, 8 Oct 2024 17:53:19 +0200 Subject: [PATCH] Add support for LiteralTemplates in `@kbn/telemetry-tools` --- .../__fixture__/telemetry_collectors/constants.ts | 3 +++ .../kbn-telemetry-tools/src/tools/serializer.test.ts | 7 +++++++ packages/kbn-telemetry-tools/src/tools/serializer.ts | 11 ++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts b/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts index c9b0cbfe0281a..ec8b935912cd7 100644 --- a/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts +++ b/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts @@ -60,6 +60,9 @@ export interface MappedTypes { mappedTypeWithOneInlineProp: { [key in 'prop3']: number; }; + mappedTypeWithLiteralTemplates: { + [key in MappedTypeProps | `templated_prop/${string}`]: number; + }; } export type RecordWithKnownProps = Record; diff --git a/packages/kbn-telemetry-tools/src/tools/serializer.test.ts b/packages/kbn-telemetry-tools/src/tools/serializer.test.ts index 4301ab30fd09e..86f897767a025 100644 --- a/packages/kbn-telemetry-tools/src/tools/serializer.test.ts +++ b/packages/kbn-telemetry-tools/src/tools/serializer.test.ts @@ -105,6 +105,13 @@ describe('getDescriptor', () => { mappedTypeWithOneInlineProp: { prop3: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, }, + mappedTypeWithLiteralTemplates: { + prop1: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, + prop2: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, + // ideally, it'd be `templated_prop/@@INDEX@@` to be more explicit. But we're going with the fuzzier approach + // for now as it may require more changes downstream that are not worth it. + '@@INDEX@@': { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, + }, }); }); diff --git a/packages/kbn-telemetry-tools/src/tools/serializer.ts b/packages/kbn-telemetry-tools/src/tools/serializer.ts index 2523bc246d2a8..0269dc446b4a0 100644 --- a/packages/kbn-telemetry-tools/src/tools/serializer.ts +++ b/packages/kbn-telemetry-tools/src/tools/serializer.ts @@ -99,10 +99,15 @@ export function getConstraints(node: ts.Node, program: ts.Program): any { return node.literal.text; } - if (ts.isStringLiteral(node)) { + if (ts.isStringLiteral(node) || ts.isStringLiteralLike(node)) { return node.text; } + // template literals such as `smth/${string}` + if (ts.isTemplateLiteralTypeNode(node) || ts.isTemplateExpression(node)) { + return '@@INDEX@@'; // just map it to any kind of string. We can enforce it further in the future if we see fit. + } + if (ts.isImportSpecifier(node) || ts.isExportSpecifier(node)) { const source = node.getSourceFile(); const importedModuleName = getModuleSpecifier(node); @@ -180,9 +185,9 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor | const constraintsArray = Array.isArray(constraints) ? constraints : [constraints]; if (typeof constraintsArray[0] === 'string') { return constraintsArray.reduce((acc, c) => { - (acc as Record)[c] = descriptor; + acc[c] = descriptor; return acc; - }, {}); + }, {} as Record); } } return { '@@INDEX@@': descriptor };