From 58bed088f512e3387b34812ff6f412026b7a7bc0 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 4 Feb 2015 17:35:24 +1100 Subject: [PATCH] throw an error when destructuring a null or undefined value on an empty object pattern - fixes #681 --- lib/6to5/file.js | 3 ++- .../transformation/templates/object-destructuring-empty.js | 3 +++ lib/6to5/transformation/transformers/es6/destructuring.js | 6 ++++++ .../es6-destructuring/empty-object-pattern/actual.js | 1 + .../es6-destructuring/empty-object-pattern/exec.js | 3 +++ .../es6-destructuring/empty-object-pattern/expected.js | 7 +++++++ 6 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 lib/6to5/transformation/templates/object-destructuring-empty.js create mode 100644 test/fixtures/transformation/es6-destructuring/empty-object-pattern/actual.js create mode 100644 test/fixtures/transformation/es6-destructuring/empty-object-pattern/exec.js create mode 100644 test/fixtures/transformation/es6-destructuring/empty-object-pattern/expected.js 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);