8000 ref(core): Remove guards around scope usage (#7554) · GingerAdonis/sentry-javascript@2738b5f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2738b5f

Browse files
authored
ref(core): Remove guards around scope usage (getsentry#7554)
1 parent 72dca3e commit 2738b5f

File tree

7 files changed

+15
-25
lines changed

7 files changed

+15
-25
lines changed

packages/core/src/hub.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class Hub implements HubInterface {
326326
*/
327327
public configureScope(callback: (scope: Scope) => void): void {
328328
const { scope, client } = this.getStackTop();
329-
if (scope && client) {
329+
if (client) {
330330
callback(scope);
331331
}
332332
}
@@ -413,7 +413,7 @@ export class Hub implements HubInterface {
413413
const session = makeSession({
414414
release,
415415
environment,
416-
...(scope && { user: scope.getUser() }),
416+
user: scope.getUser(),
417417
...(userAgent && { userAgent }),
418418
...context,
419419
});

packages/core/src/sdk.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ export function initAndBind<F extends Client, O extends ClientOptions>(
2828
}
2929
const hub = getCurrentHub();
3030
const scope = hub.getScope();
31-
if (scope) {
32-
scope.update(options.initialScope);
33-
}
31+
scope.update(optio 8000 ns.initialScope);
3432

3533
const client = new clientClass(options);
3634
hub.bindClient(client);

packages/core/src/sessionflusher.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ export class SessionFlusher implements SessionFlusherLike {
7272
return;
7373
}
7474
const scope = getCurrentHub().getScope();
75-
const requestSession = scope && scope.getRequestSession();
75+
const requestSession = scope.getRequestSession();
7676

7777
if (requestSession && requestSession.status) {
7878
this._incrementSessionStatusCount(requestSession.status, new Date());
7979
// This is not entirely necessarily but is added as a safe guard to indicate the bounds of a request and so in
8080
// case captureRequestSession is called more than once to prevent double count
81-
if (scope) {
82-
scope.setRequestSession(undefined);
83-
}
81+
scope.setRequestSession(undefined);
8482
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
8583
}
8684
}

packages/core/src/tracing/hubextensions.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import { Transaction } from './transaction';
1111
/** Returns all trace headers that are currently on the top scope. */
1212
function traceHeaders(this: Hub): { [key: string]: string } {
1313
const scope = this.getScope();
14-
if (scope) {
15-
const span = scope.getSpan();
16-
if (span) {
17-
return {
14+
const span = scope.getSpan();
15+
16+
return span
17+
? {
1818
'sentry-trace': span.toTraceparent(),
19-
};
20-
}
21-
}
22-
return {};
19+
}
20+
: {};
2321
}
2422

2523
/**

packages/core/src/tracing/idletransaction.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ export class IdleTransaction extends Transaction {
346346
*/
347347
function clearActiveTransaction(hub: Hub): void {
348348
const scope = hub.getScope();
349-
if (scope) {
350-
const transaction = scope.getTransaction();
351-
if (transaction) {
352-
scope.setSpan(undefined);
353-
}
349+
if (scope.getTransaction()) {
350+
scope.setSpan(undefined);
354351
}
355352
}

packages/core/src/tracing/transaction.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
256256
const maybeSampleRate = this.metadata.sampleRate;
257257
const sample_rate = maybeSampleRate !== undefined ? maybeSampleRate.toString() : undefined;
258258

259-
const scope = hub.getScope();
260-
const { segment: user_segment } = (scope && scope.getUser()) || {};
259+
const { segment: user_segment } = hub.getScope().getUser() || {};
261260

262261
const source = this.metadata.source;
263262

packages/core/src/tracing/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export { TRACEPARENT_REGEXP, extractTraceparentData } from '@sentry/utils';
2121
export function getActiveTransaction<T extends Transaction>(maybeHub?: Hub): T | undefined {
2222
const hub = maybeHub || getCurrentHub();
2323
const scope = hub.getScope();
24-
return scope && (scope.getTransaction() as T | undefined);
24+
return scope.getTransaction() as T | undefined;
2525
}
2626

2727
// so it can be used in manual instrumentation without necessitating a hard dependency on @sentry/utils

0 commit comments

Comments
 (0)
0