Skip to content

Commit

Permalink
Implement noCallClass assumption
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Feb 4, 2021
1 parent 9251047 commit e473d46
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/babel-core/src/config/validation/options.js
Expand Up @@ -341,6 +341,7 @@ export const assumptionsNames = new Set<string>([
"iterableIsArray",
"mutableTemplateObject",
"newableArrowFunctions",
"noCallClass",
"noDocumentAll",
"objectRestNoSymbols",
"privateFieldsAsProperties",
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-plugin-transform-classes/src/index.js
Expand Up @@ -25,6 +25,7 @@ export default declare((api, options) => {
const constantSuper = api.assumption("constantSuper") ?? options.loose;
const superIsCallableConstructor =
api.assumption("superIsCallableConstructor") ?? options.loose;
const noCallClass = api.assumption("noCallClass") ?? options.loose;

// todo: investigate traversal requeueing
const VISITED = Symbol();
Expand Down Expand Up @@ -67,6 +68,7 @@ export default declare((api, options) => {
setClassMethods,
constantSuper,
superIsCallableConstructor,
noCallClass,
}),
);

Expand Down
Expand Up @@ -15,6 +15,7 @@ type ClassAssumptions = {
setClassMethods: boolean,
constantSuper: boolean,
superIsCallableConstructor: boolean,
noCallClass: boolean,
};

function buildConstructor(classRef, constructorBody, node) {
Expand Down Expand Up @@ -670,7 +671,7 @@ export default function transformClass(
buildBody();

// make sure this class isn't directly called (with A() instead new A())
if (!classState.isLoose) {
if (!assumptions.noCallClass) {
constructorBody.body.unshift(
t.expressionStatement(
t.callExpression(classState.file.addHelper("classCallCheck"), [
Expand Down
@@ -0,0 +1 @@
class A {}
@@ -0,0 +1,3 @@
var A = function A() {
"use strict";
};
@@ -0,0 +1,12 @@
{
"plugins": [
["external-helpers", { "helperVersion": "7.100.0" }],
"transform-function-name",
"transform-classes",
["transform-spread", { "loose": true }],
"transform-block-scoping"
],
"assumptions": {
"noCallClass": true
}
}
@@ -0,0 +1,11 @@
class A {
constructor() {
console.log('a');
}
}

class B {
b() {
console.log('b');
}
}
@@ -0,0 +1,19 @@
var A = function A() {
"use strict";

console.log('a');
};

var B = /*#__PURE__*/function () {
"use strict";

function B() {}

babelHelpers.createClass(B, [{
key: "b",
value: function b() {
console.log('b');
}
}]);
return B;
}();
@@ -0,0 +1,8 @@
class B {}

class A extends B {
constructor(track) {
if (track !== undefined) super(track);
else super();
}
}
@@ -0,0 +1,20 @@
var B = function B() {
"use strict";
};

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

babelHelpers.inherits(A, _B);

var _super = babelHelpers.createSuper(A);

function A(track) {
var _this;

if (track !== undefined) _this = _super.call(this, track);else _this = _super.call(this);
return babelHelpers.possibleConstructorReturn(_this);
}

return A;
}(B);

0 comments on commit e473d46

Please sign in to comment.