Skip to content

Commit

Permalink
fix(core): Export Scope interface as Scope (#12067)
Browse files Browse the repository at this point in the history
To make interop with e.g. importing `Scope` from `@sentry/node` easier.

Closes #12053
  • Loading branch information
mydea authored May 16, 2024
1 parent 4b18edd commit 4a1f2ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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(),
Expand Down
11 changes: 11 additions & 0 deletions packages/core/test/lib/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 4a1f2ac

Please sign in to comment.