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

Implement setClassMethods assumption #12407

Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -333,6 +333,7 @@ export type NestingPath = RootPath | OverridesPath | EnvPath;

export const assumptionsNames = new Set<string>([
"mutableTemplateObject",
"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 = api.assumption("setClassMethods") ?? options.loose;

// 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;
}();