Throw better errors for non-iterables when Symbol doesn't exist (#11264)

This commit is contained in:
Nicolò Ribaudo
2020-03-16 16:34:33 +01:00
committed by GitHub
parent 10058901d0
commit 1ba41f2084
17 changed files with 115 additions and 26 deletions

View File

@@ -0,0 +1,13 @@
var a = (() => [1, 2, 3])();
// Simulate old environment
let _Symbol = Symbol;
Symbol = void 0;
try {
var [first, ...rest] = a;
expect(first).toBe(1);
expect(rest).toEqual([2, 3]);
} finally {
Symbol = _Symbol;
}

View File

@@ -0,0 +1,6 @@
var a = (() => [1, 2, 3])();
// !!! In order to run this test, this shouldn't be optimized using type inference
// If it's optimized and doesn't call toArray, please modify this test
// and exec.js
var [first, ...rest] = a;

View File

@@ -0,0 +1,8 @@
var a = (() => [1, 2, 3])(); // !!! In order to run this test, this shouldn't be optimized using type inference
// If it's optimized and doesn't call toArray, please modify this test
// and exec.js
var _a = babelHelpers.toArray(a),
first = _a[0],
rest = _a.slice(1);

View File

@@ -1,9 +1,22 @@
expect(
() => {
var [foo, bar] = undefined;
}).toThrow("Invalid attempt to destructure non-iterable instance");
var foo, bar;
expect(
() => {
var foo = [ ...undefined ];
}).toThrow("Invalid attempt to spread non-iterable instance");
() => [foo, bar] = undefined
).toThrow(/destructure non-iterable/);
expect(
() => [foo, bar] = {}
).toThrow(/destructure non-iterable/);
// Simulate old browser
let _Symbol = Symbol;
Symbol = void 0;
try {
expect(
() => [foo, bar] = {}
).toThrow(/destructure non-iterable/);
} finally {
Symbol = _Symbol;
}

View File

@@ -0,0 +1,5 @@
{
"plugins": [
"transform-destructuring"
]
}