refactor(oxc): add napi feature, change napi parser to use oxc crate (#6265)

This commit is contained in:
Boshen 2024-10-03 04:28:38 +00:00
parent 7e4fa337d9
commit aa0dbb6375
8 changed files with 63 additions and 55 deletions

8
Cargo.lock generated
View file

@ -1400,6 +1400,8 @@ checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f"
name = "oxc"
version = "0.30.5"
dependencies = [
"napi",
"napi-derive",
"oxc_allocator",
"oxc_ast",
"oxc_cfg",
@ -1790,12 +1792,8 @@ dependencies = [
"napi",
"napi-build",
"napi-derive",
"oxc_allocator",
"oxc_ast",
"oxc_diagnostics",
"oxc",
"oxc_module_lexer",
"oxc_parser",
"oxc_span",
"serde_json",
]

View file

@ -44,6 +44,9 @@ oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_transformer = { workspace = true, optional = true }
napi = { workspace = true, optional = true, features = ["async"] }
napi-derive = { workspace = true, optional = true }
[features]
full = [
"codegen",
@ -62,6 +65,7 @@ minifier = ["oxc_mangler", "oxc_minifier"]
codegen = ["oxc_codegen"]
mangler = ["oxc_mangler"]
cfg = ["oxc_cfg"]
isolated_declarations = ["oxc_isolated_declarations"]
serialize = [
"oxc_ast/serialize",
@ -73,9 +77,8 @@ serialize = [
sourcemap = ["oxc_sourcemap"]
sourcemap_concurrent = ["oxc_sourcemap/concurrent", "sourcemap"]
isolated_declarations = ["oxc_isolated_declarations"]
wasm = ["oxc_transformer/wasm"]
napi = ["dep:napi", "dep:napi-derive"]
[package.metadata.docs.rs]
all-features = true

View file

@ -3,6 +3,9 @@
#[cfg(feature = "full")]
mod compiler;
#[cfg(feature = "napi")]
pub mod napi;
#[cfg(feature = "full")]
pub use compiler::{Compiler, CompilerInterface};

View file

@ -0,0 +1,3 @@
mod parse;
pub use parse::*;

View file

@ -0,0 +1,36 @@
use napi_derive::napi;
/// Babel Parser Options
///
/// <https://github.com/babel/babel/blob/main/packages/babel-parser/typings/babel-parser.d.ts>
#[napi(object)]
#[derive(Default)]
pub struct ParserOptions {
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
pub source_type: Option<String>,
pub source_filename: Option<String>,
/// Emit `ParenthesizedExpression` in AST.
///
/// If this option is true, parenthesized expressions are represented by
/// (non-standard) `ParenthesizedExpression` nodes that have a single `expression` property
/// containing the expression inside parentheses.
///
/// Default: true
pub preserve_parens: Option<bool>,
}
#[napi(object)]
pub struct ParseResult {
pub program: String,
pub comments: Vec<Comment>,
pub errors: Vec<String>,
}
#[napi(object)]
pub struct Comment {
#[napi(ts_type = "'Line' | 'Block'")]
pub r#type: &'static str,
pub value: String,
pub start: u32,
pub end: u32,
}

View file

@ -21,12 +21,8 @@ test = false
doctest = false
[dependencies]
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true, features = ["serialize"] }
oxc_diagnostics = { workspace = true }
oxc_module_lexer = { path = "../../crates/oxc_module_lexer" }
oxc_parser = { workspace = true }
oxc_span = { workspace = true }
oxc = { workspace = true, features = ["napi", "serialize"] }
oxc_module_lexer = { workspace = true }
napi = { workspace = true, features = ["async"] }
napi-derive = { workspace = true }

View file

@ -4,50 +4,18 @@ use std::sync::Arc;
use napi::{bindgen_prelude::AsyncTask, Task};
use napi_derive::napi;
use oxc_allocator::Allocator;
pub use oxc_ast::ast::Program;
use oxc_ast::CommentKind;
use oxc_diagnostics::{Error, NamedSource};
use oxc_parser::{ParseOptions, Parser, ParserReturn};
use oxc_span::SourceType;
use oxc::{
allocator::Allocator,
ast::CommentKind,
diagnostics::{Error, NamedSource},
napi::{Comment, ParseResult, ParserOptions},
parser::{ParseOptions, Parser, ParserReturn},
span::SourceType,
};
pub use crate::module_lexer::*;
/// Babel Parser Options
///
/// <https://github.com/babel/babel/blob/main/packages/babel-parser/typings/babel-parser.d.ts>
#[napi(object)]
#[derive(Default)]
pub struct ParserOptions {
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
pub source_type: Option<String>,
pub source_filename: Option<String>,
/// Emit `ParenthesizedExpression` in AST.
///
/// If this option is true, parenthesized expressions are represented by
/// (non-standard) `ParenthesizedExpression` nodes that have a single `expression` property
/// containing the expression inside parentheses.
///
/// Default: true
pub preserve_parens: Option<bool>,
}
#[napi(object)]
pub struct ParseResult {
pub program: String,
pub comments: Vec<Comment>,
pub errors: Vec<String>,
}
#[napi(object)]
pub struct Comment {
#[napi(ts_type = "'Line' | 'Block'")]
pub r#type: &'static str,
pub value: String,
pub start: u32,
pub end: u32,
}
fn parse<'a>(
allocator: &'a Allocator,
source_text: &'a str,

View file

@ -1,6 +1,7 @@
use napi::{bindgen_prelude::AsyncTask, Task};
use napi_derive::napi;
use oxc_allocator::Allocator;
use oxc::allocator::Allocator;
use oxc_module_lexer::ImportType;
use crate::{parse, ParserOptions};