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

Define class elements in the correct order #12723

Merged
merged 5 commits into from Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions packages/babel-plugin-transform-classes/src/transformClass.js
Expand Up @@ -413,24 +413,25 @@ export default function transformClass(
const placement = node.static ? "static" : "instance";
const methods = classState.methods[placement];

const { computed } = node;
if (computed) methods.hasComputed = true;

const key = t.toComputedKey(node);
const descKey = node.kind === "method" ? "value" : node.kind;
const key =
t.isNumericLiteral(node.key) || t.isBigIntLiteral(node.key)
? t.stringLiteral(String(node.key.value))
: t.toComputedKey(node);

let fn = t.toExpression(node);
// infer function name
if (t.isStringLiteral(key) && node.kind === "method") {
fn = nameFunction({ id: key, node: node, scope });

if (t.isStringLiteral(key)) {
// infer function name
if (node.kind === "method") {
fn = nameFunction({ id: key, node: node, scope });
}
Copy link
Member Author

Choose a reason for hiding this comment

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

We can also infer the name of getters and setters, but I'll do that in a separate PR since it's not strictly related to this one.

} else {
methods.hasComputed = true;
}

let descriptor;
if (
!methods.hasComputed &&
t.isStringLiteral(key) &&
methods.map.has(key.value)
) {
if (!methods.hasComputed && methods.map.has(key.value)) {
descriptor = methods.map.get(key.value);
descriptor[descKey] = fn;

Expand Down
@@ -0,0 +1,17 @@
class A {
get 1() {}
set [1](_) {}

get 2() {}
set "2"(_) {}

get 3n() {}
set 3(_) {}

get [4n]() {}
set "4"(_) {}

// Different keys
get 5n() {}
set "5n"(_) {}
}
@@ -0,0 +1,40 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

let A = /*#__PURE__*/function () {
"use strict";

function A() {
_classCallCheck(this, A);
}

_createClass(A, [{
key: "1",
get: function () {},
set: function (_) {}
}, {
key: "2",
get: function () {},
set: function (_) {}
}, {
key: "3",
get: function () {},
set: function (_) {}
}, {
key: "4",
get: function () {},
set: function (_) {} // Different keys

}, {
key: "5",
get: function () {}
}, {
key: "5n",
set: function (_) {}
}]);

return A;
}();