From 9695667561e9be8ad006557a574f01706d969161 Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 11 Dec 2024 11:23:10 +0800 Subject: [PATCH 1/5] fix: should be None if original tokens hasn't the line --- src/sourcemap.rs | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/sourcemap.rs b/src/sourcemap.rs index 8c98caa..f9abd2d 100644 --- a/src/sourcemap.rs +++ b/src/sourcemap.rs @@ -163,32 +163,38 @@ impl SourceMap { } /// Generate a lookup table, it will be used at `lookup_token` or `lookup_source_view_token`. - pub fn generate_lookup_table(&self) -> Vec<(u32, u32, u32)> { - let mut table = self - .tokens - .iter() - .enumerate() - .map(|(idx, token)| (token.dst_line, token.dst_col, idx as u32)) - .collect::>(); - table.sort_unstable(); - table + pub fn generate_lookup_table(&self) -> Vec { + if let Some(token) = self.tokens.last() { + let mut table: Vec> = vec![vec![]; token.dst_line as usize + 1]; + for (idx, token) in self.tokens.iter().enumerate() { + table[token.dst_line as usize].push((token.dst_line, token.dst_col, idx as u32)); + } + table.iter_mut().for_each(|line| line.sort_unstable()); + table + } else { + vec![] + } } /// Lookup a token by line and column, it will used at remapping. pub fn lookup_token( &self, - lookup_table: &[(u32, u32, u32)], + lookup_table: &[LineLookupTable], line: u32, col: u32, ) -> Option<&Token> { - let table = greatest_lower_bound(lookup_table, &(line, col), |table| (table.0, table.1))?; + // If the line is greater than the number of lines in the lookup table, it hasn't corresponding origin token. + if line >= lookup_table.len() as u32 { + return None; + } + let table = greatest_lower_bound(&lookup_table[line as usize], &(line, col), |table| (table.0, table.1))?; self.get_token(table.2) } /// Lookup a token by line and column, it will used at remapping. See `SourceViewToken`. pub fn lookup_source_view_token( &self, - lookup_table: &[(u32, u32, u32)], + lookup_table: &[LineLookupTable], line: u32, col: u32, ) -> Option> { @@ -196,6 +202,8 @@ impl SourceMap { } } +type LineLookupTable = Vec<(u32, u32, u32)>; + fn greatest_lower_bound<'a, T, K: Ord, F: Fn(&'a T) -> K>( slice: &'a [T], key: &K, @@ -253,10 +261,8 @@ fn test_sourcemap_lookup_token() { (Some("coolstuff.js"), 2, 8, None) ); - // Token can return prior lines. - assert_eq!( - sm.lookup_source_view_token(&lookup_table, 1000, 0).unwrap().to_tuple(), - (Some("coolstuff.js"), 2, 8, None) + assert!( + sm.lookup_source_view_token(&lookup_table, 1000, 0).is_none() ); } From fef43a7d69a7c036bb22f70643ab10b2e1f9f76b Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 11 Dec 2024 11:38:13 +0800 Subject: [PATCH 2/5] fix: fmt --- src/sourcemap.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sourcemap.rs b/src/sourcemap.rs index f9abd2d..bc1dd61 100644 --- a/src/sourcemap.rs +++ b/src/sourcemap.rs @@ -187,7 +187,9 @@ impl SourceMap { if line >= lookup_table.len() as u32 { return None; } - let table = greatest_lower_bound(&lookup_table[line as usize], &(line, col), |table| (table.0, table.1))?; + let table = greatest_lower_bound(&lookup_table[line as usize], &(line, col), |table| { + (table.0, table.1) + })?; self.get_token(table.2) } @@ -261,9 +263,7 @@ fn test_sourcemap_lookup_token() { (Some("coolstuff.js"), 2, 8, None) ); - assert!( - sm.lookup_source_view_token(&lookup_table, 1000, 0).is_none() - ); + assert!(sm.lookup_source_view_token(&lookup_table, 1000, 0).is_none()); } #[test] From 7a0aa31d150e30feb834916370922dae8d60934a Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 11 Dec 2024 14:39:03 +0800 Subject: [PATCH 3/5] fix: make sure the raw token order is stable --- src/sourcemap.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/sourcemap.rs b/src/sourcemap.rs index bc1dd61..8ee99ce 100644 --- a/src/sourcemap.rs +++ b/src/sourcemap.rs @@ -164,12 +164,18 @@ impl SourceMap { /// Generate a lookup table, it will be used at `lookup_token` or `lookup_source_view_token`. pub fn generate_lookup_table(&self) -> Vec { - if let Some(token) = self.tokens.last() { - let mut table: Vec> = vec![vec![]; token.dst_line as usize + 1]; - for (idx, token) in self.tokens.iter().enumerate() { - table[token.dst_line as usize].push((token.dst_line, token.dst_col, idx as u32)); + let mut tokens = self + .tokens + .iter() + .enumerate() + .map(|(idx, token)| (token.dst_line, token.dst_col, idx as u32)) + .collect::>(); + tokens.sort_unstable(); + if let Some(last_token) = tokens.last() { + let mut table: Vec> = vec![vec![]; last_token.0 as usize + 1]; + for token in tokens.into_iter() { + table[token.0 as usize].push(token); } - table.iter_mut().for_each(|line| line.sort_unstable()); table } else { vec![] From d6e152ba6954a8e4bbf298d6fb1dc49d53aae9ea Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 11 Dec 2024 14:44:57 +0800 Subject: [PATCH 4/5] fix: revert order --- src/sourcemap.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/sourcemap.rs b/src/sourcemap.rs index 8ee99ce..798f518 100644 --- a/src/sourcemap.rs +++ b/src/sourcemap.rs @@ -164,18 +164,13 @@ impl SourceMap { /// Generate a lookup table, it will be used at `lookup_token` or `lookup_source_view_token`. pub fn generate_lookup_table(&self) -> Vec { - let mut tokens = self - .tokens - .iter() - .enumerate() - .map(|(idx, token)| (token.dst_line, token.dst_col, idx as u32)) - .collect::>(); - tokens.sort_unstable(); - if let Some(last_token) = tokens.last() { - let mut table: Vec> = vec![vec![]; last_token.0 as usize + 1]; - for token in tokens.into_iter() { - table[token.0 as usize].push(token); + // The dst line/dst col always has increasing order. + if let Some(last_token) = self.tokens.last() { + let mut table: Vec> = vec![vec![]; last_token.dst_line as usize + 1]; + for (idx, token) in self.tokens.iter().enumerate() { + table[token.dst_line as usize].push((token.dst_line, token.dst_col, idx as u32)); } + table.iter_mut().for_each(|line| line.sort_unstable()); table } else { vec![] From 708bcb172ca198a727ab6615d37bf3a910e807a5 Mon Sep 17 00:00:00 2001 From: underfin Date: Wed, 11 Dec 2024 14:47:33 +0800 Subject: [PATCH 5/5] fix: fmt --- src/sourcemap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sourcemap.rs b/src/sourcemap.rs index 798f518..9b8e4fc 100644 --- a/src/sourcemap.rs +++ b/src/sourcemap.rs @@ -166,7 +166,7 @@ impl SourceMap { pub fn generate_lookup_table(&self) -> Vec { // The dst line/dst col always has increasing order. if let Some(last_token) = self.tokens.last() { - let mut table: Vec> = vec![vec![]; last_token.dst_line as usize + 1]; + let mut table = vec![vec![]; last_token.dst_line as usize + 1]; for (idx, token) in self.tokens.iter().enumerate() { table[token.dst_line as usize].push((token.dst_line, token.dst_col, idx as u32)); }