Fix last element of an array being null (#232)

This appears in so many of the test262.
master
Christopher Chedeau 2017-01-16 09:53:39 -08:00 committed by James Long
parent d9ea466cd3
commit 65a2a150b5
3 changed files with 30 additions and 0 deletions

View File

@ -641,6 +641,18 @@ function genericPrintNoParens(path, options, print) {
if (n.elements.length === 0) {
parts.push("[]");
} else {
// 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
// a comma to ensure JavaScript recognizes it.
// [,].length === 1
// [1,].length === 1
// [1,,].length === 2
//
// Note that util.getLast returns null if the array is empty, but
// we already check for an empty array just above so we are safe
const needsForcedTrailingComma = util.getLast(n.elements) === null;
parts.push(
multilineGroup(
concat([
@ -652,6 +664,7 @@ function genericPrintNoParens(path, options, print) {
join(concat([ ",", line ]), path.map(print, "elements"))
])
),
needsForcedTrailingComma ? "," : "",
ifBreak(options.trailingComma ? "," : ""),
options.bracketSpacing ? line : softline,
"]"

View File

@ -81,6 +81,19 @@ module.exports = \"arrays\";
"
`;
exports[`test last.js 1`] = `
"[,];
[,,];
[,,1,];
[,,1,1];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ , ];
[ , , ];
[ , , 1 ];
[ , , 1, 1 ];
"
`;
exports[`test numeric_elem.js 1`] = `
"var arr = [];
var day = new Date;

4
tests/arrays/last.js Normal file
View File

@ -0,0 +1,4 @@
[,];
[,,];
[,,1,];
[,,1,1];