All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.build.cjs.buildPolyfills._optionalChain.js.map Maven / Gradle / Ivy

There is a newer version: 8.38.0
Show newest version
{"version":3,"file":"_optionalChain.js","sources":["../../../src/buildPolyfills/_optionalChain.ts"],"sourcesContent":["// https://github.com/alangpierce/sucrase/tree/265887868966917f3b924ce38dfad01fbab1329f\n//\n// The MIT License (MIT)\n//\n// Copyright (c) 2012-2018 various contributors (see AUTHORS)\n//\n// Permission is hereby granted, free of charge, to any person obtaining a copy\n// of this software and associated documentation files (the \"Software\"), to deal\n// in the Software without restriction, including without limitation the rights\n// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n// copies of the Software, and to permit persons to whom the Software is\n// furnished to do so, subject to the following conditions:\n//\n// The above copyright notice and this permission notice shall be included in all\n// copies or substantial portions of the Software.\n//\n// THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n// SOFTWARE.\n\nimport type { GenericFunction } from './types';\n\n/**\n * Polyfill for the optional chain operator, `?.`, given previous conversion of the expression into an array of values,\n * descriptors, and functions.\n *\n * Adapted from Sucrase (https://github.com/alangpierce/sucrase)\n * See https://github.com/alangpierce/sucrase/blob/265887868966917f3b924ce38dfad01fbab1329f/src/transformers/OptionalChainingNullishTransformer.ts#L15\n *\n * @param ops Array result of expression conversion\n * @returns The value of the expression\n */\nexport function _optionalChain(ops: unknown[]): unknown {\n  let lastAccessLHS: unknown = undefined;\n  let value = ops[0];\n  let i = 1;\n  while (i < ops.length) {\n    const op = ops[i] as string;\n    const fn = ops[i + 1] as (intermediateValue: unknown) => unknown;\n    i += 2;\n    // by checking for loose equality to `null`, we catch both `null` and `undefined`\n    if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {\n      // really we're meaning to return `undefined` as an actual value here, but it saves bytes not to write it\n      return;\n    }\n    if (op === 'access' || op === 'optionalAccess') {\n      lastAccessLHS = value;\n      value = fn(value);\n    } else if (op === 'call' || op === 'optionalCall') {\n      value = fn((...args: unknown[]) => (value as GenericFunction).call(lastAccessLHS, ...args));\n      lastAccessLHS = undefined;\n    }\n  }\n  return value;\n}\n\n// Sucrase version\n// function _optionalChain(ops) {\n//   let lastAccessLHS = undefined;\n//   let value = ops[0];\n//   let i = 1;\n//   while (i < ops.length) {\n//     const op = ops[i];\n//     const fn = ops[i + 1];\n//     i += 2;\n//     if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) {\n//       return undefined;\n//     }\n//     if (op === 'access' || op === 'optionalAccess') {\n//       lastAccessLHS = value;\n//       value = fn(value);\n//     } else if (op === 'call' || op === 'optionalCall') {\n//       value = fn((...args) => value.call(lastAccessLHS, ...args));\n//       lastAccessLHS = undefined;\n//     }\n//   }\n//   return value;\n// }\n"],"names":[],"mappings":";;AA0BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,cAAc,CAAC,GAAG,EAAsB;AACxD,EAAE,IAAI,aAAa,GAAY,SAAS,CAAA;AACxC,EAAE,IAAI,KAAM,GAAE,GAAG,CAAC,CAAC,CAAC,CAAA;AACpB,EAAE,IAAI,CAAE,GAAE,CAAC,CAAA;AACX,EAAE,OAAO,CAAA,GAAI,GAAG,CAAC,MAAM,EAAE;AACzB,IAAI,MAAM,EAAG,GAAE,GAAG,CAAC,CAAC,CAAE,EAAA;AACtB,IAAI,MAAM,KAAK,GAAG,CAAC,CAAE,GAAE,CAAC,CAAE,EAAA;AAC1B,IAAI,CAAA,IAAK,CAAC,CAAA;AACV;AACA,IAAI,IAAI,CAAC,EAAA,KAAO,gBAAiB,IAAG,EAAG,KAAI,cAAc,KAAK,KAAM,IAAG,IAAI,EAAE;AAC7E;AACA,MAAM,OAAM;AACZ,KAAI;AACJ,IAAI,IAAI,EAAG,KAAI,YAAY,EAAA,KAAO,gBAAgB,EAAE;AACpD,MAAM,aAAA,GAAgB,KAAK,CAAA;AAC3B,MAAM,KAAM,GAAE,EAAE,CAAC,KAAK,CAAC,CAAA;AACvB,KAAI,MAAO,IAAI,EAAA,KAAO,MAAA,IAAU,EAAA,KAAO,cAAc,EAAE;AACvD,MAAM,KAAA,GAAQ,EAAE,CAAC,CAAC,GAAG,IAAI,KAAgB,CAAC,QAA0B,IAAI,CAAC,aAAa,EAAE,GAAG,IAAI,CAAC,CAAC,CAAA;AACjG,MAAM,aAAA,GAAgB,SAAS,CAAA;AAC/B,KAAI;AACJ,GAAE;AACF,EAAE,OAAO,KAAK,CAAA;AACd,CAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;;"}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy