From a6ec181566dd15b90bd2a808ccabaa14bf5671e4 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Thu, 19 Jan 2017 07:01:31 -0800 Subject: [PATCH] Fix crash for single rest on class declaration (#315) I thought I didn't need to check the length but forgot that the rest argument is not in the list for class declaration. Now it doesn't crash anymore and there's a test... --- src/printer.js | 2 +- tests/rest/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ tests/rest/trailing-commas.js | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/printer.js b/src/printer.js index 344fd354..8408f434 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1741,7 +1741,7 @@ function printFunctionParams(path, print, options) { } const lastParam = util.getLast(path.getValue().params); - const canHaveTrailingComma = lastParam.type !== "RestElement" && !fun.rest; + const canHaveTrailingComma = !(lastParam && lastParam.type === "RestElement") && !fun.rest; return concat([ "(", diff --git a/tests/rest/__snapshots__/jsfmt.spec.js.snap b/tests/rest/__snapshots__/jsfmt.spec.js.snap index 70b59847..79cff5aa 100644 --- a/tests/rest/__snapshots__/jsfmt.spec.js.snap +++ b/tests/rest/__snapshots__/jsfmt.spec.js.snap @@ -10,6 +10,8 @@ function f( superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong, ...args ) {} + +declare class C { f(...superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong): void; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ declare class C { f( @@ -23,5 +25,11 @@ function f( ...args ) { } + +declare class C { + f( + ...superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong + ): void, +} " `; diff --git a/tests/rest/trailing-commas.js b/tests/rest/trailing-commas.js index 39ebdccd..65a05498 100644 --- a/tests/rest/trailing-commas.js +++ b/tests/rest/trailing-commas.js @@ -9,3 +9,5 @@ function f( superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong, ...args ) {} + +declare class C { f(...superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLong): void; }