From 741f3f645cc68bd3f3ace88417a27f7dc3691bce Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Fri, 8 Aug 2025 16:18:37 +0200 Subject: [PATCH] fix: Tokens only extend to the end of the line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/types.rs | 7 +++++++ tests/test_index.rs | 9 +++++---- tests/test_regular.rs | 7 ++----- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/types.rs b/src/types.rs index cc6fd7b2..5e6ec3c4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -713,6 +713,13 @@ impl SourceMap { let (idx, raw) = greatest_lower_bound(&self.tokens, &(line, col), |t| (t.dst_line, t.dst_col))?; + // If the token has a lower minified line number, + // it actually belongs to the previous line. That means it should + // not match. + if raw.dst_line < line { + return None; + } + let mut token = Token { raw, sm: self, diff --git a/tests/test_index.rs b/tests/test_index.rs index b3983576..4e8fd124 100644 --- a/tests/test_index.rs +++ b/tests/test_index.rs @@ -100,10 +100,11 @@ fn test_basic_indexed_sourcemap() { ism.lookup_token(0, 0).unwrap().to_tuple(), ("file1.js", 0, 0, None) ); - assert_eq!( - ism.lookup_token(1, 0).unwrap().to_tuple(), - ("file1.js", 2, 12, Some("b")) - ); + + // Line 1, column 0 (zero-based) falls into the first section, + // but there are no mappings for line 1 there. + assert!(ism.lookup_token(1, 0).is_none()); + assert_eq!( ism.lookup_token(1, 1).unwrap().to_tuple(), ("file2.js", 0, 0, None) diff --git a/tests/test_regular.rs b/tests/test_regular.rs index 93db0d46..a1153299 100644 --- a/tests/test_regular.rs +++ b/tests/test_regular.rs @@ -29,11 +29,8 @@ fn test_basic_sourcemap() { ("coolstuff.js", 2, 8, None) ); - // Token can return prior lines. - assert_eq!( - sm.lookup_token(1000, 0).unwrap().to_tuple(), - ("coolstuff.js", 2, 8, None) - ); + // There are no mappings for line 1000. + assert!(sm.lookup_token(1000, 0).is_none()); } #[test]