-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #4071 Co-authored-by: Boshen <[email protected]>
- Loading branch information
Showing
4 changed files
with
66 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::sync::Arc; | ||
|
||
use napi_derive::napi; | ||
|
||
use oxc_allocator::Allocator; | ||
use oxc_codegen::CodeGenerator; | ||
use oxc_diagnostics::{Error, NamedSource}; | ||
use oxc_isolated_declarations::IsolatedDeclarations; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
#[napi(object)] | ||
pub struct IsolatedDeclarationsResult { | ||
pub source_text: String, | ||
pub errors: Vec<String>, | ||
} | ||
|
||
/// TypeScript Isolated Declarations for Standalone DTS Emit | ||
#[allow(clippy::needless_pass_by_value)] | ||
#[napi] | ||
pub fn isolated_declaration(filename: String, source_text: String) -> IsolatedDeclarationsResult { | ||
let source_type = SourceType::from_path(&filename).unwrap_or_default().with_typescript(true); | ||
let allocator = Allocator::default(); | ||
let parser_ret = Parser::new(&allocator, &source_text, source_type).parse(); | ||
let transformed_ret = IsolatedDeclarations::new(&allocator).build(&parser_ret.program); | ||
let printed = CodeGenerator::new().build(&transformed_ret.program).source_text; | ||
|
||
let mut errors = vec![]; | ||
if !parser_ret.errors.is_empty() || !transformed_ret.errors.is_empty() { | ||
let source = Arc::new(NamedSource::new(filename, source_text.to_string())); | ||
errors.extend( | ||
parser_ret | ||
.errors | ||
.into_iter() | ||
.chain(transformed_ret.errors) | ||
.map(|diagnostic| Error::from(diagnostic).with_source_code(Arc::clone(&source))) | ||
.map(|error| format!("{error:?}")), | ||
); | ||
} | ||
|
||
IsolatedDeclarationsResult { source_text: printed, errors } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,4 @@ | ||
pub mod transformer; | ||
mod isolated_declaration; | ||
mod transformer; | ||
|
||
use std::sync::Arc; | ||
|
||
use napi_derive::napi; | ||
|
||
use oxc_allocator::Allocator; | ||
use oxc_codegen::CodeGenerator; | ||
use oxc_diagnostics::{Error, NamedSource}; | ||
use oxc_isolated_declarations::IsolatedDeclarations; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
#[napi(object)] | ||
pub struct IsolatedDeclarationsResult { | ||
pub source_text: String, | ||
pub errors: Vec<String>, | ||
} | ||
|
||
/// TypeScript Isolated Declarations for Standalone DTS Emit | ||
#[allow(clippy::needless_pass_by_value)] | ||
#[napi] | ||
pub fn isolated_declaration(filename: String, source_text: String) -> IsolatedDeclarationsResult { | ||
let source_type = SourceType::from_path(&filename).unwrap_or_default().with_typescript(true); | ||
let allocator = Allocator::default(); | ||
let parser_ret = Parser::new(&allocator, &source_text, source_type).parse(); | ||
let transformed_ret = IsolatedDeclarations::new(&allocator).build(&parser_ret.program); | ||
let printed = CodeGenerator::new().build(&transformed_ret.program).source_text; | ||
|
||
let mut errors = vec![]; | ||
if !parser_ret.errors.is_empty() || !transformed_ret.errors.is_empty() { | ||
let source = Arc::new(NamedSource::new(filename, source_text.to_string())); | ||
errors.extend( | ||
parser_ret | ||
.errors | ||
.into_iter() | ||
.chain(transformed_ret.errors) | ||
.map(|diagnostic| Error::from(diagnostic).with_source_code(Arc::clone(&source))) | ||
.map(|error| format!("{error:?}")), | ||
); | ||
} | ||
|
||
IsolatedDeclarationsResult { source_text: printed, errors } | ||
} | ||
pub use crate::{isolated_declaration::*, transformer::*}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters