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
Set function name for decorated private non-field elements #16144
Set function name for decorated private non-field elements #16144
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55932/ |
setFunctionName(decInfo[4], "#" + name, "set"); | ||
} | ||
setFunctionName(decInfo[3], "#" + name, prefix); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TS emits dedicated setFunctionName
call here:
// input
class C {
@dec static accessor #f = 1
}
// output
let C = (() => {
var _a, _C_f_accessor_storage, _C_f_get, _C_f_set;
let _staticExtraInitializers = [];
let _static_private_f_decorators;
let _static_private_f_initializers = [];
let _static_private_f_descriptor;
return _a = class C {
},
_C_f_get = function _C_f_get() { return _static_private_f_descriptor.get.call(this); },
_C_f_set = function _C_f_set(value) { return _static_private_f_descriptor.set.call(this, value); },
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
_static_private_f_decorators = [dec];
/* ⬇️⬇️⬇️ __setFunctionName call here ⬇️⬇️⬇️ */
__esDecorate(_a, _static_private_f_descriptor = { get: __setFunctionName(function () { return __classPrivateFieldGet(_a, _a, "f", _C_f_accessor_storage); }, "#f", "get"), set: __setFunctionName(function (value) { __classPrivateFieldSet(_a, _a, value, "f", _C_f_accessor_storage); }, "#f", "set") }, _static_private_f_decorators, { kind: "accessor", name: "#f", static: true, private: true, access: { has: obj => __classPrivateFieldIn(_a, obj), get: obj => __classPrivateFieldGet(obj, _a, "a", _C_f_get), set: (obj, value) => { __classPrivateFieldSet(obj, _a, value, "a", _C_f_set); } }, metadata: _metadata }, _static_private_f_initializers, _staticExtraInitializers);
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_a, _staticExtraInitializers);
})(),
_C_f_accessor_storage = { value: __runInitializers(_a, _static_private_f_initializers, 1) },
_a;
})();
Here we merge the setFunctionName
call with the applyMemberDecs
to optimize the output size.
Co-authored-by: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com>
/* @minVersion 7.23.5 */ | ||
|
||
// https://tc39.es/ecma262/#sec-setfunctionname | ||
export default function setFunctionName( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the various decorators helpers are exclusive (and everything except for legacy and 2023-05) will be dropped in Babel 8, could we just inline the helper instead?
Or maybe we could even just fix this in 2023-05.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The helper is isolated because we will eventually emit setFunctionName
when transforming the decorated anonymous class expression (Part 1 of #16117). For example
// input.js
function dec(target, context) {
// print '[Symbol.iterator]'
console.log(target.name);
}
class Dummy {
[Symbol.iterator] = @dec class {}
}
to
// output.js
function dec(_, context) {
// print '[Symbol.iterator]'
console.log(context.name);
}
var computedKey = Symbol.iterator;
class Dummy {
[computedKey] = (setFunctionName(class {
static {
[_decorated_class, _initClass] = _applyDecs(this, [], [dec]).c;
}
static {
_initClass();
}
}, computedKey), _decorated_class);
}
In this example we will also pass a symbol, which is also a valid property key, to the setFunctionName
helper.
Currently we have not implemented such transform yet. It was addressed in #15122 but I will open new PR to do so.
// https://tc39.es/ecma262/#sec-setfunctionname | ||
export default function setFunctionName( | ||
fn: Function, | ||
name: symbol | string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In which case we are passing a symbol name to this helper?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #16144 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok 👍
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [@babel/traverse](https://babel.dev/docs/en/next/babel-traverse) ([source](https://github.com/babel/babel)) | resolutions | patch | [`7.23.5` -> `7.23.7`](https://renovatebot.com/diffs/npm/@babel%2ftraverse/7.23.5/7.23.7) | --- ### Release Notes <details> <summary>babel/babel (@​babel/traverse)</summary> ### [`v7.23.7`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7237-2023-12-29) [Compare Source](babel/babel@v7.23.6...v7.23.7) ##### 🐛 Bug Fix - `babel-traverse` - [#​16191](babel/babel#16191) fix: Crash when removing without `Program` ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-helpers`, `babel-plugin-proposal-decorators` - [#​16180](babel/babel#16180) fix: Class decorator `ctx.kind` is wrong ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-plugin-proposal-decorators` - [#​16170](babel/babel#16170) Fix decorator initProto usage in derived classes ([@​JLHwung](https://github.com/JLHwung)) - `babel-core` - [#​16167](babel/babel#16167) Avoid unpreventable `unhandledRejection` events ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 🏠 Internal - `babel-helper-create-class-features-plugin` - [#​16186](babel/babel#16186) chore: Update deps ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-helper-create-class-features-plugin`, `babel-plugin-proposal-decorators` - [#​16177](babel/babel#16177) Merge decorators into class features ([@​JLHwung](https://github.com/JLHwung)) ### [`v7.23.6`](https://github.com/babel/babel/blob/HEAD/CHANGELOG.md#v7236-2023-12-11) [Compare Source](babel/babel@v7.23.5...v7.23.6) ##### 👓 Spec Compliance - `babel-generator`, `babel-parser`, `babel-types` - [#​16154](babel/babel#16154) Remove `TSPropertySignature.initializer` ([@​fisker](https://github.com/fisker)) - `babel-helpers`, `babel-plugin-proposal-decorators`, `babel-plugin-transform-class-properties`, `babel-plugin-transform-class-static-block`, `babel-plugin-transform-runtime`, `babel-preset-env`, `babel-runtime-corejs2`, `babel-runtime-corejs3`, `babel-runtime`, `babel-types` - [#​16139](babel/babel#16139) Apply `toPropertyKey` on decorator context name ([@​JLHwung](https://github.com/JLHwung)) ##### 🐛 Bug Fix - `babel-generator` - [#​16166](babel/babel#16166) fix: Correctly indenting when `retainLines` is enabled ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-helpers`, `babel-plugin-proposal-explicit-resource-management` - [#​16150](babel/babel#16150) `using`: Allow looking up `Symbol.dispose` on a function ([@​odinho](https://github.com/odinho)) - `babel-plugin-proposal-decorators`, `babel-plugin-transform-class-properties` - [#​16161](babel/babel#16161) Ensure the `[[@​@​toPrimitive]]` call of a decorated class member key is invoked once ([@​JLHwung](https://github.com/JLHwung)) - [#​16148](babel/babel#16148) Support named evaluation for decorated anonymous class exp ([@​JLHwung](https://github.com/JLHwung)) - `babel-plugin-transform-for-of`, `babel-preset-env` - [#​16011](babel/babel#16011) fix: `for of` with `iterableIsArray` and shadowing variable ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) - `babel-helpers`, `babel-plugin-proposal-decorators`, `babel-runtime-corejs2`, `babel-runtime-corejs3`, `babel-runtime` - [#​16144](babel/babel#16144) Set function name for decorated private non-field elements ([@​JLHwung](https://github.com/JLHwung)) - `babel-plugin-transform-typescript` - [#​16137](babel/babel#16137) Fix references to enum values with merging ([@​nicolo-ribaudo](https://github.com/nicolo-ribaudo)) ##### 🔬 Output optimization - `babel-helper-create-class-features-plugin`, `babel-plugin-transform-class-properties` - [#​16159](babel/babel#16159) Reuse computed key memoiser ([@​JLHwung](https://github.com/JLHwung)) - `babel-helpers`, `babel-plugin-proposal-decorators` - [#​16160](babel/babel#16160) Optimize decorator helper size ([@​liuxingbaoyu](https://github.com/liuxingbaoyu)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4wLjAiLCJ1cGRhdGVkSW5WZXIiOiIzNy4wLjAiLCJ0YXJnZXRCcmFuY2giOiJkZXZlbG9wIn0=--> Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/129 Co-authored-by: Renovate Bot <renovate@vylpes.com> Co-committed-by: Renovate Bot <renovate@vylpes.com>
In this PR we introduced a new
setFunctionName
helper, used by the decorator runtime to set the function name for any decorated private non-field elements.As a follow-up to #16139, this PR is the second part of #15122.
In the future, the
setFunctionName
helper will set the class name of any anonymous class expression so that its own class decorator can access the inferred class name from the NamedEvaluation, which is the last part of #15122.