From 2425018f3bc1d4b3c5317b43d6e29a209d0036d3 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 3 Oct 2024 12:38:15 +0800 Subject: [PATCH] feat(oxc): add napi transform options --- Cargo.lock | 12 +----- crates/oxc/Cargo.toml | 5 ++- crates/oxc/src/napi/isolated_declarations.rs | 24 ++++++++++++ crates/oxc/src/napi/mod.rs | 12 +++++- .../oxc/src/napi/source_map.rs | 0 .../oxc/src/napi/transform.rs | 2 +- napi/parser/Cargo.toml | 2 +- napi/parser/src/lib.rs | 2 +- napi/transform/Cargo.toml | 11 +----- napi/transform/src/context.rs | 21 +++++----- napi/transform/src/isolated_declaration.rs | 39 ++++++------------- napi/transform/src/lib.rs | 10 +---- napi/transform/src/transformer.rs | 21 +++++----- 13 files changed, 80 insertions(+), 81 deletions(-) create mode 100644 crates/oxc/src/napi/isolated_declarations.rs rename napi/transform/src/sourcemap.rs => crates/oxc/src/napi/source_map.rs (100%) rename napi/transform/src/options.rs => crates/oxc/src/napi/transform.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 899b6ef1a0b069..0ee9bf60c7768c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1418,6 +1418,7 @@ dependencies = [ "oxc_span", "oxc_syntax", "oxc_transformer", + "rustc-hash", ] [[package]] @@ -1974,16 +1975,7 @@ dependencies = [ "napi", "napi-build", "napi-derive", - "oxc_allocator", - "oxc_ast", - "oxc_codegen", - "oxc_diagnostics", - "oxc_isolated_declarations", - "oxc_parser", - "oxc_semantic", - "oxc_sourcemap", - "oxc_span", - "oxc_transformer", + "oxc", "rustc-hash", ] diff --git a/crates/oxc/Cargo.toml b/crates/oxc/Cargo.toml index c3047ff38dc27e..f46db9ef6dc589 100644 --- a/crates/oxc/Cargo.toml +++ b/crates/oxc/Cargo.toml @@ -47,6 +47,8 @@ oxc_transformer = { workspace = true, optional = true } napi = { workspace = true, optional = true, features = ["async"] } napi-derive = { workspace = true, optional = true } +rustc-hash = { workspace = true, optional = true } + [features] full = [ "codegen", @@ -59,6 +61,7 @@ full = [ "cfg", ] +parser = [] # for napi semantic = ["oxc_semantic"] transformer = ["oxc_transformer"] minifier = ["oxc_mangler", "oxc_minifier"] @@ -78,7 +81,7 @@ sourcemap = ["oxc_sourcemap"] sourcemap_concurrent = ["oxc_sourcemap/concurrent", "sourcemap"] wasm = ["oxc_transformer/wasm"] -napi = ["dep:napi", "dep:napi-derive"] +napi = ["dep:napi", "dep:napi-derive", "dep:rustc-hash"] [package.metadata.docs.rs] all-features = true diff --git a/crates/oxc/src/napi/isolated_declarations.rs b/crates/oxc/src/napi/isolated_declarations.rs new file mode 100644 index 00000000000000..db2583e872354d --- /dev/null +++ b/crates/oxc/src/napi/isolated_declarations.rs @@ -0,0 +1,24 @@ +use napi_derive::napi; + +use super::source_map::SourceMap; + +#[napi(object)] +pub struct IsolatedDeclarationsResult { + pub code: String, + pub map: Option, + pub errors: Vec, +} + +#[napi(object)] +#[derive(Debug, Default, Clone, Copy)] +pub struct IsolatedDeclarationsOptions { + /// Do not emit declarations for code that has an @internal annotation in its JSDoc comment. + /// This is an internal compiler option; use at your own risk, because the compiler does not check that the result is valid. + /// + /// Default: `false` + /// + /// See + pub strip_internal: Option, + + pub sourcemap: Option, +} diff --git a/crates/oxc/src/napi/mod.rs b/crates/oxc/src/napi/mod.rs index 4f42021da88b5c..d61a33ec2eb005 100644 --- a/crates/oxc/src/napi/mod.rs +++ b/crates/oxc/src/napi/mod.rs @@ -1,3 +1,11 @@ -mod parse; +#[cfg(feature = "parser")] +pub mod parse; -pub use parse::*; +#[cfg(feature = "sourcemap")] +pub mod source_map; + +#[cfg(feature = "isolated_declarations")] +pub mod isolated_declarations; + +#[cfg(feature = "transformer")] +pub mod transform; diff --git a/napi/transform/src/sourcemap.rs b/crates/oxc/src/napi/source_map.rs similarity index 100% rename from napi/transform/src/sourcemap.rs rename to crates/oxc/src/napi/source_map.rs diff --git a/napi/transform/src/options.rs b/crates/oxc/src/napi/transform.rs similarity index 99% rename from napi/transform/src/options.rs rename to crates/oxc/src/napi/transform.rs index 8be934e4c6cc52..59232297d9fead 100644 --- a/napi/transform/src/options.rs +++ b/crates/oxc/src/napi/transform.rs @@ -8,7 +8,7 @@ use rustc_hash::FxHashMap; use oxc_transformer::{JsxRuntime, RewriteExtensionsMode}; -use crate::IsolatedDeclarationsOptions; +use super::isolated_declarations::IsolatedDeclarationsOptions; /// Options for transforming a JavaScript or TypeScript file. /// diff --git a/napi/parser/Cargo.toml b/napi/parser/Cargo.toml index 8edd893eef398e..c524b64ea580fc 100644 --- a/napi/parser/Cargo.toml +++ b/napi/parser/Cargo.toml @@ -21,7 +21,7 @@ test = false doctest = false [dependencies] -oxc = { workspace = true, features = ["napi", "serialize"] } +oxc = { workspace = true, features = ["napi", "serialize", "parser"] } oxc_module_lexer = { workspace = true } napi = { workspace = true, features = ["async"] } diff --git a/napi/parser/src/lib.rs b/napi/parser/src/lib.rs index 5228fe2d0a2413..6744e534c4d8d0 100644 --- a/napi/parser/src/lib.rs +++ b/napi/parser/src/lib.rs @@ -9,7 +9,7 @@ use oxc::{ allocator::Allocator, ast::CommentKind, diagnostics::{Error, NamedSource}, - napi::{Comment, ParseResult, ParserOptions}, + napi::parse::{Comment, ParseResult, ParserOptions}, parser::{ParseOptions, Parser, ParserReturn}, span::SourceType, }; diff --git a/napi/transform/Cargo.toml b/napi/transform/Cargo.toml index dd641e87f2643e..4a531cc46502ee 100644 --- a/napi/transform/Cargo.toml +++ b/napi/transform/Cargo.toml @@ -21,16 +21,7 @@ test = false doctest = false [dependencies] -oxc_allocator = { workspace = true } -oxc_ast = { workspace = true } -oxc_codegen = { workspace = true } -oxc_diagnostics = { workspace = true } -oxc_isolated_declarations = { workspace = true } -oxc_parser = { workspace = true } -oxc_semantic = { workspace = true } -oxc_sourcemap = { workspace = true } -oxc_span = { workspace = true } -oxc_transformer = { workspace = true } +oxc = { workspace = true, features = ["napi", "isolated_declarations", "transformer", "sourcemap", "codegen", "semantic"] } rustc-hash = { workspace = true } diff --git a/napi/transform/src/context.rs b/napi/transform/src/context.rs index a9210119f3e6fc..9ae6bc9f30b226 100644 --- a/napi/transform/src/context.rs +++ b/napi/transform/src/context.rs @@ -4,14 +4,15 @@ use std::{ sync::Arc, }; -use oxc_allocator::Allocator; -use oxc_ast::{ast::Program, Trivias}; -use oxc_codegen::Codegen; -use oxc_diagnostics::{Error, NamedSource, OxcDiagnostic}; -use oxc_parser::{Parser, ParserReturn}; -use oxc_span::SourceType; - -use crate::{IsolatedDeclarationsOptions, TransformOptions}; +use oxc::{ + allocator::Allocator, + ast::{ast::Program, Trivias}, + codegen::Codegen, + diagnostics::{Error, NamedSource, OxcDiagnostic}, + napi::{isolated_declarations::IsolatedDeclarationsOptions, transform::TransformOptions}, + parser::{Parser, ParserReturn}, + span::SourceType, +}; #[must_use] pub(crate) struct TransformContext<'a> { @@ -21,13 +22,13 @@ pub(crate) struct TransformContext<'a> { /// Generate source maps? source_map: bool, + /// Generate `.d.ts` files? - /// - /// Used by [`crate::transform`]. declarations: Option, /// Path to the file being transformed. filename: &'a str, + /// Source text of the file being transformed. source_text: &'a str, source_type: SourceType, diff --git a/napi/transform/src/isolated_declaration.rs b/napi/transform/src/isolated_declaration.rs index 59d4f26047f3ce..c125a08c841703 100644 --- a/napi/transform/src/isolated_declaration.rs +++ b/napi/transform/src/isolated_declaration.rs @@ -1,31 +1,16 @@ use napi_derive::napi; -use oxc_allocator::Allocator; -use oxc_codegen::{CodegenReturn, CommentOptions}; -use oxc_isolated_declarations::IsolatedDeclarations; -use oxc_span::SourceType; +use oxc::{ + allocator::Allocator, + codegen::{CodegenReturn, CommentOptions}, + isolated_declarations::IsolatedDeclarations, + napi::{ + isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult}, + transform::TransformOptions, + }, + span::SourceType, +}; -use crate::{context::TransformContext, SourceMap, TransformOptions}; - -#[napi(object)] -pub struct IsolatedDeclarationsResult { - pub code: String, - pub map: Option, - pub errors: Vec, -} - -#[napi(object)] -#[derive(Debug, Default, Clone, Copy)] -pub struct IsolatedDeclarationsOptions { - /// Do not emit declarations for code that has an @internal annotation in its JSDoc comment. - /// This is an internal compiler option; use at your own risk, because the compiler does not check that the result is valid. - /// - /// Default: `false` - /// - /// See - pub strip_internal: Option, - - pub sourcemap: Option, -} +use crate::context::TransformContext; /// TypeScript Isolated Declarations for Standalone DTS Emit #[allow(clippy::needless_pass_by_value)] @@ -62,7 +47,7 @@ pub(crate) fn build_declarations( ctx.allocator, ctx.source_text(), &ctx.trivias, - oxc_isolated_declarations::IsolatedDeclarationsOptions { + oxc::isolated_declarations::IsolatedDeclarationsOptions { strip_internal: options.strip_internal.unwrap_or(false), }, ) diff --git a/napi/transform/src/lib.rs b/napi/transform/src/lib.rs index d75efc8ea5b8b6..bcab5391a0575d 100644 --- a/napi/transform/src/lib.rs +++ b/napi/transform/src/lib.rs @@ -1,14 +1,6 @@ -// NOTE: the strange order of struct and `mod` statements is to establish the -// desired order in generated `index.d.ts` code. We want options to be on top. -// This is not only for aesthetics, but using declarations before they're parsed -// breaks NAPI typegen. mod context; -mod options; -pub use crate::options::*; - -mod sourcemap; -pub use crate::sourcemap::*; +pub use oxc::napi::{isolated_declarations, transform}; mod isolated_declaration; pub use isolated_declaration::*; diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index b21f1ffeef19e2..01e891b798e28c 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -2,16 +2,19 @@ use napi::Either; use napi_derive::napi; use rustc_hash::FxHashMap; -use oxc_allocator::Allocator; -use oxc_codegen::CodegenReturn; -use oxc_semantic::{ScopeTree, SemanticBuilder, SymbolTable}; -use oxc_span::SourceType; -use oxc_transformer::{ - InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport, ReplaceGlobalDefines, - ReplaceGlobalDefinesConfig, Transformer, +use oxc::{ + allocator::Allocator, + codegen::CodegenReturn, + napi::{source_map::SourceMap, transform::TransformOptions}, + semantic::{ScopeTree, SemanticBuilder, SymbolTable}, + span::SourceType, + transformer::{ + InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport, ReplaceGlobalDefines, + ReplaceGlobalDefinesConfig, Transformer, + }, }; -use crate::{context::TransformContext, isolated_declaration, SourceMap, TransformOptions}; +use crate::{context::TransformContext, isolated_declaration}; // NOTE: Use JSDoc syntax for all doc comments, not rustdoc. // NOTE: Types must be aligned with [@types/babel__core](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/babel__core/index.d.ts). @@ -115,7 +118,7 @@ fn transpile(ctx: &TransformContext<'_>, options: Option) -> C let define = options.as_mut().and_then(|options| options.define.take()); let inject = options.as_mut().and_then(|options| options.inject.take()); - let options = options.map(oxc_transformer::TransformOptions::from).unwrap_or_default(); + let options = options.map(oxc::transformer::TransformOptions::from).unwrap_or_default(); let (mut symbols, mut scopes) = semantic_ret.semantic.into_symbol_table_and_scope_tree();