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(transformer): arrow function transform: correctly resolve this in class accessor properties #6386

Merged
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
11 changes: 11 additions & 0 deletions crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
//! Babel gets this wrong: <https://babeljs.io/repl#?code_lz=GYVwdgxgLglg9mABMOcAUAPRBeRaCUOAfIlABYwDOhA3gL5A&presets=&externalPlugins=%40babel%2Fplugin-transform-arrow-functions%407.24.7>
//! * Error on arrow functions in class properties.
//! <https://babeljs.io/repl#?code_lz=MYGwhgzhAEDC0G8BQ1oDMD2HoF5oAoBKXAPmgBcALASwgG4kBfJIA&presets=&externalPlugins=%40babel%2Fplugin-transform-arrow-functions%407.24.7>
//! or we can support it:
//! `class C { x = () => this; }`
//! -> `class C { x = (function(_this) { return () => _this; })(this); }`
//! * Error on `super` in arrow functions.
//! <https://babeljs.io/repl#?code_lz=MYGwhgzhAEBiD29oG8C-AoUkYCEwCdoBTADwBciA7AExgSWXWmgFsiyALeagCgEoUTZtHzsArvkrR-0ALwA-aBDEAHIvgB0AM0QBuIRgxA&presets=&externalPlugins=%40babel%2Fplugin-transform-arrow-functions%407.24.7>
//!
Expand Down Expand Up @@ -296,6 +299,7 @@ impl<'a> ArrowFunctions<'a> {
// * `extends` clause
// * Computed method key
// * Computed property key
// * Computed accessor property key (but `this` in this position is not legal TS)
//
// ```js
// // All these `this` refer to global `this`
Expand All @@ -304,6 +308,8 @@ impl<'a> ArrowFunctions<'a> {
// static [this] = 123;
// [this]() {}
// static [this]() {}
// accessor [this] = 123;
// static accessor [this] = 123;
// }
// ```
//
Expand All @@ -323,6 +329,8 @@ impl<'a> ArrowFunctions<'a> {
// static e() { this }
// #f() { this }
// g(x = this) {}
// accessor h = this;
// static accessor i = this;
// static { this }
// }
// ```
Expand All @@ -337,8 +345,11 @@ impl<'a> ArrowFunctions<'a> {
| Ancestor::FunctionBody(_)
// Class property body
| Ancestor::PropertyDefinitionValue(_)
// Class accessor property body
| Ancestor::AccessorPropertyValue(_)
// Class static block
| Ancestor::StaticBlockBody(_) => return None,
// Arrow function
Ancestor::ArrowFunctionExpressionParams(func) => {
return Some(func.scope_id().get().unwrap())
}
Expand Down