diff --git a/napi/parser/wrap.cjs b/napi/parser/wrap.cjs index 9fd9f87529c88..9afac73bcc33d 100644 --- a/napi/parser/wrap.cjs +++ b/napi/parser/wrap.cjs @@ -1,29 +1,11 @@ +// Note: This code is repeated in `wrap.mjs`. +// Any changes should be applied in that file too. + module.exports.wrap = function wrap(result) { let program, module, comments, errors; return { get program() { - if (!program) { - // Note: This code is repeated in `crates/oxc-wasm/update-bindings.mjs` and `napi/parser/wrap.cjs`. - // 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; - }); - } + if (!program) program = jsonParseAst(result.program); return program; }, get module() { @@ -40,3 +22,26 @@ module.exports.wrap = function wrap(result) { }, }; }; + +function jsonParseAst(program) { + return JSON.parse(program, transform); +} + +function transform(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; +} diff --git a/napi/parser/wrap.mjs b/napi/parser/wrap.mjs index 49c42c7b954ef..42230d0082dc8 100644 --- a/napi/parser/wrap.mjs +++ b/napi/parser/wrap.mjs @@ -1,10 +1,11 @@ +// Note: This code is repeated in `wrap.cjs`. +// Any changes should be applied in that file too. + export function wrap(result) { let program, module, comments, errors; return { get program() { - if (!program) { - program = jsonParseAst(result.program); - } + if (!program) program = jsonParseAst(result.program); return program; }, get module() { @@ -23,23 +24,25 @@ export function wrap(result) { } // Used by napi/playground/patch.mjs -export function jsonParseAst(ast) { - return JSON.parse(ast, 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 - } +export function jsonParseAst(program) { + return JSON.parse(program, transform); +} + +function transform(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 value; }