Fix last element of an array being null (#232)
This appears in so many of the test262.
This commit is contained in:
1 parent
d9ea466cd3
commit
65a2a150b5
3 files changed
+30
No files matched your search
@@ -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,
|
||||
"]"
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
[,];
|
||||
[,,];
|
||||
[,,1,];
|
||||
[,,1,1];
|
||||
Reference in new issue
Block a user