Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Index source map lookup #53

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ impl SourceMap {

while low < high {
let mid = (low + high) / 2;
let ii = &self.index[mid as usize];
let ii = &self.index[mid];
if (line, col) < (ii.0, ii.1) {
high = mid;
} else {
Expand All @@ -628,10 +628,12 @@ impl SourceMap {
}

if low > 0 && low <= self.index.len() {
self.get_token(self.index[low as usize - 1].2)
} else {
None
let ii = &self.index[low - 1];
if line == ii.0 {
return self.get_token(ii.2);
}
}
None
}

/// Given a location, name and minified source file resolve a minified
Expand Down Expand Up @@ -935,15 +937,27 @@ impl SourceMapIndex {
/// If a sourcemap is encountered that is not embedded but just
/// externally referenced it is silently skipped.
pub fn lookup_token(&self, line: u32, col: u32) -> Option<Token<'_>> {
for section in self.sections() {
let (off_line, off_col) = section.get_offset();
if off_line < line || off_col < col {
continue;
let mut low = 0;
let mut high = self.sections.len();

while low < high {
let mid = (low + high) / 2;
let section = &self.sections[mid];
if (line, col) < section.get_offset() {
high = mid;
} else {
low = mid + 1;
}
}

if low > 0 && low <= self.sections.len() {
let section = &self.sections[low - 1];
let (off_line, off_col) = section.get_offset();
if let Some(map) = section.get_sourcemap() {
if let Some(tok) = map.lookup_token(line - off_line, col - off_col) {
return Some(tok);
}
return map.lookup_token(
line - off_line,
if line == off_line { col - off_col } else { col },
);
}
}
None
Expand Down
20 changes: 19 additions & 1 deletion tests/test_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn test_basic_indexed_sourcemap() {
{
"offset": {
"line": 1,
"column": 0
"column": 1
},
"map": {
"version":3,
Expand Down Expand Up @@ -95,6 +95,24 @@ fn test_basic_indexed_sourcemap() {
.unwrap_or_else(|| panic!("no source for {}", filename));
assert_eq!(&view.lines().collect::<Vec<_>>(), ref_contents);
}

assert_eq!(
ism.lookup_token(0, 0).unwrap().to_tuple(),
("file1.js", 0, 0, None)
);
assert_eq!(ism.lookup_token(1, 0), None);
assert_eq!(
ism.lookup_token(1, 1).unwrap().to_tuple(),
("file2.js", 0, 0, None)
);
assert_eq!(
ism.lookup_token(1, 8).unwrap().to_tuple(),
("file2.js", 0, 0, None)
);
assert_eq!(
ism.lookup_token(1, 9).unwrap().to_tuple(),
("file2.js", 0, 9, Some("multiply"))
);
}

#[test]
Expand Down
34 changes: 34 additions & 0 deletions tests/test_regular.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use sourcemap::SourceMap;

#[test]
fn test_basic_sourcemap() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let sm = SourceMap::from_reader(input).unwrap();

assert_eq!(
sm.lookup_token(0, 0).unwrap().to_tuple(),
("coolstuff.js", 0, 0, None)
);
assert_eq!(
sm.lookup_token(0, 3).unwrap().to_tuple(),
("coolstuff.js", 0, 4, Some("x"))
);
assert_eq!(
sm.lookup_token(0, 24).unwrap().to_tuple(),
("coolstuff.js", 2, 8, None)
);

// Lines continue out to infinity
assert_eq!(
sm.lookup_token(0, 1000).unwrap().to_tuple(),
("coolstuff.js", 2, 8, None)
);

// Token must be on the same line to be found.
assert_eq!(sm.lookup_token(1000, 0), None);
}