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

environmentVisitor should skip decorator expressions #14371

Merged
merged 12 commits into from May 21, 2022
13 changes: 8 additions & 5 deletions packages/babel-helper-environment-visitor/src/index.ts
Expand Up @@ -32,7 +32,7 @@ export function requeueComputedKeyAndDecorators(
// environmentVisitor should be used when traversing the whole class and not for specific class elements/methods.
// For perf reasons, the environmentVisitor might be traversed with `{ noScope: true }`, which means `path.scope` is undefined.
// Avoid using `path.scope` here
export default {
const visitor: Visitor = {
FunctionParent(path) {
if (path.isArrowFunctionExpression()) {
// arrows are not skipped because they inherit the context.
Expand All @@ -44,10 +44,13 @@ export default {
}
}
},
"ClassProperty|ClassPrivateProperty"(
path: NodePath<t.ClassProperty | t.ClassPrivateProperty>,
) {
Property(path) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The visitor now hooks on Property alias so that it supports older @babel/types without ClassAccessorProperty definitions.

if (path.isObjectProperty()) {
return;
}
path.skip();
requeueComputedKeyAndDecorators(path);
},
} as Visitor<unknown>;
};

export default visitor;
Expand Up @@ -16,7 +16,7 @@ let Outer = /*#__PURE__*/function (_Hello) {
var _this;

babelHelpers.classCallCheck(this, Outer);
_dec = _this = _super.call(this)
_dec = _this = _super.call(this);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know why this line is changed after rebasing. Maybe a generator update?

Copy link
Member

Choose a reason for hiding this comment

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

#14398 probably

let Inner = /*#__PURE__*/babelHelpers.createClass(function Inner() {
babelHelpers.classCallCheck(this, Inner);
babelHelpers.defineProperty(this, "hello", _init_hello(this));
Expand Down
@@ -0,0 +1,19 @@
"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');
@@ -0,0 +1,19 @@
"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');
@@ -0,0 +1,8 @@
{
"plugins": [
["proposal-decorators", { "version": "2021-12" }],
"proposal-class-static-block",
"proposal-class-properties",
"transform-classes"
]
}
@@ -0,0 +1,64 @@
"use strict";

let Hello = /*#__PURE__*/function () {
function Hello() {
babelHelpers.classCallCheck(this, Hello);
}

babelHelpers.createClass(Hello, [{
key: "toString",
value: function toString() {
return 'hello';
}
}]);
return Hello;
}();

let Outer = /*#__PURE__*/function (_Hello) {
babelHelpers.inherits(Outer, _Hello);

var _super = babelHelpers.createSuper(Outer);

function Outer() {
let _babelHelpers$get$cal, _babelHelpers$get$cal2;

var _thisSuper, _this;

babelHelpers.classCallCheck(this, Outer);
_this = _super.call(this);

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);

let Inner = /*#__PURE__*/function () {
function Inner() {
babelHelpers.classCallCheck(this, Inner);
babelHelpers.classPrivateFieldInitSpec(this, _A, {
writable: true,
value: 'hello'
});
}

babelHelpers.createClass(Inner, [{
key: _babelHelpers$get$cal,
get: function () {
return babelHelpers.classPrivateFieldGet(this, _A);
}
}, {
key: _babelHelpers$get$cal2,
set: function (v) {
babelHelpers.classPrivateFieldSet(this, _A, v);
}
}]);
return Inner;
}();

return babelHelpers.possibleConstructorReturn(_this, new Inner());
}

return babelHelpers.createClass(Outer);
}(Hello);

expect(new Outer().hello).toBe('hello');
Expand Up @@ -26,7 +26,7 @@ let Outer = /*#__PURE__*/function (_Hello) {

babelHelpers.classCallCheck(this, Outer);
_this = _super.call(this);
_dec = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "dec", _thisSuper)
_dec = babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "dec", _thisSuper);
let Inner = /*#__PURE__*/babelHelpers.createClass(function Inner() {
babelHelpers.classCallCheck(this, Inner);
babelHelpers.defineProperty(this, "hello", _init_hello(this));
Expand Down
Expand Up @@ -502,7 +502,7 @@ function transformClass(
if (element.node.decorators && element.node.decorators.length > 0) {
hasElementDecorators = true;
} else if (element.node.type === "ClassAccessorProperty") {
const { key, value, static: isStatic } = element.node;
const { key, value, static: isStatic, computed } = element.node;

const newId = generateClassPrivateUid();

Expand All @@ -511,7 +511,7 @@ function transformClass(
const newField = generateClassProperty(newId, valueNode, isStatic);

const [newPath] = element.replaceWith(newField);
addProxyAccessorsFor(newPath, key, newId, element.node.computed);
addProxyAccessorsFor(newPath, key, newId, computed);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a bug fix, the element.node.computed is of the replacement node, which is always false.

}
}

Expand Down
Expand Up @@ -38,11 +38,11 @@ class Foo {
babelHelpers.classPrivateFieldSet(this, _B, v);
}

get 'c'() {
get ['c']() {
return babelHelpers.classPrivateFieldGet(this, _C);
}

set 'c'(v) {
set ['c'](v) {
babelHelpers.classPrivateFieldSet(this, _C, v);
}

Expand Down
Expand Up @@ -17,11 +17,11 @@ class Foo {
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _B, v);
}

static get 'c'() {
static get ['c']() {
return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _C);
}

static set 'c'(v) {
static set ['c'](v) {
babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _C, v);
}

Expand Down
Expand Up @@ -23,11 +23,11 @@ class Foo {

#C = 456;

get 'c'() {
get ['c']() {
return this.#C;
}

set 'c'(v) {
set ['c'](v) {
this.#C = v;
}

Expand Down
Expand Up @@ -23,11 +23,11 @@ class Foo {

static #C = 456;

static get 'c'() {
static get ['c']() {
return this.#C;
}

static set 'c'(v) {
static set ['c'](v) {
this.#C = v;
}

Expand Down