From 72de8f5c9b64ff6e014494d3ae56ad0783809fb2 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 3 Feb 2015 15:35:24 +1100 Subject: [PATCH] fix class tests to reflect new call behaviour --- .../templates/class-super-constructor-call.js | 4 ++-- lib/6to5/transformation/transformers/es6/classes.js | 4 +++- test/_transformation-helper.js | 6 +++--- .../out-files/lib/bar/bar.js | 8 ++++++-- .../dir --out-dir --source-maps/out-files/lib/bar/bar.js | 6 +++++- .../out-files/lib/bar/bar.js.map | 2 +- .../bin/6to5/dir --out-dir/out-files/lib/bar/bar.js | 6 +++++- .../out-files/script3.js | 8 ++++++-- .../out-files/script3.js | 6 +++++- .../out-files/script3.js.map | 2 +- .../bin/6to5/filenames --out-file/out-files/script3.js | 6 +++++- .../super-class-id-member-expression/expected.js | 6 +++--- .../transformation/es6-classes/super-class/expected.js | 4 ++-- .../transformation/source-maps/class/source-mappings.json | 2 +- .../transformation/spec-proto-to-assign/class/expected.js | 4 ++-- 15 files changed, 50 insertions(+), 24 deletions(-) diff --git a/lib/6to5/transformation/templates/class-super-constructor-call.js b/lib/6to5/transformation/templates/class-super-constructor-call.js index 7395389e91..35dc0b8f58 100644 --- a/lib/6to5/transformation/templates/class-super-constructor-call.js +++ b/lib/6to5/transformation/templates/class-super-constructor-call.js @@ -1,5 +1,5 @@ -(function (Constructor) { +(function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { - Object.getPrototypeOf(Constructor).apply(this, arguments); + Object.getPrototypeOf(Constructor).apply(instance, arguments); } }); diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index c320f06064..43785d92f0 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -177,7 +177,9 @@ Class.prototype.buildBody = function () { }, true)); } else { constructor.body.body.push( - t.expressionStatement(t.callExpression(this.file.addHelper(helperName), [className])) + t.expressionStatement( + t.callExpression(this.file.addHelper(helperName), [t.thisExpression(), className]) + ) ); } } diff --git a/test/_transformation-helper.js b/test/_transformation-helper.js index d35fa20c4e..09535e04f3 100644 --- a/test/_transformation-helper.js +++ b/test/_transformation-helper.js @@ -114,10 +114,10 @@ var run = function (task, done) { var consumer = new sourceMap.SourceMapConsumer(result.map); _.each(task.sourceMappings, function (mapping, i) { - var expect = mapping.original; + var actual = mapping.original; - var actual = consumer.originalPositionFor(mapping.generated); - chai.expect({ line: actual.line, column: actual.column }).to.deep.equal(expect); + var expect = consumer.originalPositionFor(mapping.generated); + chai.expect({ line: expect.line, column: expect.column }).to.deep.equal(actual); }); } }; diff --git a/test/fixtures/bin/6to5/dir --out-dir --source-maps-inline/out-files/lib/bar/bar.js b/test/fixtures/bin/6to5/dir --out-dir --source-maps-inline/out-files/lib/bar/bar.js index 00b4a82321..9f72a758e4 100644 --- a/test/fixtures/bin/6to5/dir --out-dir --source-maps-inline/out-files/lib/bar/bar.js +++ b/test/fixtures/bin/6to5/dir --out-dir --source-maps-inline/out-files/lib/bar/bar.js @@ -1,4 +1,8 @@ "use strict"; -var Test = function Test() {}; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNyYy9iYXIvYmFyLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0lBQU0sZ0JBQUEiLCJmaWxlIjoic3JjL2Jhci9iYXIuanMiLCJzb3VyY2VzQ29udGVudCI6WyJjbGFzcyBUZXN0IHtcblxufSJdfQ== +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); +}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNyYy9iYXIvYmFyLmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7SUFBTSxnQkFBQTt3QkFBQSIsImZpbGUiOiJzcmMvYmFyL2Jhci5qcyIsInNvdXJjZXNDb250ZW50IjpbImNsYXNzIFRlc3Qge1xuXG59Il19 diff --git a/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js b/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js index 549259d579..557c46fb08 100644 --- a/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js +++ b/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js @@ -1,4 +1,8 @@ "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); +}; //# sourceMappingURL=bar.js.map diff --git a/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map b/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map index 11bc242c46..91c1a7162f 100644 --- a/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map +++ b/test/fixtures/bin/6to5/dir --out-dir --source-maps/out-files/lib/bar/bar.js.map @@ -1 +1 @@ -{"version":3,"sources":["src/bar/bar.js"],"names":[],"mappings":";;IAAM,gBAAA","file":"src/bar/bar.js","sourcesContent":["class Test {\n\n}"]} +{"version":3,"sources":["src/bar/bar.js"],"names":[],"mappings":";;;;IAAM,gBAAA;wBAAA","file":"src/bar/bar.js","sourcesContent":["class Test {\n\n}"]} diff --git a/test/fixtures/bin/6to5/dir --out-dir/out-files/lib/bar/bar.js b/test/fixtures/bin/6to5/dir --out-dir/out-files/lib/bar/bar.js index 5f98760f68..2d641dbf79 100644 --- a/test/fixtures/bin/6to5/dir --out-dir/out-files/lib/bar/bar.js +++ b/test/fixtures/bin/6to5/dir --out-dir/out-files/lib/bar/bar.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); +}; diff --git a/test/fixtures/bin/6to5/filenames --out-file --source-maps-inline/out-files/script3.js b/test/fixtures/bin/6to5/filenames --out-file --source-maps-inline/out-files/script3.js index 0630d2870d..7750f534d9 100644 --- a/test/fixtures/bin/6to5/filenames --out-file --source-maps-inline/out-files/script3.js +++ b/test/fixtures/bin/6to5/filenames --out-file --source-maps-inline/out-files/script3.js @@ -1,10 +1,14 @@ "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); +}; "use strict"; arr.map(function (x) { return x * MULTIPLIER; }); -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyIsInNjcmlwdDIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7SUFBTSxnQkFBQTs7OztBQ0FOLElBQUksSUFBSSxVQUFBO1NBQUssSUFBSSIsImZpbGUiOiJzY3JpcHQzLmpzIiwic291cmNlc0NvbnRlbnQiOlsiY2xhc3MgVGVzdCB7XG5cbn0iLCJhcnIubWFwKHggPT4geCAqIE1VTFRJUExJRVIpOyJdfQ== +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjcmlwdC5qcyIsInNjcmlwdDIuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztJQUFNLGdCQUFBO3dCQUFBOzs7OztBQ0FOLElBQUksSUFBSSxVQUFBO1NBQUssSUFBSSIsImZpbGUiOiJzY3JpcHQzLmpzIiwic291cmNlc0NvbnRlbnQiOlsiY2xhc3MgVGVzdCB7XG5cbn0iLCJhcnIubWFwKHggPT4geCAqIE1VTFRJUExJRVIpOyJdfQ== diff --git a/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js b/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js index 695ecc125b..d9c62f1906 100644 --- a/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js +++ b/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js @@ -1,6 +1,10 @@ "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); +}; "use strict"; arr.map(function (x) { diff --git a/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js.map b/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js.map index 6645537c9e..3427ac430a 100644 --- a/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js.map +++ b/test/fixtures/bin/6to5/filenames --out-file --source-maps/out-files/script3.js.map @@ -1 +1 @@ -{"version":3,"sources":["script.js","script2.js"],"names":[],"mappings":";;IAAM,gBAAA;;;;ACAN,IAAI,IAAI,UAAA;SAAK,IAAI","file":"script3.js","sourcesContent":["class Test {\n\n}","arr.map(x => x * MULTIPLIER);"]} +{"version":3,"sources":["script.js","script2.js"],"names":[],"mappings":";;;;IAAM,gBAAA;wBAAA;;;;;ACAN,IAAI,IAAI,UAAA;SAAK,IAAI","file":"script3.js","sourcesContent":["class Test {\n\n}","arr.map(x => x * MULTIPLIER);"]} diff --git a/test/fixtures/bin/6to5/filenames --out-file/out-files/script3.js b/test/fixtures/bin/6to5/filenames --out-file/out-files/script3.js index 7421f83f81..8090f27e08 100644 --- a/test/fixtures/bin/6to5/filenames --out-file/out-files/script3.js +++ b/test/fixtures/bin/6to5/filenames --out-file/out-files/script3.js @@ -1,6 +1,10 @@ "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); +}; "use strict"; arr.map(function (x) { 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 1adf5661ac..63ffd096b1 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,6 +1,6 @@ "use strict"; -var _classSuperConstructorCall = function (Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(this, arguments); } }; +var _classSuperConstructorCall = function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(instance, 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; }; @@ -10,7 +10,7 @@ var BaseController = (function (_Chaplin$Controller) { function BaseController() { _classCallCheck(this, BaseController); - _classSuperConstructorCall(BaseController); + _classSuperConstructorCall(this, BaseController); } _inherits(BaseController, _Chaplin$Controller); @@ -22,7 +22,7 @@ var BaseController2 = (function (_Chaplin$Controller$Another) { function BaseController2() { _classCallCheck(this, BaseController2); - _classSuperConstructorCall(BaseController2); + _classSuperConstructorCall(this, 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 ab0dc10d23..6c7d2d245f 100644 --- a/test/fixtures/transformation/es6-classes/super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/super-class/expected.js @@ -1,6 +1,6 @@ "use strict"; -var _classSuperConstructorCall = function (Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(this, arguments); } }; +var _classSuperConstructorCall = function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(instance, 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; }; @@ -10,7 +10,7 @@ var Test = (function (Foo) { function Test() { _classCallCheck(this, Test); - _classSuperConstructorCall(Test); + _classSuperConstructorCall(this, Test); } _inherits(Test, Foo); diff --git a/test/fixtures/transformation/source-maps/class/source-mappings.json b/test/fixtures/transformation/source-maps/class/source-mappings.json index 5d8c4d8816..0bb3b62b7f 100644 --- a/test/fixtures/transformation/source-maps/class/source-mappings.json +++ b/test/fixtures/transformation/source-maps/class/source-mappings.json @@ -4,7 +4,7 @@ "column": 10 }, "generated": { - "line": 11, + "line": 15, "column": 15 } }] 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 6217651685..3d5b539004 100644 --- a/test/fixtures/transformation/spec-proto-to-assign/class/expected.js +++ b/test/fixtures/transformation/spec-proto-to-assign/class/expected.js @@ -2,7 +2,7 @@ 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 _classSuperConstructorCall = function (instance, Constructor) { if (Object.getPrototypeOf(Constructor) !== null) { Object.getPrototypeOf(Constructor).apply(instance, 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); }; @@ -12,7 +12,7 @@ var Foo = (function (Bar) { function Foo() { _classCallCheck(this, Foo); - _classSuperConstructorCall(Foo); + _classSuperConstructorCall(this, Foo); } _inherits(Foo, Bar);