fix: SourceMap::span_to_lines for empty file (#1198)

Co-authored-by: 강동윤 <kdy1997.dev@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2020-11-02 03:01:48 +01:00 committed by GitHub
parent bae0eda8bd
commit 546a01cdc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_common"
repository = "https://github.com/swc-project/swc.git"
version = "0.10.4"
version = "0.10.5"
[features]
concurrent = ["parking_lot"]

View File

@ -457,6 +457,14 @@ impl SourceMap {
}
assert!(hi.line >= lo.line);
// Empty file contains no lines
if lo.file.src.is_empty() {
return Ok(FileLines {
file: lo.file,
lines: vec![],
});
}
let mut lines = Vec::with_capacity(hi.line - lo.line + 1);
// The span starts partway through the first line,
@ -1313,6 +1321,18 @@ mod tests {
assert_eq!(sstr, "blork.rs:2:1: 2:12");
}
#[test]
fn t10() {
// Test span_to_lines for a span of empty file
let sm = SourceMap::new(FilePathMapping::empty());
sm.new_source_file(PathBuf::from("blork.rs").into(), "".to_string());
let span = Span::new(BytePos(0), BytePos(0), NO_EXPANSION);
let file_lines = sm.span_to_lines(span).unwrap();
assert_eq!(file_lines.file.name, PathBuf::from("blork.rs").into());
assert_eq!(file_lines.lines.len(), 0);
}
/// Test failing to merge two spans on different lines
#[test]
fn span_merging_fail() {