New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve array destructuring spec compliance #15183
Changes from 3 commits
e9a06d5
a165501
575d0cc
b8bc947
6936cde
6f41ba0
6820861
b0c4eac
42d95d5
94f03b9
4f951f3
e952071
f53c65d
1f404cb
e7e040f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -788,7 +788,13 @@ helpers.iterableToArrayLimit = helper("7.0.0-beta.0")` | |
var _d = false; | ||
var _s, _e; | ||
try { | ||
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { | ||
_i = _i.call(arr); | ||
if (i === 0) { | ||
if (typeof _i !== "object" && typeof _i !== "function") return; | ||
_n = false; | ||
return _arr; | ||
} | ||
for (; !(_n = (_s = _i.next()).done); _n = true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm from mobile so this code might be completely wrong, but does it work? for (_i = _i.call(arr); i < _arr.length && !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
} We should try keeping these helpers as small as possible :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I change it to this: try {
for (_i = _i.call(arr); _arr.length < i && !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
}
if (i === 0) {
if (Object(_i) !== _i) return;
_n = false;
}
} // ... Does this looks good? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that seems to be not working since the order of try {
_i = _i.call(arr);
if (i === 0) {
if (Object(_i) !== _i) return;
_n = false;
} else {
for (; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if(_arr.length === i) break;
}
}
} // ... |
||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
|
@@ -812,8 +818,13 @@ helpers.iterableToArrayLimitLoose = helper("7.0.0-beta.0")` | |
if (_i == null) return; | ||
|
||
var _arr = []; | ||
for (_i = _i.call(arr), _step; !(_step = _i.next()).done;) { | ||
_arr.push(_step.value); | ||
_i = _i.call(arr); | ||
if (i === 0) { | ||
if (typeof _i !== "object" && typeof _i !== "function") return; | ||
return _arr; | ||
} | ||
for (var _s; !(_s = _i.next()).done;) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
return _arr; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
expect(function () { | ||
var [] = null; | ||
}).toThrow("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
|
||
expect(function () { | ||
var [] = 42; | ||
}).toThrow("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
|
||
expect(function () { | ||
var [] = {}; | ||
}).toThrow("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
|
||
expect(function () { | ||
var [] = { [Symbol.iterator]: function() {} }; | ||
}).toThrow("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
|
||
var [] = []; | ||
var [] = [0, 1, 2]; | ||
var [] = "foo"; | ||
var [] = (function*() { throw new Error("Should not throw"); })(); | ||
var [] = { [Symbol.iterator]: function() { return {}; } } | ||
var [] = { [Symbol.iterator]: function() { return function() {}; } } | ||
var [] = { [Symbol.iterator]: async function*() {} } | ||
|
||
var returnCalled = false; | ||
var [] = { | ||
[Symbol.iterator]: function() { | ||
return { | ||
return: function() { | ||
returnCalled = true; | ||
return {}; | ||
} | ||
}; | ||
} | ||
}; | ||
expect(returnCalled).toStrictEqual(true); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var [] = null; | ||
var [] = 42; | ||
var [] = {}; | ||
var [] = { [Symbol.iterator]: function() {} }; | ||
|
||
var [] = []; | ||
var [] = [0, 1, 2]; | ||
var [] = "foo"; | ||
var [] = (function*() { throw new Error("Should not throw"); })(); | ||
var [] = { [Symbol.iterator]: function() { return {}; } } | ||
var [] = { [Symbol.iterator]: function() { return function() {}; } } | ||
var [] = { [Symbol.iterator]: async function*() {} } | ||
|
||
var returnCalled = false; | ||
var [] = { | ||
[Symbol.iterator]: function() { | ||
return { | ||
return: function() { | ||
returnCalled = true; | ||
return {}; | ||
} | ||
}; | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
var _ref = null, | ||
_ref2 = babelHelpers.slicedToArray(_ref, 0); | ||
var _ = 42, | ||
_ref3 = babelHelpers.slicedToArray(_, 0); | ||
var _ref4 = {}, | ||
_ref5 = babelHelpers.slicedToArray(_ref4, 0); | ||
var _Symbol$iterator = { | ||
[Symbol.iterator]: function () {} | ||
}, | ||
_Symbol$iterator2 = babelHelpers.slicedToArray(_Symbol$iterator, 0); | ||
var _ref6 = [0, 1, 2]; | ||
var _foo = "foo", | ||
_foo2 = babelHelpers.slicedToArray(_foo, 0); | ||
var _ref7 = /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee() { | ||
return babelHelpers.regeneratorRuntime().wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
throw new Error("Should not throw"); | ||
case 1: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
})(), | ||
_ref8 = babelHelpers.slicedToArray(_ref7, 0); | ||
var _Symbol$iterator3 = { | ||
[Symbol.iterator]: function () { | ||
return {}; | ||
} | ||
}, | ||
_Symbol$iterator4 = babelHelpers.slicedToArray(_Symbol$iterator3, 0); | ||
var _Symbol$iterator5 = { | ||
[Symbol.iterator]: function () { | ||
return function () {}; | ||
} | ||
}, | ||
_Symbol$iterator6 = babelHelpers.slicedToArray(_Symbol$iterator5, 0); | ||
var _Symbol$iterator7 = { | ||
[Symbol.iterator]: /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee2() { | ||
return babelHelpers.regeneratorRuntime().async(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, null, null, Promise); | ||
}) | ||
}, | ||
_Symbol$iterator8 = babelHelpers.slicedToArray(_Symbol$iterator7, 0); | ||
var returnCalled = false; | ||
var _Symbol$iterator9 = { | ||
[Symbol.iterator]: function () { | ||
return { | ||
return: function () { | ||
returnCalled = true; | ||
return {}; | ||
} | ||
}; | ||
} | ||
}, | ||
_Symbol$iterator10 = babelHelpers.slicedToArray(_Symbol$iterator9, 0); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
Object(_i) !== _i
, which checks for both functions and objects but it excludes null.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!