Skip to content

Commit

Permalink
fix(napi): rename Error to OxcError to avoid name collision (#7780)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Dec 10, 2024
1 parent 4f1ab49 commit 18d0ce3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ErrorLabel>,
pub help_message: Option<String>,
}

impl Error {
impl OxcError {
pub fn new(message: String) -> Self {
Self { severity: Severity::Error, message, labels: vec![], help_message: None }
}
}

impl From<OxcDiagnostic> for Error {
impl From<OxcDiagnostic> for OxcError {
fn from(diagnostic: OxcDiagnostic) -> Self {
let labels = diagnostic
.labels
Expand Down
16 changes: 8 additions & 8 deletions napi/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export declare class ParseResult {
get program(): import("@oxc-project/types").Program
get module(): EcmaScriptModule
get comments(): Array<Comment>
get errors(): Array<Error>
get errors(): Array<OxcError>
get magicString(): MagicString
}

Expand Down Expand Up @@ -54,13 +54,6 @@ export interface EcmaScriptModule {
importMetas: Array<Span>
}

export interface Error {
severity: Severity
message: string
labels: Array<ErrorLabel>
helpMessage?: string
}

export interface ErrorLabel {
message?: string
start: number
Expand Down Expand Up @@ -145,6 +138,13 @@ export interface OverwriteOptions {
contentOnly: boolean
}

export interface OxcError {
severity: Severity
message: string
labels: Array<ErrorLabel>
helpMessage?: string
}

/**
* Parse asynchronously.
*
Expand Down
4 changes: 2 additions & 2 deletions napi/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Vec<_>>();
let errors = ret.errors.into_iter().map(OxcError::from).collect::<Vec<_>>();

let comments = ret
.program
Expand Down
6 changes: 3 additions & 3 deletions napi/parser/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use napi_derive::napi;
use std::mem;

use oxc_napi::Error;
use oxc_napi::OxcError;

use crate::magic_string::MagicString;

Expand Down Expand Up @@ -31,7 +31,7 @@ pub struct ParseResult {
pub(crate) program: String,
pub(crate) module: EcmaScriptModule,
pub(crate) comments: Vec<Comment>,
pub(crate) errors: Vec<Error>,
pub(crate) errors: Vec<OxcError>,
}

#[napi]
Expand All @@ -52,7 +52,7 @@ impl ParseResult {
}

#[napi(getter)]
pub fn errors(&mut self) -> Vec<Error> {
pub fn errors(&mut self) -> Vec<OxcError> {
mem::take(&mut self.errors)
}

Expand Down
18 changes: 9 additions & 9 deletions napi/transform/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ export interface CompilerAssumptions {
setPublicClassFields?: boolean
}

export interface Error {
severity: Severity
message: string
labels: Array<ErrorLabel>
helpMessage?: string
}

export interface ErrorLabel {
message?: string
start: number
Expand Down Expand Up @@ -85,7 +78,7 @@ export interface IsolatedDeclarationsOptions {
export interface IsolatedDeclarationsResult {
code: string
map?: SourceMap
errors: Array<Error>
errors: Array<OxcError>
}

/**
Expand Down Expand Up @@ -183,6 +176,13 @@ export interface JsxOptions {
refresh?: boolean | ReactRefreshOptions
}

export interface OxcError {
severity: Severity
message: string
labels: Array<ErrorLabel>
helpMessage?: string
}

export interface ReactRefreshOptions {
/**
* Specify the identifier of the refresh registration variable.
Expand Down Expand Up @@ -332,7 +332,7 @@ export interface TransformResult {
* transformed code may still be available even if there are errors in this
* list.
*/
errors: Array<Error>
errors: Array<OxcError>
}

export interface TypeScriptOptions {
Expand Down
6 changes: 3 additions & 3 deletions napi/transform/src/isolated_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceMap>,
pub errors: Vec<Error>,
pub errors: Vec<OxcError>,
}

#[napi(object)]
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions napi/transform/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use oxc::{
},
CompilerInterface,
};
use oxc_napi::Error;
use oxc_napi::OxcError;
use oxc_sourcemap::napi::SourceMap;

use crate::IsolatedDeclarationsOptions;
Expand Down Expand Up @@ -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<Error>,
pub errors: Vec<OxcError>,
}

/// Options for transforming a JavaScript or TypeScript file.
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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()
}
}
Expand All @@ -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(),
}
}

0 comments on commit 18d0ce3

Please sign in to comment.