parse-BywQARUG.mjs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { n as __toESM, t as require_binding } from "./binding-s-V_wTpj.mjs";
  2. //#region ../../node_modules/.pnpm/oxc-parser@0.124.0_@emnapi+core@1.9.2_@emnapi+runtime@1.9.2/node_modules/oxc-parser/src-js/wrap.js
  3. var import_binding = /* @__PURE__ */ __toESM(require_binding(), 1);
  4. function wrap(result) {
  5. let program, module, comments, errors;
  6. return {
  7. get program() {
  8. if (!program) program = jsonParseAst(result.program);
  9. return program;
  10. },
  11. get module() {
  12. if (!module) module = result.module;
  13. return module;
  14. },
  15. get comments() {
  16. if (!comments) comments = result.comments;
  17. return comments;
  18. },
  19. get errors() {
  20. if (!errors) errors = result.errors;
  21. return errors;
  22. }
  23. };
  24. }
  25. function jsonParseAst(programJson) {
  26. const { node: program, fixes } = JSON.parse(programJson);
  27. for (const fixPath of fixes) applyFix(program, fixPath);
  28. return program;
  29. }
  30. function applyFix(program, fixPath) {
  31. let node = program;
  32. for (const key of fixPath) node = node[key];
  33. if (node.bigint) node.value = BigInt(node.bigint);
  34. else try {
  35. node.value = RegExp(node.regex.pattern, node.regex.flags);
  36. } catch {}
  37. }
  38. //#endregion
  39. //#region src/utils/parse.ts
  40. /**
  41. * Parse JS/TS source asynchronously on a separate thread.
  42. *
  43. * Note that not all of the workload can happen on a separate thread.
  44. * Parsing on Rust side does happen in a separate thread, but deserialization of the AST to JS objects
  45. * has to happen on current thread. This synchronous deserialization work typically outweighs
  46. * the asynchronous parsing by a factor of between 3 and 20.
  47. *
  48. * i.e. the majority of the workload cannot be parallelized by using this method.
  49. *
  50. * Generally {@linkcode parseSync} is preferable to use as it does not have the overhead of spawning a thread.
  51. * If you need to parallelize parsing multiple files, it is recommended to use worker threads.
  52. *
  53. * @category Utilities
  54. */
  55. async function parse(filename, sourceText, options) {
  56. return wrap(await (0, import_binding.parse)(filename, sourceText, options));
  57. }
  58. /**
  59. * Parse JS/TS source synchronously on current thread.
  60. *
  61. * This is generally preferable over {@linkcode parse} (async) as it does not have the overhead
  62. * of spawning a thread, and the majority of the workload cannot be parallelized anyway
  63. * (see {@linkcode parse} documentation for details).
  64. *
  65. * If you need to parallelize parsing multiple files, it is recommended to use worker threads
  66. * with {@linkcode parseSync} rather than using {@linkcode parse}.
  67. *
  68. * @category Utilities
  69. */
  70. function parseSync(filename, sourceText, options) {
  71. return wrap((0, import_binding.parseSync)(filename, sourceText, options));
  72. }
  73. //#endregion
  74. export { parseSync as n, parse as t };