diff --git a/lib/6to5/file.js b/lib/6to5/file.js index cbb26b5218..f0e6da985b 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -51,7 +51,9 @@ File.helpers = [ "typeof", "extends", "get", - "set" + "set", + "class-call-check", + "class-super-constructor-call" ]; File.validOptions = [ diff --git a/lib/6to5/transformation/templates/class-call-check.js b/lib/6to5/transformation/templates/class-call-check.js new file mode 100644 index 0000000000..041dd4b753 --- /dev/null +++ b/lib/6to5/transformation/templates/class-call-check.js @@ -0,0 +1,5 @@ +(function (instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } +}); diff --git a/lib/6to5/transformation/templates/class-super-constructor-call.js b/lib/6to5/transformation/templates/class-super-constructor-call.js index 21034b6201..7395389e91 100644 --- a/lib/6to5/transformation/templates/class-super-constructor-call.js +++ b/lib/6to5/transformation/templates/class-super-constructor-call.js @@ -1,3 +1,5 @@ -if (Object.getPrototypeOf(CLASS_NAME) !== null) { - Object.getPrototypeOf(CLASS_NAME).apply(this, arguments); -} +(function (Constructor) { + if (Object.getPrototypeOf(Constructor) !== null) { + Object.getPrototypeOf(Constructor).apply(this, arguments); + } +}); diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index 1b025d72f6..c320f06064 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -67,12 +67,19 @@ Class.prototype.run = function () { var body = this.body = []; + var constructorBody = t.blockStatement([ + t.expressionStatement(t.callExpression(file.addHelper("class-call-check"), [ + t.thisExpression(), + className + ])) + ]); var constructor; + if (this.node.id) { - constructor = t.functionDeclaration(className, [], t.blockStatement([])); + constructor = t.functionDeclaration(className, [], constructorBody); body.push(constructor); } else { - constructor = t.functionExpression(null, [], t.blockStatement([])); + constructor = t.functionExpression(null, [], constructorBody); body.push(t.variableDeclaration("var", [ t.variableDeclarator(className, constructor) ])); @@ -161,13 +168,18 @@ Class.prototype.buildBody = function () { // we have no constructor, we have a super, and the super doesn't appear to be falsy if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) { - var defaultConstructorTemplate = "class-super-constructor-call"; - if (this.isLoose) defaultConstructorTemplate += "-loose"; + var helperName = "class-super-constructor-call"; - constructor.body.body.push(util.template(defaultConstructorTemplate, { - CLASS_NAME: className, - SUPER_NAME: this.superName - }, true)); + if (this.isLoose) { + constructor.body.body.push(util.template(helperName + "-loose", { + CLASS_NAME: className, + SUPER_NAME: this.superName + }, true)); + } else { + constructor.body.body.push( + t.expressionStatement(t.callExpression(this.file.addHelper(helperName), [className])) + ); + } } var instanceProps; @@ -256,5 +268,5 @@ Class.prototype.pushConstructor = function (method) { construct._ignoreUserWhitespace = true; construct.params = fn.params; - construct.body = fn.body; + construct.body.body = construct.body.body.concat(fn.body.body); }; diff --git a/test/fixtures/transformation/api/blacklist/expected.js b/test/fixtures/transformation/api/blacklist/expected.js index 01fd9509ac..934fea14ff 100644 --- a/test/fixtures/transformation/api/blacklist/expected.js +++ b/test/fixtures/transformation/api/blacklist/expected.js @@ -1,5 +1,9 @@ "use strict"; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = function Test() { + _classCallCheck(this, Test); + arr.map(x => x * x); -}; +}; \ No newline at end of file diff --git a/test/fixtures/transformation/async-to-generator/async/expected.js b/test/fixtures/transformation/async-to-generator/async/expected.js index 7a1578f1ca..ff97e4796e 100644 --- a/test/fixtures/transformation/async-to-generator/async/expected.js +++ b/test/fixtures/transformation/async-to-generator/async/expected.js @@ -4,8 +4,12 @@ var _asyncToGenerator = function (fn) { return function () { var gen = fn.apply( var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Foo = (function () { - function Foo() {} + function Foo() { + _classCallCheck(this, Foo); + } _prototypeProperties(Foo, null, { foo: { diff --git a/test/fixtures/transformation/bluebird-coroutines/class/expected.js b/test/fixtures/transformation/bluebird-coroutines/class/expected.js index 9217530fdc..fdb5965add 100644 --- a/test/fixtures/transformation/bluebird-coroutines/class/expected.js +++ b/test/fixtures/transformation/bluebird-coroutines/class/expected.js @@ -4,8 +4,12 @@ var _bluebird = require("bluebird"); var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Foo = (function () { - function Foo() {} + function Foo() { + _classCallCheck(this, Foo); + } _prototypeProperties(Foo, null, { foo: { diff --git a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js index 1aee2c69ea..0af6c4445f 100644 --- a/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/accessing-super-class/expected.js @@ -3,9 +3,13 @@ var _slice = Array.prototype.slice; var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { var _Foo$prototype$test, _Foo$prototype$test2; + _classCallCheck(this, Test); + woops["super"].test(); Foo.call(this); Foo.prototype.test.call(this); diff --git a/test/fixtures/transformation/es6-classes-loose/accessing-super-properties/expected.js b/test/fixtures/transformation/es6-classes-loose/accessing-super-properties/expected.js index 9847c19e45..915798aa48 100644 --- a/test/fixtures/transformation/es6-classes-loose/accessing-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/accessing-super-properties/expected.js @@ -2,8 +2,12 @@ var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { + _classCallCheck(this, Test); + Foo.prototype.test; Foo.prototype.test.whatever; } diff --git a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js index 4caab68366..8b66f1fafe 100644 --- a/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/calling-super-properties/expected.js @@ -2,8 +2,12 @@ var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { + _classCallCheck(this, Test); + Foo.prototype.test.whatever(); Foo.prototype.test.call(this); } diff --git a/test/fixtures/transformation/es6-classes-loose/super-class-id-member-expression/expected.js b/test/fixtures/transformation/es6-classes-loose/super-class-id-member-expression/expected.js index 708616d6a9..e277b544fd 100644 --- a/test/fixtures/transformation/es6-classes-loose/super-class-id-member-expression/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/super-class-id-member-expression/expected.js @@ -2,8 +2,12 @@ var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var BaseController = (function (_Chaplin$Controller) { function BaseController() { + _classCallCheck(this, BaseController); + if (_Chaplin$Controller != null) { _Chaplin$Controller.apply(this, arguments); } @@ -16,6 +20,8 @@ var BaseController = (function (_Chaplin$Controller) { var BaseController2 = (function (_Chaplin$Controller$Another) { function BaseController2() { + _classCallCheck(this, BaseController2); + if (_Chaplin$Controller$Another != null) { _Chaplin$Controller$Another.apply(this, arguments); } diff --git a/test/fixtures/transformation/es6-classes-loose/super-class/expected.js b/test/fixtures/transformation/es6-classes-loose/super-class/expected.js index b57997b371..338d66b696 100644 --- a/test/fixtures/transformation/es6-classes-loose/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/super-class/expected.js @@ -2,8 +2,12 @@ var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { + _classCallCheck(this, Test); + if (Foo != null) { Foo.apply(this, arguments); } diff --git a/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js b/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js index e17e74db06..8f393151d3 100644 --- a/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js +++ b/test/fixtures/transformation/es6-classes-loose/super-function-fallback/expected.js @@ -1,5 +1,9 @@ "use strict"; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = function Test() { + _classCallCheck(this, Test); + Function.prototype.hasOwnProperty.call(this, "test"); -}; +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index 264c6a4146..a8f74b0cb7 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -7,9 +7,13 @@ var _get = function get(object, property, receiver) { var desc = Object.getOwnPr var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { var _get2, _get3; + _classCallCheck(this, Test); + woops["super"].test(); _get(Object.getPrototypeOf(Test.prototype), "constructor", this).call(this); _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); diff --git a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js index 037d172454..7ee14a53f1 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-properties/expected.js @@ -4,8 +4,12 @@ var _get = function get(object, property, receiver) { var desc = Object.getOwnPr var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { + _classCallCheck(this, Test); + _get(Object.getPrototypeOf(Test.prototype), "test", this); _get(Object.getPrototypeOf(Test.prototype), "test", this).whatever; } diff --git a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js index b83d270ae3..4ae369971d 100644 --- a/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js +++ b/test/fixtures/transformation/es6-classes/calling-super-properties/expected.js @@ -6,8 +6,12 @@ var _get = function get(object, property, receiver) { var desc = Object.getOwnPr var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { + _classCallCheck(this, Test); + _get(Object.getPrototypeOf(Test.prototype), "test", this).whatever(); _get(Object.getPrototypeOf(Test.prototype), "test", this).call(this); } diff --git a/test/fixtures/transformation/es6-classes/constructor/expected.js b/test/fixtures/transformation/es6-classes/constructor/expected.js index 6750599c80..69e8c55d3b 100644 --- a/test/fixtures/transformation/es6-classes/constructor/expected.js +++ b/test/fixtures/transformation/es6-classes/constructor/expected.js @@ -2,12 +2,18 @@ var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = function Test() { + _classCallCheck(this, Test); + this.state = "test"; }; var Foo = (function (Bar) { function Foo() { + _classCallCheck(this, Foo); + this.state = "test"; } diff --git a/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js b/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js index 031271c3b8..f4bb836401 100644 --- a/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-getter-and-setter/expected.js @@ -2,8 +2,12 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function () { - function Test() {} + function Test() { + _classCallCheck(this, Test); + } _prototypeProperties(Test, null, { test: { diff --git a/test/fixtures/transformation/es6-classes/instance-getter/expected.js b/test/fixtures/transformation/es6-classes/instance-getter/expected.js index 2dc7c8a867..17f3dc1e1a 100644 --- a/test/fixtures/transformation/es6-classes/instance-getter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-getter/expected.js @@ -2,8 +2,12 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function () { - function Test() {} + function Test() { + _classCallCheck(this, Test); + } _prototypeProperties(Test, null, { test: { diff --git a/test/fixtures/transformation/es6-classes/instance-method/expected.js b/test/fixtures/transformation/es6-classes/instance-method/expected.js index 141bd252f7..ff77c0006c 100644 --- a/test/fixtures/transformation/es6-classes/instance-method/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-method/expected.js @@ -2,8 +2,12 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function () { - function Test() {} + function Test() { + _classCallCheck(this, Test); + } _prototypeProperties(Test, null, { test: { diff --git a/test/fixtures/transformation/es6-classes/instance-setter/expected.js b/test/fixtures/transformation/es6-classes/instance-setter/expected.js index b45f7b9668..6bd83dcef7 100644 --- a/test/fixtures/transformation/es6-classes/instance-setter/expected.js +++ b/test/fixtures/transformation/es6-classes/instance-setter/expected.js @@ -2,8 +2,12 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function () { - function Test() {} + function Test() { + _classCallCheck(this, Test); + } _prototypeProperties(Test, null, { test: { diff --git a/test/fixtures/transformation/es6-classes/plain-class/expected.js b/test/fixtures/transformation/es6-classes/plain-class/expected.js index 5f98760f68..ab3583fca2 100644 --- a/test/fixtures/transformation/es6-classes/plain-class/expected.js +++ b/test/fixtures/transformation/es6-classes/plain-class/expected.js @@ -1,3 +1,7 @@ "use strict"; -var Test = function Test() {}; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + +var Test = function Test() { + _classCallCheck(this, Test); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-classes/statement/expected.js b/test/fixtures/transformation/es6-classes/statement/expected.js index a54f9ae84a..fb8ea66a32 100644 --- a/test/fixtures/transformation/es6-classes/statement/expected.js +++ b/test/fixtures/transformation/es6-classes/statement/expected.js @@ -2,16 +2,24 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var BaseView = function BaseView() { + _classCallCheck(this, BaseView); + this.autoRender = true; }; var BaseView = function BaseView() { + _classCallCheck(this, BaseView); + this.autoRender = true; }; var BaseView = (function () { - function BaseView() {} + function BaseView() { + _classCallCheck(this, BaseView); + } _prototypeProperties(BaseView, null, { foo: { diff --git a/test/fixtures/transformation/es6-classes/static/expected.js b/test/fixtures/transformation/es6-classes/static/expected.js index 08f1142ffa..0eb86b84bf 100644 --- a/test/fixtures/transformation/es6-classes/static/expected.js +++ b/test/fixtures/transformation/es6-classes/static/expected.js @@ -2,8 +2,12 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var A = (function () { - function A() {} + function A() { + _classCallCheck(this, A); + } _prototypeProperties(A, { a: { diff --git a/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js b/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js index 9ec5c36b3c..1adf5661ac 100644 --- a/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class-id-member-expression/expected.js @@ -1,12 +1,16 @@ "use strict"; +var _classSuperConstructorCall = function (Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(this, arguments); } }; + var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var BaseController = (function (_Chaplin$Controller) { function BaseController() { - if (Object.getPrototypeOf(BaseController) !== null) { - Object.getPrototypeOf(BaseController).apply(this, arguments); - } + _classCallCheck(this, BaseController); + + _classSuperConstructorCall(BaseController); } _inherits(BaseController, _Chaplin$Controller); @@ -16,9 +20,9 @@ var BaseController = (function (_Chaplin$Controller) { var BaseController2 = (function (_Chaplin$Controller$Another) { function BaseController2() { - if (Object.getPrototypeOf(BaseController2) !== null) { - Object.getPrototypeOf(BaseController2).apply(this, arguments); - } + _classCallCheck(this, BaseController2); + + _classSuperConstructorCall(BaseController2); } _inherits(BaseController2, _Chaplin$Controller$Another); diff --git a/test/fixtures/transformation/es6-classes/super-class/expected.js b/test/fixtures/transformation/es6-classes/super-class/expected.js index 845d1c9f91..ab0dc10d23 100644 --- a/test/fixtures/transformation/es6-classes/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class/expected.js @@ -1,12 +1,16 @@ "use strict"; +var _classSuperConstructorCall = function (Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(this, arguments); } }; + var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function (Foo) { function Test() { - if (Object.getPrototypeOf(Test) !== null) { - Object.getPrototypeOf(Test).apply(this, arguments); - } + _classCallCheck(this, Test); + + _classSuperConstructorCall(Test); } _inherits(Test, Foo); diff --git a/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js b/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js index 09fb4b2cea..e278a09b33 100644 --- a/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js +++ b/test/fixtures/transformation/es6-classes/super-function-fallback/expected.js @@ -2,6 +2,10 @@ var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc && desc.writable) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = function Test() { + _classCallCheck(this, Test); + _get(Object.getPrototypeOf(Test.prototype), "hasOwnProperty", this).call(this, "test"); }; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js index 502245b6a1..f67a1d2731 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-default/expected.js @@ -1,6 +1,8 @@ define(["exports", "module"], function (exports, module) { "use strict"; + var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + module.exports = foo; module.exports = 42; module.exports = {}; @@ -8,11 +10,15 @@ define(["exports", "module"], function (exports, module) { module.exports = foo; module.exports = function () {}; - var _default = function _default() {}; + var _default = function _default() { + _classCallCheck(this, _default); + }; module.exports = _default; function foo() {} - var Foo = function Foo() {}; + var Foo = function Foo() { + _classCallCheck(this, Foo); + }; module.exports = Foo; }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js index e8d155830e..156c8b1bac 100644 --- a/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-amd/exports-variable/expected.js @@ -1,6 +1,8 @@ define(["exports"], function (exports) { "use strict"; + var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + exports.foo7 = foo7; var foo = exports.foo = 1; var foo = exports.foo = 1; @@ -11,7 +13,9 @@ define(["exports"], function (exports) { var foo5 = exports.foo5 = undefined; var foo6 = exports.foo6 = 3; function foo7() {} - var foo8 = exports.foo8 = function foo8() {}; + var foo8 = exports.foo8 = function foo8() { + _classCallCheck(this, foo8); + }; exports.__esModule = true; -}); +}); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-common/exports-default/expected.js b/test/fixtures/transformation/es6-modules-common/exports-default/expected.js index d5c084b553..f23a52e713 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-default/expected.js @@ -1,5 +1,7 @@ "use strict"; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + module.exports = foo; module.exports = 42; module.exports = {}; @@ -7,10 +9,14 @@ module.exports = []; module.exports = foo; module.exports = function () {}; -var _default = function _default() {}; +var _default = function _default() { + _classCallCheck(this, _default); +}; module.exports = _default; function foo() {} -var Foo = function Foo() {}; +var Foo = function Foo() { + _classCallCheck(this, Foo); +}; module.exports = Foo; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js index 4a196a3ffe..446d62e7e6 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-variable/expected.js @@ -1,5 +1,7 @@ "use strict"; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + exports.foo7 = foo7; var foo = exports.foo = 1; var foo = exports.foo = 1; @@ -10,6 +12,8 @@ var foo4 = exports.foo4 = 2; var foo5 = exports.foo5 = undefined; var foo6 = exports.foo6 = 3; function foo7() {} -var foo8 = exports.foo8 = function foo8() {}; +var foo8 = exports.foo8 = function foo8() { + _classCallCheck(this, foo8); +}; -exports.__esModule = true; +exports.__esModule = true; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js index 2484b584b3..ea2cae4338 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-default/expected.js @@ -1,6 +1,12 @@ "use strict"; -var _default = function _default() {}; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + +var _default = function _default() { + _classCallCheck(this, _default); +}; function foo() {} -var Foo = function Foo() {}; \ No newline at end of file +var Foo = function Foo() { + _classCallCheck(this, Foo); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js index 11ae89321c..bff6026c5a 100644 --- a/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-ignore/exports-variable/expected.js @@ -1,5 +1,7 @@ "use strict"; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var foo = 1; var foo = 1, bar = 2; @@ -9,4 +11,6 @@ var foo4 = 2; var foo5 = undefined; var foo6 = 3; function foo7() {} -var foo8 = function foo8() {}; +var foo8 = function foo8() { + _classCallCheck(this, foo8); +}; \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-system/exports-default/expected.js b/test/fixtures/transformation/es6-modules-system/exports-default/expected.js index 88a37c3f83..116c8ce6b6 100644 --- a/test/fixtures/transformation/es6-modules-system/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-system/exports-default/expected.js @@ -1,13 +1,15 @@ System.register([], function (_export) { "use strict"; - var _default, Foo; + var _classCallCheck, _default, Foo; _export("default", foo); function foo() {} return { setters: [], execute: function () { + _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + _export("default", 42); _export("default", {}); @@ -18,11 +20,15 @@ System.register([], function (_export) { _export("default", function () {}); - _default = function _default() {}; + _default = function _default() { + _classCallCheck(this, _default); + }; _export("default", _default); - Foo = function Foo() {}; + Foo = function Foo() { + _classCallCheck(this, Foo); + }; _export("default", Foo); } diff --git a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js index 740ae60f6c..770cb08145 100644 --- a/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-system/exports-variable/expected.js @@ -1,21 +1,25 @@ System.register([], function (_export) { "use strict"; - var foo, foo2, foo3, foo4, foo5, foo6, foo8; + var _classCallCheck, foo, foo2, foo3, foo4, foo5, foo6, foo8; _export("foo7", foo7); function foo7() {} return { setters: [], execute: function () { + _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + foo = _export("foo", 1); foo2 = _export("foo2", function () {}); foo3 = _export("foo3", undefined); foo4 = _export("foo4", 2); foo5 = _export("foo5", undefined); foo6 = _export("foo6", 3); - foo8 = _export("foo8", function foo8() {}); + foo8 = _export("foo8", function foo8() { + _classCallCheck(this, foo8); + }); _export("foo3", foo3 = 5); } }; -}); +}); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js index cd21b0b58b..8470debb5a 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-default/expected.js @@ -7,6 +7,8 @@ })(function (exports, module) { "use strict"; + var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + module.exports = foo; module.exports = 42; module.exports = {}; @@ -14,11 +16,15 @@ module.exports = foo; module.exports = function () {}; - var _default = function _default() {}; + var _default = function _default() { + _classCallCheck(this, _default); + }; module.exports = _default; function foo() {} - var Foo = function Foo() {}; + var Foo = function Foo() { + _classCallCheck(this, Foo); + }; module.exports = Foo; }); \ No newline at end of file diff --git a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js index b5c8cf6185..4857328e4b 100644 --- a/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js +++ b/test/fixtures/transformation/es6-modules-umd/exports-variable/expected.js @@ -7,6 +7,8 @@ })(function (exports) { "use strict"; + var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + exports.foo7 = foo7; var foo = exports.foo = 1; var foo = exports.foo = 1; @@ -17,7 +19,9 @@ var foo5 = exports.foo5 = undefined; var foo6 = exports.foo6 = 3; function foo7() {} - var foo8 = exports.foo8 = function foo8() {}; + var foo8 = exports.foo8 = function foo8() { + _classCallCheck(this, foo8); + }; exports.__esModule = true; -}); +}); \ No newline at end of file diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js index 1af25539ca..08e3dde5f8 100644 --- a/test/fixtures/transformation/es7-abstract-references/private/expected.js +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -1,5 +1,7 @@ "use strict"; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var A = new WeakMap(); var B = new WeakMap(), C = new WeakMap(); @@ -7,7 +9,9 @@ var D = (function () { var F = new WeakMap(), G = new WeakMap(); var E = new WeakMap(); - function D() {} + function D() { + _classCallCheck(this, D); + } return D; })(); @@ -16,7 +20,9 @@ var H = (function () { var J = new WeakMap(), K = new WeakMap(); var I = new WeakMap(); - function H() {} + function H() { + _classCallCheck(this, H); + } return H; -})(); +})(); \ No newline at end of file diff --git a/test/fixtures/transformation/playground/object-getter-memoization/expected.js b/test/fixtures/transformation/playground/object-getter-memoization/expected.js index 6476a64b68..00f7966f7b 100644 --- a/test/fixtures/transformation/playground/object-getter-memoization/expected.js +++ b/test/fixtures/transformation/playground/object-getter-memoization/expected.js @@ -2,10 +2,14 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var _defineProperty = function (obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); }; var Foo = (function () { - function Foo() {} + function Foo() { + _classCallCheck(this, Foo); + } _prototypeProperties(Foo, null, _defineProperty({ bar: { diff --git a/test/fixtures/transformation/source-maps/class/expected.js b/test/fixtures/transformation/source-maps/class/expected.js index 40b4dc7220..f559425ef5 100644 --- a/test/fixtures/transformation/source-maps/class/expected.js +++ b/test/fixtures/transformation/source-maps/class/expected.js @@ -2,8 +2,12 @@ var _prototypeProperties = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Test = (function () { - function Test() {} + function Test() { + _classCallCheck(this, Test); + } _prototypeProperties(Test, null, { bar: { diff --git a/test/fixtures/transformation/spec-proto-to-assign/class/expected.js b/test/fixtures/transformation/spec-proto-to-assign/class/expected.js index cec2fa5ac4..6217651685 100644 --- a/test/fixtures/transformation/spec-proto-to-assign/class/expected.js +++ b/test/fixtures/transformation/spec-proto-to-assign/class/expected.js @@ -2,13 +2,17 @@ var _defaults = function (obj, defaults) { for (var key in defaults) { if (obj[key] === undefined) { obj[key] = defaults[key]; } } return obj; }; +var _classSuperConstructorCall = function (Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(this, arguments); } }; + var _inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) _defaults(subClass, superClass); }; +var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; + var Foo = (function (Bar) { function Foo() { - if (Object.getPrototypeOf(Foo) !== null) { - Object.getPrototypeOf(Foo).apply(this, arguments); - } + _classCallCheck(this, Foo); + + _classSuperConstructorCall(Foo); } _inherits(Foo, Bar);