Workaround flow parser bug with spread in arrays (#285)
The current output of ```js [...a, ...b] ``` is ```js [...a, , ...b] ``` because flow parses it as ``` ArrayExpression(SpreadExpression, null, SpreadExpression) ``` This is a bug in the flow parser. Until it gets fixed, we can workaround it by deleting the `null` after a `SpreadExpression`.master
parent
083876e66d
commit
a123e31e82
|
@ -641,6 +641,26 @@ function genericPrintNoParens(path, options, print) {
|
|||
if (n.elements.length === 0) {
|
||||
parts.push("[]");
|
||||
} else {
|
||||
// The flow parser in 0.37 has a bug where
|
||||
// [ ...a, ...a ]
|
||||
// is being parsed as
|
||||
// ArrayExpression(SpreadElement, null, SpreadElement)
|
||||
// The issue is that it treats the comma as a null element.
|
||||
//
|
||||
// In order to workaround this bug, we delete the null of a sequence
|
||||
// [SpreadElement, null].
|
||||
//
|
||||
// Note: remove this block when we upgrade to 0.38
|
||||
if (options.useFlowParser) {
|
||||
for (let i = 0; i < n.elements.length; ++i) {
|
||||
const element = n.elements[i];
|
||||
if (element && element.type === 'SpreadElement' &&
|
||||
n.elements[i + 1] === null) {
|
||||
n.elements.splice(i + 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// JavaScript allows you to have empty elements in an array which
|
||||
// changes its length based on the number of commas. The algorithm
|
||||
// is that if the last argument is null, we need to force insert
|
||||
|
|
|
@ -1,3 +1,21 @@
|
|||
exports[`test multiple.js 1`] = `
|
||||
"[...a, ...b,];
|
||||
[...a, ...b];
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
[ ...a, ...b ];
|
||||
[ ...a, ...b ];
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`test multiple.js 2`] = `
|
||||
"[...a, ...b,];
|
||||
[...a, ...b];
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
[ ...a, ...b ];
|
||||
[ ...a, ...b ];
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`test test.js 1`] = `
|
||||
"var A = [1,2,3];
|
||||
var B = [...A];
|
||||
|
@ -18,3 +36,24 @@ var x: Array<string> = [ \"1\", \"2\" ];
|
|||
var y: Array<string> = [ \"3\", ...x ];
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`test test.js 2`] = `
|
||||
"var A = [1,2,3];
|
||||
var B = [...A];
|
||||
var C = [1,2,3];
|
||||
B.sort((a, b) => a - b);
|
||||
C.sort((a, b) => a - b);
|
||||
|
||||
var x: Array<string> = [\'1\', \'2\'];
|
||||
var y: Array<string> = [\'3\', ...x];
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
var A = [ 1, 2, 3 ];
|
||||
var B = [ ...A ];
|
||||
var C = [ 1, 2, 3 ];
|
||||
B.sort((a, b) => a - b);
|
||||
C.sort((a, b) => a - b);
|
||||
|
||||
var x: Array<string> = [ \"1\", \"2\" ];
|
||||
var y: Array<string> = [ \"3\", ...x ];
|
||||
"
|
||||
`;
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
run_spec(__dirname);
|
||||
run_spec(__dirname, {useFlowParser: false});
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
[...a, ...b,];
|
||||
[...a, ...b];
|
Loading…
Reference in New Issue