From 2740ea6ee8bea4dd99969c693c84928633a80dfa Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Sun, 14 Mar 2021 12:15:53 +0200 Subject: [PATCH] Avoid last-argument hugging for number-only arrays (#10517) --- changelog_unreleased/javascript/10517.md | 33 +++++++++++++++ src/language-js/print/array.js | 40 ++++++++++--------- src/language-js/print/call-arguments.js | 15 +++++-- .../__snapshots__/jsfmt.spec.js.snap | 24 +++++++++++ .../number-only-array.js | 4 ++ 5 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 changelog_unreleased/javascript/10517.md create mode 100644 tests/js/last-argument-expansion/number-only-array.js diff --git a/changelog_unreleased/javascript/10517.md b/changelog_unreleased/javascript/10517.md new file mode 100644 index 000000000..abfdcf5ea --- /dev/null +++ b/changelog_unreleased/javascript/10517.md @@ -0,0 +1,33 @@ +#### Avoid last-argument hugging for number-only arrays (#10517 by @thorn0) + +Another special case for number-only arrays. + + +```jsx +// Input +instantiate(game, [ + transform([-0.7, 0.5, 0]), + render_colored_diffuse(game.MaterialDiffuse, game.Meshes["monkey_flat"], [1, 1, 0.3, 1]), +]); + +// Prettier stable +instantiate(game, [ + transform([-0.7, 0.5, 0]), + render_colored_diffuse(game.MaterialDiffuse, game.Meshes["monkey_flat"], [ + 1, + 1, + 0.3, + 1, + ]), +]); + +// Prettier main +instantiate(game, [ + transform([-0.7, 0.5, 0]), + render_colored_diffuse( + game.MaterialDiffuse, + game.Meshes["monkey_flat"], + [1, 1, 0.3, 1] + ), +]); +``` diff --git a/src/language-js/print/array.js b/src/language-js/print/array.js index aa16d383b..0f06dae53 100644 --- a/src/language-js/print/array.js +++ b/src/language-js/print/array.js @@ -80,23 +80,7 @@ function printArray(path, options, print) { return element[itemsKey] && element[itemsKey].length > 1; }); - const shouldUseConciseFormatting = - node.elements.length > 1 && - node.elements.every( - (element) => - element && - (isNumericLiteral(element) || - (isSignedNumericLiteral(element) && - !hasComment(element.argument))) && - !hasComment( - element, - CommentCheckFlags.Trailing | CommentCheckFlags.Line, - (comment) => - !hasNewline(options.originalText, locStart(comment), { - backwards: true, - }) - ) - ); + const shouldUseConciseFormatting = isConciselyPrintedArray(node, options); const trailingComma = !canHaveTrailingComma ? "" @@ -138,6 +122,26 @@ function printArray(path, options, print) { return parts; } +function isConciselyPrintedArray(node, options) { + return ( + node.elements.length > 1 && + node.elements.every( + (element) => + element && + (isNumericLiteral(element) || + (isSignedNumericLiteral(element) && !hasComment(element.argument))) && + !hasComment( + element, + CommentCheckFlags.Trailing | CommentCheckFlags.Line, + (comment) => + !hasNewline(options.originalText, locStart(comment), { + backwards: true, + }) + ) + ) + ); +} + function printArrayItems(path, options, printPath, print) { const printedElements = []; let separatorParts = []; @@ -182,4 +186,4 @@ function printArrayItemsConcisely(path, options, print, trailingComma) { return fill(parts); } -module.exports = { printArray, printArrayItems }; +module.exports = { printArray, printArrayItems, isConciselyPrintedArray }; diff --git a/src/language-js/print/call-arguments.js b/src/language-js/print/call-arguments.js index eb1a205f8..c843bf5ee 100644 --- a/src/language-js/print/call-arguments.js +++ b/src/language-js/print/call-arguments.js @@ -31,6 +31,8 @@ const { utils: { willBreak }, } = require("../../document"); +const { isConciselyPrintedArray } = require("./array"); + function printCallArguments(path, options, print) { const node = path.getValue(); const isDynamicImport = node.type === "ImportExpression"; @@ -133,7 +135,7 @@ function printCallArguments(path, options, print) { } const shouldGroupFirst = shouldGroupFirstArg(args); - const shouldGroupLast = shouldGroupLastArg(args); + const shouldGroupLast = shouldGroupLastArg(args, options); if (shouldGroupFirst || shouldGroupLast) { const shouldBreak = (shouldGroupFirst @@ -255,7 +257,7 @@ function couldGroupArg(arg, arrowChainRecursion = false) { ); } -function shouldGroupLastArg(args) { +function shouldGroupLastArg(args, options) { const lastArg = getLast(args); const penultimateArg = getPenultimate(args); return ( @@ -267,8 +269,13 @@ function shouldGroupLastArg(args) { (!penultimateArg || penultimateArg.type !== lastArg.type) && // useMemo(() => func(), [foo, bar, baz]) (args.length !== 2 || - args[0].type !== "ArrowFunctionExpression" || - args[1].type !== "ArrayExpression") + penultimateArg.type !== "ArrowFunctionExpression" || + lastArg.type !== "ArrayExpression") && + !( + args.length > 1 && + lastArg.type === "ArrayExpression" && + isConciselyPrintedArray(lastArg, options) + ) ); } diff --git a/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap b/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap index 7988c9b81..a29b175a7 100644 --- a/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/last-argument-expansion/__snapshots__/jsfmt.spec.js.snap @@ -404,6 +404,30 @@ const els = items.map((item) => ( ================================================================================ `; +exports[`number-only-array.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +instantiate(game, [ + transform([-0.7, 0.5, 0]), + render_colored_diffuse(game.MaterialDiffuse, game.Meshes["monkey_flat"], [1, 1, 0.3, 1]), +]); + +=====================================output===================================== +instantiate(game, [ + transform([-0.7, 0.5, 0]), + render_colored_diffuse( + game.MaterialDiffuse, + game.Meshes["monkey_flat"], + [1, 1, 0.3, 1] + ), +]); + +================================================================================ +`; + exports[`object.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow"] diff --git a/tests/js/last-argument-expansion/number-only-array.js b/tests/js/last-argument-expansion/number-only-array.js new file mode 100644 index 000000000..0ba0591cf --- /dev/null +++ b/tests/js/last-argument-expansion/number-only-array.js @@ -0,0 +1,4 @@ +instantiate(game, [ + transform([-0.7, 0.5, 0]), + render_colored_diffuse(game.MaterialDiffuse, game.Meshes["monkey_flat"], [1, 1, 0.3, 1]), +]);