Skip to content

Commit

Permalink
Improve array destructuring spec compliance (#15183)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
Fixes #15154
Fixes #15168
  • Loading branch information
SuperSodaSea authored Nov 25, 2022
1 parent dad741a commit f8b47f9
Show file tree
Hide file tree
Showing 26 changed files with 430 additions and 110 deletions.
8 changes: 8 additions & 0 deletions packages/babel-helpers/src/helpers-generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ export default Object.freeze({
"7.20.1",
'export default function _checkInRHS(value){if(Object(value)!==value)throw TypeError("right-hand side of \'in\' should be an object, got "+(null!==value?typeof value:"null"));return value}',
),
iterableToArrayLimit: helper(
"7.0.0-beta.0",
'export default function _iterableToArrayLimit(arr,i){var _i=null==arr?null:"undefined"!=typeof Symbol&&arr[Symbol.iterator]||arr["@@iterator"];if(null!=_i){var _s,_e,_x,_r,_arr=[],_n=!0,_d=!1;try{if(_x=(_i=_i.call(arr)).next,0===i){if(Object(_i)!==_i)return;_n=!1}else for(;!(_n=(_s=_x.call(_i)).done)&&(_arr.push(_s.value),_arr.length!==i);_n=!0);}catch(err){_d=!0,_e=err}finally{try{if(!_n&&null!=_i.return&&(_r=_i.return(),Object(_r)!==_r))return}finally{if(_d)throw _e}}return _arr}}',
),
iterableToArrayLimitLoose: helper(
"7.0.0-beta.0",
'export default function _iterableToArrayLimitLoose(arr,i){var _i=arr&&("undefined"!=typeof Symbol&&arr[Symbol.iterator]||arr["@@iterator"]);if(null!=_i){var _s,_arr=[];for(_i=_i.call(arr);arr.length<i&&!(_s=_i.next()).done;)_arr.push(_s.value);return _arr}}',
),
jsx: helper(
"7.0.0-beta.0",
'var REACT_ELEMENT_TYPE;export default function _createRawReactElement(type,props,key,children){REACT_ELEMENT_TYPE||(REACT_ELEMENT_TYPE="function"==typeof Symbol&&Symbol.for&&Symbol.for("react.element")||60103);var defaultProps=type&&type.defaultProps,childrenLength=arguments.length-3;if(props||0===childrenLength||(props={children:void 0}),1===childrenLength)props.children=children;else if(childrenLength>1){for(var childArray=new Array(childrenLength),i=0;i<childrenLength;i++)childArray[i]=arguments[i+3];props.children=childArray}if(props&&defaultProps)for(var propName in defaultProps)void 0===props[propName]&&(props[propName]=defaultProps[propName]);else props||(props=defaultProps||{});return{$$typeof:REACT_ELEMENT_TYPE,type:type,key:void 0===key?null:""+key,ref:null,props:props,_owner:null}}',
Expand Down
52 changes: 0 additions & 52 deletions packages/babel-helpers/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,58 +771,6 @@ helpers.iterableToArray = helper("7.0.0-beta.0")`
}
`;

helpers.iterableToArrayLimit = helper("7.0.0-beta.0")`
export default function _iterableToArrayLimit(arr, i) {
// this is an expanded form of \`for...of\` that properly supports abrupt completions of
// iterators etc. variable names have been minimised to reduce the size of this massive
// helper. sometimes spec compliance is annoying :(
//
// _n = _iteratorNormalCompletion
// _d = _didIteratorError
// _e = _iteratorError
// _i = _iterator
// _s = _step
var _i = arr == null ? null : (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
`;

helpers.iterableToArrayLimitLoose = helper("7.0.0-beta.0")`
export default function _iterableToArrayLimitLoose(arr, i) {
var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
if (_i == null) return;
var _arr = [];
for (_i = _i.call(arr), _step; !(_step = _i.next()).done;) {
_arr.push(_step.value);
if (i && _arr.length === i) break;
}
return _arr;
}
`;

helpers.unsupportedIterableToArray = helper("7.9.0")`
import arrayLikeToArray from "arrayLikeToArray";
Expand Down
54 changes: 54 additions & 0 deletions packages/babel-helpers/src/helpers/iterableToArrayLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* @minVersion 7.0.0-beta.0 */

export default function _iterableToArrayLimit(arr, i) {
// this is an expanded form of \`for...of\` that properly supports abrupt completions of
// iterators etc. variable names have been minimised to reduce the size of this massive
// helper. sometimes spec compliance is annoying :(
//
// _n = _iteratorNormalCompletion
// _d = _didIteratorError
// _e = _iteratorError
// _i = _iterator
// _s = _step
// _x = _next
// _r = _return

var _i =
arr == null
? null
: (typeof Symbol !== "undefined" && arr[Symbol.iterator]) ||
arr["@@iterator"];
if (_i == null) return;

var _arr = [];
var _n = true;
var _d = false;
var _s, _e, _x, _r;
try {
_x = (_i = _i.call(arr)).next;
if (i === 0) {
if (Object(_i) !== _i) return;
_n = false;
} else {
for (; !(_n = (_s = _x.call(_i)).done); _n = true) {
_arr.push(_s.value);
if (_arr.length === i) break;
}
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) {
_r = _i["return"]();
// eslint-disable-next-line no-unsafe-finally
if (Object(_r) !== _r) return;
}
} finally {
// eslint-disable-next-line no-unsafe-finally
if (_d) throw _e;
}
}
return _arr;
}
16 changes: 16 additions & 0 deletions packages/babel-helpers/src/helpers/iterableToArrayLimitLoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* @minVersion 7.0.0-beta.0 */

export default function _iterableToArrayLimitLoose(arr, i) {
var _i =
arr &&
((typeof Symbol !== "undefined" && arr[Symbol.iterator]) ||
arr["@@iterator"]);
if (_i == null) return;

var _arr = [];
var _s;
for (_i = _i.call(arr); arr.length < i && !(_s = _i.next()).done; ) {
_arr.push(_s.value);
}
return _arr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
expect(() => {
var [] = {
[Symbol.iterator]: () => {
return {
return: () => {},
};
},
};
}).toThrow(TypeError);

expect(() => {
var [] = {
[Symbol.iterator]: () => {
return {
return: () => ({}),
};
},
};
}).not.toThrow();

expect(() => {
var [x] = {
[Symbol.iterator]: () => {
return {
next: () => ({ done: false, value: 1 }),
return: () => {},
};
},
};
}).toThrow(TypeError);

expect(() => {
var [x] = {
[Symbol.iterator]: () => {
return {
next: () => ({ done: false, value: 1 }),
return: () => ({}),
};
},
};
}).not.toThrow();
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var [] = {
[Symbol.iterator]: () => {
return {
return: () => {},
};
},
};

var [] = {
[Symbol.iterator]: () => {
return {
return: () => ({}),
};
},
};

var [x] = {
[Symbol.iterator]: () => {
return {
next: () => ({ done: false, value: 1 }),
return: () => {},
};
},
};

var [x] = {
[Symbol.iterator]: () => {
return {
next: () => ({ done: false, value: 1 }),
return: () => ({}),
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var _Symbol$iterator = {
[Symbol.iterator]: () => {
return {
return: () => {}
};
}
},
_Symbol$iterator2 = babelHelpers.slicedToArray(_Symbol$iterator, 0);
var _Symbol$iterator3 = {
[Symbol.iterator]: () => {
return {
return: () => ({})
};
}
},
_Symbol$iterator4 = babelHelpers.slicedToArray(_Symbol$iterator3, 0);
var _Symbol$iterator5 = {
[Symbol.iterator]: () => {
return {
next: () => ({
done: false,
value: 1
}),
return: () => {}
};
}
},
_Symbol$iterator6 = babelHelpers.slicedToArray(_Symbol$iterator5, 1),
x = _Symbol$iterator6[0];
var _Symbol$iterator7 = {
[Symbol.iterator]: () => {
return {
next: () => ({
done: false,
value: 1
}),
return: () => ({})
};
}
},
_Symbol$iterator8 = babelHelpers.slicedToArray(_Symbol$iterator7, 1),
x = _Symbol$iterator8[0];
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
expect(() => {
var [] = null;
}).toThrow(TypeError);

expect(() => {
var [] = 42;
}).toThrow(TypeError);

expect(() => {
var [] = {};
}).toThrow(TypeError);

expect(() => {
var [] = { [Symbol.iterator]: () => {} };
}).toThrow(TypeError);

expect(() => {
var [] = [];
var [] = [0, 1, 2];
var [] = "foo";
var [] = (function*() { throw new Error("Should not throw"); })();
var [] = { [Symbol.iterator]: () => ({}) };
var [] = { [Symbol.iterator]: () => () => {} };
var [] = { [Symbol.iterator]: async function*() {} };
}).not.toThrow();

var returnCalled = false;
var [] = {
[Symbol.iterator]: () => {
return {
return: () => {
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]: () => {} };

var [] = [];
var [] = [0, 1, 2];
var [] = "foo";
var [] = (function*() { throw new Error("Should not throw"); })();
var [] = { [Symbol.iterator]: () => ({}) };
var [] = { [Symbol.iterator]: () => () => {} };
var [] = { [Symbol.iterator]: async function*() {} };

var returnCalled = false;
var [] = {
[Symbol.iterator]: () => {
return {
return: () => {
returnCalled = true;
return {};
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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]: () => {}
},
_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]: () => ({})
},
_Symbol$iterator4 = babelHelpers.slicedToArray(_Symbol$iterator3, 0);
var _Symbol$iterator5 = {
[Symbol.iterator]: () => () => {}
},
_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]: () => {
return {
return: () => {
returnCalled = true;
return {};
}
};
}
},
_Symbol$iterator10 = babelHelpers.slicedToArray(_Symbol$iterator9, 0);
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ try {
expect(thrown).toEqual(true);
try {
thrown = false;
var _ = babelHelpers.toArray(void 0);
var _ = babelHelpers.slicedToArray(void 0, 0);
} catch (e) {
thrown = true;
}
Expand Down
Loading

0 comments on commit f8b47f9

Please sign in to comment.