Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly transform spreads to use proper concat method (#9108)
* Correctly transform spreads to use proper concat method * Add tests to ensure array spread clones elements
- Loading branch information
Showing
9 changed files
with
62 additions
and
5 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
11 changes: 11 additions & 0 deletions
11
packages/babel-plugin-transform-spread/test/fixtures/regression/issue-8907/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,11 @@ | ||
const arr = []; | ||
|
||
arr.concat = () => { | ||
throw new Error('Should not be called'); | ||
}; | ||
let x; | ||
expect(() => { | ||
x = [...arr]; | ||
}).not.toThrow(); | ||
|
||
expect(x).not.toBe(arr); |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-transform-spread/test/fixtures/regression/issue-8907/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,7 @@ | ||
const arr = []; | ||
|
||
arr.concat = () => { | ||
throw new Error('Should not be called'); | ||
}; | ||
|
||
const x = [...arr]; |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-transform-spread/test/fixtures/regression/issue-8907/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,7 @@ | ||
const arr = []; | ||
|
||
arr.concat = () => { | ||
throw new Error('Should not be called'); | ||
}; | ||
|
||
const x = [].concat(arr); |
9 changes: 9 additions & 0 deletions
9
packages/babel-plugin-transform-spread/test/fixtures/spread/arguments-array/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,9 @@ | ||
// test that toConsumableArray clones the array. | ||
|
||
function foo() { | ||
const x = [...arguments]; | ||
|
||
expect(x).not.toBe(arguments); | ||
} | ||
|
||
foo(1,2); |
2 changes: 1 addition & 1 deletion
2
packages/babel-plugin-transform-spread/test/fixtures/spread/array-literal-first/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 |
---|---|---|
@@ -1 +1 @@ | ||
var lyrics = babelHelpers.toConsumableArray(parts).concat(["head", "and", "toes"]); | ||
var lyrics = [].concat(babelHelpers.toConsumableArray(parts), ["head", "and", "toes"]); |
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 |
---|---|---|
|
@@ -3,5 +3,5 @@ function foo() { | |
bar[_key] = arguments[_key]; | ||
} | ||
|
||
return bar.concat(); | ||
return [].concat(bar); | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-transform-spread/test/fixtures/spread/single/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,7 @@ | ||
// test that toConsumableArray clones the array. | ||
const arr = []; | ||
const foo = () => arr; | ||
|
||
const x = [...foo()]; | ||
|
||
expect(x).not.toBe(arr); |
you dont need to cast bar to array
is enough