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
45 changes: 1 addition & 44 deletions napi/parser/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
};
Expand All @@ -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));
};

Expand Down
6 changes: 4 additions & 2 deletions napi/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions napi/parser/wasm.mjs
Original file line number Diff line number Diff line change
@@ -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));
}
42 changes: 42 additions & 0 deletions napi/parser/wrap.cjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
};
42 changes: 42 additions & 0 deletions napi/parser/wrap.mjs
Original file line number Diff line number Diff line change
@@ -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;
},
};
}
Loading