forced closure on super classes - fixes #339

This commit is contained in:
Sebastian McKenzie
2015-01-02 01:59:42 +11:00
parent 9529f93690
commit b1d1909c64
7 changed files with 95 additions and 64 deletions

View File

@@ -21,7 +21,7 @@ exports.ClassDeclaration = function (node, parent, file, scope) {
});
return t.assignmentExpression("=", node.id, newNode);
} else {
// likely has a PrivateDeclaration etc
// has a super class or PrivateDeclaration etc
return t.variableDeclaration("let", [
t.variableDeclarator(node.id, newNode)
]);
@@ -85,7 +85,8 @@ Class.prototype.run = function () {
//
if (superName && t.isDynamic(superName)) {
if (superName) {
this.closure = true;
// so we're only evaluating it once
var superRefName = "super";
if (className) superRefName = className.name + "Super";
@@ -95,13 +96,8 @@ Class.prototype.run = function () {
t.variableDeclarator(superRef, superName)
]));
superName = superRef;
}
this.superName = superName;
//
if (superName) {
this.superName = superName;
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("inherits"), [className, superName])));
}

View File

@@ -13,29 +13,34 @@ var _inherits = function (child, parent) {
if (parent) child.__proto__ = parent;
};
var Test = function Test() {
woops["super"].test();
Foo.call(this);
Foo.prototype.test.call(this);
foob(Foo);
var Test = (function () {
var _TestSuper = Foo;
var Test = function Test() {
woops["super"].test();
_TestSuper.call(this);
_TestSuper.prototype.test.call(this);
foob(_TestSuper);
Foo.call.apply(Foo, [this].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments)));
_TestSuper.call.apply(_TestSuper, [this].concat(_slice.call(arguments)));
_TestSuper.call.apply(_TestSuper, [this, "test"].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments)));
};
_TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this, "test"].concat(_slice.call(arguments)));
};
_inherits(Test, Foo);
_inherits(Test, _TestSuper);
Test.prototype.test = function () {
Foo.prototype.test.call(this);
Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments)));
Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
};
Test.prototype.test = function () {
_TestSuper.prototype.test.call(this);
_TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this, "test"].concat(_slice.call(arguments)));
};
Test.foo = function () {
Foo.foo.call(this);
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
};
Test.foo = function () {
_TestSuper.foo.call(this);
_TestSuper.foo.call.apply(_TestSuper.foo, [this].concat(_slice.call(arguments)));
_TestSuper.foo.call.apply(_TestSuper.foo, [this, "test"].concat(_slice.call(arguments)));
};
return Test;
})();

View File

@@ -12,9 +12,14 @@ var _inherits = function (child, parent) {
if (parent) child.__proto__ = parent;
};
var Test = function Test() {
Foo.prototype.test;
Foo.prototype.test.whatever;
};
var Test = (function () {
var _TestSuper = Foo;
var Test = function Test() {
_TestSuper.prototype.test;
_TestSuper.prototype.test.whatever;
};
_inherits(Test, Foo);
_inherits(Test, _TestSuper);
return Test;
})();

View File

@@ -12,13 +12,18 @@ var _inherits = function (child, parent) {
if (parent) child.__proto__ = parent;
};
var Test = function Test() {
Foo.prototype.test.whatever();
Foo.prototype.test.call(this);
};
var Test = (function () {
var _TestSuper = Foo;
var Test = function Test() {
_TestSuper.prototype.test.whatever();
_TestSuper.prototype.test.call(this);
};
_inherits(Test, Foo);
_inherits(Test, _TestSuper);
Test.test = function () {
return Foo.wow.call(this);
};
Test.test = function () {
return _TestSuper.wow.call(this);
};
return Test;
})();

View File

@@ -16,8 +16,13 @@ var Test = function Test() {
this.state = "test";
};
var Foo = function Foo() {
this.state = "test";
};
var Foo = (function () {
var _FooSuper = Bar;
var Foo = function Foo() {
this.state = "test";
};
_inherits(Foo, Bar);
_inherits(Foo, _FooSuper);
return Foo;
})();

View File

@@ -12,18 +12,28 @@ var _inherits = function (child, parent) {
if (parent) child.__proto__ = parent;
};
var BaseController = function BaseController() {
if (Chaplin.Controller) {
Chaplin.Controller.apply(this, arguments);
}
};
var BaseController = (function () {
var _BaseControllerSuper = Chaplin.Controller;
var BaseController = function BaseController() {
if (_BaseControllerSuper) {
_BaseControllerSuper.apply(this, arguments);
}
};
_inherits(BaseController, Chaplin.Controller);
_inherits(BaseController, _BaseControllerSuper);
var BaseController2 = function BaseController2() {
if (Chaplin.Controller.Another) {
Chaplin.Controller.Another.apply(this, arguments);
}
};
return BaseController;
})();
_inherits(BaseController2, Chaplin.Controller.Another);
var BaseController2 = (function () {
var _BaseController2Super = Chaplin.Controller.Another;
var BaseController2 = function BaseController2() {
if (_BaseController2Super) {
_BaseController2Super.apply(this, arguments);
}
};
_inherits(BaseController2, _BaseController2Super);
return BaseController2;
})();

View File

@@ -12,10 +12,15 @@ var _inherits = function (child, parent) {
if (parent) child.__proto__ = parent;
};
var Test = function Test() {
if (Foo) {
Foo.apply(this, arguments);
}
};
var Test = (function () {
var _TestSuper = Foo;
var Test = function Test() {
if (_TestSuper) {
_TestSuper.apply(this, arguments);
}
};
_inherits(Test, Foo);
_inherits(Test, _TestSuper);
return Test;
})();