Skip to content

Fix detection of newline/dot before this in replace_this_prop - #383

Merged
benoit74 merged 1 commit into
mainfrom
benoit74/detection-of-new-line-before-this-in-replace_thi
Nov 6, 2026
Merged

benoit74 merged 1 commit into
mainfrom
benoit74/detection-of-new-line-before-this-in-replace_thi

Conversation

@benoit74

@benoit74 benoit74 commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • The this.<prop> JS rewriting rule (replace_this_prop / its regex in create_js_rules) could only look back 1-2 characters from this, so:
    • it only detected a preceding newline (and prepended the ASI-guarding ;) when there were exactly 2 whitespace characters between the newline and this — any other amount (0, 1, 3, 4, ...) silently skipped the ;.
    • it wrongly rewrote this when it was actually a property access (preceded by . or $) separated by 3+ spaces, since the regex couldn't see that far back.
  • Replaced the bounded-lookback regex with a backward scan over the full original string done in the rewrite callback, which correctly handles any amount of whitespace and still works even when an earlier rule (e.g. the let/const location = passthrough) has already consumed whatever token immediately preceded the whitespace.
  • Added tests covering 0-4 spaces after a newline, and ./$ followed by 3 spaces.

Fixes #381

The `this.<prop>` rewriting rule used a regex that could only look 1-2
characters back from `this`, so it would miss a preceding newline (and
thus skip the needed ASI-guarding `;`) for any amount of whitespace
other than exactly 2 characters, and would wrongly rewrite `this` when
it was actually preceded by `.`/`$` with 3 or more spaces in between.

Replace that bounded lookback with an explicit backward scan over the
original string (done in the rewrite callback rather than the regex,
since an earlier rule may already have consumed whatever immediately
preceded the whitespace, e.g. in `const location = this.location`).

Fixes #381
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3a14c7e) to head (a129b66).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #383   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           42        42           
  Lines         2900      2904    +4     
  Branches       438       439    +1     
=========================================
+ Hits          2900      2904    +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@benoit74 benoit74 self-assigned this Nov 6, 2026
@benoit74
benoit74 merged commit 4afb8ed into main Nov 6, 2026
10 checks passed
@benoit74
benoit74 deleted the benoit74/detection-of-new-line-before-this-in-replace_thi branch November 6, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detection of new line before this in replace_this_non_prop in probably buggy

1 participant