remove possibleConstructorReturn in loose mode as well

This commit is contained in:
Henry Zhu 2017-06-09 18:12:12 -04:00
parent 4081f3e23e
commit cdca54aed3
22 changed files with 80 additions and 61 deletions

View File

@ -45,7 +45,7 @@ export default function ({ types: t }) {
let Constructor = VanillaTransformer;
if (state.opts.loose) Constructor = LooseTransformer;
path.replaceWith(new Constructor(path, state.file, state.opts).run());
path.replaceWith(new Constructor(path, state.file).run());
if (path.isCallExpression() && path.get("callee").isArrowFunctionExpression()) {
path.get("callee").arrowFunctionToExpression();

View File

@ -66,7 +66,7 @@ const findThisesVisitor = visitors.merge([noMethodVisitor, {
}]);
export default class ClassTransformer {
constructor(path: NodePath, file, opts) {
constructor(path: NodePath, file) {
this.parent = path.parent;
this.scope = path.scope;
this.node = path.node;
@ -86,7 +86,6 @@ export default class ClassTransformer {
this.pushedConstructor = false;
this.pushedInherits = false;
this.isLoose = false;
this.removeClassCallCheck = opts.removeClassCallCheck;
this.superThises = [];
@ -129,14 +128,14 @@ export default class ClassTransformer {
//
this.buildBody();
// make sure this class isn't directly called
if (!this.removeClassCallCheck) {
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)));
@ -378,10 +377,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));
@ -450,10 +455,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

View File

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

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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
let B = function B() {};
let A = function (_B) {
_inherits(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

@ -4,17 +4,15 @@ var Test = function (_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

@ -2,9 +2,7 @@ var Test = function (_Foo) {
babelHelpers.inherits(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

@ -2,9 +2,7 @@ var Test = function (_Foo) {
babelHelpers.inherits(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

@ -2,8 +2,7 @@ var BaseController = function (_Chaplin$Controller) {
babelHelpers.inherits(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;
@ -13,8 +12,7 @@ var BaseController2 = function (_Chaplin$Controller$A) {
babelHelpers.inherits(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

@ -2,8 +2,7 @@ var Test = function (_Foo) {
babelHelpers.inherits(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;
}();
}();