remove IIFE on class declarations #288

This commit is contained in:
Sebastian McKenzie
2014-12-13 17:33:35 +11:00
parent 525169460a
commit 833a4900da
19 changed files with 180 additions and 247 deletions

View File

@@ -1,7 +0,0 @@
(function () {
var CLASS_NAME = function () {
};
return CLASS_NAME;
})()

View File

@@ -4,24 +4,11 @@ var t = require("../../types");
var _ = require("lodash");
exports.ClassDeclaration = function (node, parent, file, scope) {
var built = new Class(node, file, scope).run();
var declar = t.variableDeclaration("let", [
t.variableDeclarator(node.id, built)
]);
t.inheritsComments(declar, node);
return declar;
return new Class(node, file, scope, true).run();
};
exports.ClassExpression = function (node, parent, file, scope) {
return new Class(node, file, scope).run();
};
var getMemberExpressionObject = function (node) {
while (t.isMemberExpression(node)) {
node = node.object;
}
return node;
return new Class(node, file, scope, false).run();
};
/**
@@ -30,12 +17,14 @@ var getMemberExpressionObject = function (node) {
* @param {Node} node
* @param {File} file
* @param {Scope} scope
* @param {Boolean} isStatement
*/
function Class(node, file, scope) {
this.scope = scope;
this.node = node;
this.file = file;
function Class(node, file, scope, isStatement) {
this.isStatement = isStatement;
this.scope = scope;
this.node = node;
this.file = file;
this.instanceMutatorMap = {};
this.staticMutatorMap = {};
@@ -51,50 +40,59 @@ function Class(node, file, scope) {
*/
Class.prototype.run = function () {
var superClassArgument = this.superName;
var superClassCallee = this.superName;
var superName = this.superName;
var className = this.className;
var file = this.file;
var superName = this.superName;
var className = this.className;
var file = this.file;
if (superName) {
if (t.isMemberExpression(superName)) {
superClassArgument = superClassCallee = getMemberExpressionObject(superName);
} else if (!t.isIdentifier(superName)) {
superClassArgument = superName;
superClassCallee = superName = file.generateUidIdentifier("ref", this.scope);
}
//
var body = this.body = [];
var constructor = t.functionExpression(null, [], t.blockStatement([]));
if (this.node.id) constructor.id = className;
this.constructor = constructor;
body.push(t.variableDeclaration("let", [
t.variableDeclarator(className, constructor)
]));
//
if (superName && t.isDynamic(superName)) {
// so we're only evaluating it once
var superRefName = "super";
if (className) superRefName = className.name + "Super";
var superRef = file.generateUidIdentifier(superRefName, this.scope);
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(superRef, superName)
]));
superName = superRef;
}
this.superName = superName;
var container = util.template("class", {
CLASS_NAME: className
});
var block = container.callee.expression.body;
var body = this.body = block.body;
var constructor = this.constructor = body[0].declarations[0].init;
if (this.node.id) constructor.id = className;
var returnStatement = body.pop();
//
if (superName) {
body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName])));
container.arguments.push(superClassArgument);
container.callee.expression.params.push(superClassCallee);
}
this.buildBody();
if (body.length === 1) {
// only a constructor so no need for a closure container
return constructor;
if (this.isStatement) {
return body;
} else {
body.push(returnStatement);
return container;
if (body.length === 1) {
// only a constructor so no need for a closure container
return constructor;
} else {
body.push(t.returnStatement(className));
return t.callExpression(
t.functionExpression(null, [], t.blockStatement(body)),
[]
);
}
}
};

View File

@@ -13,33 +13,29 @@ var _extends = function (child, parent) {
child.__proto__ = parent;
};
var Test = (function (Foo) {
var Test = function Test() {
woops["super"].test();
Foo.call(this);
Foo.prototype.test.call(this);
foob(Foo);
var Test = function Test() {
woops["super"].test();
Foo.call(this);
Foo.prototype.test.call(this);
foob(Foo);
Foo.call.apply(Foo, [this].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [this].concat(_slice.call(arguments)));
Foo.call.apply(Foo, [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)));
};
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)));
};
_extends(Test, Foo);
_extends(Test, Foo);
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 () {
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.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)));
};
return Test;
})(Foo);
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)));
};

View File

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

View File

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

View File

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

View File

@@ -5,20 +5,16 @@ var _classProps = function (child, staticProps, instanceProps) {
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = (function () {
var Test = function Test() {};
var Test = function Test() {};
_classProps(Test, null, {
test: {
get: function () {
return 5 + 5;
},
set: function (val) {
this._test = val;
},
enumerable: true
}
});
return Test;
})();
_classProps(Test, null, {
test: {
get: function () {
return 5 + 5;
},
set: function (val) {
this._test = val;
},
enumerable: true
}
});

View File

@@ -5,17 +5,13 @@ var _classProps = function (child, staticProps, instanceProps) {
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = (function () {
var Test = function Test() {};
var Test = function Test() {};
_classProps(Test, null, {
test: {
get: function () {
return 5 + 5;
},
enumerable: true
}
});
return Test;
})();
_classProps(Test, null, {
test: {
get: function () {
return 5 + 5;
},
enumerable: true
}
});

View File

@@ -1,11 +1,7 @@
"use strict";
var Test = (function () {
var Test = function Test() {};
var Test = function Test() {};
Test.prototype.test = function () {
return 5 + 5;
};
return Test;
})();
Test.prototype.test = function () {
return 5 + 5;
};

View File

@@ -5,17 +5,13 @@ var _classProps = function (child, staticProps, instanceProps) {
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = (function () {
var Test = function Test() {};
var Test = function Test() {};
_classProps(Test, null, {
test: {
set: function (val) {
this._test = val;
},
enumerable: true
}
});
return Test;
})();
_classProps(Test, null, {
test: {
set: function (val) {
this._test = val;
},
enumerable: true
}
});

View File

@@ -5,18 +5,14 @@ var _classProps = function (child, staticProps, instanceProps) {
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var A = (function () {
var A = function A() {};
var A = function A() {};
A.a = function () {};
A.a = function () {};
_classProps(A, {
b: {
get: function () {},
set: function (b) {},
enumerable: true
}
});
return A;
})();
_classProps(A, {
b: {
get: function () {},
set: function (b) {},
enumerable: true
}
});

View File

@@ -12,22 +12,14 @@ var _extends = function (child, parent) {
child.__proto__ = parent;
};
var BaseController = (function (Chaplin) {
var BaseController = function BaseController() {
Chaplin.Controller.apply(this, arguments);
};
var BaseController = function BaseController() {
Chaplin.Controller.apply(this, arguments);
};
_extends(BaseController, Chaplin.Controller);
_extends(BaseController, Chaplin.Controller);
return BaseController;
})(Chaplin);
var BaseController2 = function BaseController2() {
Chaplin.Controller.Another.apply(this, arguments);
};
var BaseController2 = (function (Chaplin) {
var BaseController2 = function BaseController2() {
Chaplin.Controller.Another.apply(this, arguments);
};
_extends(BaseController2, Chaplin.Controller.Another);
return BaseController2;
})(Chaplin);
_extends(BaseController2, Chaplin.Controller.Another);

View File

@@ -12,12 +12,10 @@ var _extends = function (child, parent) {
child.__proto__ = parent;
};
var Q = (function (_ref) {
var Q = function Q() {
_ref.apply(this, arguments);
};
var _QSuper = function () {};
_extends(Q, _ref);
var Q = function Q() {
_QSuper.apply(this, arguments);
};
return Q;
})(function () {});
_extends(Q, _QSuper);

View File

@@ -12,12 +12,8 @@ var _extends = function (child, parent) {
child.__proto__ = parent;
};
var Test = (function (Foo) {
var Test = function Test() {
Foo.apply(this, arguments);
};
var Test = function Test() {
Foo.apply(this, arguments);
};
_extends(Test, Foo);
return Test;
})(Foo);
_extends(Test, Foo);

View File

@@ -1,4 +1,4 @@
"use strict";
var string = "foo\ud83d\udca9bar";
var string = "foo💩bar";
var match = string.match(/foo((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uDC00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF]))bar/);

View File

@@ -1,4 +1,4 @@
"use strict";
var string = "foo\ud83d\udca9bar";
var string = "foo💩bar";
var match = string.match(/foo(.)bar/);

View File

@@ -5,32 +5,28 @@ var _classProps = function (child, staticProps, instanceProps) {
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Foo = (function () {
var Foo = function Foo() {};
var Foo = function Foo() {};
_classProps(Foo, null, (function (_ref) {
_ref[bar] = {
get: function () {
if (this._memoDone) return this._memo;
this._memoDone = true;
return this._memo = complex();
},
enumerable: true
};
return _ref;
})({
bar: {
get: function () {
if (this._barDone) return this._bar;
this._barDone = true;
return this._bar = complex();
},
enumerable: true
}
}));
return Foo;
})();
_classProps(Foo, null, (function (_ref) {
_ref[bar] = {
get: function () {
if (this._memoDone) return this._memo;
this._memoDone = true;
return this._memo = complex();
},
enumerable: true
};
return _ref;
})({
bar: {
get: function () {
if (this._barDone) return this._bar;
this._barDone = true;
return this._bar = complex();
},
enumerable: true
}
}));
var foo = (function (_foo) {
_foo[bar] = function () {

View File

@@ -5,20 +5,16 @@ var _classProps = function (child, staticProps, instanceProps) {
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = (function () {
var Test = function Test() {};
var Test = function Test() {};
_classProps(Test, null, {
bar: {
get: function () {
throw new Error("wow");
},
enumerable: true
}
});
return Test;
})();
_classProps(Test, null, {
bar: {
get: function () {
throw new Error("wow");
},
enumerable: true
}
});
var test = new Test();
test.bar;

View File

@@ -4,7 +4,7 @@
"column": 10
},
"generated": {
"line": 14,
"column": 15
"line": 13,
"column": 13
}
}]