Skip to content

Commit

Permalink
feat(oxc): add napi transform options
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 3, 2024
1 parent 37cbabb commit 2425018
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 81 deletions.
12 changes: 2 additions & 10 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion crates/oxc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -59,6 +61,7 @@ full = [
"cfg",
]

parser = [] # for napi
semantic = ["oxc_semantic"]
transformer = ["oxc_transformer"]
minifier = ["oxc_mangler", "oxc_minifier"]
Expand All @@ -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
24 changes: 24 additions & 0 deletions crates/oxc/src/napi/isolated_declarations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use napi_derive::napi;

use super::source_map::SourceMap;

#[napi(object)]
pub struct IsolatedDeclarationsResult {
pub code: String,
pub map: Option<SourceMap>,
pub errors: Vec<String>,
}

#[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 <https://www.typescriptlang.org/tsconfig/#stripInternal>
pub strip_internal: Option<bool>,

pub sourcemap: Option<bool>,
}
12 changes: 10 additions & 2 deletions crates/oxc/src/napi/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
2 changes: 1 addition & 1 deletion napi/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion napi/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
11 changes: 1 addition & 10 deletions napi/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
21 changes: 11 additions & 10 deletions napi/transform/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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<IsolatedDeclarationsOptions>,

/// Path to the file being transformed.
filename: &'a str,

/// Source text of the file being transformed.
source_text: &'a str,
source_type: SourceType,
Expand Down
39 changes: 12 additions & 27 deletions napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
@@ -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<SourceMap>,
pub errors: Vec<String>,
}

#[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 <https://www.typescriptlang.org/tsconfig/#stripInternal>
pub strip_internal: Option<bool>,

pub sourcemap: Option<bool>,
}
use crate::context::TransformContext;

/// TypeScript Isolated Declarations for Standalone DTS Emit
#[allow(clippy::needless_pass_by_value)]
Expand Down Expand Up @@ -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),
},
)
Expand Down
10 changes: 1 addition & 9 deletions napi/transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down
21 changes: 12 additions & 9 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -115,7 +118,7 @@ fn transpile(ctx: &TransformContext<'_>, options: Option<TransformOptions>) -> 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();

Expand Down

0 comments on commit 2425018

Please sign in to comment.