sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Use optional chaining internally

Open VIKTORVAV99 opened this issue 1 year ago • 0 comments

Problem Statement

Now that the JS SDK supports ES2018 and forward would it be possible to use optional chaining internally to reduce bundle sizes? It is currently being blocked by Avoid using optional chaining eslint(@sentry-internal/sdk/no-optional-chaining).

I don't expect any large size reductions but I don't really see any drawbacks to using it since it should be supported in all targets.

Solution Brainstorm

There are multiple places where this can be used I the JS SDKs but the biggest benefits would be to reduce statements like this:

  const isInsideChromeExtension =
    windowWithMaybeChrome &&
    windowWithMaybeChrome.chrome &&
    windowWithMaybeChrome.chrome.runtime &&
    windowWithMaybeChrome.chrome.runtime.id;

to just:

  const isInsideChromeExtension =
    windowWithMaybeChrome?.chrome?.runtime?.id;

I would be happy to open some PRs myself if the mentioned ESLint rule is revised.

VIKTORVAV99 avatar May 18 '24 10:05 VIKTORVAV99