Skip to content

Commit 050a636

Browse files
authored
ref: Add no-class-field-initializers eslint rule (#8700)
Adding a lint rule (see #5316) to enforce initializing of fields in constructor, to reduce bundle size of transpiled code (and streamline this a bit overall). Closes #5316
1 parent 6196bc5 commit 050a636

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+400
-147
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ class SentryErrorHandler implements AngularErrorHandler {
8383
protected readonly _options: ErrorHandlerOptions;
8484

8585
/* indicates if we already registered our the afterSendEvent handler */
86-
private _registeredAfterSendEventHandler = false;
86+
private _registeredAfterSendEventHandler;
8787

8888
public constructor(@Inject('errorHandlerOptions') options?: ErrorHandlerOptions) {
89+
this._registeredAfterSendEventHandler = false;
90+
8991
this._options = {
9092
logErrors: true,
9193
...options,

packages/angular/src/tracing.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ export class TraceService implements OnDestroy {
145145
}),
146146
);
147147

148-
private _routingSpan: Span | null = null;
148+
private _routingSpan: Span | null;
149149

150-
private _subscription: Subscription = new Subscription();
150+
private _subscription: Subscription;
151151

152152
public constructor(private readonly _router: Router) {
153+
this._routingSpan = null;
154+
this._subscription = new Subscription();
155+
153156
this._subscription.add(this.navStart$.subscribe());
154157
this._subscription.add(this.resEnd$.subscribe());
155158
this._subscription.add(this.navEnd$.subscribe());

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class Breadcrumbs implements Integration {
5656
/**
5757
* @inheritDoc
5858
*/
59-
public name: string = Breadcrumbs.id;
59+
public name: string;
6060

6161
/**
6262
* Options of the breadcrumbs integration.
@@ -68,6 +68,7 @@ export class Breadcrumbs implements Integration {
6868
* @inheritDoc
6969
*/
7070
public constructor(options?: Partial<BreadcrumbsOptions>) {
71+
this.name = Breadcrumbs.id;
7172
this.options = {
7273
console: true,
7374
dom: true,

packages/browser/src/integrations/dedupe.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ export class Dedupe implements Integration {
1111
/**
1212
* @inheritDoc
1313
*/
14-
public name: string = Dedupe.id;
14+
public name: string;
1515

1616
/**
1717
* @inheritDoc
1818
*/
1919
private _previousEvent?: Event;
2020

21+
public constructor() {
22+
this.name = Dedupe.id;
23+
}
24+
2125
/**
2226
* @inheritDoc
2327
*/

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class GlobalHandlers implements Integration {
3030
/**
3131
* @inheritDoc
3232
*/
33-
public name: string = GlobalHandlers.id;
33+
public name: string;
3434

3535
/** JSDoc */
3636
private readonly _options: GlobalHandlersIntegrations;
@@ -39,18 +39,21 @@ export class GlobalHandlers implements Integration {
3939
* Stores references functions to installing handlers. Will set to undefined
4040
* after they have been run so that they are not used twice.
4141
*/
42-
private _installFunc: Record<GlobalHandlersIntegrationsOptionKeys, (() => void) | undefined> = {
43-
onerror: _installGlobalOnErrorHandler,
44-
onunhandledrejection: _installGlobalOnUnhandledRejectionHandler,
45-
};
42+
private _installFunc: Record<GlobalHandlersIntegrationsOptionKeys, (() => void) | undefined>;
4643

4744
/** JSDoc */
4845
public constructor(options?: GlobalHandlersIntegrations) {
46+
this.name = GlobalHandlers.id;
4947
this._options = {
5048
onerror: true,
5149
onunhandledrejection: true,
5250
...options,
5351
};
52+
53+
this._installFunc = {
54+
onerror: _installGlobalOnErrorHandler,
55+
onunhandledrejection: _installGlobalOnUnhandledRejectionHandler,
56+
};
5457
}
5558
/**
5659
* @inheritDoc

packages/browser/src/integrations/httpcontext.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export class HttpContext implements Integration {
1313
/**
1414
* @inheritDoc
1515
*/
16-
public name: string = HttpContext.id;
16+
public name: string;
17+
18+
public constructor() {
19+
this.name = HttpContext.id;
20+
}
1721

1822
/**
1923
* @inheritDoc

packages/browser/src/integrations/linkederrors.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class LinkedErrors implements Integration {
2121
/**
2222
* @inheritDoc
2323
*/
24-
public readonly name: string = LinkedErrors.id;
24+
public readonly name: string;
2525

2626
/**
2727
* @inheritDoc
@@ -37,6 +37,7 @@ export class LinkedErrors implements Integration {
3737
* @inheritDoc
3838
*/
3939
public constructor(options: Partial<LinkedErrorsOptions> = {}) {
40+
this.name = LinkedErrors.id;
4041
this._key = options.key || DEFAULT_KEY;
4142
this._limit = options.limit || DEFAULT_LIMIT;
4243
}

packages/browser/src/integrations/trycatch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class TryCatch implements Integration {
5656
/**
5757
* @inheritDoc
5858
*/
59-
public name: string = TryCatch.id;
59+
public name: string;
6060

6161
/** JSDoc */
6262
private readonly _options: TryCatchOptions;
@@ -65,6 +65,7 @@ export class TryCatch implements Integration {
6565
* @inheritDoc
6666
*/
6767
public constructor(options?: Partial<TryCatchOptions>) {
68+
this.name = TryCatch.id;
6869
this._options = {
6970
XMLHttpRequest: true,
7071
eventTarget: true,

packages/browser/src/profiling/integration.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ import {
2222
* @experimental
2323
*/
2424
export class BrowserProfilingIntegration implements Integration {
25-
public readonly name: string = 'BrowserProfilingIntegration';
26-
public getCurrentHub?: () => Hub = undefined;
25+
public static id: string = 'BrowserProfilingIntegration';
26+
27+
public readonly name: string;
28+
29+
public getCurrentHub?: () => Hub;
30+
31+
public constructor() {
32+
this.name = BrowserProfilingIntegration.id;
33+
}
2734

2835
/**
2936
* @inheritDoc

packages/core/src/baseclient.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
9393
protected readonly _transport?: Transport;
9494

9595
/** Array of set up integrations. */
96-
protected _integrations: IntegrationIndex = {};
96+
protected _integrations: IntegrationIndex;
9797

9898
/** Indicates whether this client's integrations have been set up. */
99-
protected _integrationsInitialized: boolean = false;
99+
protected _integrationsInitialized: boolean;
100100

101101
/** Number of calls being processed */
102-
protected _numProcessing: number = 0;
102+
protected _numProcessing: number;
103103

104104
/** Holds flushable */
105-
private _outcomes: { [key: string]: number } = {};
105+
private _outcomes: { [key: string]: number };
106106

107107
// eslint-disable-next-line @typescript-eslint/ban-types
108-
private _hooks: Record<string, Function[]> = {};
108+
private _hooks: Record<string, Function[]>;
109109

110110
/**
111111
* Initializes this client instance.
@@ -114,6 +114,11 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
114114
*/
115115
protected constructor(options: O) {
116116
this._options = options;
117+
this._integrations = {};
118+
this._integrationsInitialized = false;
119+
this._numProcessing = 0;
120+
this._outcomes = {};
121+
this._hooks = {};
117122

118123
if (options.dsn) {
119124
this._dsn = makeDsn(options.dsn);

packages/core/src/integrations/functiontostring.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export class FunctionToString implements Integration {
1313
/**
1414
* @inheritDoc
1515
*/
16-
public name: string = FunctionToString.id;
16+
public name: string;
17+
18+
public constructor() {
19+
this.name = FunctionToString.id;
20+
}
1721

1822
/**
1923
* @inheritDoc

packages/core/src/integrations/inboundfilters.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ export class InboundFilters implements Integration {
3636
/**
3737
* @inheritDoc
3838
*/
39-
public name: string = InboundFilters.id;
39+
public name: string;
4040

41-
public constructor(private readonly _options: Partial<InboundFiltersOptions> = {}) {}
41+
private readonly _options: Partial<InboundFiltersOptions>;
42+
43+
public constructor(options: Partial<InboundFiltersOptions> = {}) {
44+
this.name = InboundFilters.id;
45+
this._options = options;
46+
}
4247

4348
/**
4449
* @inheritDoc

packages/core/src/integrations/metadata.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export class ModuleMetadata implements Integration {
2121
/**
2222
* @inheritDoc
2323
*/
24-
public name: string = ModuleMetadata.id;
24+
public name: string;
25+
26+
public constructor() {
27+
this.name = ModuleMetadata.id;
28+
}
2529

2630
/**
2731
* @inheritDoc

packages/core/src/sessionflusher.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ type ReleaseHealthAttributes = {
1818
* @inheritdoc
1919
*/
2020
export class SessionFlusher implements SessionFlusherLike {
21-
public readonly flushTimeout: number = 60;
22-
private _pendingAggregates: Record<number, AggregationCounts> = {};
21+
public readonly flushTimeout: number;
22+
private _pendingAggregates: Record<number, AggregationCounts>;
2323
private _sessionAttrs: ReleaseHealthAttributes;
2424
private _intervalId: ReturnType<typeof setInterval>;
25-
private _isEnabled: boolean = true;
25+
private _isEnabled: boolean;
2626
private _client: Client;
2727

2828
public constructor(client: Client, attrs: ReleaseHealthAttributes) {
2929
this._client = client;
30+
this.flushTimeout = 60;
31+
this._pendingAggregates = {};
32+
this._isEnabled = true;
33+
3034
// Call to setInterval, so that flush is called every 60 seconds
3135
this._intervalId = setInterval(() => this.flush(), this.flushTimeout * 1000);
3236
this._sessionAttrs = attrs;

packages/core/src/tracing/idletransaction.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,27 @@ export type BeforeFinishCallback = (transactionSpan: IdleTransaction, endTimesta
6969
*/
7070
export class IdleTransaction extends Transaction {
7171
// Activities store a list of active spans
72-
public activities: Record<string, boolean> = {};
73-
72+
public activities: Record<string, boolean>;
7473
// Track state of activities in previous heartbeat
7574
private _prevHeartbeatString: string | undefined;
7675

7776
// Amount of times heartbeat has counted. Will cause transaction to finish after 3 beats.
78-
private _heartbeatCounter: number = 0;
77+
private _heartbeatCounter: number;
7978

8079
// We should not use heartbeat if we finished a transaction
81-
private _finished: boolean = false;
80+
private _finished: boolean;
8281

8382
// Idle timeout was canceled and we should finish the transaction with the last span end.
84-
private _idleTimeoutCanceledPermanently: boolean = false;
83+
private _idleTimeoutCanceledPermanently: boolean;
8584

86-
private readonly _beforeFinishCallbacks: BeforeFinishCallback[] = [];
85+
private readonly _beforeFinishCallbacks: BeforeFinishCallback[];
8786

8887
/**
8988
* Timer that tracks Transaction idleTimeout
9089
*/
9190
private _idleTimeoutID: ReturnType<typeof setTimeout> | undefined;
9291

93-
private _finishReason: (typeof IDLE_TRANSACTION_FINISH_REASONS)[number] = IDLE_TRANSACTION_FINISH_REASONS[4];
92+
private _finishReason: (typeof IDLE_TRANSACTION_FINISH_REASONS)[number];
9493

9594
public constructor(
9695
transactionContext: TransactionContext,
@@ -110,6 +109,13 @@ export class IdleTransaction extends Transaction {
110109
) {
111110
super(transactionContext, _idleHub);
112111

112+
this.activities = {};
113+
this._heartbeatCounter = 0;
114+
this._finished = false;
115+
this._idleTimeoutCanceledPermanently = false;
116+
this._beforeFinishCallbacks = [];
117+
this._finishReason = IDLE_TRANSACTION_FINISH_REASONS[4];
118+
113119
if (_onScope) {
114120
// We set the transaction here on the scope so error events pick up the trace
115121
// context and attach it to the error.

packages/core/src/tracing/span.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import { dropUndefinedKeys, generateSentryTraceHeader, logger, timestampInSecond
1616
* @hidden
1717
*/
1818
export class SpanRecorder {
19-
public spans: Span[] = [];
19+
public spans: Span[];
2020

2121
private readonly _maxlen: number;
2222

2323
public constructor(maxlen: number = 1000) {
2424
this._maxlen = maxlen;
25+
this.spans = [];
2526
}
2627

2728
/**
@@ -46,12 +47,12 @@ export class Span implements SpanInterface {
4647
/**
4748
* @inheritDoc
4849
*/
49-
public traceId: string = uuid4();
50+
public traceId: string;
5051

5152
/**
5253
* @inheritDoc
5354
*/
54-
public spanId: string = uuid4().substring(16);
55+
public spanId: string;
5556

5657
/**
5758
* @inheritDoc
@@ -71,7 +72,7 @@ export class Span implements SpanInterface {
7172
/**
7273
* Timestamp in seconds when the span was created.
7374
*/
74-
public startTimestamp: number = timestampInSeconds();
75+
public startTimestamp: number;
7576

7677
/**
7778
* Timestamp in seconds when the span ended.
@@ -91,13 +92,13 @@ export class Span implements SpanInterface {
9192
/**
9293
* @inheritDoc
9394
*/
94-
public tags: { [key: string]: Primitive } = {};
95+
public tags: { [key: string]: Primitive };
9596

9697
/**
9798
* @inheritDoc
9899
*/
99100
// eslint-disable-next-line @typescript-eslint/no-explicit-any
100-
public data: { [key: string]: any } = {};
101+
public data: { [key: string]: any };
101102

102103
/**
103104
* List of spans that were finalized
@@ -112,7 +113,7 @@ export class Span implements SpanInterface {
112113
/**
113114
* The instrumenter that created this span.
114115
*/
115-
public instrumenter: Instrumenter = 'sentry';
116+
public instrumenter: Instrumenter;
116117

117118
/**
118119
* You should never call the constructor manually, always use `Sentry.startTransaction()`
@@ -122,6 +123,13 @@ export class Span implements SpanInterface {
122123
* @hidden
123124
*/
124125
public constructor(spanContext?: SpanContext) {
126+
this.traceId = uuid4();
127+
this.spanId = uuid4().substring(16);
128+
this.startTimestamp = timestampInSeconds();
129+
this.tags = {};
130+
this.data = {};
131+
this.instrumenter = 'sentry';
132+
125133
if (!spanContext) {
126134
return this;
127135
}

0 commit comments

Comments
 (0)