throw an error when destructuring a null or undefined value on an empty object pattern - fixes #681
This commit is contained in:
@@ -53,7 +53,8 @@ File.helpers = [
|
||||
"extends",
|
||||
"get",
|
||||
"set",
|
||||
"class-call-check"
|
||||
"class-call-check",
|
||||
"object-destructuring-empty"
|
||||
];
|
||||
|
||||
File.validOptions = [
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
(function (obj) {
|
||||
if (obj == null) throw new TypeError("Cannot destructure undefined");
|
||||
});
|
||||
@@ -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)) {
|
||||
|
||||
1
test/fixtures/transformation/es6-destructuring/empty-object-pattern/actual.js
vendored
Normal file
1
test/fixtures/transformation/es6-destructuring/empty-object-pattern/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var {} = null;
|
||||
3
test/fixtures/transformation/es6-destructuring/empty-object-pattern/exec.js
vendored
Normal file
3
test/fixtures/transformation/es6-destructuring/empty-object-pattern/exec.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
assert.throws(function () {
|
||||
var {} = null;
|
||||
}, /Cannot destructure undefined/);
|
||||
7
test/fixtures/transformation/es6-destructuring/empty-object-pattern/expected.js
vendored
Normal file
7
test/fixtures/transformation/es6-destructuring/empty-object-pattern/expected.js
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
"use strict";
|
||||
|
||||
var _objectDestructuringEmpty = function (obj) { if (obj == null) throw new TypeError("Cannot destructure undefined"); };
|
||||
|
||||
var _ref = null;
|
||||
|
||||
_objectDestructuringEmpty(_ref);
|
||||
Reference in New Issue
Block a user