diff --git a/Cargo.toml b/Cargo.toml index f4182df2a0f91..38dca4cf8ca97 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,9 +109,9 @@ oxc_tasks_common = { path = "tasks/common" } oxc_tasks_transform_checker = { path = "tasks/transform_checker" } # Relaxed version so the user can decide which version to use. -napi = "3.0.0-alpha.11" +napi = "3.0.0-alpha" napi-build = "2.1.3" -napi-derive = "3.0.0-alpha.11" +napi-derive = "3.0.0-alpha" # Relaxed version so the user can decide which version to use. proc-macro2 = "1" diff --git a/crates/oxc_napi/src/lib.rs b/crates/oxc_napi/src/lib.rs index e05e785bb1fa9..db11f25eb7a5c 100644 --- a/crates/oxc_napi/src/lib.rs +++ b/crates/oxc_napi/src/lib.rs @@ -3,20 +3,20 @@ use napi_derive::napi; use oxc_diagnostics::{LabeledSpan, OxcDiagnostic}; #[napi(object)] -pub struct Error { +pub struct OxcError { pub severity: Severity, pub message: String, pub labels: Vec, pub help_message: Option, } -impl Error { +impl OxcError { pub fn new(message: String) -> Self { Self { severity: Severity::Error, message, labels: vec![], help_message: None } } } -impl From for Error { +impl From for OxcError { fn from(diagnostic: OxcDiagnostic) -> Self { let labels = diagnostic .labels diff --git a/napi/parser/index.d.ts b/napi/parser/index.d.ts index a9d6e24bbd725..b7b6dd4d4c9dd 100644 --- a/napi/parser/index.d.ts +++ b/napi/parser/index.d.ts @@ -26,7 +26,7 @@ export declare class ParseResult { get program(): import("@oxc-project/types").Program get module(): EcmaScriptModule get comments(): Array - get errors(): Array + get errors(): Array get magicString(): MagicString } @@ -54,13 +54,6 @@ export interface EcmaScriptModule { importMetas: Array } -export interface Error { - severity: Severity - message: string - labels: Array - helpMessage?: string -} - export interface ErrorLabel { message?: string start: number @@ -145,6 +138,13 @@ export interface OverwriteOptions { contentOnly: boolean } +export interface OxcError { + severity: Severity + message: string + labels: Array + helpMessage?: string +} + /** * Parse asynchronously. * diff --git a/napi/parser/src/lib.rs b/napi/parser/src/lib.rs index 9596451cd81ce..bf9c6e2227467 100644 --- a/napi/parser/src/lib.rs +++ b/napi/parser/src/lib.rs @@ -17,7 +17,7 @@ use oxc::{ parser::{ParseOptions, Parser, ParserReturn}, span::SourceType, }; -use oxc_napi::Error; +use oxc_napi::OxcError; pub use crate::{ magic_string::MagicString, @@ -74,7 +74,7 @@ fn parse_with_return(filename: &str, source_text: String, options: &ParserOption let ret = parse(&allocator, source_type, &source_text, options); let program = serde_json::to_string(&ret.program).unwrap(); - let errors = ret.errors.into_iter().map(Error::from).collect::>(); + let errors = ret.errors.into_iter().map(OxcError::from).collect::>(); let comments = ret .program diff --git a/napi/parser/src/types.rs b/napi/parser/src/types.rs index 3212e3414f526..be6ef3abb96e7 100644 --- a/napi/parser/src/types.rs +++ b/napi/parser/src/types.rs @@ -1,7 +1,7 @@ use napi_derive::napi; use std::mem; -use oxc_napi::Error; +use oxc_napi::OxcError; use crate::magic_string::MagicString; @@ -31,7 +31,7 @@ pub struct ParseResult { pub(crate) program: String, pub(crate) module: EcmaScriptModule, pub(crate) comments: Vec, - pub(crate) errors: Vec, + pub(crate) errors: Vec, } #[napi] @@ -52,7 +52,7 @@ impl ParseResult { } #[napi(getter)] - pub fn errors(&mut self) -> Vec { + pub fn errors(&mut self) -> Vec { mem::take(&mut self.errors) } diff --git a/napi/transform/index.d.ts b/napi/transform/index.d.ts index c5bd41e8e6ad9..a40af6a822fcf 100644 --- a/napi/transform/index.d.ts +++ b/napi/transform/index.d.ts @@ -20,13 +20,6 @@ export interface CompilerAssumptions { setPublicClassFields?: boolean } -export interface Error { - severity: Severity - message: string - labels: Array - helpMessage?: string -} - export interface ErrorLabel { message?: string start: number @@ -85,7 +78,7 @@ export interface IsolatedDeclarationsOptions { export interface IsolatedDeclarationsResult { code: string map?: SourceMap - errors: Array + errors: Array } /** @@ -183,6 +176,13 @@ export interface JsxOptions { refresh?: boolean | ReactRefreshOptions } +export interface OxcError { + severity: Severity + message: string + labels: Array + helpMessage?: string +} + export interface ReactRefreshOptions { /** * Specify the identifier of the refresh registration variable. @@ -332,7 +332,7 @@ export interface TransformResult { * transformed code may still be available even if there are errors in this * list. */ - errors: Array + errors: Array } export interface TypeScriptOptions { diff --git a/napi/transform/src/isolated_declaration.rs b/napi/transform/src/isolated_declaration.rs index 6d985b2d97bad..ffbaf37263fb0 100644 --- a/napi/transform/src/isolated_declaration.rs +++ b/napi/transform/src/isolated_declaration.rs @@ -9,14 +9,14 @@ use oxc::{ parser::Parser, span::SourceType, }; -use oxc_napi::Error; +use oxc_napi::OxcError; use oxc_sourcemap::napi::SourceMap; #[napi(object)] pub struct IsolatedDeclarationsResult { pub code: String, pub map: Option, - pub errors: Vec, + pub errors: Vec, } #[napi(object)] @@ -70,7 +70,7 @@ pub fn isolated_declaration( .with_options(CodegenOptions { source_map_path, ..CodegenOptions::default() }) .build(&transformed_ret.program); - let errors = ret.errors.into_iter().chain(transformed_ret.errors).map(Error::from).collect(); + let errors = ret.errors.into_iter().chain(transformed_ret.errors).map(OxcError::from).collect(); IsolatedDeclarationsResult { code: codegen_ret.code, diff --git a/napi/transform/src/transformer.rs b/napi/transform/src/transformer.rs index 1ed28f8735a99..0f64a7b2060b8 100644 --- a/napi/transform/src/transformer.rs +++ b/napi/transform/src/transformer.rs @@ -20,7 +20,7 @@ use oxc::{ }, CompilerInterface, }; -use oxc_napi::Error; +use oxc_napi::OxcError; use oxc_sourcemap::napi::SourceMap; use crate::IsolatedDeclarationsOptions; @@ -70,7 +70,7 @@ pub struct TransformResult { /// Oxc's parser recovers from common syntax errors, meaning that /// transformed code may still be available even if there are errors in this /// list. - pub errors: Vec, + pub errors: Vec, } /// Options for transforming a JavaScript or TypeScript file. @@ -627,7 +627,7 @@ pub fn transform( Some("tsx") => SourceType::tsx(), Some(lang) => { return TransformResult { - errors: vec![Error::new(format!("Incorrect lang '{lang}'"))], + errors: vec![OxcError::new(format!("Incorrect lang '{lang}'"))], ..Default::default() } } @@ -647,7 +647,7 @@ pub fn transform( Ok(compiler) => compiler, Err(errors) => { return TransformResult { - errors: errors.into_iter().map(Error::from).collect(), + errors: errors.into_iter().map(OxcError::from).collect(), ..Default::default() } } @@ -661,6 +661,6 @@ pub fn transform( declaration: compiler.declaration, declaration_map: compiler.declaration_map, helpers_used: compiler.helpers_used, - errors: compiler.errors.into_iter().map(Error::from).collect(), + errors: compiler.errors.into_iter().map(OxcError::from).collect(), } }