oden/src/script/typescript.rs
John Doty c02eb25873 [oden] Use deno_ast instead of raw swc for typescript
OK, look, I hate SWC and I really don't want to use it if I can help
it, but the other options are worse.
2023-09-16 22:56:21 -07:00

33 lines
1 KiB
Rust

use deno_ast::{parse_module, MediaType, ParseParams, SourceTextInfo};
use oden_js::{Error, Result};
pub fn transpile_to_javascript(path: &str, input: String) -> Result<String> {
let text_info = SourceTextInfo::new(input.into());
let parsed_source = parse_module(ParseParams {
specifier: path.to_string(),
media_type: MediaType::TypeScript,
text_info,
capture_tokens: true,
maybe_syntax: None,
scope_analysis: false,
})
.map_err(|e| {
let position = e.display_position();
Error::ParseError(
path.into(),
format!(
"{} at {}:{}:{}",
e.message(),
path,
position.line_number,
position.column_number
),
)
})?;
let options: deno_ast::EmitOptions = Default::default();
let transpiled = parsed_source
.transpile(&options)
.map_err(|e| Error::ParseError(path.into(), e.to_string()))?;
return Ok(transpiled.text);
}