Skip to content

Commit

Permalink
Extract computed keys from the class closure
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jul 26, 2021
1 parent 1d48bb0 commit 4536255
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 29 deletions.
29 changes: 28 additions & 1 deletion packages/babel-plugin-transform-classes/src/transformClass.ts
Expand Up @@ -62,6 +62,8 @@ export default function transformClass(
protoAlias: null,
isLoose: false,

dynamicKeys: new Map(),

methods: {
// 'list' is in the same order as the elements appear in the class body.
// if there aren't computed keys, we can safely reorder class elements
Expand Down Expand Up @@ -600,8 +602,26 @@ export default function transformClass(
);
}

function extractDynamicKeys() {
const { dynamicKeys, node, scope } = classState as {
dynamicKeys: Map<string, t.Expression>;
node: t.Class;
scope: NodePath["scope"];
};

node.body.body.forEach(elem => {
if (!t.isClassMethod(elem) || !elem.computed) return;
if (scope.isPure(elem.key, /* constatns only*/ true)) return;

const id = scope.generateUidIdentifierBasedOnNode(elem.key);
dynamicKeys.set(id.name, elem.key);

elem.key = id;
});
}

function setupClosureParamsArgs() {
const { superName } = classState;
const { superName, dynamicKeys } = classState;
const closureParams = [];
const closureArgs = [];

Expand All @@ -623,6 +643,11 @@ export default function transformClass(
setState({ superName: t.cloneNode(param) });
}

for (const [name, value] of dynamicKeys) {
closureParams.push(t.identifier(name));
closureArgs.push(value);
}

return { closureParams, closureArgs };
}

Expand Down Expand Up @@ -668,6 +693,8 @@ export default function transformClass(
construct: buildConstructor(classRef, constructorBody, node),
});

extractDynamicKeys();

let { body } = classState;
const { closureParams, closureArgs } = setupClosureParamsArgs();

Expand Down
@@ -1,19 +1,19 @@
let A = /*#__PURE__*/function () {
let A = /*#__PURE__*/function (_x, _ref, _x2) {
"use strict";

function A() {
babelHelpers.classCallCheck(this, A);
}

babelHelpers.createClass(A, [{
key: x,
key: _x,
get: function () {}
}, {
key: (x = 2, 3),
key: _ref,
value: function () {}
}, {
key: x,
key: _x2,
set: function (_) {}
}]);
return A;
}();
}(x, (x = 2, 3), x);
@@ -0,0 +1,5 @@
expect(() => {
class A {
[A.name]() {}
}
}).toThrow(ReferenceError);
@@ -0,0 +1,4 @@
{
"plugins": ["transform-classes"],
"minNodeVersion": "10.0.0"
}
@@ -0,0 +1,11 @@
var log = [];

class A {
[log.push(1)]() {}
static [log.push(2)]() {}
[log.push(3)]() {}
static [log.push(4)]() {}
}

expect(log).toEqual([1, 2, 3, 4]);

@@ -0,0 +1,9 @@
async function* fn() {
class A {
[yield 1]() {}
}

class B extends A {
[await 1]() {}
}
}
@@ -0,0 +1,34 @@
async function* fn() {
var A = /*#__PURE__*/function (_yield$) {
"use strict";

function A() {
babelHelpers.classCallCheck(this, A);
}

babelHelpers.createClass(A, [{
key: _yield$,
value: function value() {}
}]);
return A;
}(yield 1);

var B = /*#__PURE__*/function (_A, _await$) {
"use strict";

babelHelpers.inherits(B, _A);

var _super = babelHelpers.createSuper(B);

function B() {
babelHelpers.classCallCheck(this, B);
return _super.apply(this, arguments);
}

babelHelpers.createClass(B, [{
key: _await$,
value: function value() {}
}]);
return B;
}(A, await 1);
}
@@ -1,4 +1,4 @@
var Foo = /*#__PURE__*/function () {
var Foo = /*#__PURE__*/function (_bar, _ref) {
"use strict";

function Foo() {
Expand All @@ -11,11 +11,11 @@ var Foo = /*#__PURE__*/function () {
"second";
}
}, {
key: bar,
key: _bar,
value: function value() {}
}, {
key: bar + "foo",
key: _ref,
value: function value() {}
}]);
return Foo;
}();
}(bar, bar + "foo");
Expand Up @@ -11,21 +11,21 @@ var Foo = /*#__PURE__*/function (_Bar) {
babelHelpers.classCallCheck(this, Foo);
_this = _super.call(this);

var X = /*#__PURE__*/function () {
var X = /*#__PURE__*/function (_ref) {
function X() {
babelHelpers.classCallCheck(this, X);
}

babelHelpers.createClass(X, [{
key: (() => {
var _Foo;

babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Foo.prototype)), "method", _thisSuper).call(_thisSuper);
})(),
key: _ref,
value: function value() {}
}]);
return X;
}();
}((() => {
var _Foo;

babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Foo.prototype)), "method", _thisSuper).call(_thisSuper);
})());

return _this;
}
Expand Down
Expand Up @@ -16,25 +16,23 @@ var Outer = /*#__PURE__*/function (_Hello) {
var _super = babelHelpers.createSuper(Outer);

function Outer() {
var _this2 = this;

var _this;

babelHelpers.classCallCheck(this, Outer);

var Inner = /*#__PURE__*/function () {
var Inner = /*#__PURE__*/function (_this2) {
function Inner() {
babelHelpers.classCallCheck(this, Inner);
}

babelHelpers.createClass(Inner, [{
key: _this = _super.call(_this2),
key: _this2,
value: function value() {
return 'hello';
}
}]);
return Inner;
}();
}(_this = _super.call(this));

return babelHelpers.possibleConstructorReturn(_this, new Inner());
}
Expand Down
Expand Up @@ -25,19 +25,19 @@ var Outer = /*#__PURE__*/function (_Hello) {
babelHelpers.classCallCheck(this, Outer);
_this = _super.call(this);

var Inner = /*#__PURE__*/function () {
var Inner = /*#__PURE__*/function (_babelHelpers$get$cal) {
function Inner() {
babelHelpers.classCallCheck(this, Inner);
}

babelHelpers.createClass(Inner, [{
key: babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper),
key: _babelHelpers$get$cal,
value: function value() {
return 'hello';
}
}]);
return Inner;
}();
}(babelHelpers.get((_thisSuper = babelHelpers.assertThisInitialized(_this), babelHelpers.getPrototypeOf(Outer.prototype)), "toString", _thisSuper).call(_thisSuper));

return babelHelpers.possibleConstructorReturn(_this, new Inner());
}
Expand Down
@@ -1,17 +1,17 @@
// #1649
var Foo = /*#__PURE__*/function () {
var Foo = /*#__PURE__*/function (_Symbol, _Symbol2) {
"use strict";

function Foo() {
babelHelpers.classCallCheck(this, Foo);
}

babelHelpers.createClass(Foo, [{
key: Symbol(),
key: _Symbol,
value: function value() {}
}, {
key: Symbol(),
key: _Symbol2,
value: function value() {}
}]);
return Foo;
}();
}(Symbol(), Symbol());

0 comments on commit 4536255

Please sign in to comment.