diff --git a/src/printer.js b/src/printer.js index 86987dc30..2f62dcc5a 100644 --- a/src/printer.js +++ b/src/printer.js @@ -2189,9 +2189,25 @@ function printFunctionParams(path, print, options) { return concat(["(", join(", ", printed), ")"]); } + // Single object destructuring should hug + // + // function({ + // a, + // b, + // c + // }) {} + if (fun.params.length === 1 && + !fun.params[0].comments && + (fun.params[0].type === "ObjectPattern" || + fun.params[0].type === "FunctionTypeParam" && + fun.params[0].typeAnnotation.type === "ObjectTypeAnnotation") && + !fun.rest) { + return concat(["(", join(", ", printed), ")"]); + } + const isFlowShorthandWithOneArg = (isObjectTypePropertyAFunction(parent) || isTypeAnnotationAFunction(parent) || parent.type === "TypeAlias") && - fun[paramsField].length === 1 && fun[paramsField][0].name === null && fun.rest === null; + fun[paramsField].length === 1 && fun[paramsField][0].name === null && !fun.rest; return concat([ isFlowShorthandWithOneArg ? "" : "(", diff --git a/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap b/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..81025372d --- /dev/null +++ b/tests/function_single_destructuring/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,126 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`test.js 1`] = ` +"function StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, +}) { + return
+} + +const StatelessFunctionalComponent = ({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, +}: { + isActive: number, + onFiltersUpdated: number, + onSelect: number, + onSubmitAndDeselect: number, + onCancel: number, + searchFilters: number, + title: number, + items: number, +}) => { + return +}; + +class C { + StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, + }) { + return + } +} + +type T = ({ + isActive: number, + onFiltersUpdated: number, + onSelect: number, + onSubmitAndDeselect: number, + onCancel: number, + searchFilters: number, + title: number, + items: number, +}) => void; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items +}) { + return ; +} + +const StatelessFunctionalComponent = ({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items +}: { + isActive: number, + onFiltersUpdated: number, + onSelect: number, + onSubmitAndDeselect: number, + onCancel: number, + searchFilters: number, + title: number, + items: number +}) => { + return ; +}; + +class C { + StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items + }) { + return ; + } +} + +type T = ({ + isActive: number, + onFiltersUpdated: number, + onSelect: number, + onSubmitAndDeselect: number, + onCancel: number, + searchFilters: number, + title: number, + items: number +}) => void; +" +`; diff --git a/tests/function_single_destructuring/jsfmt.spec.js b/tests/function_single_destructuring/jsfmt.spec.js new file mode 100644 index 000000000..989047bcc --- /dev/null +++ b/tests/function_single_destructuring/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname); diff --git a/tests/function_single_destructuring/test.js b/tests/function_single_destructuring/test.js new file mode 100644 index 000000000..0ef346dea --- /dev/null +++ b/tests/function_single_destructuring/test.js @@ -0,0 +1,60 @@ +function StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, +}) { + return +} + +const StatelessFunctionalComponent = ({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, +}: { + isActive: number, + onFiltersUpdated: number, + onSelect: number, + onSubmitAndDeselect: number, + onCancel: number, + searchFilters: number, + title: number, + items: number, +}) => { + return +}; + +class C { + StatelessFunctionalComponent({ + isActive, + onFiltersUpdated, + onSelect, + onSubmitAndDeselect, + onCancel, + searchFilters, + title, + items, + }) { + return + } +} + +type T = ({ + isActive: number, + onFiltersUpdated: number, + onSelect: number, + onSubmitAndDeselect: number, + onCancel: number, + searchFilters: number, + title: number, + items: number, +}) => void;