From 41dee7079565809d7ffb646d83c0db98f404f15d Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Fri, 7 Apr 2017 10:51:02 -0700 Subject: [PATCH] Allow to break after = for strings (#1142) This is not a general fix but it solves some issues that people are seeing where prettier goes > 80 columns. Fixes #1141 Also fixes part of #1110 --- src/printer.js | 7 ++++++- .../testproj2/__snapshots__/jsfmt.spec.js.snap | 3 ++- .../variable_declarator/__snapshots__/jsfmt.spec.js.snap | 9 +++++++++ tests/variable_declarator/jsfmt.spec.js | 1 + tests/variable_declarator/string.js | 1 + 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/variable_declarator/jsfmt.spec.js create mode 100644 tests/variable_declarator/string.js diff --git a/src/printer.js b/src/printer.js index 6a88f722b..93a307730 100644 --- a/src/printer.js +++ b/src/printer.js @@ -3070,7 +3070,12 @@ function printAssignment(printedLeft, operator, rightNode, printedRight, options printed = indent( concat([hardline, printedRight]) ); - } else if (isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) { + } else if ( + (isBinaryish(rightNode) && !shouldInlineLogicalExpression(rightNode)) || + rightNode.type === "StringLiteral" || + (rightNode.type === "Literal" && + typeof rightNode.value === "string") + ) { printed = indent( concat([line, printedRight]) ); diff --git a/tests/flow/config_module_system_node_resolve_dirname/subdir/custom_resolve_dir/testproj2/__snapshots__/jsfmt.spec.js.snap b/tests/flow/config_module_system_node_resolve_dirname/subdir/custom_resolve_dir/testproj2/__snapshots__/jsfmt.spec.js.snap index c63d663fd..839f1fcf1 100644 --- a/tests/flow/config_module_system_node_resolve_dirname/subdir/custom_resolve_dir/testproj2/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/config_module_system_node_resolve_dirname/subdir/custom_resolve_dir/testproj2/__snapshots__/jsfmt.spec.js.snap @@ -7,6 +7,7 @@ export var name: \\"subdir/custom_resolve_dir/testproj2\\" = \\"subdir/custom_re ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // @flow -export var name: \\"subdir/custom_resolve_dir/testproj2\\" = \\"subdir/custom_resolve_dir/testproj2\\"; +export var name: \\"subdir/custom_resolve_dir/testproj2\\" = + \\"subdir/custom_resolve_dir/testproj2\\"; " `; diff --git a/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap b/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..9d35090a9 --- /dev/null +++ b/tests/variable_declarator/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,9 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`string.js 1`] = ` +"elements[0].innerHTML = '
'; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +elements[0].innerHTML = + \\"
\\"; +" +`; diff --git a/tests/variable_declarator/jsfmt.spec.js b/tests/variable_declarator/jsfmt.spec.js new file mode 100644 index 000000000..c426c19cc --- /dev/null +++ b/tests/variable_declarator/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, null, ["babylon", "typescript"]); diff --git a/tests/variable_declarator/string.js b/tests/variable_declarator/string.js new file mode 100644 index 000000000..3c53a75cf --- /dev/null +++ b/tests/variable_declarator/string.js @@ -0,0 +1 @@ +elements[0].innerHTML = '
';