Fix default class super inheritance (#7732)

Fixes #7683.
This commit is contained in:
Justin Ridgewell 2018-04-14 15:15:40 -04:00 committed by GitHub
parent 858a2c74e7
commit 0ee9a4e612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 118 additions and 4 deletions

View File

@ -382,7 +382,6 @@ export default function transformClass(
let guaranteedSuperBeforeFinish = !!classState.bareSupers.size; let guaranteedSuperBeforeFinish = !!classState.bareSupers.size;
const superRef = classState.superName || t.identifier("Function");
let thisRef = function() { let thisRef = function() {
const ref = path.scope.generateDeclaredUidIdentifier("this"); const ref = path.scope.generateDeclaredUidIdentifier("this");
thisRef = () => t.cloneNode(ref); thisRef = () => t.cloneNode(ref);
@ -390,7 +389,7 @@ export default function transformClass(
}; };
for (const bareSuper of classState.bareSupers) { for (const bareSuper of classState.bareSupers) {
wrapSuperCall(bareSuper, superRef, thisRef, body); wrapSuperCall(bareSuper, classState.superName, thisRef, body);
if (guaranteedSuperBeforeFinish) { if (guaranteedSuperBeforeFinish) {
bareSuper.find(function(parentPath) { bareSuper.find(function(parentPath) {
@ -638,7 +637,7 @@ export default function transformClass(
classRef: classState.node.id classRef: classState.node.id
? t.identifier(classState.node.id.name) ? t.identifier(classState.node.id.name)
: classState.scope.generateUidIdentifier("class"), : classState.scope.generateUidIdentifier("class"),
superName: classState.node.superClass || t.identifier("Function"), superName: classState.node.superClass,
isDerived: !!classState.node.superClass, isDerived: !!classState.node.superClass,
constructorBody: t.blockStatement([]), constructorBody: t.blockStatement([]),
}); });

View File

@ -0,0 +1,17 @@
class Test {
constructor() {
return super.constructor;
}
static test() {
return super.constructor;
}
}
// Instances
expect(Object.getPrototypeOf(Test.prototype)).toBe(Object.prototype);
expect(new Test()).toBe(Object);
// Static
expect(Object.getPrototypeOf(Test)).toBe(Function.prototype);
expect(Test.test()).toBe(Function);

View File

@ -0,0 +1,17 @@
class Test {
constructor() {
return super.constructor;
}
static test() {
return super.constructor;
}
}
// Instances
expect(Object.getPrototypeOf(Test.prototype)).toBe(Object.prototype);
expect(new Test()).toBe(Object);
// Static
expect(Object.getPrototypeOf(Test)).toBe(Function.prototype);
expect(Test.test()).toBe(Function);

View File

@ -0,0 +1,22 @@
var Test =
/*#__PURE__*/
function () {
"use strict";
function Test() {
return Object.prototype.constructor;
}
Test.test = function test() {
return Function.prototype.constructor;
};
return Test;
}(); // Instances
expect(Object.getPrototypeOf(Test.prototype)).toBe(Object.prototype);
expect(new Test()).toBe(Object); // Static
expect(Object.getPrototypeOf(Test)).toBe(Function.prototype);
expect(Test.test()).toBe(Function);

View File

@ -1,5 +1,5 @@
var Test = function Test() { var Test = function Test() {
"use strict"; "use strict";
Function.prototype.hasOwnProperty.call(this, "test"); Object.prototype.hasOwnProperty.call(this, "test");
}; };

View File

@ -0,0 +1,17 @@
class Test {
constructor() {
return super.constructor;
}
static test() {
return super.constructor;
}
}
// Instances
expect(Object.getPrototypeOf(Test.prototype)).toBe(Object.prototype);
expect(new Test()).toBe(Object);
// Static
expect(Object.getPrototypeOf(Test)).toBe(Function.prototype);
expect(Test.test()).toBe(Function);

View File

@ -0,0 +1,17 @@
class Test {
constructor() {
return super.constructor;
}
static test() {
return super.constructor;
}
}
// Instances
expect(Object.getPrototypeOf(Test.prototype)).toBe(Object.prototype);
expect(new Test()).toBe(Object);
// Static
expect(Object.getPrototypeOf(Test)).toBe(Function.prototype);
expect(Test.test()).toBe(Function);

View File

@ -0,0 +1,25 @@
var Test =
/*#__PURE__*/
function () {
"use strict";
function Test() {
babelHelpers.classCallCheck(this, Test);
return babelHelpers.get(babelHelpers.getPrototypeOf(Test.prototype), "constructor", babelHelpers.assertThisInitialized(this));
}
babelHelpers.createClass(Test, null, [{
key: "test",
value: function test() {
return babelHelpers.get(babelHelpers.getPrototypeOf(Test), "constructor", this);
}
}]);
return Test;
}(); // Instances
expect(Object.getPrototypeOf(Test.prototype)).toBe(Object.prototype);
expect(new Test()).toBe(Object); // Static
expect(Object.getPrototypeOf(Test)).toBe(Function.prototype);
expect(Test.test()).toBe(Function);