8000 [v7] Use || instead of ?? wherever applicable to save some bytes afte… · xiaohuoni/sentry-javascript@14200d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14200d1

Browse files
committed
[v7] Use || instead of ?? wherever applicable to save some bytes after TS compilation
1 parent 41e3200 commit 14200d1

File tree

20 files changed

+56
-75
lines changed

20 files changed

+56
-75
lines changed

packages/browser/src/sdk.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ export function showReportDialog(
9090
return;
9191
}
9292

93-
const client = customClient ?? getCurrentClient();
93+
const client = customClient || getCurrentClient();
9494
if (!client) {
9595
return;
9696
}
9797

98-
options.eventId = options.eventId ?? client.lastEventId();
99-
options.dsn = options.dsn ?? client.options.dsn;
98+
options.eventId = options.eventId || client.lastEventId();
99+
options.dsn = options.dsn || client.options.dsn;
100100

101101
if (client.options.enabled === false) {
102102
client.logger.error(`${errPrefix} disabled client`);

packages/core/src/baseclient.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
8585
* @param options Options for the client.
8686
*/
8787
protected constructor(options: O) {
88-
this.options = options ?? {};
88+
this.options = options || {};
8989
this.logger.enabled = !!this.options.debug;
9090

9191
if (this.options.dsn) {
@@ -343,12 +343,12 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
343343
if (this.options._internal?.sdk) {
344344
const { name, version, integrations, packages } = this.options._internal?.sdk;
345345

346-
event.sdk = event.sdk ?? {
346+
event.sdk = event.sdk || {
347347
name,
348348
version,
349349
};
350-
event.sdk.name = event.sdk.name ?? name;
351-
event.sdk.version = event.sdk.version ?? version;
350+
event.sdk.name = event.sdk.name || name;
351+
event.sdk.version = event.sdk.version || version;
352352
event.sdk.integrations = [...(event.sdk.integrations || []), ...(integrations || [])];
353353
event.sdk.packages = [...(event.sdk.packages || []), ...(packages || [])];
354354
}
@@ -438,8 +438,8 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
438438
try {
439439
let processedEvent: SentryEvent | null = {
440440
...event,
441-
event_id: event.event_id ?? uuid4(),
442-
timestamp: event.timestamp ?? dateTimestampInSeconds(),
441+
event_id: event.event_id || uuid4(),
442+
timestamp: event.timestamp || dateTimestampInSeconds(),
443443
};
444444

445445
this._applyClientOptions(processedEvent);
@@ -476,7 +476,7 @@ export abstract class BaseClient<O extends Options> implements ClientLike<O> {
476476
}
477477

478478
const normalizeDepth = this.options.normalizeDepth ?? 3;
479-
if (typeof normalizeDepth === 'number' && normalizeDepth > 0) {
479+
if (normalizeDepth > 0) {
480480
processedEvent = this._normalizeEvent(processedEvent, normalizeDepth);
481481
}
482482

packages/eventbuilder-browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function eventFromMessage(options: Options, message: string, captureConte
5151
if (captureContext.hint?.event_id) {
5252
event.event_id = captureContext.hint?.event_id;
5353
}
54-
event.level = captureContext.scope?.level ?? Severity.Info;
54+
event.level = captureContext.scope?.level || Severity.Info;
5555
event.platform = 'javascript';
5656
return event;
5757
}

packages/eventbuilder-node/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ export function eventFromException(options: Options, exception: unknown, capture
5151
event.server_name = (options as { serverName?: string }).serverName;
5252
}
5353
if (shouldSerializeException) {
54-
event.extra = event.extra ?? {};
54+
event.extra = event.extra || {};
5555
event.extra.__serialized__ = normalizeToSize(exception as Record<string, unknown>);
5656
}
5757
return event;
5858
}
5959

6060
export function eventFromMessage(options: Options, message: string, captureContext: CaptureContext): SentryEvent {
6161
const event: SentryEvent = {
62-
level: captureContext.scope?.level ?? Severity.Info,
62+
level: captureContext.scope?.level || Severity.Info,
6363
message,
6464
platform: 'node',
6565
};

packages/integration-browser-breadcrumbs/src/history.ts

-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class HistoryBreadcrumbs implements Integration {
1212
let from = handlerData.from;
1313
let to = handlerData.to;
1414
const parsedLoc = parseUrl(global.location.href);
15-
let parsedFrom = parseUrl(from ?? '');
16-
const parsedTo = parseUrl(to ?? '');
15+
let parsedFrom = parseUrl(from || '');
16+
const parsedTo = parseUrl(to || '');
1717

1818
// Initial pushState doesn't provide `from` information
1919
if (!parsedFrom.path) {

packages/integration-browser-linkederrors/src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ import { ClientLike, EventHint, Exception, ExtendedError, Integration, SentryEve
22
import { isInstanceOf } from '@sentry/utils';
33
import { computeStackTrace, exceptionFromStacktrace } from '@sentry/eventbuilder-browser';
44

5-
const DEFAULT_KEY = 'cause';
6-
const DEFAULT_LIMIT = 5;
7-
85
export class LinkedErrors implements Integration {
96
public name = this.constructor.name;
107

118
private readonly _key: string;
129
private readonly _limit: number;
1310

1411
public constructor(options: { key?: string; limit?: number } = {}) {
15-
this._key = options.key ?? DEFAULT_KEY;
16-
this._limit = options.limit ?? DEFAULT_LIMIT;
12+
this._key = options.key || 'cause';
13+
this._limit = options.limit ?? 5;
1714
}
1815

1916
public install(client: ClientLike): void {

packages/integration-browser-offline/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Offline implements Integration {
3333
* @inheritDoc
3434
*/
3535
public constructor(options: OfflineOptions = {}) {
36-
this.maxStoredEvents = options.maxStoredEvents ?? 30; // set a reasonable default
36+
this.maxStoredEvents = options.maxStoredEvents ?? 30;
3737
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
3838
this.offlineEventStore = localForage.createInstance({
3939
name: 'sentry/offlineEventStore',

packages/integration-browser-wrap/src/timers.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,29 @@ type TimersWrapOptions = {
1212
export class TimersWrap implements Integration {
1313
public name = this.constructor.name;
1414

15-
private _setTimeout: boolean;
16-
private _setInterval: boolean;
17-
private _requestAnimationFrame: boolean;
18-
19-
public constructor({ setTimeout, setInterval, requestAnimationFrame }: TimersWrapOptions = {}) {
20-
this._setTimeout = setTimeout ?? true;
21-
this._setInterval = setInterval ?? true;
22-
this._requestAnimationFrame = requestAnimationFrame ?? true;
15+
private _options: TimersWrapOptions;
16+
17+
public constructor(options: TimersWrapOptions = {}) {
18+
this._options = {
19+
setTimeout: true,
20+
setInterval: true,
21+
requestAnimationFrame: true,
22+
...options,
23+
};
2324
}
2425

2526
public install(): void {
2627
const global = getGlobalObject();
2728

28-
if (this._setTimeout) {
29+
if (this._options.setTimeout) {
2930
fill(global, 'setTimeout', this._wrapTimerFunction.bind(this));
3031
}
3132

32-
if (this._setInterval) {
33+
if (this._options.setInterval) {
3334
fill(global, 'setInterval', this._wrapTimerFunction.bind(this));
3435
}
3536

36-
if (this._requestAnimationFrame) {
37+
if (this._options.requestAnimationFrame) {
3738
fill(global, 'requestAnimationFrame', this._wrapTimerFunction.bind(this));
3839
}
3940
}

packages/integration-common-captureconsole/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class CaptureConsole implements Integration {
1414
private _levels: Level[];
1515

1616
public constructor(options: CaptoreConsoleOptions = {}) {
17-
this._levels = options.levels ?? ((LEVELS as unknown) as Level[]);
17+
this._levels = options.levels || ((LEVELS as unknown) as Level[]);
1818
}
1919

2020
public install(client: ClientLike): void {

packages/integration-node-contextlines/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ClientLike, Integration, StackFrame } from '@sentry/types';
44
import { snipLine } from '@sentry/utils';
55
import { LRUMap } from 'lru_map';
66

7-
const DEFAULT_LINES_OF_CONTEXT: number = 7;
87
const FILE_CONTENT_CACHE = new LRUMap<string, string | null>(100);
98

109
// TODO: Write some performance tests for LRU/Promise memory issue.
@@ -22,8 +21,7 @@ export class ContextLines implements Integration {
2221
public name = this.constructor.name;
2322

2423
public install(client: ClientLike): void {
25-
const linesOfContext =
26-
(client.options as { frameContextLines?: number }).frameContextLines ?? DEFAULT_LINES_OF_CONTEXT;
24+
const linesOfContext = (client.options as { frameContextLines?: number }).frameContextLines ?? 7;
2725

2826
client.addEventProcessor(event => {
2927
const frames = event.exception?.values?.[0].stacktrace?.frames;
@@ -66,7 +64,7 @@ export class ContextLines implements Integration {
6664
* @param frame StackFrame that will be mutated
6765
* @param linesOfContext number of context lines we want to add pre/post
6866
*/
69-
function addContextToFrame(lines: string[], frame: StackFrame, linesOfContext: number = 5): void {
67+
function addContextToFrame(lines: string[], frame: StackFrame, linesOfContext: number): void {
7068
const lineno = frame.lineno || 0;
7169
const maxLines = lines.length;
7270
const sourceLine = Math.max(Math.min(maxLines, lineno - 1), 0);

0 commit comments

Comments
 (0)
0