throw an error when destructuring a null or undefined value on an empty object pattern - fixes #681

This commit is contained in:
Sebastian McKenzie
2015-02-04 17:35:24 +11:00
parent 4f023e83f8
commit 58bed088f5
6 changed files with 22 additions and 1 deletions

View File

@@ -53,7 +53,8 @@ File.helpers = [
"extends",
"get",
"set",
"class-call-check"
"class-call-check",
"object-destructuring-empty"
];
File.validOptions = [

View File

@@ -0,0 +1,3 @@
(function (obj) {
if (obj == null) throw new TypeError("Cannot destructure undefined");
});

View File

@@ -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)) {