Skip to content

Commit

Permalink
refactor(oxc): add napi feature, change napi parser to use oxc crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 3, 2024
1 parent 7bb745b commit 43bdaae
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 54 deletions.
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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
3 changes: 3 additions & 0 deletions crates/oxc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#[cfg(feature = "full")]
mod compiler;

#[cfg(feature = "napi")]
pub mod napi;

#[cfg(feature = "full")]
pub use compiler::{Compiler, CompilerInterface};

Expand Down
3 changes: 3 additions & 0 deletions crates/oxc/src/napi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod parse;

pub use parse::*;
36 changes: 36 additions & 0 deletions crates/oxc/src/napi/parse.rs
Original file line number Diff line number Diff line change
@@ -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,
}
6 changes: 1 addition & 5 deletions napi/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ test = false
doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true, features = ["serialize"] }
oxc_diagnostics = { workspace = true }
oxc = { workspace = true, features = ["napi"] }
oxc_module_lexer = { path = "../../crates/oxc_module_lexer" }
oxc_parser = { workspace = true }
oxc_span = { workspace = true }

napi = { workspace = true, features = ["async"] }
napi-derive = { workspace = true }
Expand Down
50 changes: 9 additions & 41 deletions napi/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,17 @@ 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;

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>,
}
use oxc::{
allocator::Allocator,
ast::CommentKind,
diagnostics::{Error, NamedSource},
napi::{Comment, ParseResult, ParserOptions},
parser::{ParseOptions, Parser, ParserReturn},
span::SourceType,
};

#[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,
}
pub use crate::module_lexer::*;

fn parse<'a>(
allocator: &'a Allocator,
Expand Down
3 changes: 2 additions & 1 deletion napi/parser/src/module_lexer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down

0 comments on commit 43bdaae

Please sign in to comment.