Skip to content

Comments

fix(transformer/optional-chaining): fix context of optional chain call#9526

Merged
graphite-app[bot] merged 1 commit intomainfrom
03-04-fix_transformer_optional-chaining_keep_this_when_transforming_x.y_._
Mar 4, 2025
Merged

fix(transformer/optional-chaining): fix context of optional chain call#9526
graphite-app[bot] merged 1 commit intomainfrom
03-04-fix_transformer_optional-chaining_keep_this_when_transforming_x.y_._

Conversation

@hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Mar 4, 2025

this.f?.() is handled by #9505, but it looks like the same needs to be applied for member expressions in general, such as repro.x.f?.().

Here is an output of esbuild
https://esbuild.github.io/try/#dAAwLjI1LjAALS10YXJnZXQ9ZXMyMDE1AGNvbnN0IHJlcHJvID0ge30KcmVwcm8uZj8uKCk7CnJlcHJvLnguZj8uKCk7CnJlcHJvLnhbImYiXT8uKCk7


This PR fixes following case:

  • Not only member expressions but also arbitrary expressions can be a call context, such as:
(0, repro).f?.();  
  • same logic is required for transform_and_join_expression, which is used in cases, such as
repro.x?.y.f?.()

Copy link
Contributor Author

hi-ogawa commented Mar 4, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-bug Category - Bug labels Mar 4, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 4, 2025

CodSpeed Performance Report

Merging #9526 will not alter performance

Comparing 03-04-fix_transformer_optional-chaining_keep_this_when_transforming_x.y_._ (cead38d) with main (803f061)

Summary

✅ 33 untouched benchmarks

@hi-ogawa hi-ogawa force-pushed the 03-04-fix_transformer_optional-chaining_keep_this_when_transforming_x.y_._ branch from 7f91270 to 63c1ba7 Compare March 4, 2025 01:38
@hi-ogawa hi-ogawa changed the title fix(transformer/optional-chaining): keep this when transforming x.y?.() fix(transformer/optional-chaining): keep this when transforming member expressions Mar 4, 2025
@hi-ogawa hi-ogawa marked this pull request as ready for review March 4, 2025 01:42
@hi-ogawa hi-ogawa marked this pull request as draft March 4, 2025 03:02
@Dunqing
Copy link
Member

Dunqing commented Mar 4, 2025

Have you compared the output to Babel? The transformer implementation almost port from Babel.

@hi-ogawa
Copy link
Contributor Author

hi-ogawa commented Mar 4, 2025

Have you compared the output to Babel? The transformer implementation almost port from Babel.

I tested on babel repl (link) and the output of keep-this/input.ts matches except ? void 0 : vs || difference.

I updated some conformance snapshots with --filter and --override, but I'm not sure if I've done it correctly. Please let me know if I need to fix up something 🙏

@hi-ogawa hi-ogawa marked this pull request as ready for review March 4, 2025 03:55
@hi-ogawa hi-ogawa changed the title fix(transformer/optional-chaining): keep this when transforming member expressions fix(transformer/optional-chaining): fix context of optional chain call Mar 4, 2025
Copy link
Member

@Dunqing Dunqing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! The optional chaining plugin is very complicated and not easy to debug. Thank you for finding and fixing this bug!

@Dunqing Dunqing added the 0-merge Merge with Graphite Merge Queue label Mar 4, 2025
Copy link
Member

Dunqing commented Mar 4, 2025

Merge activity

  • Mar 4, 7:15 AM EST: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Mar 4, 7:15 AM EST: A user added this pull request to the Graphite merge queue.
  • Mar 4, 7:20 AM EST: A user merged this pull request with the Graphite merge queue.

#9526)

- closes #9498

`this.f?.()` is handled by #9505, but it looks like the same needs to be applied for member expressions in general, such as `repro.x.f?.()`.

Here is an output of esbuild
https://esbuild.github.io/try/#dAAwLjI1LjAALS10YXJnZXQ9ZXMyMDE1AGNvbnN0IHJlcHJvID0ge30KcmVwcm8uZj8uKCk7CnJlcHJvLnguZj8uKCk7CnJlcHJvLnhbImYiXT8uKCk7

---

This PR fixes following case:

- Not only member expressions but also arbitrary expressions can be a call context, such as:

```js
(0, repro).f?.();
```

- same logic is required for `transform_and_join_expression`, which is used in cases, such as

```js
repro.x?.y.f?.()
```
@graphite-app graphite-app bot force-pushed the 03-04-fix_transformer_optional-chaining_keep_this_when_transforming_x.y_._ branch from 6fe8f95 to cead38d Compare March 4, 2025 12:15
@graphite-app graphite-app bot merged commit cead38d into main Mar 4, 2025
26 checks passed
@graphite-app graphite-app bot deleted the 03-04-fix_transformer_optional-chaining_keep_this_when_transforming_x.y_._ branch March 4, 2025 12:20
@oxc-bot oxc-bot mentioned this pull request Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0-merge Merge with Graphite Merge Queue A-transformer Area - Transformer / Transpiler C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

optional chaining transform loses this context

2 participants