skypackjs / skypack-cdn

An issue tracker for the CDN
107 stars 5 forks source link

Broken optional chaining transpilation #348

Open fabiospampinato opened 1 year ago

fabiospampinato commented 1 year ago

I'm importing the following:

https://cdn.skypack.dev/voby

Which highlights that the transform that SkyPack is using for transpiling optional chaining is broken. I have the following source code:

const IS_BROWSER = !!globalThis.CDATASection?.toString?.().match ( /^\s*function\s+CDATASection\s*\(\s*\)\s*\{\s*\[native code\]\s*\}\s*$/ );

But it gets transpiled to this:

var a, b;
const IS_BROWSER = !!((_b = (_a = globalThis.CDATASection) == null ? void 0 : _a.toString) == null ? void 0 : _b().match(/^\s*function\s+CDATASection\s*\(\s*\)\s*\{\s*\[native code\]\s*\}\s*$/));

Which seems correct at first look, but it isn't, because when _b() is called the this of the function call is undefined, while in the original code that this would have been globalThis.CDATASection itself, i.e. the thing to stringify.