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 eecf87b commit 25d572c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
6 changes: 6 additions & 0 deletions crates/node_binding/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ pub fn format_diagnostic(diagnostic: JsDiagnostic) -> Result<External<Diagnostic
"Format diagnostic failed: Invalid `length` in location.",
));
}
if !source_code.is_char_boundary(offset) || !source_code.is_char_boundary(end_byte) {
return Err(Error::new(
Status::Unknown,
"Format diagnostic failed: Invalid char boundary. Did you pass the correct line, column and length?",
));
}
d = d.with_label(LabeledSpan::new(location.text, offset, length));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/rspack_util/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ pub fn try_line_column_length_to_location(
let sc = column;

let sb = rope.try_line_to_byte(sl).ok()?;
let char_index = rope.try_byte_to_char(sb + sc + length).ok()?;
let el = rope.try_char_to_line(char_index).ok()?;
let ec = char_index - rope.try_line_to_char(el).ok()?;
let end_byte = sb + sc + length;
let el = rope.try_byte_to_line(end_byte).ok()?;
let ec = end_byte - rope.try_line_to_byte(el).ok()?;

Some(Location {
sl: sl as u32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ require("!./with-location.js!")

require("!./with-multiple-line.js!")

require("!./with-multi-byte-char.js!")
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ ERROR in (./with-location.js!) 1:1-4
Β· ───
╰────

ERROR in (./with-multi-byte-char.js!) 1:0-13
Γ— ModuleError: Multi-byte character error
╭────
1 β”‚ πŸ‘―β€β™€οΈπŸ‘―β€β™€οΈπŸ‘―β€β™€οΈπŸ‘―β€β™€οΈ
Β· ───
╰────

ERROR in (./with-multi-byte-char.js!)
Γ— Module build failed:
╰─▢ Γ— Error: Format diagnostic failed: Invalid char boundary. Did you pass the correct line, column and length?
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚ at xxx
β”‚

ERROR in (./with-multiple-line.js!) 1:0-2:4
Γ— ModuleError: Multiple line error
╭─[1:0]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @type {import("@rspack/core").LoaderDefinition} */
module.exports = function() {
this.experiments.emitDiagnostic({
message: "Multi-byte character error",
severity: "error",
sourceCode: `πŸ‘―β€β™€οΈπŸ‘―β€β™€οΈπŸ‘―β€β™€οΈπŸ‘―β€β™€οΈ`,
location: {
line: 1,
column: 0,
length: 13,
},
});
// Boundary error
this.experiments.emitDiagnostic({
message: "Multi-byte character error",
severity: "error",
sourceCode: `"β€οΈπŸ§‘πŸ’›πŸ’šπŸ’™πŸ’œ"`,
location: {
line: 1,
column: 0,
length: 13,
},
});
return ""
}

0 comments on commit 25d572c

Please sign in to comment.