From fdea18a289a1736624fd9b0300de80fd07b71d79 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 19 Mar 2015 01:47:19 +1100 Subject: [PATCH] properly support iterators in sliced-to-array helper... --- .../templates/sliced-to-array.js | 31 +++++++++++++++++-- .../assignment-expression/expected.js | 4 +-- .../assignment-statement/expected.js | 4 +-- .../es6-destructuring/empty/expected.js | 6 ++-- .../es6-destructuring/for-in/expected.js | 4 +-- .../es6-destructuring/for-of/expected.js | 6 ++-- .../member-expression/expected.js | 4 +-- .../es6-destructuring/mixed/expected.js | 6 ++-- .../es6-destructuring/options.json | 3 ++ .../es6-destructuring/parameters/expected.js | 6 ++-- 10 files changed, 43 insertions(+), 31 deletions(-) create mode 100644 test/fixtures/transformation/es6-destructuring/options.json diff --git a/src/babel/transformation/templates/sliced-to-array.js b/src/babel/transformation/templates/sliced-to-array.js index acfd0ef28b..aac859f7f4 100644 --- a/src/babel/transformation/templates/sliced-to-array.js +++ b/src/babel/transformation/templates/sliced-to-array.js @@ -2,10 +2,35 @@ if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { + // 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 compliancy is annoying :() + // + // _n = _iteratorNormalCompletion + // _d = _didIteratorError + // _e = _iteratorError + // _i = _iterator + // _s = _step + var _arr = []; - for (var val of arr) { - _arr.push(_step.value); - if (i && _arr.length === i) break; + var _n = true; + var _d = false; + var _e = undefined; + try { + for (var _i = arr[Symbol.iterator](), _s; !(_n (_s = _i.next()).done); _n = true) { + var val = _s.value; + _arr.push(_step.value); + if (i && _arr.length === i) break; + } + } catch (err) { + _d = true; + _e = err; + } finally { + try { + if (!_n && _i["return"]) _i["return"](); + } finally { + if (_d) throw _e; + } } return _arr; } else { diff --git a/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js b/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js index fc9e842f53..9ef5dc3448 100644 --- a/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js +++ b/test/fixtures/transformation/es6-destructuring/assignment-expression/expected.js @@ -2,6 +2,4 @@ var _temp, _temp2; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - -console.log((_temp = [123], _temp2 = _slicedToArray(_temp, 1), x = _temp2[0], _temp)); +console.log((_temp = [123], _temp2 = babelHelpers.slicedToArray(_temp, 1), x = _temp2[0], _temp)); diff --git a/test/fixtures/transformation/es6-destructuring/assignment-statement/expected.js b/test/fixtures/transformation/es6-destructuring/assignment-statement/expected.js index 02e5751c6a..8a076f9f6e 100644 --- a/test/fixtures/transformation/es6-destructuring/assignment-statement/expected.js +++ b/test/fixtures/transformation/es6-destructuring/assignment-statement/expected.js @@ -1,10 +1,8 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - var _ref = f(); -var _ref2 = _slicedToArray(_ref, 2); +var _ref2 = babelHelpers.slicedToArray(_ref, 2); a = _ref2[0]; b = _ref2[1]; diff --git a/test/fixtures/transformation/es6-destructuring/empty/expected.js b/test/fixtures/transformation/es6-destructuring/empty/expected.js index 7f96ccd8e3..c1f22824cf 100644 --- a/test/fixtures/transformation/es6-destructuring/empty/expected.js +++ b/test/fixtures/transformation/es6-destructuring/empty/expected.js @@ -1,15 +1,13 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - var _ref = ["foo", "hello", [", ", "junk"], ["world"]]; var a = _ref[1]; -var _ref$2 = _slicedToArray(_ref[2], 1); +var _ref$2 = babelHelpers.slicedToArray(_ref[2], 1); var b = _ref$2[0]; -var _ref$3 = _slicedToArray(_ref[3], 1); +var _ref$3 = babelHelpers.slicedToArray(_ref[3], 1); var c = _ref$3[0]; var d = _ref[4]; diff --git a/test/fixtures/transformation/es6-destructuring/for-in/expected.js b/test/fixtures/transformation/es6-destructuring/for-in/expected.js index 64d9b33e42..2874e65c28 100644 --- a/test/fixtures/transformation/es6-destructuring/for-in/expected.js +++ b/test/fixtures/transformation/es6-destructuring/for-in/expected.js @@ -1,9 +1,7 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - for (var _ref in obj) { - var _ref2 = _slicedToArray(_ref, 2); + var _ref2 = babelHelpers.slicedToArray(_ref, 2); var name = _ref2[0]; var value = _ref2[1]; diff --git a/test/fixtures/transformation/es6-destructuring/for-of/expected.js b/test/fixtures/transformation/es6-destructuring/for-of/expected.js index b7e8a6bcf2..f61fa64066 100644 --- a/test/fixtures/transformation/es6-destructuring/for-of/expected.js +++ b/test/fixtures/transformation/es6-destructuring/for-of/expected.js @@ -1,14 +1,12 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = test.expectation.registers[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { - var _step$value = _slicedToArray(_step.value, 3); + var _step$value = babelHelpers.slicedToArray(_step.value, 3); var name = _step$value[0]; var before = _step$value[1]; @@ -27,4 +25,4 @@ try { throw _iteratorError; } } -} \ No newline at end of file +} diff --git a/test/fixtures/transformation/es6-destructuring/member-expression/expected.js b/test/fixtures/transformation/es6-destructuring/member-expression/expected.js index b5f3f8d67c..57c5ef8472 100644 --- a/test/fixtures/transformation/es6-destructuring/member-expression/expected.js +++ b/test/fixtures/transformation/es6-destructuring/member-expression/expected.js @@ -1,10 +1,8 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - var _ref = [1, 2]; -var _ref2 = _slicedToArray(_ref, 2); +var _ref2 = babelHelpers.slicedToArray(_ref, 2); foo.foo = _ref2[0]; foo.bar = _ref2[1]; diff --git a/test/fixtures/transformation/es6-destructuring/mixed/expected.js b/test/fixtures/transformation/es6-destructuring/mixed/expected.js index aff44e464d..8a7d6a658b 100644 --- a/test/fixtures/transformation/es6-destructuring/mixed/expected.js +++ b/test/fixtures/transformation/es6-destructuring/mixed/expected.js @@ -1,13 +1,11 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - -var _rect$topLeft = _slicedToArray(rect.topLeft, 2); +var _rect$topLeft = babelHelpers.slicedToArray(rect.topLeft, 2); var x1 = _rect$topLeft[0]; var y1 = _rect$topLeft[1]; -var _rect$bottomRight = _slicedToArray(rect.bottomRight, 2); +var _rect$bottomRight = babelHelpers.slicedToArray(rect.bottomRight, 2); var x2 = _rect$bottomRight[0]; var y2 = _rect$bottomRight[1]; diff --git a/test/fixtures/transformation/es6-destructuring/options.json b/test/fixtures/transformation/es6-destructuring/options.json new file mode 100644 index 0000000000..7d6e6cda1c --- /dev/null +++ b/test/fixtures/transformation/es6-destructuring/options.json @@ -0,0 +1,3 @@ +{ + "externalHelpers": true +} diff --git a/test/fixtures/transformation/es6-destructuring/parameters/expected.js b/test/fixtures/transformation/es6-destructuring/parameters/expected.js index 1f93fcdafa..d88a23e959 100644 --- a/test/fixtures/transformation/es6-destructuring/parameters/expected.js +++ b/test/fixtures/transformation/es6-destructuring/parameters/expected.js @@ -1,7 +1,5 @@ "use strict"; -var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; for (var _iterator = arr[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { _arr.push(_step.value); if (i && _arr.length === i) break; } return _arr; } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; - function somethingAdvanced(_ref) { var _ref$topLeft = _ref.topLeft; var x1 = _ref$topLeft.x; @@ -21,13 +19,13 @@ function unpackObject(_ref) { console.log(unpackObject({ title: "title", author: "author" })); var unpackArray = function unpackArray(_ref, _ref3) { - var _ref2 = _slicedToArray(_ref, 3); + var _ref2 = babelHelpers.slicedToArray(_ref, 3); var a = _ref2[0]; var b = _ref2[1]; var c = _ref2[2]; - var _ref32 = _slicedToArray(_ref3, 3); + var _ref32 = babelHelpers.slicedToArray(_ref3, 3); var x = _ref32[0]; var y = _ref32[1];