diff --git a/lib/6to5/file.js b/lib/6to5/file.js index dce01a04cc..cd2d34a6ac 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -53,7 +53,8 @@ File.helpers = [ "extends", "get", "set", - "class-call-check" + "class-call-check", + "object-destructuring-empty" ]; File.validOptions = [ diff --git a/lib/6to5/transformation/templates/object-destructuring-empty.js b/lib/6to5/transformation/templates/object-destructuring-empty.js new file mode 100644 index 0000000000..b2252a935a --- /dev/null +++ b/lib/6to5/transformation/templates/object-destructuring-empty.js @@ -0,0 +1,3 @@ +(function (obj) { + if (obj == null) throw new TypeError("Cannot destructure undefined"); +}); diff --git a/lib/6to5/transformation/transformers/es6/destructuring.js b/lib/6to5/transformation/transformers/es6/destructuring.js index 6dc61bfd6e..34e9895a81 100644 --- a/lib/6to5/transformation/transformers/es6/destructuring.js +++ b/lib/6to5/transformation/transformers/es6/destructuring.js @@ -66,6 +66,12 @@ var pushAssignmentPattern = function (opts, nodes, pattern, parentId) { }; var pushObjectPattern = function (opts, nodes, pattern, parentId) { + if (!pattern.properties.length) { + nodes.push(t.expressionStatement( + t.callExpression(opts.file.addHelper("object-destructuring-empty"), [parentId]) + )); + } + for (var i = 0; i < pattern.properties.length; i++) { var prop = pattern.properties[i]; if (t.isSpreadProperty(prop)) { diff --git a/test/fixtures/transformation/es6-destructuring/empty-object-pattern/actual.js b/test/fixtures/transformation/es6-destructuring/empty-object-pattern/actual.js new file mode 100644 index 0000000000..c216969cbd --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/empty-object-pattern/actual.js @@ -0,0 +1 @@ +var {} = null; diff --git a/test/fixtures/transformation/es6-destructuring/empty-object-pattern/exec.js b/test/fixtures/transformation/es6-destructuring/empty-object-pattern/exec.js new file mode 100644 index 0000000000..5cb51af584 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/empty-object-pattern/exec.js @@ -0,0 +1,3 @@ +assert.throws(function () { + var {} = null; +}, /Cannot destructure undefined/); diff --git a/test/fixtures/transformation/es6-destructuring/empty-object-pattern/expected.js b/test/fixtures/transformation/es6-destructuring/empty-object-pattern/expected.js new file mode 100644 index 0000000000..93c853b604 --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/empty-object-pattern/expected.js @@ -0,0 +1,7 @@ +"use strict"; + +var _objectDestructuringEmpty = function (obj) { if (obj == null) throw new TypeError("Cannot destructure undefined"); }; + +var _ref = null; + +_objectDestructuringEmpty(_ref);