Skip to content

Commit f3000c9

Browse files
authored
fix: Allow an off-by-one in Token matching (#15)
As some minifiers do not output exact tokens but include a leading whitespace.
1 parent d028173 commit f3000c9

File tree

5 files changed

+32
-3
lines changed

5 files changed

+32
-3
lines changed

src/name_resolver.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ impl<'a, T: AsRef<str>> NameResolver<'a, T> {
3232
let token = self
3333
.sourcemap
3434
.lookup_token(source_position.line, source_position.column)?;
35-
let is_exact_match = token.get_dst() == (source_position.line, source_position.column);
3635

37-
if is_exact_match {
36+
let is_exactish_match = token.get_dst_line() == source_position.line
37+
&& token.get_dst_col() >= source_position.column - 1;
38+
39+
if is_exactish_match {
3840
token.get_name()
3941
} else {
4042
None
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This SourceMap has an "off by one" problem.
2+
3+
Minified positions for tokens use the preceding tokens end rather than
4+
accounting for whitespace and having an exact start.
5+
6+
This looks a little bit like so:
7+
8+
`var| a|=...|function| b()`
9+
... but should rather be (with exact matches):
10+
`var |a|=...|function |b()`

tests/fixtures/off-by-one/test.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/fixtures/off-by-one/test.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn parses_large_vendors() {
205205
}
206206

207207
#[test]
208-
fn should_only_resolve_exact() {
208+
fn should_not_resolve_mismatch() {
209209
let minified = fixture("prototype-chain/minified.js");
210210
let map = fixture("prototype-chain/minified.js.map");
211211

@@ -218,3 +218,17 @@ fn should_only_resolve_exact() {
218218

219219
assert_eq!(resolved_scopes[1].2, Some("Foo.prototype.bar".into()));
220220
}
221+
222+
#[test]
223+
fn should_resolve_exactish() {
224+
let minified = fixture("off-by-one/test.min.js");
225+
let map = fixture("off-by-one/test.map");
226+
227+
let scopes = extract_scope_names(&minified).unwrap();
228+
229+
let resolved_scopes = resolve_original_scopes(&minified, &map, scopes);
230+
231+
assert_eq!(resolved_scopes[2].2, Some("onFailure".into()));
232+
assert_eq!(resolved_scopes[3].2, Some("invoke".into()));
233+
assert_eq!(resolved_scopes[4].2, Some("test".into()));
234+
}

0 commit comments

Comments
 (0)