Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions napi/parser/wrap.cjs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
}
43 changes: 23 additions & 20 deletions napi/parser/wrap.mjs
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
}
Loading