From 5e584d4d5b52c66ef726f638fd18267d6ce2816b Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Thu, 16 May 2024 13:17:21 +0200 Subject: [PATCH] fix(core): Export Scope interface as `Scope` To make interop with e.g. importing `Scope` from `@sentry/node` easier. --- packages/core/src/scope.ts | 20 +++++++++++++++++--- packages/core/test/lib/scope.test.ts | 11 +++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/core/src/scope.ts b/packages/core/src/scope.ts index 4bf17b936563..724c9b621ce9 100644 --- a/packages/core/src/scope.ts +++ b/packages/core/src/scope.ts @@ -34,7 +34,7 @@ const DEFAULT_MAX_BREADCRUMBS = 100; /** * Holds additional event information. */ -export class Scope implements ScopeInterface { +class ScopeClass implements ScopeInterface { /** Flag if notifying is happening. */ protected _notifyingListeners: boolean; @@ -116,8 +116,8 @@ export class Scope implements ScopeInterface { /** * @inheritDoc */ - public clone(): Scope { - const newScope = new Scope(); + public clone(): ScopeClass { + const newScope = new ScopeClass(); newScope._breadcrumbs = [...this._breadcrumbs]; newScope._tags = { ...this._tags }; newScope._extra = { ...this._extra }; @@ -587,6 +587,20 @@ export class Scope implements ScopeInterface { } } +// NOTE: By exporting this here as const & type, instead of doing `export class`, +// We can get the correct class when importing from `@sentry/core`, but the original type (from `@sentry/types`) +// This is helpful for interop, e.g. when doing `import type { Scope } from '@sentry/node';` (which re-exports this) + +/** + * Holds additional event information. + */ +export const Scope = ScopeClass; + +/** + * Holds additional event information. + */ +export type Scope = ScopeInterface; + function generatePropagationContext(): PropagationContext { return { traceId: uuid4(), diff --git a/packages/core/test/lib/scope.test.ts b/packages/core/test/lib/scope.test.ts index aadc26856c6e..a58370a1b632 100644 --- a/packages/core/test/lib/scope.test.ts +++ b/packages/core/test/lib/scope.test.ts @@ -985,4 +985,15 @@ describe('withIsolationScope()', () => { done(); }); }); + + it('Scope type is equal to Scope from @sentry/types', () => { + // We pass the Scope _class_ here to the callback, + // Which actually is typed as using the Scope from @sentry/types + // This should not TS-error, as we export the type from core as well + const scope = withScope((scope: Scope) => { + return scope; + }); + + expect(scope).toBeDefined(); + }); });