From a8eb09553a6b34189f607325a8336bfc2a90a4ab Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Wed, 18 Jan 2017 09:42:20 -0800 Subject: [PATCH] Do not output trailing commas with rest arguments (#283) It turns out that this is not valid by the spec! --- src/printer.js | 5 +++- tests/rest/__snapshots__/jsfmt.spec.js.snap | 27 +++++++++++++++++++++ tests/rest/jsfmt.spec.js | 1 + tests/rest/trailing-commas.js | 11 +++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/rest/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/rest/jsfmt.spec.js create mode 100644 tests/rest/trailing-commas.js 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 +) {}