Skip to content

Commit

Permalink
Reuse greatest_lower_bound in get_scope_for_token
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Oct 31, 2022
1 parent 98f78e2 commit a8dbb2e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
22 changes: 5 additions & 17 deletions src/hermes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::encoder::{encode, Encodable};
use crate::errors::{Error, Result};
use crate::jsontypes::{FacebookScopeMapping, FacebookSources, RawSourceMap};
use crate::types::{DecodedMap, RewriteOptions, SourceMap};
use crate::utils::greatest_lower_bound;
use crate::vlq::parse_vlq_segment_into;
use crate::Token;
use std::cmp::Ordering;
use std::io::{Read, Write};
use std::ops::{Deref, DerefMut};

Expand Down Expand Up @@ -104,24 +104,12 @@ impl SourceMapHermes {

// Find the closest mapping, just like here:
// https://github.com/facebook/metro/blob/63b523eb20e7bdf62018aeaf195bb5a3a1a67f36/packages/metro-symbolicate/src/SourceMetadataMapConsumer.js#L204-L231
let mapping =
function_map
.mappings
.binary_search_by(|o| match o.line.cmp(&token.get_src_line()) {
Ordering::Equal => o.column.cmp(&token.get_src_col()),
x => x,
});
let name_index = function_map
.mappings
.get(match mapping {
Ok(a) => a,
Err(a) => a.saturating_sub(1),
})?
.name_index;

let mapping = greatest_lower_bound(&function_map.mappings, &token.get_src(), |o| {
(o.line, o.column)
})?;
function_map
.names
.get(name_index as usize)
.get(mapping.name_index as usize)
.map(|n| n.as_str())
}

Expand Down
19 changes: 1 addition & 18 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::encoder::encode;
use crate::errors::{Error, Result};
use crate::hermes::SourceMapHermes;
use crate::sourceview::SourceView;
use crate::utils::find_common_prefix;
use crate::utils::{find_common_prefix, greatest_lower_bound};

/// Controls the `SourceMap::rewrite` behavior
///
Expand Down Expand Up @@ -1053,20 +1053,3 @@ impl SourceMapSection {
self.map = sm.map(Box::new);
}
}

fn greatest_lower_bound<'a, T, K: Ord, F: Fn(&'a T) -> K>(
slice: &'a [T],
key: &K,
map: F,
) -> Option<&'a T> {
match slice.binary_search_by_key(key, map) {
Ok(index) => Some(&slice[index]),
Err(index) => {
if index > 0 {
Some(&slice[index - 1])
} else {
None
}
}
}
}
17 changes: 17 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ pub fn make_relative_path(base: &str, target: &str) -> String {
}
}

pub fn greatest_lower_bound<'a, T, K: Ord, F: Fn(&'a T) -> K>(
slice: &'a [T],
key: &K,
map: F,
) -> Option<&'a T> {
match slice.binary_search_by_key(key, map) {
Ok(index) => Some(&slice[index]),
Err(index) => {
if index > 0 {
Some(&slice[index - 1])
} else {
None
}
}
}
}

#[test]
fn test_is_abs_path() {
assert!(is_abs_path("C:\\foo.txt"));
Expand Down

0 comments on commit a8dbb2e

Please sign in to comment.