-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve array destructuring spec compliance (#15183)
- Loading branch information
1 parent
dad741a
commit f8b47f9
Showing
26 changed files
with
430 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/babel-helpers/src/helpers/iterableToArrayLimit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
packages/babel-helpers/src/helpers/iterableToArrayLimitLoose.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
41 changes: 41 additions & 0 deletions
41
...-plugin-transform-destructuring/test/fixtures/destructuring/check-iterator-return/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
33 changes: 33 additions & 0 deletions
33
...plugin-transform-destructuring/test/fixtures/destructuring/check-iterator-return/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: () => ({}), | ||
}; | ||
}, | ||
}; |
42 changes: 42 additions & 0 deletions
42
...lugin-transform-destructuring/test/fixtures/destructuring/check-iterator-return/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
38 changes: 38 additions & 0 deletions
38
...el-plugin-transform-destructuring/test/fixtures/destructuring/empty-array-pattern/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
24 changes: 24 additions & 0 deletions
24
...l-plugin-transform-destructuring/test/fixtures/destructuring/empty-array-pattern/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
}, | ||
}; | ||
}, | ||
}; |
61 changes: 61 additions & 0 deletions
61
...-plugin-transform-destructuring/test/fixtures/destructuring/empty-array-pattern/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.