From 5bf345cd1040969f799c22652fa4f2834e50e91d Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sun, 9 Mar 2025 13:33:53 +0000 Subject: [PATCH 1/2] feat(napi/parser): `JSON.parse` the returned AST in wasm (#9630) --- napi/parser/index.js | 45 +--------------------------------------- napi/parser/package.json | 6 ++++-- napi/parser/wasm.mjs | 11 ++++++++++ napi/parser/wrap.cjs | 42 +++++++++++++++++++++++++++++++++++++ napi/parser/wrap.mjs | 42 +++++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 46 deletions(-) create mode 100644 napi/parser/wasm.mjs create mode 100644 napi/parser/wrap.cjs create mode 100644 napi/parser/wrap.mjs diff --git a/napi/parser/index.js b/napi/parser/index.js index 81e9e006aea8f..43c4b06c3cf4e 100644 --- a/napi/parser/index.js +++ b/napi/parser/index.js @@ -1,6 +1,7 @@ const bindings = require('./bindings.js'); const deserializeJS = require('./deserialize-js.js'); const deserializeTS = require('./deserialize-ts.js'); +const { wrap } = require('./wrap.cjs'); module.exports.ParseResult = bindings.ParseResult; module.exports.ExportExportNameKind = bindings.ExportExportNameKind; @@ -10,49 +11,6 @@ module.exports.ImportNameKind = bindings.ImportNameKind; module.exports.parseWithoutReturn = bindings.parseWithoutReturn; module.exports.Severity = bindings.Severity; -function wrap(result) { - let program, module, comments, errors; - return { - get program() { - if (!program) { - // Note: This code is repeated in `wasm/parser/update-bindings.mjs` and `crates/oxc-wasm/update-bindings.mjs`. - // Any changes should be applied in those 2 scripts too. - program = JSON.parse(result.program, function(key, value) { - // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s. - // This is not possible to do on Rust side, as neither can be represented correctly in JSON. - if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') { - if (Object.hasOwn(this, 'bigint')) { - return BigInt(this.bigint); - } - if (Object.hasOwn(this, 'regex')) { - const { regex } = this; - try { - return RegExp(regex.pattern, regex.flags); - } catch (_err) { - // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS - } - } - } - return value; - }); - } - return program; - }, - get module() { - if (!module) module = result.module; - return module; - }, - get comments() { - if (!comments) comments = result.comments; - return comments; - }, - get errors() { - if (!errors) errors = result.errors; - return errors; - }, - }; -} - module.exports.parseAsync = async function parseAsync(...args) { return wrap(await bindings.parseAsync(...args)); }; @@ -61,7 +19,6 @@ module.exports.parseSync = function parseSync(filename, sourceText, options) { if (options?.experimentalRawTransfer) { return parseSyncRaw(filename, sourceText, options); } - return wrap(bindings.parseSync(filename, sourceText, options)); }; diff --git a/napi/parser/package.json b/napi/parser/package.json index d2d46d8a2b716..12a9fbf7039d1 100644 --- a/napi/parser/package.json +++ b/napi/parser/package.json @@ -2,7 +2,7 @@ "name": "oxc-parser", "version": "0.58.1", "main": "index.js", - "browser": "browser.js", + "browser": "wasm.mjs", "scripts": { "build-dev": "napi build --no-dts-cache --platform --js bindings.js", "build": "pnpm run build-dev --release", @@ -32,7 +32,9 @@ "files": [ "index.d.ts", "index.js", - "browser.js", + "wrap.cjs", + "wrap.mjs", + "wasm.mjs", "bindings.js", "deserialize-js.js", "deserialize-ts.js" diff --git a/napi/parser/wasm.mjs b/napi/parser/wasm.mjs new file mode 100644 index 0000000000000..57dda29f6d55c --- /dev/null +++ b/napi/parser/wasm.mjs @@ -0,0 +1,11 @@ +export * from '@oxc-parser/binding-wasm32-wasi'; +import * as bindings from '@oxc-parser/binding-wasm32-wasi'; +import { wrap } from './wrap.mjs'; + +export async function parseAsync(...args) { + return wrap(await bindings.parseAsync(...args)); +} + +export function parseSync(filename, sourceText, options) { + return wrap(bindings.parseSync(filename, sourceText, options)); +} diff --git a/napi/parser/wrap.cjs b/napi/parser/wrap.cjs new file mode 100644 index 0000000000000..1dfbb22724884 --- /dev/null +++ b/napi/parser/wrap.cjs @@ -0,0 +1,42 @@ +module.exports.wrap = function wrap(result) { + let program, module, comments, errors; + return { + get program() { + if (!program) { + // Note: This code is repeated in `wasm/parser/update-bindings.mjs` and `crates/oxc-wasm/update-bindings.mjs`. + // Any changes should be applied in those 2 scripts too. + program = JSON.parse(result.program, function(key, value) { + // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s. + // This is not possible to do on Rust side, as neither can be represented correctly in JSON. + if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') { + if (Object.hasOwn(this, 'bigint')) { + return BigInt(this.bigint); + } + if (Object.hasOwn(this, 'regex')) { + const { regex } = this; + try { + return RegExp(regex.pattern, regex.flags); + } catch (_err) { + // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS + } + } + } + return value; + }); + } + return program; + }, + get module() { + if (!module) module = result.module; + return module; + }, + get comments() { + if (!comments) comments = result.comments; + return comments; + }, + get errors() { + if (!errors) errors = result.errors; + return errors; + }, + }; +}; diff --git a/napi/parser/wrap.mjs b/napi/parser/wrap.mjs new file mode 100644 index 0000000000000..b53a7e4b8d0a8 --- /dev/null +++ b/napi/parser/wrap.mjs @@ -0,0 +1,42 @@ +export function wrap(result) { + let program, module, comments, errors; + return { + get program() { + if (!program) { + // Note: This code is repeated in `wasm/parser/update-bindings.mjs` and `crates/oxc-wasm/update-bindings.mjs`. + // Any changes should be applied in those 2 scripts too. + program = JSON.parse(result.program, function(key, value) { + // Set `value` field of `Literal`s for `BigInt`s and `RegExp`s. + // This is not possible to do on Rust side, as neither can be represented correctly in JSON. + if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') { + if (Object.hasOwn(this, 'bigint')) { + return BigInt(this.bigint); + } + if (Object.hasOwn(this, 'regex')) { + const { regex } = this; + try { + return RegExp(regex.pattern, regex.flags); + } catch (_err) { + // Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS + } + } + } + return value; + }); + } + return program; + }, + get module() { + if (!module) module = result.module; + return module; + }, + get comments() { + if (!comments) comments = result.comments; + return comments; + }, + get errors() { + if (!errors) errors = result.errors; + return errors; + }, + }; +}; From 49f173ad851e92871adce630f885dbd49fb38e02 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 18 Mar 2025 02:05:27 +0000 Subject: [PATCH 2/2] [autofix.ci] apply automated fixes --- napi/parser/wrap.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napi/parser/wrap.mjs b/napi/parser/wrap.mjs index b53a7e4b8d0a8..667fa5e74671a 100644 --- a/napi/parser/wrap.mjs +++ b/napi/parser/wrap.mjs @@ -39,4 +39,4 @@ export function wrap(result) { return errors; }, }; -}; +}