Permalink
Browse files

remove native super inheritance from classes - fixes #1424

  • Loading branch information...
Sebastian McKenzie
Sebastian McKenzie committed May 3, 2015
1 parent 0717ead commit 3878bd812c73bdd18b1011be59515dad985940fd
@@ -96,10 +96,6 @@ var verifyConstructorVisitor = traverse.explode({
if (state.hasSuper && !state.hasBareSuper) {
throw this.errorWithNode("'this' is not allowed before super()");
}

if (state.isNativeSuper) {
return state.nativeSuperRef;
}
}
}
});
@@ -154,15 +150,6 @@ class ClassTransformer {

//

var superClass = this.node.superClass;
this.isNativeSuper = superClass && t.isIdentifier(superClass) && t.NATIVE_TYPE_NAMES.indexOf(superClass.name) >= 0;

if (this.isNativeSuper) {
this.nativeSuperRef = this.scope.generateUidIdentifier("this");
}

//

var body = this.body;

//
@@ -234,12 +221,6 @@ class ClassTransformer {
}
}

if (this.isNativeSuper) {
// we've determined this is inheriting from a native class so return the constructed
// instance
constructorBody.body.push(t.returnStatement(this.nativeSuperRef));
}

if (this.className) {
// named class with only a constructor
if (body.length === 1) return t.toExpression(body[0]);
@@ -344,9 +325,7 @@ class ClassTransformer {
if (!this.hasConstructor && this.hasSuper) {
var helperName = "class-super-constructor-call";
if (this.isLoose) helperName += "-loose";
if (this.isNativeSuper) helperName = "class-super-native-constructor-call";
constructorBody.body.push(util.template(helperName, {
NATIVE_REF: this.nativeSuperRef,
CLASS_NAME: this.classRef,
SUPER_NAME: this.superName
}, true));
@@ -475,12 +454,10 @@ class ClassTransformer {

verifyConstructor(path: TraversalPath) {
var state = {
nativeSuperRef: this.nativeSuperRef,
isNativeSuper: this.isNativeSuper,
hasBareSuper: false,
bareSuper: null,
hasSuper: this.hasSuper,
file: this.file
hasBareSuper: false,
bareSuper: null,
hasSuper: this.hasSuper,
file: this.file
};

path.get("value").traverse(verifyConstructorVisitor, state);
@@ -490,22 +467,7 @@ class ClassTransformer {
if (!state.hasBareSuper && this.hasSuper) {
throw path.errorWithNode("Derived constructor must call super()");
}

if (this.isNativeSuper && this.bareSuper) {
this.bareSuper.replaceWithMultiple([
t.variableDeclaration("var", [
t.variableDeclarator(this.nativeSuperRef, t.newExpression(this.superName, this.bareSuper.node.arguments))
]),

t.expressionStatement(t.assignmentExpression(
"=",
t.memberExpression(this.nativeSuperRef, t.identifier("__proto__")),
t.memberExpression(this.classRef, t.identifier("prototype"))
)),
t.expressionStatement(this.nativeSuperRef)
]);
}
}
}

/**
* Push a method to its respective mutatorMap.
@@ -605,10 +567,6 @@ class ClassTransformer {
fnPath.scope.rename(this.classRef.name);
}

if (this.isNativeSuper) {
fnPath.traverse(constructorVisitor, this.nativeSuperRef);
}

var construct = this.constructor;
var fn = method.value;

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

2 comments on commit 3878bd8

@paglias

This comment has been minimized.

Copy link

paglias replied Nov 22, 2015

this is also affecting extending from error?

@loganfsmyth

This comment has been minimized.

Copy link
Member

loganfsmyth replied Nov 22, 2015

@paglias Happy to answer your question, but in the future, please see the README:

For questions and support please visit the Slack community or StackOverflow.

The answer to your question is essentially yes. Extending builtin constructors like Error is not supported in that it will not work like you expect.

Please sign in to comment.