oxc/napi/transform/src/errors.rs
Boshen a0ccc26c12 feat(napi/transform): add lang option to change source type (#6309)
part of #6274 and #6156

```
    /// Treat the source text as `js`, `jsx`, `ts`, or `tsx`.
    #[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")]
    pub lang: Option<String>,
```
2024-10-06 04:53:47 +00:00

41 lines
1.1 KiB
Rust

use std::{path::Path, sync::Arc};
use oxc::{
diagnostics::{Error, NamedSource, OxcDiagnostic},
span::SourceType,
};
pub fn wrap_diagnostics(
filename: &Path,
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.to_string_lossy(), 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()
}