From cea254857afcf8212abc603c8c8c9b3b99b10ae6 Mon Sep 17 00:00:00 2001 From: James Long Date: Fri, 24 Feb 2017 00:21:13 -0500 Subject: [PATCH] Eagerly evaluate `ifBreak` when processing template literals (fixes #795) (#798) --- src/printer.js | 3 + .../strings/__snapshots__/jsfmt.spec.js.snap | 126 ++++++++++++++++-- tests/strings/jsfmt.spec.js | 1 + tests/strings/strings.js | 10 -- tests/strings/template-literals.js | 13 ++ 5 files changed, 133 insertions(+), 20 deletions(-) create mode 100644 tests/strings/template-literals.js diff --git a/src/printer.js b/src/printer.js index 875448b1b..a6f3cb4d9 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1388,6 +1388,9 @@ function genericPrintNoParens(path, options, print) { if (d.type === "line" && !d.hard) { return d.soft ? "" : " "; } + else if(d.type === "if-break") { + return d.flatContents || ""; + } return d; }); } diff --git a/tests/strings/__snapshots__/jsfmt.spec.js.snap b/tests/strings/__snapshots__/jsfmt.spec.js.snap index cf292b02c..44d7a05d8 100644 --- a/tests/strings/__snapshots__/jsfmt.spec.js.snap +++ b/tests/strings/__snapshots__/jsfmt.spec.js.snap @@ -26,16 +26,6 @@ exports[`strings.js 1`] = ` '\\\\uD801\\\\uDC28', ]; - -foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 } with expr\`); - -const x = \`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() {return 3 })() + 3 + 2 + 3 + 2 + 3 } with expr\`; - -foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() { - const x = 5; - - return x; - })() + 3 + 2 + 3 + 2 + 3 } with expr\`); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [ \\"abc\\", @@ -61,17 +51,133 @@ foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( functi \\"🐶\\", '\\\\uD801\\\\uDC28' ]; +" +`; + +exports[`strings.js 2`] = ` +"[ + \\"abc\\", + 'abc', + + '\\\\'', + + '\\"', + '\\\\\\"', + '\\\\\\\\\\"', + + \\"'\\", + \\"\\\\'\\", + \\"\\\\\\\\'\\", + + \\"'\\\\\\"\\", + '\\\\'\\"', + + '\\\\\\\\', + \\"\\\\\\\\\\", + + '\\\\0', + '🐶', + + '\\\\uD801\\\\uDC28', +]; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +[ + \\"abc\\", + \\"abc\\", + + \\"'\\", + + '\\"', + '\\"', + '\\\\\\\\\\"', + + \\"'\\", + \\"'\\", + \\"\\\\\\\\'\\", + + \\"'\\\\\\"\\", + \\"'\\\\\\"\\", + + \\"\\\\\\\\\\", + \\"\\\\\\\\\\", + + \\"\\\\0\\", + \\"🐶\\", + '\\\\uD801\\\\uDC28', +]; +" +`; + +exports[`template-literals.js 1`] = ` +"foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 } with expr\`); + +const x = \`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() {return 3 })() + 3 + 2 + 3 + 2 + 3 } with expr\`; + +foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() { + const x = 5; + + return x; + })() + 3 + 2 + 3 + 2 + 3 } with expr\`); + +pipe.write( + \`\\\\n \${chalk.dim(\`\\\\u203A and \${more} more \${more} more \${more} more \${more}\`)}\`, +); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ foo( \`a long string \${1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3} with expr\` ); + const x = \`a long string \${1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + (function() { return 3; })() + 3 + 2 + 3 + 2 + 3} with expr\`; + foo( \`a long string \${1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + (function() { const x = 5; + return x; })() + 3 + 2 + 3 + 2 + 3} with expr\` ); + +pipe.write( + \`\\\\n \${chalk.dim(\`\\\\u203A and \${more} more \${more} more \${more} more \${more}\`)}\` +); +" +`; + +exports[`template-literals.js 2`] = ` +"foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 } with expr\`); + +const x = \`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() {return 3 })() + 3 + 2 + 3 + 2 + 3 } with expr\`; + +foo(\`a long string \${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() { + const x = 5; + + return x; + })() + 3 + 2 + 3 + 2 + 3 } with expr\`); + +pipe.write( + \`\\\\n \${chalk.dim(\`\\\\u203A and \${more} more \${more} more \${more} more \${more}\`)}\`, +); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +foo( + \`a long string \${1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3} with expr\`, +); + +const x = \`a long string \${1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + (function() { + return 3; + })() + 3 + 2 + 3 + 2 + 3} with expr\`; + +foo( + \`a long string \${1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + (function() { + const x = 5; + + return x; + })() + 3 + 2 + 3 + 2 + 3} with expr\`, +); + +pipe.write( + \`\\\\n \${chalk.dim(\`\\\\u203A and \${more} more \${more} more \${more} more \${more}\`)}\`, +); " `; diff --git a/tests/strings/jsfmt.spec.js b/tests/strings/jsfmt.spec.js index 989047bcc..9b85c446c 100644 --- a/tests/strings/jsfmt.spec.js +++ b/tests/strings/jsfmt.spec.js @@ -1 +1,2 @@ run_spec(__dirname); +run_spec(__dirname, { trailingComma: "all" }); diff --git a/tests/strings/strings.js b/tests/strings/strings.js index 8eea02e75..d31dceb2b 100644 --- a/tests/strings/strings.js +++ b/tests/strings/strings.js @@ -23,13 +23,3 @@ '\uD801\uDC28', ]; - -foo(`a long string ${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 } with expr`); - -const x = `a long string ${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() {return 3 })() + 3 + 2 + 3 + 2 + 3 } with expr`; - -foo(`a long string ${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() { - const x = 5; - - return x; - })() + 3 + 2 + 3 + 2 + 3 } with expr`); diff --git a/tests/strings/template-literals.js b/tests/strings/template-literals.js new file mode 100644 index 000000000..21befa4e6 --- /dev/null +++ b/tests/strings/template-literals.js @@ -0,0 +1,13 @@ +foo(`a long string ${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 } with expr`); + +const x = `a long string ${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() {return 3 })() + 3 + 2 + 3 + 2 + 3 } with expr`; + +foo(`a long string ${ 1 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + 3 + 2 + ( function() { + const x = 5; + + return x; + })() + 3 + 2 + 3 + 2 + 3 } with expr`); + +pipe.write( + `\n ${chalk.dim(`\u203A and ${more} more ${more} more ${more} more ${more}`)}`, +);