Skip to content

Commit

Permalink
Implement setClassMethods
Browse files Browse the repository at this point in the history
- transform-classes
  • Loading branch information
nicolo-ribaudo committed Nov 27, 2020
1 parent da76ea2 commit a4f47b8
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -334,6 +334,7 @@ export type NestingPath = RootPath | OverridesPath | EnvPath;
export const assumptionsNames = new Set<string>([
"mutableTemplateObject",
"newableArrowFunctions",
"setClassMethods",
"setPublicClassFields",
]);

Expand Down
6 changes: 5 additions & 1 deletion packages/babel-plugin-transform-classes/src/index.js
Expand Up @@ -21,6 +21,8 @@ export default declare((api, options) => {

const { loose } = options;

const setClassMethods = options.loose || api.assumption("setClassMethods");

// todo: investigate traversal requeueing
const VISITED = Symbol();

Expand Down Expand Up @@ -58,7 +60,9 @@ export default declare((api, options) => {
node[VISITED] = true;

path.replaceWith(
transformClass(path, state.file, builtinClasses, loose),
transformClass(path, state.file, builtinClasses, loose, {
setClassMethods,
}),
);

if (path.isCallExpression()) {
Expand Down
Expand Up @@ -12,6 +12,8 @@ import addCreateSuperHelper from "./inline-createSuper-helpers";

type ReadonlySet<T> = Set<T> | { has(val: T): boolean };

type ClassAssumptions = { setClassMethods: boolean };

function buildConstructor(classRef, constructorBody, node) {
const func = t.functionDeclaration(
t.cloneNode(classRef),
Expand All @@ -27,6 +29,7 @@ export default function transformClass(
file: any,
builtinClasses: ReadonlySet<string>,
isLoose: boolean,
assumptions: ClassAssumptions,
) {
const classState = {
parent: undefined,
Expand Down Expand Up @@ -431,7 +434,7 @@ export default function transformClass(
}

function processMethod(node, scope) {
if (classState.isLoose && !node.decorators) {
if (assumptions.setClassMethods && !node.decorators) {
// use assignments instead of define properties for loose classes
let { classRef } = classState;
if (!node.static) {
Expand Down
@@ -0,0 +1,5 @@
class Foo {
"bar"() {

}
}
@@ -0,0 +1,13 @@
var Foo = /*#__PURE__*/function () {
"use strict";

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

var _proto = Foo.prototype;

_proto["bar"] = function bar() {};

return Foo;
}();
@@ -0,0 +1,6 @@
// @flow
class C {
m(x: number): string {
return 'a';
}
}
@@ -0,0 +1,10 @@
{
"plugins": [
"external-helpers",
"transform-function-name",
["transform-classes", { "loose": true }],
"transform-spread",
"transform-block-scoping",
"syntax-flow"
]
}
@@ -0,0 +1,14 @@
// @flow
var C = /*#__PURE__*/function () {
"use strict";

function C() {}

var _proto = C.prototype;

_proto.m = function m(x: number): string {
return 'a';
};

return C;
}();
@@ -0,0 +1,5 @@
class Test {
a() {}
static b() {}
c() {}
}
@@ -0,0 +1,17 @@
var Test = /*#__PURE__*/function () {
"use strict";

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

var _proto = Test.prototype;

_proto.a = function a() {};

Test.b = function b() {};

_proto.c = function c() {};

return Test;
}();
@@ -0,0 +1,12 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
"transform-function-name",
"transform-classes",
["transform-spread", { "loose": true }],
"transform-block-scoping"
],
"assumptions": {
"setClassMethods": true
}
}
@@ -0,0 +1,7 @@
"use strict";

class Foo {
method() {

}
}
@@ -0,0 +1,13 @@
"use strict";

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

var _proto = Foo.prototype;

_proto.method = function method() {};

return Foo;
}();

0 comments on commit a4f47b8

Please sign in to comment.