Do not output trailing commas with rest arguments (#283)

It turns out that this is not valid by the spec!
master
Christopher Chedeau 2017-01-18 09:42:20 -08:00 committed by James Long
parent c2d1b49b62
commit a8eb09553a
4 changed files with 43 additions and 1 deletions

View File

@ -1760,13 +1760,16 @@ function printFunctionParams(path, print, options) {
return "()";
}
const lastParam = util.getLast(path.getValue().params);
const canHaveTrailingComma = lastParam.type !== "RestElement" && !fun.rest;
return concat([
"(",
indent(
options.tabWidth,
concat([ softline, join(concat([ ",", line ]), printed) ])
),
ifBreak(options.trailingComma ? "," : ""),
ifBreak(canHaveTrailingComma && options.trailingComma ? "," : ""),
softline,
")"
]);

View File

@ -0,0 +1,27 @@
exports[`test trailing-commas.js 1`] = `
"declare class C {
f(
superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong,
...args
): void,
}
function f(
superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong,
...args
) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare class C {
f(
superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong,
...args
): void,
}
function f(
superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong,
...args
) {
}
"
`;

1
tests/rest/jsfmt.spec.js Normal file
View File

@ -0,0 +1 @@
run_spec(__dirname, {trailingComma: true});

View File

@ -0,0 +1,11 @@
declare class C {
f(
superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong,
...args
): void,
}
function f(
superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong,
...args
) {}