Skip to content

Commit

Permalink
Fix tests on node 4
Browse files Browse the repository at this point in the history
I DON'T KNOW WHY IT WORKS.
  • Loading branch information
nicolo-ribaudo committed Mar 11, 2018
1 parent d06f831 commit 36e9f2d
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 18 deletions.
Expand Up @@ -101,11 +101,11 @@ export function runCodeInTestContext(
// Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment.
// Note: This isn't doing .call(module.exports, ...) because some of our tests currently
// rely on 'this === global'.
const src = `(function(exports, require, module, __filename, __dirname, opts) {${code}\n});`;
const src = `(function(exports, require, module, __filename, __dirname, opts, console) {${code}\n});`;
return vm.runInContext(src, testContext, {
filename,
displayErrors: true,
})(module.exports, req, module, filename, dirname, opts);
})(module.exports, req, module, filename, dirname, opts, console);
}

function wrapPackagesArray(type, names, optionsDir) {
Expand Down
15 changes: 12 additions & 3 deletions packages/babel-helpers/src/helpers.js
Expand Up @@ -447,8 +447,15 @@ helpers.inheritsLoose = () => template.program.ast`
helpers.wrapNativeSuper = () => template.program.ast`
var _gPO = Object.getPrototypeOf || function _gPO(o) { return o.__proto__ };
var _sPO = Object.setPrototypeOf || function _sPO(o, p) { o.__proto__ = p; return o };
var _construct = (typeof Reflect === "object" && Reflect.construct) ||
function _construct(Parent, args, Class) {
// Here we don't use (Reflect.construct || function _construct(...) {})
// because, when running the exec tests in node 4, there is a buggy
// Reflect.construct implementation which ignores the third parameter.
// I don't know why, since Reflect shouldn't exists on node 4 (and it
// doesn't exist if you try to run the compiled code, it only exists in exec
// tests).
// @nicolo-ribaudo
function _construct(Parent, args, Class) {
var Constructor, a = [null];
a.push.apply(a, args);
Constructor = Parent.bind.apply(Parent, a);
Expand All @@ -458,6 +465,8 @@ helpers.wrapNativeSuper = () => template.program.ast`
var _cache = typeof Map === "function" && new Map();
export default function _wrapNativeSuper(Class) {
if (Class === null) return null;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
Expand All @@ -468,7 +477,7 @@ helpers.wrapNativeSuper = () => template.program.ast`
}
function Wrapper() {
return _construct(Class, arguments, _gPO(this).constructor)
return _construct(Class, arguments, _gPO(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
Expand Down
Expand Up @@ -4,11 +4,13 @@ var _gPO = Object.getPrototypeOf || function _gPO(o) { return o.__proto__; };

var _sPO = Object.setPrototypeOf || function _sPO(o, p) { o.__proto__ = p; return o; };

var _construct = typeof Reflect === "object" && Reflect.construct || function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); };
function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); }

;

var _cache = typeof Map === "function" && new Map();

function _wrapNativeSuper(Class) { if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _gPO(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _sPO(Wrapper, Class); }
function _wrapNativeSuper(Class) { if (Class === null) return null; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _gPO(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _sPO(Wrapper, Class); }

var List =
/*#__PURE__*/
Expand Down
@@ -0,0 +1,9 @@
var env = {
Array: null,
};

// Wee need to use "with" to avoid leaking the modified Array to other tests.
with (env) {
class List extends Array {}
assert.equal(List.prototype.__proto__, null);
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-classes", "transform-block-scoping"]
}
Expand Up @@ -10,11 +10,13 @@ var _gPO = Object.getPrototypeOf || function _gPO(o) { return o.__proto__; };

var _sPO = Object.setPrototypeOf || function _sPO(o, p) { o.__proto__ = p; return o; };

var _construct = typeof Reflect === "object" && Reflect.construct || function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); };
function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); }

;

var _cache = typeof Map === "function" && new Map();

function _wrapNativeSuper(Class) { if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _gPO(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _sPO(Wrapper, Class); }
function _wrapNativeSuper(Class) { if (Class === null) return null; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _gPO(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _sPO(Wrapper, Class); }

var List =
/*#__PURE__*/
Expand Down
@@ -1,10 +1,15 @@
var called = false;

this.Array = function _Array() {
called = true;
}
var env = {
Array: function Array() {
called = true;
}
};

class List extends Array {};
new List();
// Wee need to use "with" to avoid leaking the modified Array to other tests.
with (env) {
class List extends Array {};
new List();

assert.equal(called, true);
assert.equal(called, true);
}
@@ -1,3 +1,3 @@
{
"plugins": ["transform-classes"]
"plugins": ["transform-classes","transform-block-scoping"]
}
Expand Up @@ -4,3 +4,5 @@ class MyDate extends Date {
}
}
let myDate = new MyDate();

assert.equal(myDate.toString, Date.prototype.toString);
Expand Up @@ -14,11 +14,13 @@ var _gPO = Object.getPrototypeOf || function _gPO(o) { return o.__proto__; };

var _sPO = Object.setPrototypeOf || function _sPO(o, p) { o.__proto__ = p; return o; };

var _construct = (typeof Reflect === "undefined" ? "undefined" : _typeof(Reflect)) === "object" && Reflect.construct || function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); };
function _construct(Parent, args, Class) { var Constructor, a = [null]; a.push.apply(a, args); Constructor = Parent.bind.apply(Parent, a); return _sPO(new Constructor(), Class.prototype); }

;

var _cache = typeof Map === "function" && new Map();

function _wrapNativeSuper(Class) { if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _gPO(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _sPO(Wrapper, Class); }
function _wrapNativeSuper(Class) { if (Class === null) return null; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _gPO(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _sPO(Wrapper, Class); }

var MyDate =
/*#__PURE__*/
Expand Down

0 comments on commit 36e9f2d

Please sign in to comment.