Skip to content

Commit

Permalink
test: add
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Sep 19, 2024
1 parent 3142def commit eecf87b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
26 changes: 19 additions & 7 deletions crates/node_binding/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use napi::bindgen_prelude::External;
use napi::bindgen_prelude::*;
use rspack_error::{
miette::{self, LabeledSpan, MietteDiagnostic, Severity},
Diagnostic,
Expand Down Expand Up @@ -32,7 +32,7 @@ pub struct JsDiagnostic {
}

#[napi(ts_return_type = "ExternalObject<'Diagnostic'>")]
pub fn format_diagnostic(diagnostic: JsDiagnostic) -> External<Diagnostic> {
pub fn format_diagnostic(diagnostic: JsDiagnostic) -> Result<External<Diagnostic>> {
let JsDiagnostic {
message,
help,
Expand All @@ -59,25 +59,37 @@ pub fn format_diagnostic(diagnostic: JsDiagnostic) -> External<Diagnostic> {
location.column as usize,
location.length as usize,
);
if let Some((offset, length)) = try_line_column_length_to_offset_length(
let (offset, length) = try_line_column_length_to_offset_length(
&rope,
location.line as usize,
location.column as usize,
location.length as usize,
) {
d = d.with_label(LabeledSpan::new(location.text, offset, length));
)
.ok_or_else(|| {
Error::new(
Status::Unknown,
"Format diagnostic failed: Invalid location. Did you pass the correct line, column and length?",
)
})?;
let end_byte = offset.saturating_add(length);
if end_byte > rope.len_bytes() {
return Err(Error::new(
Status::Unknown,
"Format diagnostic failed: Invalid `length` in location.",
));
}
d = d.with_label(LabeledSpan::new(location.text, offset, length));
}
}

let mut error = miette::Error::new(d);
if let Some(source_code) = source_code {
error = error.with_source_code(source_code);
}
External::new(
Ok(External::new(
Diagnostic::from(error)
.with_file(file.map(Into::into))
.with_loc(loc.map(|l| l.to_string()))
.with_module_identifier(module_identifier.map(Into::into)),
)
))
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ ERROR in (./with-multiple-line.js!) 1:0-2:4
Β· ╰──── unexpected '~'
╰────

ERROR in (./with-multiple-line.js!)
Γ— Module build failed:
╰─▢ Γ— Error: Format diagnostic failed: Invalid `length` in location.
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚

ERROR in (./with-multiple-line.js!) 3:4
Γ— ModuleError: Multiple line snippet
╭─[3:4]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,21 @@ module.exports = function() {
length: 0,
},
});
// Length overflow
this.experiments.emitDiagnostic({
message: "Multiple line snippet",
severity: "error",
sourceCode: `~~~~~
~~~~~
~~~~~
~~~~~
~~~~~`,
location: {
text: "unexpected '~'",
line: 3,
column: 4,
length: 100,
},
});
return ""
}

0 comments on commit eecf87b

Please sign in to comment.