Skip to content

Commit 741f3f6

Browse files
committed
fix: Tokens only extend to the end of the line
Previously, when looking up a line and column, we would always return the token at or immediately before that position, even if it belonged to an earlier minified line. This is 1. wrong—tokens are only meant to extend to the end of a line. 2. inconsistent—this means that an unmapped line before any mapped line would return no mapping, but an unmapped line after a mapped line would.
1 parent 986b691 commit 741f3f6

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,13 @@ impl SourceMap {
713713
let (idx, raw) =
714714
greatest_lower_bound(&self.tokens, &(line, col), |t| (t.dst_line, t.dst_col))?;
715715

716+
// If the token has a lower minified line number,
717+
// it actually belongs to the previous line. That means it should
718+
// not match.
719+
if raw.dst_line < line {
720+
return None;
721+
}
722+
716723
let mut token = Token {
717724
raw,
718725
sm: self,

tests/test_index.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ fn test_basic_indexed_sourcemap() {
100100
ism.lookup_token(0, 0).unwrap().to_tuple(),
101101
("file1.js", 0, 0, None)
102102
);
103-
assert_eq!(
104-
ism.lookup_token(1, 0).unwrap().to_tuple(),
105-
("file1.js", 2, 12, Some("b"))
106-
);
103+
104+
// Line 1, column 0 (zero-based) falls into the first section,
105+
// but there are no mappings for line 1 there.
106+
assert!(ism.lookup_token(1, 0).is_none());
107+
107108
assert_eq!(
108109
ism.lookup_token(1, 1).unwrap().to_tuple(),
109110
("file2.js", 0, 0, None)

tests/test_regular.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ fn test_basic_sourcemap() {
2929
("coolstuff.js", 2, 8, None)
3030
);
3131

32-
// Token can return prior lines.
33-
assert_eq!(
34-
sm.lookup_token(1000, 0).unwrap().to_tuple(),
35-
("coolstuff.js", 2, 8, None)
36-
);
32+
// There are no mappings for line 1000.
33+
assert!(sm.lookup_token(1000, 0).is_none());
3734
}
3835

3936
#[test]

0 commit comments

Comments
 (0)