diff --git a/src/printer.js b/src/printer.js index 78ed5c08..fbd6c54d 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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, ")" ]); diff --git a/tests/rest/__snapshots__/jsfmt.spec.js.snap b/tests/rest/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..70b59847 --- /dev/null +++ b/tests/rest/__snapshots__/jsfmt.spec.js.snap @@ -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 +) { +} +" +`; diff --git a/tests/rest/jsfmt.spec.js b/tests/rest/jsfmt.spec.js new file mode 100644 index 00000000..5495c026 --- /dev/null +++ b/tests/rest/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, {trailingComma: true}); diff --git a/tests/rest/trailing-commas.js b/tests/rest/trailing-commas.js new file mode 100644 index 00000000..39ebdccd --- /dev/null +++ b/tests/rest/trailing-commas.js @@ -0,0 +1,11 @@ +declare class C { + f( + superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong, + ...args + ): void, +} + +function f( + superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong, + ...args +) {}