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: Allow an off-by-one in Token matching #15

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions src/name_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ impl<'a, T: AsRef<str>> NameResolver<'a, T> {
let token = self
.sourcemap
.lookup_token(source_position.line, source_position.column)?;
let is_exact_match = token.get_dst() == (source_position.line, source_position.column);

if is_exact_match {
let is_exactish_match = token.get_dst_line() == source_position.line
&& token.get_dst_col() >= source_position.column - 1;

if is_exactish_match {
token.get_name()
} else {
None
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/off-by-one/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This SourceMap has an "off by one" problem.

Minified positions for tokens use the preceding tokens end rather than
accounting for whitespace and having an exact start.

This looks a little bit like so:

`var| a|=...|function| b()`
... but should rather be (with exact matches):
`var |a|=...|function |b()`
1 change: 1 addition & 0 deletions tests/fixtures/off-by-one/test.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/fixtures/off-by-one/test.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn parses_large_vendors() {
}

#[test]
fn should_only_resolve_exact() {
fn should_not_resolve_mismatch() {
let minified = fixture("prototype-chain/minified.js");
let map = fixture("prototype-chain/minified.js.map");

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

assert_eq!(resolved_scopes[1].2, Some("Foo.prototype.bar".into()));
}

#[test]
fn should_resolve_exactish() {
let minified = fixture("off-by-one/test.min.js");
let map = fixture("off-by-one/test.map");

let scopes = extract_scope_names(&minified).unwrap();

let resolved_scopes = resolve_original_scopes(&minified, &map, scopes);

assert_eq!(resolved_scopes[2].2, Some("onFailure".into()));
assert_eq!(resolved_scopes[3].2, Some("invoke".into()));
assert_eq!(resolved_scopes[4].2, Some("test".into()));
}