Skip to content

ref: Use optional chaining wherever possible #17017

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 2 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/browser/src/integrations/spotlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ export function isSpotlightInteraction(event: Event): boolean {
return Boolean(
event.type === 'transaction' &&
event.spans &&
event.contexts &&
event.contexts.trace &&
event.contexts?.trace &&
event.contexts.trace.op === 'ui.action.click' &&
event.spans.some(({ description }) => description?.includes('#sentry-spotlight')),
);
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/profiling/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface ProfiledEvent extends Event {
}

function getTraceId(event: Event): string {
const traceId: unknown = event.contexts?.trace?.['trace_id'];
const traceId: unknown = event.contexts?.trace?.trace_id;
// Log a warning if the profile has an invalid traceId (should be uuidv4).
// All profiles and transactions are rejected if this is the case and we want to
// warn users that this is happening if they enable debug flag
Expand Down Expand Up @@ -333,7 +333,7 @@ export function findProfiledTransactionsFromEnvelope(envelope: Envelope): Event[
for (let j = 1; j < item.length; j++) {
const event = item[j] as Event;

if (event?.contexts && event.contexts['profile'] && event.contexts['profile']['profile_id']) {
if (event?.contexts?.profile?.profile_id) {
events.push(item[j] as Event);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function endSpan(span: Span, handlerData: HandlerDataFetch): void {
if (handlerData.response) {
setHttpStatus(span, handlerData.response.status);

const contentLength = handlerData.response?.headers && handlerData.response.headers.get('content-length');
const contentLength = handlerData.response?.headers?.get('content-length');

if (contentLength) {
const contentLengthNum = parseInt(contentLength);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/integrations/dedupe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ function _isSameFingerprint(currentEvent: Event, previousEvent: Event): boolean
}

function _getExceptionFromEvent(event: Event): Exception | undefined {
return event.exception?.values && event.exception.values[0];
return event.exception?.values?.[0];
}
2 changes: 1 addition & 1 deletion packages/core/src/integrations/rewriteframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const rewriteFramesIntegration = defineIntegration((options: RewriteFrame
function _processStacktrace(stacktrace?: Stacktrace): Stacktrace {
return {
...stacktrace,
frames: stacktrace?.frames && stacktrace.frames.map(f => iteratee(f)),
frames: stacktrace?.frames?.map(f => iteratee(f)),
};
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/utils/vercelWaitUntil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export function vercelWaitUntil(task: Promise<unknown>): void {
// @ts-expect-error This is not typed
GLOBAL_OBJ[Symbol.for('@vercel/request-context')];

const ctx =
vercelRequestContextGlobal?.get && vercelRequestContextGlobal.get() ? vercelRequestContextGlobal.get() : {};
const ctx = vercelRequestContextGlobal?.get?.();
Copy link
Member

Choose a reason for hiding this comment

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

is this identical or should it rather be

Suggested change
const ctx = vercelRequestContextGlobal?.get?.();
const ctx = vercelRequestContextGlobal?.get?.() ?? {}

?

Copy link
Member Author

Choose a reason for hiding this comment

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

it's not identical, but how this is used is:

if (ctx?.waitUntil) {
    ctx.waitUntil(task);
  }

which should work the same I'd say?

Copy link
Member

Choose a reason for hiding this comment

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

ah ok, sorry, should have checked the usage. Makes sense!


if (ctx?.waitUntil) {
ctx.waitUntil(task);
Expand Down
4 changes: 2 additions & 2 deletions packages/feedback/src/modal/integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export const feedbackModalIntegration = ((): FeedbackModalIntegration => {
screenshotInput={screenshotInput}
showName={options.showName || options.isNameRequired}
showEmail={options.showEmail || options.isEmailRequired}
defaultName={(userKey && user && user[userKey.name]) || ''}
defaultEmail={(userKey && user && user[userKey.email]) || ''}
defaultName={(userKey && user?.[userKey.name]) || ''}
defaultEmail={(userKey && user?.[userKey.email]) || ''}
onFormClose={() => {
renderContent(false);
options.onFormClose?.();
Expand Down
13 changes: 6 additions & 7 deletions packages/node-core/src/integrations/spotlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ const _spotlightIntegration = ((options: Partial<SpotlightConnectionOptions> = {
return {
name: INTEGRATION_NAME,
setup(client) {
if (
typeof process === 'object' &&
process.env &&
process.env.NODE_ENV &&
process.env.NODE_ENV !== 'development'
) {
logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spotlight enabled?");
try {
if (process.env.NODE_ENV && process.env.NODE_ENV !== 'development') {
logger.warn("[Spotlight] It seems you're not in dev mode. Do you really want to have Spotlight enabled?");
}
} catch {
// ignore
}
connectToSpotlight(client, _options);
},
Expand Down
3 changes: 1 addition & 2 deletions packages/replay-internal/src/coreHandlers/handleDom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ export const handleDomListener: (replay: ReplayContainer) => (handlerData: Handl
if (
isClick &&
replay.clickDetector &&
event &&
event.target &&
event?.target &&
!event.altKey &&
!event.metaKey &&
!event.ctrlKey &&
Expand Down
2 changes: 1 addition & 1 deletion packages/replay-internal/src/util/isRrwebError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Event, EventHint } from '@sentry/core';
* Returns true if we think the given event is an error originating inside of rrweb.
*/
export function isRrwebError(event: Event, hint: EventHint): boolean {
if (event.type || !event.exception || !event.exception.values || !event.exception.values.length) {
if (event.type || !event.exception?.values?.length) {
return false;
}

Expand Down
Loading