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(#1167): function calls prefixed with void together with the optional chain (?.) are skipped #1178

Closed
wants to merge 3 commits into from

Conversation

Gamote
Copy link

@Gamote Gamote commented Jan 13, 2024

Summary

Fixes: #1167

constant-folding-plugin replaces this call void foo?.(); with undefined;.

This is because the evaluate() function marks the optional call expressions as safe, making the UnaryExpression visitor think it is safe to replace it with the value returned by the evaluate(), in this case undefined.

This PR makes the evaluate() function mark the optional call expressions as unsafe.

Changelog:

* **[Fix]**: `constant-folding-plugin`: Don't fold optional function calls (`foo.?()`). (https://github.com/facebook/metro/pull/1178 by @Gamote)

Test plan

I have added 2 tests to cover these changes:

  • does not transform optional chained call into undefined
  • does not transform void prefixed optional chained call into undefined

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 13, 2024
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jan 13, 2024
This update includes the NodePath<BabelNodeOptionalCallExpression> as part of the paths considered unsafe.
@codecov-commenter
Copy link

codecov-commenter commented Jan 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (806fab3) 83.31% compared to head (aa5b9b0) 83.31%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1178   +/-   ##
=======================================
  Coverage   83.31%   83.31%           
=======================================
  Files         207      207           
  Lines       10633    10633           
  Branches     2642     2642           
=======================================
  Hits         8859     8859           
  Misses       1774     1774           

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

@Gamote Gamote changed the title fix(#1167): function calls prefixed with void together with the optional chain (?.) are skipped fix(#1167): function calls prefixed with void together with the optional chain (?.) are skipped on production (dev: false). Jan 13, 2024
@Gamote Gamote changed the title fix(#1167): function calls prefixed with void together with the optional chain (?.) are skipped on production (dev: false). fix(#1167): function calls prefixed with void together with the optional chain (?.) are skipped Jan 13, 2024
@facebook-github-bot
Copy link
Contributor

@robhogan has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@robhogan
Copy link
Contributor

This is a great catch @Gamote - thanks for the fix!

(To follow up, we ought to invert this logic so that nodes are default unsafe.)

@Gamote
Copy link
Author

Gamote commented Jan 15, 2024

Facebook Internal - Builds & Tests

@robhogan Is there anything specific that I can do to fix this?

@robhogan
Copy link
Contributor

Facebook Internal - Builds & Tests

@robhogan Is there anything specific that I can do to fix this?

Thanks, but nothing's broken, just a couple of flaky tests. We're just waiting for internal review at the moment, shouldn't be long.

@Gamote
Copy link
Author

Gamote commented Jan 15, 2024

Facebook Internal - Builds & Tests

@robhogan Is there anything specific that I can do to fix this?

Thanks, but nothing's broken, just a couple of flaky tests. We're just waiting for internal review at the moment, shouldn't be long.

Ah alright. Thank you for the information.

@facebook-github-bot
Copy link
Contributor

@robhogan merged this pull request in a7f8955.

@robhogan robhogan mentioned this pull request Jan 29, 2024
robhogan pushed a commit that referenced this pull request Jan 29, 2024
…ional chain (?.) are skipped (#1178)

Summary:
Fixes: #1167

`constant-folding-plugin` replaces this call `void foo?.();` with `undefined;`.

This is because the `evaluate()` function marks the optional call expressions as safe, making the `UnaryExpression` visitor think it is safe to replace it with the `value` returned by the `evaluate()`, in this case `undefined`.

This PR makes the `evaluate()` function mark the optional call expressions as unsafe.

```
* **[Fix]**: `constant-folding-plugin`: Don't fold optional function calls (`foo.?()`).
```

Pull Request resolved: #1178

Test Plan:
I have added 2 tests to cover these changes:
- does not transform optional chained call into `undefined`
- does not transform `void` prefixed optional chained call into `undefined`

Reviewed By: GijsWeterings

Differential Revision: D52780448

Pulled By: robhogan

fbshipit-source-id: c84288adc1fec6c2e875fd766c5a6b0997a36c62
robhogan pushed a commit that referenced this pull request Jan 30, 2024
…ional chain (?.) are skipped (#1178)

Summary:
Fixes: #1167

`constant-folding-plugin` replaces this call `void foo?.();` with `undefined;`.

This is because the `evaluate()` function marks the optional call expressions as safe, making the `UnaryExpression` visitor think it is safe to replace it with the `value` returned by the `evaluate()`, in this case `undefined`.

This PR makes the `evaluate()` function mark the optional call expressions as unsafe.

```
* **[Fix]**: `constant-folding-plugin`: Don't fold optional function calls (`foo.?()`).
```

Pull Request resolved: #1178

Test Plan:
I have added 2 tests to cover these changes:
- does not transform optional chained call into `undefined`
- does not transform `void` prefixed optional chained call into `undefined`

Reviewed By: GijsWeterings

Differential Revision: D52780448

Pulled By: robhogan

fbshipit-source-id: c84288adc1fec6c2e875fd766c5a6b0997a36c62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
4 participants