package.build.esm.buildPolyfills._optionalChain.js Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
Utilities for all Sentry JavaScript SDKs
/**
* Polyfill for the optional chain operator, `?.`, given previous conversion of the expression into an array of values,
* descriptors, and functions.
*
* Adapted from Sucrase (https://github.com/alangpierce/sucrase)
* See https://github.com/alangpierce/sucrase/blob/265887868966917f3b924ce38dfad01fbab1329f/src/transformers/OptionalChainingNullishTransformer.ts#L15
*
* @param ops Array result of expression conversion
* @returns The value of the expression
*/
function _optionalChain(ops) {
let lastAccessLHS = undefined;
let value = ops[0];
let i = 1;
while (i < ops.length) {
const op = ops[i] ;
const fn = ops[i + 1] ;
i += 2;
// by checking for loose equality to `null`, we catch both `null` and `undefined`
if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {
// really we're meaning to return `undefined` as an actual value here, but it saves bytes not to write it
return;
}
if (op === 'access' || op === 'optionalAccess') {
lastAccessLHS = value;
value = fn(value);
} else if (op === 'call' || op === 'optionalCall') {
value = fn((...args) => (value ).call(lastAccessLHS, ...args));
lastAccessLHS = undefined;
}
}
return value;
}
// Sucrase version
// function _optionalChain(ops) {
// let lastAccessLHS = undefined;
// let value = ops[0];
// let i = 1;
// while (i < ops.length) {
// const op = ops[i];
// const fn = ops[i + 1];
// i += 2;
// if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {
// return undefined;
// }
// if (op === 'access' || op === 'optionalAccess') {
// lastAccessLHS = value;
// value = fn(value);
// } else if (op === 'call' || op === 'optionalCall') {
// value = fn((...args) => value.call(lastAccessLHS, ...args));
// lastAccessLHS = undefined;
// }
// }
// return value;
// }
export { _optionalChain };
//# sourceMappingURL=_optionalChain.js.map