Skip to content

feat: Avoid class fields all-together #14887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 3, 2025
Merged

feat: Avoid class fields all-together #14887

merged 5 commits into from
Jan 3, 2025

Conversation

mydea
Copy link
Member

@mydea mydea commented Jan 2, 2025

We already have an eslint rule to avoid class fields, but had exceptions for static fields as well as for arrow functions.

This also leads to bundle size increases, so removing the exceptions and handling the (few) exceptions we have there should save some bytes.

Additionally, this has additional challenges if we want to avoid/reduce polyfills, as class fields need to be polyfilled for ES2020, sadly.

Found as part of #14882

@mydea mydea requested review from Lms24, AbhiPrasad and chargome January 2, 2025 15:00
@mydea mydea self-assigned this Jan 2, 2025
@mydea mydea requested a review from a team as a code owner January 2, 2025 15:00
Copy link
Contributor

github-actions bot commented Jan 2, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 22.78 KB -0.27% -61 B 🔽
@sentry/browser - with treeshaking flags 21.53 KB -0.28% -60 B 🔽
@sentry/browser (incl. Tracing) 35.36 KB -0.16% -57 B 🔽
@sentry/browser (incl. Tracing, Replay) 72.57 KB -0.2% -148 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 62.96 KB -0.24% -153 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas) 76.96 KB -0.2% -151 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 89.34 KB -0.17% -153 B 🔽
@sentry/browser (incl. Feedback) 39.56 KB -0.15% -57 B 🔽
@sentry/browser (incl. sendFeedback) 27.43 KB -0.24% -67 B 🔽
@sentry/browser (incl. FeedbackAsync) 32.19 KB -0.19% -60 B 🔽
@sentry/react 25.54 KB -0.29% -75 B 🔽
@sentry/react (incl. Tracing) 38.17 KB -0.21% -79 B 🔽
@sentry/vue 27.06 KB -0.21% -56 B 🔽
@sentry/vue (incl. Tracing) 37.22 KB -0.16% -60 B 🔽
@sentry/svelte 22.95 KB -0.3% -70 B 🔽
CDN Bundle 24.17 KB -0.26% -63 B 🔽
CDN Bundle (incl. Tracing) 35.71 KB -0.16% -55 B 🔽
CDN Bundle (incl. Tracing, Replay) 70.75 KB -0.21% -148 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 75.95 KB -0.19% -141 B 🔽
CDN Bundle - uncompressed 70.74 KB -0.37% -266 B 🔽
CDN Bundle (incl. Tracing) - uncompressed 106.21 KB -0.25% -270 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed 220.14 KB -0.26% -578 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 233.02 KB -0.24% -573 B 🔽
@sentry/nextjs (client) 38.48 KB -0.16% -60 B 🔽
@sentry/sveltekit (client) 35.89 KB -0.16% -56 B 🔽
@sentry/node 162.9 KB -0.05% -67 B 🔽
@sentry/node - without tracing 98.67 KB -0.07% -69 B 🔽
@sentry/aws-serverless 128.51 KB -0.06% -68 B 🔽

View base workflow run

@@ -137,59 +189,4 @@ class SyncPromise<T> implements PromiseLike<T> {
});
});
}

/** JSDoc */
private readonly _resolve = (value?: T | PromiseLike<T> | null) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Instead of defining these fields in the constructor, can we declare them as regular methods (i.e. without the assignment)? I might be missing something as to why we didn't do this before though

So basically, like the public methods above, just as private? (Which I'm aware doesn't prevent them from being called externally but that should be fine)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to do bind then a bunch of times, which... is also possible probably, let me check the bundle size diff!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote this here to generally be a bit cleaner, so this should make more sense now!

* be hidden. Likewise, moving a different window to cover the contents of the
* page will also trigger a change to a hidden state.
*/
private _handleVisibilityChange: () => void = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as with syncpromise. But I feel like I'm missing something 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here this is def. "needed" because we need to un-register the callbacks again, for which we need the same function instance. so we cannot use fn.bind(this) here. I added a comment for this here!


/**
* Custom instrumentation for nestjs event-emitter
*
* This hooks into the `OnEvent` decorator, which is applied on event handlers.
*/
export class SentryNestEventInstrumentation extends InstrumentationBase {
public static readonly COMPONENT = '@nestjs/event-emitter';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chargome & @andreiborza I do not think these are needed/do anything, looking through the instrumentation base class code 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, not sure why I copied it in the first place 👍

mydea added 5 commits January 3, 2025 08:37
We already have an eslint rule to avoid class fields, but had exceptions for static fields as well as for arrow functions.

This also leads to bundle size increases, so removing the exceptions and handling the (few) exceptions we have there should save some bytes.

Additionally, this has additional challenges if we want to avoid/reduce polyfills, as class fields need to be polyfilled for ES2020, sadly.
@mydea mydea force-pushed the fn/avoid-class-methods branch from 6ad56f1 to b2d4ca2 Compare January 3, 2025 07:37
@mydea mydea merged commit 797a4dd into develop Jan 3, 2025
152 checks passed
@mydea mydea deleted the fn/avoid-class-methods branch January 3, 2025 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants