From 1a9ce3a8b974f53971f5b2925c937ef8e87df51c Mon Sep 17 00:00:00 2001 From: Kev Date: Wed, 5 Mar 2025 17:48:49 -0500 Subject: [PATCH 1/2] Fix parameterize types --- packages/core/src/exports.ts | 4 ++-- packages/core/src/index.ts | 3 ++- packages/core/src/types-hoist/index.ts | 2 +- packages/core/src/types-hoist/parameterize.ts | 6 ++++-- packages/core/src/utils/parameterize.ts | 18 +++++++++++++++--- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/core/src/exports.ts b/packages/core/src/exports.ts index aa173f5e5237..abd21c2a3206 100644 --- a/packages/core/src/exports.ts +++ b/packages/core/src/exports.ts @@ -23,7 +23,7 @@ import { logger } from './utils-hoist/logger'; import { uuid4 } from './utils-hoist/misc'; import { timestampInSeconds } from './utils-hoist/time'; import { GLOBAL_OBJ } from './utils-hoist/worldwide'; -import { parameterize } from './utils/parameterize'; +import { parameterizeAny } from './utils/parameterize'; import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent'; import { parseEventHintOrCaptureContext } from './utils/prepareEvent'; @@ -436,7 +436,7 @@ export const _experiment_log = { * Sentry._experiment_log.fmt`This is a log statement with ${x} and ${y} params` * ``` */ - fmt: parameterize, + fmt: parameterizeAny, /** * A flexible utility to record a log with a custom level and send it to sentry. diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 859f98837b8b..6d532ded0c63 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -67,7 +67,8 @@ export { hasTracingEnabled } from './utils/hasSpansEnabled'; export { hasSpansEnabled } from './utils/hasSpansEnabled'; export { isSentryRequestUrl } from './utils/isSentryRequestUrl'; export { handleCallbackErrors } from './utils/handleCallbackErrors'; -export { parameterize } from './utils/parameterize'; +export { parameterize } from './utils/parameterize'; + export { addAutoIpAddressToSession, addAutoIpAddressToUser } from './utils/ipAddress'; export { convertSpanLinksForEnvelope, diff --git a/packages/core/src/types-hoist/index.ts b/packages/core/src/types-hoist/index.ts index 95bc5fc55bd4..d1bf6027957f 100644 --- a/packages/core/src/types-hoist/index.ts +++ b/packages/core/src/types-hoist/index.ts @@ -153,7 +153,7 @@ export type { export type { BrowserClientReplayOptions, BrowserClientProfilingOptions } from './browseroptions'; export type { CheckIn, MonitorConfig, FinishedCheckIn, InProgressCheckIn, SerializedCheckIn } from './checkin'; -export type { ParameterizedString } from './parameterize'; +export type { ParameterizedString, ParameterizedAnyValueString } from './parameterize'; export type { ContinuousProfiler, ProfilingIntegration, Profiler } from './profiling'; export type { ViewHierarchyData, ViewHierarchyWindow } from './view-hierarchy'; export type { LegacyCSPReport } from './csp'; diff --git a/packages/core/src/types-hoist/parameterize.ts b/packages/core/src/types-hoist/parameterize.ts index a94daa3684db..2fd9a6cecc31 100644 --- a/packages/core/src/types-hoist/parameterize.ts +++ b/packages/core/src/types-hoist/parameterize.ts @@ -1,4 +1,6 @@ -export type ParameterizedString = string & { +export type ParameterizedAnyValueString = string & { __sentry_template_string__?: string; - __sentry_template_values__?: string[]; + __sentry_template_values__?: T[]; }; + +export type ParameterizedString = ParameterizedAnyValueString; diff --git a/packages/core/src/utils/parameterize.ts b/packages/core/src/utils/parameterize.ts index 392f4b70b444..30efedd4f341 100644 --- a/packages/core/src/utils/parameterize.ts +++ b/packages/core/src/utils/parameterize.ts @@ -1,4 +1,4 @@ -import type { ParameterizedString } from '../types-hoist'; +import type { ParameterizedAnyValueString } from '../types-hoist'; /** * Tagged template function which returns parameterized representation of the message @@ -8,9 +8,21 @@ import type { ParameterizedString } from '../types-hoist'; * @param strings An array of string values splitted between expressions * @param values Expressions extracted from template string * @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties + * @deprecated Use parameterizeAny instead */ -export function parameterize(strings: TemplateStringsArray, ...values: string[]): ParameterizedString { - const formatted = new String(String.raw(strings, ...values)) as ParameterizedString; +export const parameterize = parameterizeAny; + +/** + * Tagged template function which returns parameterized representation of the message + * For example: parameterize`This is a log statement with ${x} and ${y} params`, would return: + * "__sentry_template_string__": 'This is a log statement with %s and %s params', + * "__sentry_template_values__": ['first', 'second'] + * @param strings An array of string values splitted between expressions + * @param values Expressions extracted from template string, types provided by the generic. + * @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties + */ +export function parameterizeAny(strings: TemplateStringsArray, ...values: T[]): ParameterizedAnyValueString { + const formatted = new String(String.raw(strings, ...values)) as ParameterizedAnyValueString; formatted.__sentry_template_string__ = strings.join('\x00').replace(/%/g, '%%').replace(/\0/g, '%s'); formatted.__sentry_template_values__ = values; return formatted; From 80cd903fbfabccafd9b2139aa65413905c1eb351 Mon Sep 17 00:00:00 2001 From: Kev Date: Wed, 5 Mar 2025 18:22:44 -0500 Subject: [PATCH 2/2] Remove deprecation for now --- packages/core/src/utils/parameterize.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/src/utils/parameterize.ts b/packages/core/src/utils/parameterize.ts index 30efedd4f341..95c521ec7788 100644 --- a/packages/core/src/utils/parameterize.ts +++ b/packages/core/src/utils/parameterize.ts @@ -8,7 +8,6 @@ import type { ParameterizedAnyValueString } from '../types-hoist'; * @param strings An array of string values splitted between expressions * @param values Expressions extracted from template string * @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties - * @deprecated Use parameterizeAny instead */ export const parameterize = parameterizeAny;