Skip to content
This repository has been archived by the owner on Dec 14, 2024. It is now read-only.

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 27, 2024
1 parent defcda9 commit 6b7f06e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ oxc_sourcemap = { version = "0.34.0" }
oxc_span = { version = "0.34.0" }
oxc_syntax = { version = "0.34.0" }
oxc_transformer = { version = "0.34.0" }
oxc_semantic = { version = "0.34.0" }

itertools = { version = "0.13.0" }
tokio = { version = "1.38.0", features = ["rt-multi-thread"] }
Expand Down
1 change: 1 addition & 0 deletions napi/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ oxc_sourcemap = { workspace = true }
oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_transformer = { workspace = true }
oxc_semantic = { workspace = true }

itertools = { workspace = true }
tokio = { workspace = true }
Expand Down
56 changes: 22 additions & 34 deletions napi/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::path::Path;

use napi::{Error, Status};
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenReturn};
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_sourcemap::SourceMap as OxcSourceMap;
use oxc_span::SourceType;
use oxc_transformer::Transformer;
Expand Down Expand Up @@ -55,9 +56,7 @@ pub struct SourceMap {
impl TryFrom<OxcSourceMap> for SourceMap {
type Error = napi::Error;
fn try_from(sourcemap: OxcSourceMap) -> Result<Self, Self::Error> {
let mappings = sourcemap
.to_json_string()
.map_err(|e| Error::from_reason(format!("{e}")))?;
let mappings = sourcemap.to_json_string();
// TODO: do not clone once fields are exposed from oxc_sourcemap
Ok(Self {
file: sourcemap.get_file().map(ToString::to_string),
Expand Down Expand Up @@ -89,13 +88,12 @@ pub fn transform(
) -> Result<TransformResult, Error> {
let options = options.unwrap_or_default();
let source_type =
SourceType::from_path(&filename).map_err(|err| Error::new(Status::InvalidArg, err.0))?;
SourceType::from_path(&filename).map_err(|err| Error::new(Status::InvalidArg, err))?;
let allocator = Allocator::default();
let parser_ret = Parser::new(&allocator, &source_text, source_type).parse();

let errors = parser_ret.errors;
let mut program = parser_ret.program;
let trivias = parser_ret.trivias;

if !errors.is_empty() {
let errors = errors
Expand All @@ -107,43 +105,33 @@ pub fn transform(
return Err(Error::new(Status::GenericFailure, message));
}

let (symbols, scopes) = SemanticBuilder::new()
.build(&program)
.semantic
.into_symbol_table_and_scope_tree();
let path = Path::new(&filename);
let transform_options = options.clone().into();
let ret = Transformer::new(
&allocator,
path,
source_type,
&source_text,
trivias,
transform_options,
)
.build(&mut program);
let ret = Transformer::new(&allocator, path, transform_options).build_with_symbols_and_scopes(
symbols,
scopes,
&mut program,
);

if !ret.errors.is_empty() {
return Err(Error::from_reason(format!("{}", ret.errors[0])));
}

// TODO: source maps before transforming
let CodegenReturn {
source_text,
source_map,
} = if options.codegen.compress.is_some() {
let mut codegen = Codegen::<true>::new();
if options.codegen.source_map {
codegen = codegen.enable_source_map(&filename, &source_text)
}
codegen.build(&program)
} else {
let mut codegen = Codegen::<false>::new();
if options.codegen.source_map {
codegen = codegen.enable_source_map(&filename, &source_text)
}
codegen.build(&program)
};

let _source_map = source_map.map(SourceMap::try_from).transpose()?;
let CodegenReturn { code, map } = CodeGenerator::new()
.with_options(CodegenOptions {
source_map_path: Some(path.to_path_buf()),
..CodegenOptions::default()
})
.build(&program);

let _source_map = map.map(SourceMap::try_from).transpose()?;
Ok(TransformResult {
source_text,
source_text: code,
// source_map: source_map.map(SourceMap::from),
source_map: None,
})
Expand Down
12 changes: 6 additions & 6 deletions napi/transform/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use napi::{bindgen_prelude::*, ValueType};
use oxc_transformer::{ReactJsxRuntime, ReactOptions, TransformOptions};
use oxc_transformer::{JsxOptions, JsxRuntime, TransformOptions};

#[napi(object, js_name = "TransformOptions")]
#[derive(Debug, Default, Clone)]
Expand All @@ -13,7 +13,7 @@ pub struct JsTransformOptions {
impl From<JsTransformOptions> for TransformOptions {
fn from(options: JsTransformOptions) -> Self {
Self {
react: options.react.into(),
jsx: options.react.into(),
..Default::default()
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ pub struct JsReactOptions {
pub runtime: JsReactJsxRuntime,
}

impl From<JsReactOptions> for ReactOptions {
impl From<JsReactOptions> for JsxOptions {
fn from(options: JsReactOptions) -> Self {
Self {
runtime: options.runtime.into(),
Expand All @@ -150,11 +150,11 @@ pub enum JsReactJsxRuntime {
#[default]
Automatic,
}
impl From<JsReactJsxRuntime> for ReactJsxRuntime {
impl From<JsReactJsxRuntime> for JsxRuntime {
fn from(runtime: JsReactJsxRuntime) -> Self {
match runtime {
JsReactJsxRuntime::Classic => ReactJsxRuntime::Classic,
JsReactJsxRuntime::Automatic => ReactJsxRuntime::Automatic,
JsReactJsxRuntime::Classic => JsxRuntime::Classic,
JsReactJsxRuntime::Automatic => JsxRuntime::Automatic,
}
}
}

0 comments on commit 6b7f06e

Please sign in to comment.