{"version":3,"file":"get-user-code-frame.cjs","sources":["../../src/get-user-code-frame.ts"],"sourcesContent":["// We try to load node dependencies\nimport fs from \"node:fs\";\nimport pc from \"picocolors\";\nimport { codeFrameColumns } from \"@babel/code-frame\";\n\nconst readFileSync = fs.readFileSync;\n\n// frame has the form \"at myMethod (location/to/my/file.js:10:2)\"\nfunction getCodeFrame(frame: string) {\n  const locationStart = frame.indexOf(\"(\") + 1;\n  const locationEnd = frame.indexOf(\")\");\n  const frameLocation = frame.slice(locationStart, locationEnd);\n\n  const frameLocationElements = frameLocation.split(\":\");\n  const [filename, line, column] = [\n    frameLocationElements[0]!,\n    parseInt(frameLocationElements[1]!, 10),\n    parseInt(frameLocationElements[2]!, 10),\n  ];\n\n  let rawFileContents = \"\";\n  try {\n    rawFileContents = readFileSync(filename, \"utf-8\");\n  } catch (e) {\n    return \"\";\n  }\n\n  const codeFrame = codeFrameColumns(\n    rawFileContents,\n    {\n      start: { line, column },\n    },\n    {\n      highlightCode: false,\n      linesBelow: 0,\n    },\n  );\n  return `${pc.dim(frameLocation)}\\n${codeFrame}\\n`;\n}\n\nfunction getUserCodeFrame() {\n  // If we couldn't load dependencies, we can't generate the user trace\n  /* istanbul ignore next */\n  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n  if (!readFileSync || !codeFrameColumns) {\n    return \"\";\n  }\n  const err = new Error();\n  const firstClientCodeFrame = err.stack\n    ?.split(\"\\n\")\n    .slice(1) // Remove first line which has the form \"Error: TypeError\"\n    .find((frame) => !frame.includes(\"node_modules/\")); // Ignore frames from 3rd party libraries\n\n  return getCodeFrame(firstClientCodeFrame!);\n}\n\nexport { getUserCodeFrame };\n"],"names":["codeFrame","codeFrameColumns"],"mappings":";;;;;AAKA,MAAM,eAAe,GAAG;AAGxB,SAAS,aAAa,OAAe;AACnC,QAAM,gBAAgB,MAAM,QAAQ,GAAG,IAAI;AACrC,QAAA,cAAc,MAAM,QAAQ,GAAG;AACrC,QAAM,gBAAgB,MAAM,MAAM,eAAe,WAAW;AAEtD,QAAA,wBAAwB,cAAc,MAAM,GAAG;AACrD,QAAM,CAAC,UAAU,MAAM,MAAM,IAAI;AAAA,IAC/B,sBAAsB,CAAC;AAAA,IACvB,SAAS,sBAAsB,CAAC,GAAI,EAAE;AAAA,IACtC,SAAS,sBAAsB,CAAC,GAAI,EAAE;AAAA,EACxC;AAEA,MAAI,kBAAkB;AAClB,MAAA;AACgB,sBAAA,aAAa,UAAU,OAAO;AAAA,WACzC,GAAG;AACH,WAAA;AAAA,EAAA;AAGT,QAAMA,cAAYC,UAAA;AAAA,IAChB;AAAA,IACA;AAAA,MACE,OAAO,EAAE,MAAM,OAAO;AAAA,IACxB;AAAA,IACA;AAAA,MACE,eAAe;AAAA,MACf,YAAY;AAAA,IAAA;AAAA,EAEhB;AACA,SAAO,GAAG,GAAG,IAAI,aAAa,CAAC;AAAA,EAAKD,WAAS;AAAA;AAC/C;AAEA,SAAS,mBAAmB;;AAItB,MAAA,CAAC,gBAAgB,CAACC,4BAAkB;AAC/B,WAAA;AAAA,EAAA;AAEH,QAAA,MAAM,IAAI,MAAM;AACtB,QAAM,wBAAuB,SAAI,UAAJ,mBACzB,MAAM,MACP,MAAM,GACN,KAAK,CAAC,UAAU,CAAC,MAAM,SAAS,eAAe;AAElD,SAAO,aAAa,oBAAqB;AAC3C;;"}