Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw better errors for non-iterables when Symbol doesn't exist (#11264)
- Loading branch information
1 parent
1005890
commit 1ba41f2
Showing
17 changed files
with
114 additions
and
25 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
13 changes: 13 additions & 0 deletions
13
...ugin-transform-destructuring/test/fixtures/destructuring/array-symbol-unsupported/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,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; | ||
} |
6 changes: 6 additions & 0 deletions
6
...gin-transform-destructuring/test/fixtures/destructuring/array-symbol-unsupported/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,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; |
8 changes: 8 additions & 0 deletions
8
...in-transform-destructuring/test/fixtures/destructuring/array-symbol-unsupported/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,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); |
25 changes: 19 additions & 6 deletions
25
...ges/babel-plugin-transform-destructuring/test/fixtures/destructuring/non-iterable/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 |
---|---|---|
@@ -1,9 +1,22 @@ | ||
var foo, bar; | ||
|
||
expect( | ||
() => { | ||
var [foo, bar] = undefined; | ||
}).toThrow("Invalid attempt to destructure non-iterable instance"); | ||
() => [foo, bar] = undefined | ||
).toThrow(/destructure non-iterable/); | ||
|
||
expect( | ||
() => { | ||
var foo = [ ...undefined ]; | ||
}).toThrow("Invalid attempt to spread non-iterable instance"); | ||
() => [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; | ||
} |
5 changes: 5 additions & 0 deletions
5
...abel-plugin-transform-destructuring/test/fixtures/destructuring/non-iterable/options.json
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,5 @@ | ||
{ | ||
"plugins": [ | ||
"transform-destructuring" | ||
] | ||
} |
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
10 changes: 10 additions & 0 deletions
10
packages/babel-plugin-transform-spread/test/fixtures/spread/array-symbol-unsupported/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,10 @@ | ||
var a = (() => [2, 3])(); | ||
|
||
// Simulate old environment | ||
let _Symbol = Symbol; | ||
Symbol = void 0; | ||
try { | ||
expect([1, ...a]).toEqual([1, 2, 3]); | ||
} finally { | ||
Symbol = _Symbol; | ||
} |
6 changes: 6 additions & 0 deletions
6
...ages/babel-plugin-transform-spread/test/fixtures/spread/array-symbol-unsupported/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,6 @@ | ||
var a = (() => [2, 3])(); | ||
|
||
// !!! In order to run this test, this shouldn't be optimized using type inference | ||
// If it's optimized and doesn't call toConsumableArray, please modify this test | ||
// and exec.js | ||
[1, ...a]; |
6 changes: 6 additions & 0 deletions
6
...ges/babel-plugin-transform-spread/test/fixtures/spread/array-symbol-unsupported/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,6 @@ | ||
var a = (() => [2, 3])(); // !!! In order to run this test, this shouldn't be optimized using type inference | ||
// If it's optimized and doesn't call toConsumableArray, please modify this test | ||
// and exec.js | ||
|
||
|
||
[1].concat(babelHelpers.toConsumableArray(a)); |
10 changes: 10 additions & 0 deletions
10
packages/babel-plugin-transform-spread/test/fixtures/spread/non-iterable/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,10 @@ | ||
var o = {}; | ||
|
||
expect(() => [...undefined]).toThrow(/spread non-iterable/); | ||
|
||
expect(() => [...o]).toThrow(/spread non-iterable/); | ||
|
||
// Simulate old browser | ||
Symbol = void 0; | ||
|
||
expect(() => [...o]).toThrow(/spread non-iterable/); |
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
4 changes: 2 additions & 2 deletions
4
packages/babel-preset-env/test/fixtures/sanity/block-scoping-for-of/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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import _Array$from from "../../core-js/array/from"; | ||
import _isIterable from "../../core-js/is-iterable"; | ||
import _Symbol from "../../core-js/symbol"; | ||
export default function _iterableToArray(iter) { | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || _isIterable(Object(iter))) return _Array$from(iter); | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || typeof _Symbol !== "undefined" && _isIterable(Object(iter))) return _Array$from(iter); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function _iterableToArray(iter) { | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
function _iterableToArray(iter) { | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof iter === 'string' || Object.prototype.toString.call(iter) === "[object Arguments]" || typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
} | ||
|
||
module.exports = _iterableToArray; |