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

rewrite: stop prepending semicolon to this. special property access… #888

Merged
merged 1 commit into from
Apr 9, 2024

Conversation

ato
Copy link
Contributor

@ato ato commented Mar 21, 2024

Fixes #850

Description

This changes removes the regex rewrite rule that prepends a semicolon when this. special property access occurs immediately after a newline.

For example:

x = 1 + 2
this.location = "foo"

will now be rewritten as:

x = 1 + 2
_____WB$wombat$check$this$function_____(this).location = "foo"

instead of:

x = 1 + 2
;_____WB$wombat$check$this$function_____(this).location = "foo"

Motivation and Context

The prepended semicolon breaks code (such as jQuery in #850) that looks like:

foo = foo ? foo :
            this.location;

I think the reason we started inserting the semicolon was because in situations like:

x = 1 + 2
this.location = "foo"

we used to rewrite to:

x = 1 + 2
(this && this._WB_wombat_obj_proxy || this).location = "foo"

which the browser would interpret as a bogus function call like 2(this && ... ). But nowadays prepending the semicolon should be unnecessary as we currently rewrite to:

x = 2 + 3
_____WB$wombat$check$this$function_____(this).location = "foo"

which will trigger JavaScript's automatic semicolon insertion rules like the original code does.

Types of changes

  • Replay fix (fixes a replay specific issue)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added or updated tests to cover my changes.
  • All new and existing tests passed.

@tw4l
Copy link
Member

tw4l commented Apr 2, 2024

Hi @ato , now that we've worked out the dependency upgrades and related test issues in main, would you mind rebasing this on latest main? Thanks!

…webrecorder#850)

The prepended semicolon breaks code (such as jQuery) that looks like:

    foo = foo ? foo :
                this.location;

I think the reason we started inserting the semicolon was because in situations like:

    x = 1 + 2
    this.location = "foo"

we used to rewrite to:

    x = 1 + 2
    (this && this._WB_wombat_obj_proxy || this).location = "foo"

which the browser would interpret as a bogus function call like `2(this && ... )`.
But nowadays prepending the semicolon should be unnecessary as we currently rewrite to:

    x = 2 + 3
    _____WB$wombat$check$this$function_____(this).location = "foo"

which will trigger JavaScript's automatic semicolon insertion rules like the original code does.
@ato ato force-pushed the rewrite-this-without-semicolon branch from a1c48b5 to d055816 Compare April 5, 2024 03:04
@ato
Copy link
Contributor Author

ato commented Apr 5, 2024

Rebased. :-)

@tw4l tw4l requested review from ikreymer and tw4l April 8, 2024 15:31
@ikreymer
Copy link
Member

ikreymer commented Apr 9, 2024

Thanks for this @ato (and remembering the history of the adding the semicolon).

In wabac.js, we made this a more involved check to see if it the previous character is actually a newline, and a semicolon only then:
https://github.com/webrecorder/wabac.js/blob/main/src/rewrite/jsrewriter.js#L76
from matching:
https://github.com/webrecorder/wabac.js/blob/main/src/rewrite/jsrewriter.js#L115
so we don't have that issue there, but maybe even that is unnecessary?

@ikreymer ikreymer merged commit 791a8d1 into webrecorder:main Apr 9, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done!
Development

Successfully merging this pull request may close these issues.

jquery rewriting causes error
3 participants