Skip to content

Commit

Permalink
fix(browser): Don't use chrome variable name (#10874)
Browse files Browse the repository at this point in the history
resolves #6880

Using `const chrome = ...` or similar breaks certain setups based on
their bundler config. This makes changes to browser code so that we
never declare a variable named `chrome`.

I tried setting up https://eslint.org/docs/latest/rules/id-denylist to
enforce this, but this unfortunately also looks as types declarations
(so `type K = { chrome: ... }` is problematic.
  • Loading branch information
AbhiPrasad committed Mar 7, 2024
1 parent 3c841ce commit ef59a91
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions packages/browser/src/stack-parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ const chromeRegex =
/^\s*at (?:(.+?\)(?: \[.+\])?|.*?) ?\((?:address at )?)?(?:async )?((?:<anonymous>|[-a-z]+:|.*bundle|\/)?.*?)(?::(\d+))?(?::(\d+))?\)?\s*$/i;
const chromeEvalRegex = /\((\S*)(?::(\d+))(?::(\d+))\)/;

const chrome: StackLineParserFn = line => {
// We cannot call this variable `chrome` because it can conflict with global `chrome` variable in certain environments
// See: https://github.com/getsentry/sentry-javascript/issues/6880
const chromeStackParserFn: StackLineParserFn = line => {
const parts = chromeRegex.exec(line);

if (parts) {
Expand All @@ -85,7 +87,7 @@ const chrome: StackLineParserFn = line => {
return;
};

export const chromeStackLineParser: StackLineParser = [CHROME_PRIORITY, chrome];
export const chromeStackLineParser: StackLineParser = [CHROME_PRIORITY, chromeStackParserFn];

// gecko regex: `(?:bundle|\d+\.js)`: `bundle` is for react native, `\d+\.js` also but specifically for ram bundles because it
// generates filenames without a prefix like `file://` the filenames in the stacktrace are just 42.js
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/vendor/supportsHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function supportsHistory(): boolean {
// borrowed from: https://github.com/angular/angular.js/pull/13945/files
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chrome = (WINDOW as any).chrome;
const isChromePackagedApp = chrome && chrome.app && chrome.app.runtime;
const chromeVar = (WINDOW as any).chrome;
const isChromePackagedApp = chromeVar && chromeVar.app && chromeVar.app.runtime;
/* eslint-enable @typescript-eslint/no-unsafe-member-access */
const hasHistoryApi = 'history' in WINDOW && !!WINDOW.history.pushState && !!WINDOW.history.replaceState;

Expand Down

0 comments on commit ef59a91

Please sign in to comment.