From 76e599ec9731a98cba8ad72b146bbc56d29b4bea Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 13 Feb 2017 06:55:09 -0800 Subject: [PATCH] Avoid unneeded parenthesis for colon with comments (#673) Because the group was too high up, the comment would be taken into consideration to determine the size and it would break and add parenthesis. Adding a group where we actually want the ifBreak seems to be passing all the existing tests and fixes this edge case. Fixes #655 --- src/printer.js | 4 ++-- .../__snapshots__/jsfmt.spec.js.snap | 17 +++++++++++++++++ tests/object-prop-break-in/comment.js | 6 ++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/object-prop-break-in/comment.js diff --git a/src/printer.js b/src/printer.js index 9a46ef7e..95acac03 100644 --- a/src/printer.js +++ b/src/printer.js @@ -684,13 +684,13 @@ function genericPrintNoParens(path, options, print) { parts.push(concat([": ", printedValue])); } else { parts.push( - concat([ + group(concat([ ":", ifBreak(" (", " "), indent(options.tabWidth, concat([softline, printedValue])), softline, ifBreak(")") - ]) + ])) ); } } diff --git a/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap b/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap index 1bc4417f..cb925d85 100644 --- a/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap +++ b/tests/object-prop-break-in/__snapshots__/jsfmt.spec.js.snap @@ -1,3 +1,20 @@ +exports[`test comment.js 1`] = ` +"function foo() { + return { + // this comment causes the problem + bar: baz() + 1 + }; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +function foo() { + return { + // this comment causes the problem + bar: baz() + 1 + }; +} +" +`; + exports[`test test.js 1`] = ` "const a = classnames({ \"some-prop\": this.state.longLongLongLongLongLongLongLongLongTooLongProp diff --git a/tests/object-prop-break-in/comment.js b/tests/object-prop-break-in/comment.js new file mode 100644 index 00000000..470a8581 --- /dev/null +++ b/tests/object-prop-break-in/comment.js @@ -0,0 +1,6 @@ +function foo() { + return { + // this comment causes the problem + bar: baz() + 1 + }; +}