Skip to content

Commit

Permalink
refactor(napi/transform): remove context
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 6, 2024
1 parent bdd9e92 commit 3f52ebe
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 186 deletions.
124 changes: 0 additions & 124 deletions napi/transform/src/context.rs

This file was deleted.

40 changes: 40 additions & 0 deletions napi/transform/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::sync::Arc;

use oxc::{
diagnostics::{Error, NamedSource, OxcDiagnostic},
span::SourceType,
};

pub fn wrap_diagnostics(
filename: &str,
source_type: SourceType,
source_text: &str,
errors: Vec<OxcDiagnostic>,
) -> Vec<String> {
if errors.is_empty() {
return vec![];
}
let source = {
let lang = match (source_type.is_javascript(), source_type.is_jsx()) {
(true, false) => "JavaScript",
(true, true) => "JSX",
(false, true) => "TypeScript React",
(false, false) => {
if source_type.is_typescript_definition() {
"TypeScript Declaration"
} else {
"TypeScript"
}
}
};

let ns = NamedSource::new(filename, source_text.to_string()).with_language(lang);
Arc::new(ns)
};

errors
.into_iter()
.map(move |diagnostic| Error::from(diagnostic).with_source_code(Arc::clone(&source)))
.map(|error| format!("{error:?}"))
.collect()
}
52 changes: 27 additions & 25 deletions napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
use napi_derive::napi;

use oxc::{
allocator::Allocator,
codegen::{CodegenReturn, CommentOptions},
codegen::{CodeGenerator, CommentOptions},
isolated_declarations::IsolatedDeclarations,
napi::isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult},
napi::{
isolated_declarations::{IsolatedDeclarationsOptions, IsolatedDeclarationsResult},
source_map::SourceMap,
},
parser::Parser,
span::SourceType,
};

use crate::context::TransformContext;
use crate::errors::wrap_diagnostics;

/// TypeScript Isolated Declarations for Standalone DTS Emit
#[allow(clippy::needless_pass_by_value)]
Expand All @@ -20,36 +25,33 @@ pub fn isolated_declaration(
let source_type = SourceType::from_path(&filename).unwrap_or_default().with_typescript(true);
let allocator = Allocator::default();
let options = options.unwrap_or_default();
let ctx =
TransformContext::new(&allocator, &filename, &source_text, source_type, options.sourcemap);
let transformed_ret = build_declarations(&ctx, options);

IsolatedDeclarationsResult {
code: transformed_ret.source_text,
map: options.sourcemap.and_then(|_| transformed_ret.source_map.map(Into::into)),
errors: ctx.take_and_render_reports(),
}
}
let ret = Parser::new(&allocator, &source_text, source_type).parse();

pub(crate) fn build_declarations(
ctx: &TransformContext<'_>,
options: IsolatedDeclarationsOptions,
) -> CodegenReturn {
let transformed_ret = IsolatedDeclarations::new(
ctx.allocator,
ctx.source_text(),
&ctx.trivias,
&allocator,
&source_text,
&ret.trivias,
oxc::isolated_declarations::IsolatedDeclarationsOptions {
strip_internal: options.strip_internal.unwrap_or(false),
},
)
.build(&ctx.program());
ctx.add_diagnostics(transformed_ret.errors);
ctx.codegen()
.build(&ret.program);

let codegen_ret = CodeGenerator::new()
.enable_comment(
ctx.source_text(),
ctx.trivias.clone(),
&source_text,
ret.trivias.clone(),
CommentOptions { preserve_annotate_comments: false },
)
.build(&transformed_ret.program)
.build(&transformed_ret.program);

let errors = ret.errors.into_iter().chain(transformed_ret.errors).collect();
let errors = wrap_diagnostics(&filename, source_type, &source_text, errors);

IsolatedDeclarationsResult {
code: codegen_ret.source_text,
map: codegen_ret.source_map.map(SourceMap::from),
errors,
}
}
2 changes: 1 addition & 1 deletion napi/transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod context;
mod errors;

pub use oxc::napi::{isolated_declarations, transform};

Expand Down
40 changes: 4 additions & 36 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{path::Path, sync::Arc};
use std::path::Path;

use napi::Either;
use napi_derive::napi;

use oxc::{
codegen::CodegenReturn,
diagnostics::{Error, NamedSource, OxcDiagnostic},
diagnostics::OxcDiagnostic,
napi::{
source_map::SourceMap,
transform::{TransformOptions, TransformResult},
Expand All @@ -15,6 +15,8 @@ use oxc::{
CompilerInterface,
};

use crate::errors::wrap_diagnostics;

#[derive(Default)]
struct Compiler {
transform_options: oxc::transformer::TransformOptions,
Expand Down Expand Up @@ -168,37 +170,3 @@ pub fn transform(
errors: wrap_diagnostics(&filename, source_type, &source_text, compiler.errors),
}
}

fn wrap_diagnostics(
filename: &str,
source_type: SourceType,
source_text: &str,
errors: Vec<OxcDiagnostic>,
) -> Vec<String> {
if errors.is_empty() {
return vec![];
}
let source = {
let lang = match (source_type.is_javascript(), source_type.is_jsx()) {
(true, false) => "JavaScript",
(true, true) => "JSX",
(false, true) => "TypeScript React",
(false, false) => {
if source_type.is_typescript_definition() {
"TypeScript Declaration"
} else {
"TypeScript"
}
}
};

let ns = NamedSource::new(filename, source_text.to_string()).with_language(lang);
Arc::new(ns)
};

errors
.into_iter()
.map(move |diagnostic| Error::from(diagnostic).with_source_code(Arc::clone(&source)))
.map(|error| format!("{error:?}"))
.collect()
}

0 comments on commit 3f52ebe

Please sign in to comment.