Merge pull request #4850 from babel/removeClassCallCheck-option

Remove ClassCallCheck, possibleConstructorReturn in loose mode
This commit is contained in:
Henry Zhu
2017-06-26 15:24:11 -04:00
committed by GitHub
22 changed files with 127 additions and 63 deletions

View File

@@ -377,6 +377,14 @@ helpers.inherits = template(`
})
`);
helpers.inheritsLoose = template(`
(function (subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype);
subClass.prototype.constructor = subClass;
subClass.__proto__ = superClass;
})
`);
helpers.instanceof = template(`
(function (left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {

View File

@@ -127,13 +127,15 @@ export default class ClassTransformer {
//
this.buildBody();
// make sure this class isn't directly called
constructorBody.body.unshift(t.expressionStatement(t.callExpression(
file.addHelper("classCallCheck"), [
t.thisExpression(),
this.classRef,
]
)));
// make sure this class isn't directly called (with A() instead new A())
if (!this.isLoose) {
constructorBody.body.unshift(t.expressionStatement(t.callExpression(
file.addHelper("classCallCheck"), [
t.thisExpression(),
this.classRef,
]
)));
}
body = body.concat(this.staticPropBody.map((fn) => fn(this.classRef)));
@@ -374,10 +376,16 @@ export default class ClassTransformer {
);
}
let call = t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[t.thisExpression(), bareSuperNode]
);
let call;
if (this.isLoose) {
call = t.logicalExpression("||", bareSuperNode, t.thisExpression());
} else {
call = t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[t.thisExpression(), bareSuperNode]
);
}
const bareSuperAfter = this.bareSuperAfter.map((fn) => fn(thisRef));
@@ -446,10 +454,18 @@ export default class ClassTransformer {
thisPath.replaceWith(thisRef);
}
const wrapReturn = (returnArg) => t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[thisRef].concat(returnArg || [])
);
let wrapReturn;
if (this.isLoose) {
wrapReturn = (returnArg) => {
return returnArg ? t.logicalExpression("||", returnArg, thisRef) : thisRef;
};
} else {
wrapReturn = (returnArg) => t.callExpression(
this.file.addHelper("possibleConstructorReturn"),
[thisRef].concat(returnArg || [])
);
}
// if we have a return as the last node in the body then we've already caught that
// return
@@ -546,7 +562,7 @@ export default class ClassTransformer {
// any properties can be assigned to the prototype.
this.pushedInherits = true;
this.body.unshift(t.expressionStatement(t.callExpression(
this.file.addHelper("inherits"),
this.isLoose ? this.file.addHelper("inheritsLoose") : this.file.addHelper("inherits"),
[this.classRef, this.superName]
)));
}

View File

@@ -0,0 +1 @@
let A = function A() {};

View File

@@ -0,0 +1,7 @@
{
"plugins": [
["transform-es2015-classes", {
"loose": true
}]
]
}

View File

@@ -0,0 +1,11 @@
class A {
constructor() {
console.log('a');
}
}
class B {
b() {
console.log('b');
}
}

View File

@@ -0,0 +1,13 @@
let A = function A() {
console.log('a');
};
let B = function () {
function B() {}
B.prototype.b = function b() {
console.log('b');
};
return B;
}();

View File

@@ -0,0 +1,8 @@
class B {}
class A extends B {
constructor(track) {
if (track !== undefined) super(track);
else super();
}
}

View File

@@ -0,0 +1,18 @@
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
let B = function B() {};
let A = function (_B) {
_inheritsLoose(A, _B);
function A(track) {
if (track !== undefined) {
var _this = _B.call(this, track) || this;
} else {
var _this = _B.call(this) || this;
}
return _this;
}
return A;
}(B);

View File

@@ -1,20 +1,18 @@
var Test = function (_Foo) {
babelHelpers.inherits(Test, _Foo);
babelHelpers.inheritsLoose(Test, _Foo);
function Test() {
var _Foo$prototype$test, _Foo$prototype$test2;
babelHelpers.classCallCheck(this, Test);
woops.super.test();
var _this = babelHelpers.possibleConstructorReturn(this, _Foo.call(this));
var _this = _Foo.call(this) || this;
_Foo.prototype.test.call(_this);
var _this = babelHelpers.possibleConstructorReturn(this, _Foo.apply(this, arguments));
var _this = _Foo.apply(this, arguments) || this;
var _this = babelHelpers.possibleConstructorReturn(this, _Foo.call.apply(_Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))));
var _this = _Foo.call.apply(_Foo, [this, "test"].concat(Array.prototype.slice.call(arguments))) || this;
(_Foo$prototype$test = _Foo.prototype.test).call.apply(_Foo$prototype$test, [_this].concat(Array.prototype.slice.call(arguments)));
(_Foo$prototype$test2 = _Foo.prototype.test).call.apply(_Foo$prototype$test2, [_this, "test"].concat(Array.prototype.slice.call(arguments)));

View File

@@ -1,10 +1,8 @@
var Test = function (_Foo) {
babelHelpers.inherits(Test, _Foo);
babelHelpers.inheritsLoose(Test, _Foo);
function Test() {
babelHelpers.classCallCheck(this, Test);
var _this = babelHelpers.possibleConstructorReturn(this, _Foo.call(this));
var _this = _Foo.call(this) || this;
_Foo.prototype.test;
_Foo.prototype.test.whatever;

View File

@@ -1,10 +1,8 @@
var Test = function (_Foo) {
babelHelpers.inherits(Test, _Foo);
babelHelpers.inheritsLoose(Test, _Foo);
function Test() {
babelHelpers.classCallCheck(this, Test);
var _this = babelHelpers.possibleConstructorReturn(this, _Foo.call(this));
var _this = _Foo.call(this) || this;
_Foo.prototype.test.whatever();
_Foo.prototype.test.call(_this);

View File

@@ -6,8 +6,6 @@ var x = function () {
};
function x() {
babelHelpers.classCallCheck(this, x);
4;
5;
6;

View File

@@ -1,7 +1,5 @@
var Foo = function () {
function Foo() {
babelHelpers.classCallCheck(this, Foo);
}
function Foo() {}
Foo.prototype["bar"] = function bar() {};

View File

@@ -1,8 +1,6 @@
// @flow
var C = function () {
function C() {
babelHelpers.classCallCheck(this, C);
}
function C() {}
C.prototype.m = function m(x: number): string {
return 'a';

View File

@@ -1,3 +1,9 @@
{
"plugins": ["external-helpers", "transform-es2015-function-name", ["transform-es2015-classes", { "loose": true }], "transform-es2015-spread", "transform-es2015-block-scoping"]
"plugins": [
"external-helpers",
"transform-es2015-function-name",
["transform-es2015-classes", { "loose": true }],
["transform-es2015-spread", { "loose": true }],
"transform-es2015-block-scoping"
]
}

View File

@@ -1,20 +1,18 @@
var BaseController = function (_Chaplin$Controller) {
babelHelpers.inherits(BaseController, _Chaplin$Controller);
babelHelpers.inheritsLoose(BaseController, _Chaplin$Controller);
function BaseController() {
babelHelpers.classCallCheck(this, BaseController);
return babelHelpers.possibleConstructorReturn(this, _Chaplin$Controller.apply(this, arguments));
return _Chaplin$Controller.apply(this, arguments) || this;
}
return BaseController;
}(Chaplin.Controller);
var BaseController2 = function (_Chaplin$Controller$A) {
babelHelpers.inherits(BaseController2, _Chaplin$Controller$A);
babelHelpers.inheritsLoose(BaseController2, _Chaplin$Controller$A);
function BaseController2() {
babelHelpers.classCallCheck(this, BaseController2);
return babelHelpers.possibleConstructorReturn(this, _Chaplin$Controller$A.apply(this, arguments));
return _Chaplin$Controller$A.apply(this, arguments) || this;
}
return BaseController2;

View File

@@ -1,9 +1,8 @@
var Test = function (_Foo) {
babelHelpers.inherits(Test, _Foo);
babelHelpers.inheritsLoose(Test, _Foo);
function Test() {
babelHelpers.classCallCheck(this, Test);
return babelHelpers.possibleConstructorReturn(this, _Foo.apply(this, arguments));
return _Foo.apply(this, arguments) || this;
}
return Test;

View File

@@ -1,5 +1,3 @@
var Test = function Test() {
babelHelpers.classCallCheck(this, Test);
Function.prototype.hasOwnProperty.call(this, "test");
};

View File

@@ -1,11 +1,7 @@
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Example = function () {
function Example() {
_classCallCheck(this, Example);
}
function Example() {}
Example.prototype.test1 = async function test1() {
await Promise.resolve(2);

View File

@@ -2,13 +2,11 @@
// @flow
var C = function () {
function C() {
babelHelpers.classCallCheck(this, C);
}
function C() {}
C.prototype.m = function m(x /*: number*/) /*: string*/ {
return 'a';
};
return C;
}();
}();

View File

@@ -1,13 +1,11 @@
"use strict";
var C = function () {
function C() {
babelHelpers.classCallCheck(this, C);
}
function C() {}
C.prototype.m = function m(x) {
return 'a';
};
return C;
}();
}();