Skip to content

Commit 08b821f

Browse files
authored
shopify theme issue fix (#2711)
* 11568-shopify theme issue fixes * plat-11568 added test cases * plat-11568 made changes inside changelog.md file
1 parent 74318d8 commit 08b821f

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
7+
- (plugin-inline-script-content) Fix strict mode compatibility by replacing `arguments` usage with rest parameters [#2711](https://github.com/bugsnag/bugsnag-js/pull/2711)
8+
59
### Added
610

711
- (delivery-react-native) Handle request and response parameters [#2667](https://github.com/bugsnag/bugsnag-js/pull/2667)

packages/plugin-inline-script-content/inline-script-content.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ module.exports = (doc = document, win = window) => ({
126126
})
127127

128128
function __traceOriginalScript (fn, callbackAccessor, alsoCallOriginal = false) {
129-
return function () {
129+
return function (...args) {
130130
// this is required for removeEventListener to remove anything added with
131131
// addEventListener before the functions started being wrapped by Bugsnag
132-
const args = [].slice.call(arguments)
133132
try {
134133
const cba = callbackAccessor(args)
135134
const cb = cba.get()
@@ -142,14 +141,14 @@ module.exports = (doc = document, win = window) => ({
142141
// this function mustn't be annonymous due to a bug in the stack
143142
// generation logic, meaning it gets tripped up
144143
// see: https://github.com/stacktracejs/stack-generator/issues/6
145-
cb.__trace__ = function __trace__ () {
144+
cb.__trace__ = function __trace__ (...cbArgs) {
146145
// set the script that called this function
147146
updateLastScript(script)
148147
// immediately unset the currentScript synchronously below, however
149148
// if this cb throws an error the line after will not get run so schedule
150149
// an almost-immediate aysnc update too
151150
_setTimeout(function () { updateLastScript(null) }, 0)
152-
const ret = cb.apply(this, arguments)
151+
const ret = cb.apply(this, cbArgs)
153152
updateLastScript(null)
154153
return ret
155154
}

packages/plugin-inline-script-content/test/inline-script-content.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,42 @@ Lorem ipsum dolor sit amet.
262262
expect(payloads.length).toEqual(1)
263263
expect(payloads[0].events[0]._metadata.script).not.toBeDefined()
264264
})
265+
266+
it('supports rest parameters correctly in wrapped callbacks', () => {
267+
const scriptContent = 'console.log("test")'
268+
const document = {
269+
scripts: [{ innerHTML: scriptContent }],
270+
currentScript: { innerHTML: scriptContent },
271+
documentElement: {
272+
outerHTML: `<script>${scriptContent}</script>`
273+
}
274+
} as unknown as Document
275+
function Window () {}
276+
Window.prototype = {
277+
addEventListener: function () {},
278+
removeEventListener: function () {}
279+
}
280+
const window = {
281+
location: { href: 'https://app.bugsnag.com/errors' }
282+
} as unknown as Window &typeof globalThis
283+
284+
Object.setPrototypeOf(window, Window.prototype)
285+
// @ts-ignore
286+
window.Window = Window
287+
288+
const testCallback = function () {
289+
console.log('Event received')
290+
}
291+
292+
const client = new Client({ apiKey: 'API_KEY_YEAH' }, undefined, [plugin(document, window)])
293+
294+
// Add event listener with wrapped callback
295+
// @ts-ignore
296+
window.addEventListener('custom', testCallback)
297+
298+
// Verify the callback was wrapped and can handle arguments correctly
299+
// @ts-ignore
300+
expect(testCallback.__trace__).toBeDefined()
301+
expect(client).toBe(client)
302+
})
265303
})

0 commit comments

Comments
 (0)