-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(napi/transform,napi/parser): return structured error object (#7724)
closes #7261
- Loading branch information
Showing
19 changed files
with
191 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,2 @@ | ||
Feature gating napi in other crates will lead to symbol not found when compiling, | ||
use this crate for common napi interfaces. |
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,28 @@ | ||
[package] | ||
name = "oxc_napi" | ||
version = "0.39.0" | ||
authors.workspace = true | ||
categories.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
include = ["/src"] | ||
keywords.workspace = true | ||
license.workspace = true | ||
publish = true | ||
repository.workspace = true | ||
rust-version.workspace = true | ||
description.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[lib] | ||
doctest = false | ||
|
||
[dependencies] | ||
napi = { workspace = true } | ||
napi-derive = { workspace = true } | ||
oxc_diagnostics = { workspace = true } | ||
|
||
[package.metadata.cargo-shear] | ||
ignored = ["napi"] |
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,68 @@ | ||
use napi_derive::napi; | ||
|
||
use oxc_diagnostics::{LabeledSpan, OxcDiagnostic}; | ||
|
||
#[napi(object)] | ||
pub struct Error { | ||
pub severity: Severity, | ||
pub message: String, | ||
pub labels: Vec<ErrorLabel>, | ||
pub help_message: Option<String>, | ||
} | ||
|
||
impl Error { | ||
pub fn new(message: String) -> Self { | ||
Self { severity: Severity::Error, message, labels: vec![], help_message: None } | ||
} | ||
} | ||
|
||
impl From<OxcDiagnostic> for Error { | ||
fn from(diagnostic: OxcDiagnostic) -> Self { | ||
let labels = diagnostic | ||
.labels | ||
.as_ref() | ||
.map(|labels| labels.iter().map(ErrorLabel::from).collect::<Vec<_>>()) | ||
.unwrap_or_default(); | ||
Self { | ||
severity: Severity::from(diagnostic.severity), | ||
message: diagnostic.message.to_string(), | ||
labels, | ||
help_message: diagnostic.help.as_ref().map(ToString::to_string), | ||
} | ||
} | ||
} | ||
|
||
#[napi(object)] | ||
pub struct ErrorLabel { | ||
pub message: Option<String>, | ||
pub start: u32, | ||
pub end: u32, | ||
} | ||
|
||
impl From<&LabeledSpan> for ErrorLabel { | ||
#[allow(clippy::cast_possible_truncation)] | ||
fn from(label: &LabeledSpan) -> Self { | ||
Self { | ||
message: label.label().map(ToString::to_string), | ||
start: label.offset() as u32, | ||
end: (label.offset() + label.len()) as u32, | ||
} | ||
} | ||
} | ||
|
||
#[napi(string_enum)] | ||
pub enum Severity { | ||
Error, | ||
Warning, | ||
Advice, | ||
} | ||
|
||
impl From<oxc_diagnostics::Severity> for Severity { | ||
fn from(value: oxc_diagnostics::Severity) -> Self { | ||
match value { | ||
oxc_diagnostics::Severity::Error => Self::Error, | ||
oxc_diagnostics::Severity::Warning => Self::Warning, | ||
oxc_diagnostics::Severity::Advice => Self::Advice, | ||
} | ||
} | ||
} |
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
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
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
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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.