Support named evaluation for decorated anonymous class exp#16148
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/55954/ |
| _computedKey = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper); | ||
| var _A = /*#__PURE__*/new WeakMap(); | ||
| _babelHelpers$get$cal = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper); | ||
| _babelHelpers$get$cal2 = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper); |
There was a problem hiding this comment.
This is a bug fix. The input is
"use strict";
class Hello {
toString() {
return 'hello';
}
}
class Outer extends Hello {
constructor() {
super();
class Inner {
accessor [super.toString()] = 'hello';
}
return new Inner();
}
}
expect(new Outer().hello).toBe('hello');As we can see from the output, the super.toString() is executed twice when we transform the accessor to get-set pairs since we forgot to memoize the computed key.
| // prettier-ignore | ||
| const logs = []; | ||
| const decFactory = (logs) => (value, context) => { | ||
| expect(value.name).toEqual(context.name); |
There was a problem hiding this comment.
The test is more hardened than #15122, here we further assert value.name should equal context.name.
| if (!classDecorators && !hasElementDecorators) return; | ||
| // If nothing is decorated and no assignments inserted, return | ||
| if (!classDecorators && !hasElementDecorators) { | ||
| if (assignments.length > 0) { |
There was a problem hiding this comment.
In this case, we don't have any decorators, but we should still transform the class accessors, the assignments at this time must be the memoized accessor keys.
There was a problem hiding this comment.
Can you add this as an in-code comment? :)
There was a problem hiding this comment.
Yes, the input is
const dec = () => {};
class Foo {
accessor a;
accessor b = 123;
accessor ['c'] = 456;
}And 'c' should not be computed twice. Though in this case there is no behaviour changes if 'c' is used in both getter and setter keys.
There was a problem hiding this comment.
https://github.com/babel/babel/pull/16148/files#diff-ec0a7c89e1c00ced2b4a8ccee78aa564e798866f0815a815ff14c99bb49bb821R600
I just noticed that we have static nodes as a special case, literals appear to be static as well. :)
nicolo-ribaudo
left a comment
There was a problem hiding this comment.
Note that in general we don't preserve correct function names, but we only try to give some useful names for debugging purposes in stack traces. For example, we compile
0, {
"ab method"() {}
}to
0,
{
"ab method": function abMethod() {}
};However, I think this PR is fine because the class name is explicitly exposed to decorators through context.name, and not just "accidentallyviaFunction.prototype.name`.
packages/babel-plugin-proposal-decorators/src/transformer-2023-05.ts
Outdated
Show resolved
Hide resolved
packages/babel-plugin-proposal-decorators/src/transformer-2023-05.ts
Outdated
Show resolved
Hide resolved
| if (!classDecorators && !hasElementDecorators) return; | ||
| // If nothing is decorated and no assignments inserted, return | ||
| if (!classDecorators && !hasElementDecorators) { | ||
| if (assignments.length > 0) { |
There was a problem hiding this comment.
Can you add this as an in-code comment? :)
…-05.ts Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
96c0657 to
5c6d28a
Compare
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>
This PR supersedes #15122.
The only change compared with #15122 is that we now apply
toPropertyKeywhen memoising the computed key passed to thesetFunctionNamehelper call. So that we can now handle the following edge case as wellThe implementation is not changed very much. I tried to reorganize the changes so the it might be easier to review by commits.
In #15122 we discussed whether we should merge the
NamedEvaluationVisitoryFactorywith our current function name utilities. To recap, we introduced asetFunctionNameruntime helper in #16144, at this point we have two helpers that can name a function:babel-helper-function-name(A): an AST transform that wraps a given function with a new function whose id is the desired name, and preserved the source of the given functionnameproperty might throw"output the best number between 2 and 3"because that is not a valid identifier. Bigger output than approach Bbabel-runtime/setFunctionName(B): a runtime call to set the name property of the given functionnameproperty might throwThey are used by two transforms: The approach A is used by
function-nametransform and the approach B is used by the decorators transform in this PR as well as the decorator runtime.I think we can consider merge
NamedEvaluationVisitoryFactorywith thefunction-nametransform. The factory generates a visitor that will take care of various AST subjects to the named evaluation, and thefunction-nametransform can decide whether it should use approach A or approach B for the related AST nodes. We can also come up with a compat table so that thefunction-nametransform can emit theSetFunctionNamecall when the browser supports it, and fallback to the heavierhelper-function-nameAST transform.