From 24433cbbf61986d08cdc4cebe268d3d25c5e395d Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Mon, 25 Jan 2021 10:07:06 +0200 Subject: [PATCH] Use fill to print number-only arrays (#10106) --- changelog_unreleased/javascript/10106.md | 45 ++ src/language-js/print/array.js | 84 ++- src/language-js/utils.js | 9 + .../arrays/__snapshots__/jsfmt.spec.js.snap | 634 +++++++++++++++++- tests/js/arrays/numbers-in-args.js | 13 + tests/js/arrays/numbers-in-assignment.js | 15 + .../numbers-negative-comment-after-minus.js | 11 + tests/js/arrays/numbers-negative.js | 11 + tests/js/arrays/numbers-trailing-comma.js | 4 + tests/js/arrays/numbers-with-holes.js | 29 + .../arrays/numbers-with-trailing-comments.js | 11 + .../js/arrays/numbers-with-tricky-comments.js | 7 + tests/js/arrays/numbers1.js | 102 +++ tests/js/arrays/numbers2.js | 49 ++ tests/js/arrays/numbers3.js | 16 + .../json/__snapshots__/jsfmt.spec.js.snap | 24 +- 16 files changed, 1045 insertions(+), 19 deletions(-) create mode 100644 changelog_unreleased/javascript/10106.md create mode 100644 tests/js/arrays/numbers-in-args.js create mode 100644 tests/js/arrays/numbers-in-assignment.js create mode 100644 tests/js/arrays/numbers-negative-comment-after-minus.js create mode 100644 tests/js/arrays/numbers-negative.js create mode 100644 tests/js/arrays/numbers-trailing-comma.js create mode 100644 tests/js/arrays/numbers-with-holes.js create mode 100644 tests/js/arrays/numbers-with-trailing-comments.js create mode 100644 tests/js/arrays/numbers-with-tricky-comments.js create mode 100644 tests/js/arrays/numbers1.js create mode 100644 tests/js/arrays/numbers2.js create mode 100644 tests/js/arrays/numbers3.js diff --git a/changelog_unreleased/javascript/10106.md b/changelog_unreleased/javascript/10106.md new file mode 100644 index 000000000..63c2da6c5 --- /dev/null +++ b/changelog_unreleased/javascript/10106.md @@ -0,0 +1,45 @@ +#### Concise formatting of number-only arrays (#10106 by @thorn0) + +While in general, Prettier avoids this kind of formatting because it's not diff-friendly, in this special case we decided that the benefits outweigh the risks. + +If at least one element has a trailing single-line (`// ...`) comment on the same line, the concise formatting isn't applied. On the other hand, single-line comments placed on separate lines don't have such an effect and – as well as empty lines – can be used for logical grouping. + + +```jsx +// Input +const lazyCatererNumbers = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46, + +// n > 10 +56, 67, 79, 92, 106, 121, 137, 154, 172, 191, 211, 232, 254, 277, 301, 326, 352, 379, 407, 436, 466, +497, 529, 562, 596, 631, 667, 704, 742, 781, +// n > 40 +821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226, 1276, 1327, 1379]; + +// Prettier stable +const lazyCatererNumbers = [ + 1, + 2, + 4, + 7, + 11, + 16, + 22, + 29, + 37, + // ... ✂ a lot of lines ✂ ... + 1379, +]; + + +// Prettier main +const lazyCatererNumbers = [ + 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, + + // n > 10 + 56, 67, 79, 92, 106, 121, 137, 154, 172, 191, 211, 232, 254, 277, 301, 326, + 352, 379, 407, 436, 466, 497, 529, 562, 596, 631, 667, 704, 742, 781, + // n > 40 + 821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226, 1276, 1327, 1379, +]; + +``` diff --git a/src/language-js/print/array.js b/src/language-js/print/array.js index d8ad213da..d503caf56 100644 --- a/src/language-js/print/array.js +++ b/src/language-js/print/array.js @@ -2,15 +2,18 @@ const { printDanglingComments } = require("../../main/comments"); const { - builders: { line, softline, group, indent, ifBreak }, + builders: { line, softline, hardline, group, indent, ifBreak, fill }, } = require("../../document"); -const { getLast } = require("../../common/util"); +const { getLast, hasNewline } = require("../../common/util"); const { shouldPrintComma, hasComment, CommentCheckFlags, isNextLineEmpty, + isNumericLiteral, + isSignedNumericLiteral, } = require("../utils"); +const { locStart } = require("../loc"); const { printOptionalToken, printTypeAnnotation } = require("./misc"); @@ -50,7 +53,17 @@ function printArray(path, options, print) { // // Note that getLast returns null if the array is empty, but // we already check for an empty array just above so we are safe - const needsForcedTrailingComma = canHaveTrailingComma && lastElem === null; + const needsForcedTrailingComma = lastElem === null; + + const groupId = Symbol("array"); + + const trailingComma = !canHaveTrailingComma + ? "" + : needsForcedTrailingComma + ? "," + : !shouldPrintComma(options) + ? "" + : ifBreak(",", "", { groupId }); const shouldBreak = !options.__inJestEach && @@ -75,24 +88,42 @@ function printArray(path, options, print) { return element[itemsKey] && element[itemsKey].length > 1; }); + const shouldUseConciseFormatting = + n.elements.length > 1 && + n.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, + }) + ) + ); + parts.push( group( [ openBracket, - indent([softline, printArrayItems(path, options, "elements", print)]), - needsForcedTrailingComma ? "," : "", - ifBreak( - canHaveTrailingComma && - !needsForcedTrailingComma && - shouldPrintComma(options) - ? "," - : "" - ), - printDanglingComments(path, options, /* sameIndent */ true), + indent([ + softline, + shouldUseConciseFormatting + ? printArrayItemsConcisely(path, options, print, trailingComma) + : [ + printArrayItems(path, options, "elements", print), + trailingComma, + ], + printDanglingComments(path, options, /* sameIndent */ true), + ]), softline, closeBracket, ], - { shouldBreak } + { shouldBreak, id: groupId } ) ); } @@ -124,4 +155,29 @@ function printArrayItems(path, options, printPath, print) { return printedElements; } +function printArrayItemsConcisely(path, options, print, trailingComma) { + const parts = []; + + path.each((childPath, i, elements) => { + const isLast = i === elements.length - 1; + + parts.push([print(childPath), isLast ? trailingComma : ","]); + + if (!isLast) { + parts.push( + isNextLineEmpty(childPath.getValue(), options) + ? [hardline, hardline] + : hasComment( + elements[i + 1], + CommentCheckFlags.Leading | CommentCheckFlags.Line + ) + ? hardline + : line + ); + } + }, "elements"); + + return fill(parts); +} + module.exports = { printArray, printArrayItems }; diff --git a/src/language-js/utils.js b/src/language-js/utils.js index 3bade04f2..56eceef21 100644 --- a/src/language-js/utils.js +++ b/src/language-js/utils.js @@ -247,6 +247,14 @@ function isNumericLiteral(node) { ); } +function isSignedNumericLiteral(node) { + return ( + node.type === "UnaryExpression" && + (node.operator === "+" || node.operator === "-") && + isNumericLiteral(node.argument) + ); +} + /** * @param {Node} node * @returns {boolean} @@ -1362,6 +1370,7 @@ module.exports = { isMemberExpressionChain, isMemberish, isNumericLiteral, + isSignedNumericLiteral, isObjectType, isObjectTypePropertyAFunction, isSimpleType, diff --git a/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap b/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap index 9c807266f..9d428d70d 100644 --- a/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/arrays/__snapshots__/jsfmt.spec.js.snap @@ -165,6 +165,632 @@ printWidth: 80 ================================================================================ `; +exports[`numbers-in-args.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +expect(bifornCringerMoshedPerplexSawder.getArrayOfNumbers()).toEqual( + [1, 2, 3, 4, 5] +); + +expect(bifornCringerMoshedPerplexSawder.getLongArrayOfNumbers()).toEqual( + [ + 66,57,45,47,33,53,82,81,76,78,10,78,15,98,24,29,32,27,28,76,41,65,84,35, + 97,90,75,24,88,45,23,75,63,86,24,39,9,51,33,40,58,17,49,86,63,59,97,91, + 98,99,5,69,51,44,34,69,17,91,27,83,26,34,93,29,66,88,49,33,49,73,9,81,4, + 36,5,14,43,31,86,27,39,75,98,99,55,19,39,21,85,86,46,82,11,44,48,77,35, + 48,78,97 + ] +); + +=====================================output===================================== +expect(bifornCringerMoshedPerplexSawder.getArrayOfNumbers()).toEqual([ + 1, 2, 3, 4, 5, +]); + +expect(bifornCringerMoshedPerplexSawder.getLongArrayOfNumbers()).toEqual([ + 66, 57, 45, 47, 33, 53, 82, 81, 76, 78, 10, 78, 15, 98, 24, 29, 32, 27, 28, + 76, 41, 65, 84, 35, 97, 90, 75, 24, 88, 45, 23, 75, 63, 86, 24, 39, 9, 51, 33, + 40, 58, 17, 49, 86, 63, 59, 97, 91, 98, 99, 5, 69, 51, 44, 34, 69, 17, 91, 27, + 83, 26, 34, 93, 29, 66, 88, 49, 33, 49, 73, 9, 81, 4, 36, 5, 14, 43, 31, 86, + 27, 39, 75, 98, 99, 55, 19, 39, 21, 85, 86, 46, 82, 11, 44, 48, 77, 35, 48, + 78, 97, +]); + +================================================================================ +`; + +exports[`numbers-in-assignment.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumbers = [ + 1, + 2, + 3, + 4, + 5 +]; + +bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumbers2 = [ + 66,57,45,47,33,53,82,81,76,78,10,78,15,98,24,29,32,27,28,76,41,65,84,35, + 97,90,75,24,88,45,23,75,63,86,24,39,9,51,33,40,58,17,49,86,63,59,97,91, + 98,99,5,69,51,44,34,69,17,91,27,83,26,34,93,29,66,88,49,33,49,73,9,81,4, + 36,5,14,43,31,86,27,39,75,98,99,55,19,39,21,85,86,46,82,11,44,48,77,35, + 48,78,97 +]; + +=====================================output===================================== +bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumbers = [ + 1, 2, 3, 4, 5, +]; + +bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumbers2 = [ + 66, 57, 45, 47, 33, 53, 82, 81, 76, 78, 10, 78, 15, 98, 24, 29, 32, 27, 28, + 76, 41, 65, 84, 35, 97, 90, 75, 24, 88, 45, 23, 75, 63, 86, 24, 39, 9, 51, 33, + 40, 58, 17, 49, 86, 63, 59, 97, 91, 98, 99, 5, 69, 51, 44, 34, 69, 17, 91, 27, + 83, 26, 34, 93, 29, 66, 88, 49, 33, 49, 73, 9, 81, 4, 36, 5, 14, 43, 31, 86, + 27, 39, 75, 98, 99, 55, 19, 39, 21, 85, 86, 46, 82, 11, 44, 48, 77, 35, 48, + 78, 97, +]; + +================================================================================ +`; + +exports[`numbers-negative.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const numbers1 = [-2017,-506252,-744011292,-7224,-70.4,-83353.6,-708.4,-174023963.52,-40385, +// comment1 +-380014, +-253951682,-728,-15.84,-2058467564.56,-43,-33,-85134845,-67092,-1,-78820379,-2371.6,-16,7, +// comment2 +-62454,-4282239912, +-10816495.36,0.88,-100622682,8.8,-67087.68000000001,-3758276,-25.5211,-54,-1184265243,-46073628,-280423.44, +-41833463,-27961.12,-305.36,-199875.28]; + +const numbers2 = [-234, -342 // comment3 +, -223, -333333.33,12345] + +=====================================output===================================== +const numbers1 = [ + -2017, -506252, -744011292, -7224, -70.4, -83353.6, -708.4, -174023963.52, + -40385, + // comment1 + -380014, -253951682, -728, -15.84, -2058467564.56, -43, -33, -85134845, + -67092, -1, -78820379, -2371.6, -16, 7, + // comment2 + -62454, -4282239912, -10816495.36, 0.88, -100622682, 8.8, -67087.68000000001, + -3758276, -25.5211, -54, -1184265243, -46073628, -280423.44, -41833463, + -27961.12, -305.36, -199875.28, +]; + +const numbers2 = [ + -234, + -342, // comment3 + -223, + -333333.33, + 12345, +]; + +================================================================================ +`; + +exports[`numbers-negative-comment-after-minus.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const numbers = [-2017,-506252,-744011292,-7224,-70.4,-83353.6,-708.4,-174023963.52,-40385,-// comment1 +380014, +-253951682,-728,-15.84,-2058467564.56,-43,-33,-85134845,-67092,-1,-78820379,-2371.6,-16,7, +// comment2 +-62454,-4282239912, +-10816495.36,0.88,-100622682,8.8,-67087.68000000001,-3758276,-25.5211,-54,-1184265243,-46073628,-280423.44, +-41833463,-27961.12,-305.36,-199875.28]; + +c = [ + - /**/ 66, 66, 57, 45, 47, 33, 53, 82, 81, 76, 66, 57, 45, 47, 33, 53, 82, 81, 223323 +]; + +=====================================output===================================== +const numbers = [ + -2017, + -506252, + -744011292, + -7224, + -70.4, + -83353.6, + -708.4, + -174023963.52, + -40385, + -( + // comment1 + 380014 + ), + -253951682, + -728, + -15.84, + -2058467564.56, + -43, + -33, + -85134845, + -67092, + -1, + -78820379, + -2371.6, + -16, + 7, + // comment2 + -62454, + -4282239912, + -10816495.36, + 0.88, + -100622682, + 8.8, + -67087.68000000001, + -3758276, + -25.5211, + -54, + -1184265243, + -46073628, + -280423.44, + -41833463, + -27961.12, + -305.36, + -199875.28, +]; + +c = [ + -(/**/ 66), + 66, + 57, + 45, + 47, + 33, + 53, + 82, + 81, + 76, + 66, + 57, + 45, + 47, + 33, + 53, + 82, + 81, + 223323, +]; + +================================================================================ +`; + +exports[`numbers-trailing-comma.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// --------------- print-width ------------------------------------------------- +c = [ + 66, 66, 57, 45, 47, 33, 53, 82, 81, 76, 66, 57, 45, 47, 33, 53, 82, 81, 223323, +]; + +=====================================output===================================== +// --------------- print-width ------------------------------------------------- +c = [ + 66, 66, 57, 45, 47, 33, 53, 82, 81, 76, 66, 57, 45, 47, 33, 53, 82, 81, + 223323, +]; + +================================================================================ +`; + +exports[`numbers-with-holes.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const numberWithHoles1 = [ + 7234932941, + 7234932722, + 7234932312, + // comment before a hole 1 + , + 7234932841, + , + 7234932843, + , + // comment after a hole 1 + 7234932436, +]; + +const numberWithHoles2 = [ + 0x234932941, + 0x234932722, + 0x234932312, + + // comment before a hole 2 + , + 0x234932841, + , + 0x234932843, + , + + // comment after a hole 2 + 0x234932436, +]; + +=====================================output===================================== +const numberWithHoles1 = [ + 7234932941, + 7234932722, + 7234932312, + , + // comment before a hole 1 + 7234932841, + , + 7234932843, + , + // comment after a hole 1 + 7234932436, +]; + +const numberWithHoles2 = [ + 0x234932941, + 0x234932722, + 0x234932312, + + , + // comment before a hole 2 + 0x234932841, + , + 0x234932843, + , + // comment after a hole 2 + 0x234932436, +]; + +================================================================================ +`; + +exports[`numbers-with-trailing-comments.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +function isUnusedDiagnostic(code) { + return [ + 6133, // '{0}' is declared but never used. + 6138, // Property '{0}' is declared but its value is never read. + 6192, // All imports in import declaration are unused. + 6196, // '{0}' is declared but its value is never read. + 6198, + 6199, + 6205, // All type parameters are unused. + ].includes(code); +} + +=====================================output===================================== +function isUnusedDiagnostic(code) { + return [ + 6133, // '{0}' is declared but never used. + 6138, // Property '{0}' is declared but its value is never read. + 6192, // All imports in import declaration are unused. + 6196, // '{0}' is declared but its value is never read. + 6198, + 6199, + 6205, // All type parameters are unused. + ].includes(code); +} + +================================================================================ +`; + +exports[`numbers-with-tricky-comments.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const lazyCatererNumbers = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46, +56, 67, 79, 92, 106, 121, 137, 154, 172, 191, 211, 232, 254, 277, 301, 326, 352, 379, 407, 436, 466, /*block*/ +// line +497, 529, 562, 596, 631, 667, 704, 742, 781, +821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226, +// line 2 +1276, 1327, 1379]; + +=====================================output===================================== +const lazyCatererNumbers = [ + 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79, 92, 106, 121, 137, 154, 172, + 191, 211, 232, 254, 277, 301, 326, 352, 379, 407, 436, 466 /*block*/, + // line + 497, 529, 562, 596, 631, 667, 704, 742, 781, 821, 862, 904, 947, 991, 1036, + 1082, 1129, 1177, 1226, + // line 2 + 1276, 1327, 1379, +]; + +================================================================================ +`; + +exports[`numbers1.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const numbers1 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + 7234932843, + 7234932978, + 7234932436, + 7234932687, + 7234932269, + 7234932573, + 7234932913, + 7234932873, + 7234932748, + 7234932354, + 7234932153, + 7234932181, + 7234932947, + 7234932563, + 7234932324, + 7234932952, + 7234932885, + 7234932911, + 7234932698, + 7234932248, + 7234932764, + 7234932431, + 7234932811, + 7234932344, + 7234932855, + 7234932430, + 7234932396, + 7234932981, + 7234932594, + 7234932131, + 7234932489, + 7234932552, + 7234932116, + 7234932833, + 7234932521, + 7234932252, + 7234932503, + 7234932540, + 7234932893, + 7234932736, + 7234932969, + 7234932145, + 7234932925, + 7234932417, + 7234932344, + 7234932108, + 7234932161, + 7234932777, + 7234932971, + 7234932159, + 7234932158, + 7234932908, + 7234932511, + 7234932876, + 7234932768, + 7234932284, + 7234932640, + 7234932309, + 7234932651, + 7234932292, + 7234932898, + 7234932284, + 7234932201, + 7234932506, + 7234932654, + 7234932840, + 7234932334, + 7234932246, + 7234932376, + 7234932398, + 7234932714, + 7234932134, + 7234932435, + 7234932181, + 7234932980, + 7234932594, + 7234932396, + 7234932100, + 7234932743, + 7234932812, + 7234932583, + 7234932622, + 7234932800, + 7234932310, + 7234932111, + 7234932537, + 7234932751, + 7234932920, + 7234932872, + 7234932700, + 7234932702, + 7234932655, + 7234932515, + 7234932298 +]; + +=====================================output===================================== +const numbers1 = [ + 7234932941, 7234932722, 7234932312, 7234932933, 7234932841, 7234932166, + 7234932843, 7234932978, 7234932436, 7234932687, 7234932269, 7234932573, + 7234932913, 7234932873, 7234932748, 7234932354, 7234932153, 7234932181, + 7234932947, 7234932563, 7234932324, 7234932952, 7234932885, 7234932911, + 7234932698, 7234932248, 7234932764, 7234932431, 7234932811, 7234932344, + 7234932855, 7234932430, 7234932396, 7234932981, 7234932594, 7234932131, + 7234932489, 7234932552, 7234932116, 7234932833, 7234932521, 7234932252, + 7234932503, 7234932540, 7234932893, 7234932736, 7234932969, 7234932145, + 7234932925, 7234932417, 7234932344, 7234932108, 7234932161, 7234932777, + 7234932971, 7234932159, 7234932158, 7234932908, 7234932511, 7234932876, + 7234932768, 7234932284, 7234932640, 7234932309, 7234932651, 7234932292, + 7234932898, 7234932284, 7234932201, 7234932506, 7234932654, 7234932840, + 7234932334, 7234932246, 7234932376, 7234932398, 7234932714, 7234932134, + 7234932435, 7234932181, 7234932980, 7234932594, 7234932396, 7234932100, + 7234932743, 7234932812, 7234932583, 7234932622, 7234932800, 7234932310, + 7234932111, 7234932537, 7234932751, 7234932920, 7234932872, 7234932700, + 7234932702, 7234932655, 7234932515, 7234932298, +]; + +================================================================================ +`; + +exports[`numbers2.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const userIds1 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, +]; + +const userIds2 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + 7234932843, + 7234932978, + 7234932436, +]; + +const userIds3 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + 7234932843, + + 7234932978, + 7234932436, +]; + +const userIds4 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + // comment 1 + 7234932843, + + 7234932978, + + // comment 2 + 7234932436, + // comment 3 +]; + + +=====================================output===================================== +const userIds1 = [7234932941, 7234932722, 7234932312, 7234932933]; + +const userIds2 = [ + 7234932941, 7234932722, 7234932312, 7234932933, 7234932841, 7234932166, + 7234932843, 7234932978, 7234932436, +]; + +const userIds3 = [ + 7234932941, 7234932722, 7234932312, 7234932933, 7234932841, 7234932166, + 7234932843, + + 7234932978, 7234932436, +]; + +const userIds4 = [ + 7234932941, 7234932722, 7234932312, 7234932933, 7234932841, 7234932166, + // comment 1 + 7234932843, + + 7234932978, + + // comment 2 + 7234932436, + // comment 3 +]; + +================================================================================ +`; + +exports[`numbers3.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +let test_case = [ + [ + 66,57,45,47,33,53,82,81,76,78,10,78,15,98,24,29,32,27,28,76,41,65,84,35, + 97,90,75,24,88,45,23,75,63,86,24,39,9,51,33,40,58,17,49,86,63,59,97,91, + 98,99,5,69,51,44,34,69,17,91,27,83,26,34,93,29,66,88,49,33,49,73,9,81,4, + 36,5,14,43,31,86,27,39,75,98,99,55,19,39,21,85,86,46,82,11,44,48,77,35, + 48,78,97 + ], + [ + 41,83,31,62,15,70,10,90,/*21,*/48,39,76,14,48,63,62,16,17,61,97,86,80,34,27, + 39,53,90,80,56,71,31,22,29,7,71,90,65,17,48,85,14,94,16,32,4,96,49,97, + 53,87,54,2,78,37,21,3,97,62,93,62,11,27,14,29,64,44,11,5,39,43,94,52,0, + 4,86,58,63,42,97,54,2,1,53,17,92,79,52,47,81,93,34,17,93,20,61,68,58,49, + 27,45 + ] +]; + +=====================================output===================================== +let test_case = [ + [ + 66, 57, 45, 47, 33, 53, 82, 81, 76, 78, 10, 78, 15, 98, 24, 29, 32, 27, 28, + 76, 41, 65, 84, 35, 97, 90, 75, 24, 88, 45, 23, 75, 63, 86, 24, 39, 9, 51, + 33, 40, 58, 17, 49, 86, 63, 59, 97, 91, 98, 99, 5, 69, 51, 44, 34, 69, 17, + 91, 27, 83, 26, 34, 93, 29, 66, 88, 49, 33, 49, 73, 9, 81, 4, 36, 5, 14, 43, + 31, 86, 27, 39, 75, 98, 99, 55, 19, 39, 21, 85, 86, 46, 82, 11, 44, 48, 77, + 35, 48, 78, 97, + ], + [ + 41, 83, 31, 62, 15, 70, 10, 90, /*21,*/ 48, 39, 76, 14, 48, 63, 62, 16, 17, + 61, 97, 86, 80, 34, 27, 39, 53, 90, 80, 56, 71, 31, 22, 29, 7, 71, 90, 65, + 17, 48, 85, 14, 94, 16, 32, 4, 96, 49, 97, 53, 87, 54, 2, 78, 37, 21, 3, 97, + 62, 93, 62, 11, 27, 14, 29, 64, 44, 11, 5, 39, 43, 94, 52, 0, 4, 86, 58, 63, + 42, 97, 54, 2, 1, 53, 17, 92, 79, 52, 47, 81, 93, 34, 17, 93, 20, 61, 68, + 58, 49, 27, 45, + ], +]; + +================================================================================ +`; + exports[`preserve_empty_lines.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] @@ -185,7 +811,13 @@ a = [ ] =====================================output===================================== -a = [1, 2, 3, 4]; +a = [ + 1, 2, + + 3, + + 4, +]; ================================================================================ `; diff --git a/tests/js/arrays/numbers-in-args.js b/tests/js/arrays/numbers-in-args.js new file mode 100644 index 000000000..1ff69eab3 --- /dev/null +++ b/tests/js/arrays/numbers-in-args.js @@ -0,0 +1,13 @@ +expect(bifornCringerMoshedPerplexSawder.getArrayOfNumbers()).toEqual( + [1, 2, 3, 4, 5] +); + +expect(bifornCringerMoshedPerplexSawder.getLongArrayOfNumbers()).toEqual( + [ + 66,57,45,47,33,53,82,81,76,78,10,78,15,98,24,29,32,27,28,76,41,65,84,35, + 97,90,75,24,88,45,23,75,63,86,24,39,9,51,33,40,58,17,49,86,63,59,97,91, + 98,99,5,69,51,44,34,69,17,91,27,83,26,34,93,29,66,88,49,33,49,73,9,81,4, + 36,5,14,43,31,86,27,39,75,98,99,55,19,39,21,85,86,46,82,11,44,48,77,35, + 48,78,97 + ] +); diff --git a/tests/js/arrays/numbers-in-assignment.js b/tests/js/arrays/numbers-in-assignment.js new file mode 100644 index 000000000..d7b6f66db --- /dev/null +++ b/tests/js/arrays/numbers-in-assignment.js @@ -0,0 +1,15 @@ +bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumbers = [ + 1, + 2, + 3, + 4, + 5 +]; + +bifornCringerMoshedPerplex.bifornCringerMoshedPerplexSawder.arrayOfNumbers2 = [ + 66,57,45,47,33,53,82,81,76,78,10,78,15,98,24,29,32,27,28,76,41,65,84,35, + 97,90,75,24,88,45,23,75,63,86,24,39,9,51,33,40,58,17,49,86,63,59,97,91, + 98,99,5,69,51,44,34,69,17,91,27,83,26,34,93,29,66,88,49,33,49,73,9,81,4, + 36,5,14,43,31,86,27,39,75,98,99,55,19,39,21,85,86,46,82,11,44,48,77,35, + 48,78,97 +]; diff --git a/tests/js/arrays/numbers-negative-comment-after-minus.js b/tests/js/arrays/numbers-negative-comment-after-minus.js new file mode 100644 index 000000000..2b6a54ae7 --- /dev/null +++ b/tests/js/arrays/numbers-negative-comment-after-minus.js @@ -0,0 +1,11 @@ +const numbers = [-2017,-506252,-744011292,-7224,-70.4,-83353.6,-708.4,-174023963.52,-40385,-// comment1 +380014, +-253951682,-728,-15.84,-2058467564.56,-43,-33,-85134845,-67092,-1,-78820379,-2371.6,-16,7, +// comment2 +-62454,-4282239912, +-10816495.36,0.88,-100622682,8.8,-67087.68000000001,-3758276,-25.5211,-54,-1184265243,-46073628,-280423.44, +-41833463,-27961.12,-305.36,-199875.28]; + +c = [ + - /**/ 66, 66, 57, 45, 47, 33, 53, 82, 81, 76, 66, 57, 45, 47, 33, 53, 82, 81, 223323 +]; diff --git a/tests/js/arrays/numbers-negative.js b/tests/js/arrays/numbers-negative.js new file mode 100644 index 000000000..968d3c4c8 --- /dev/null +++ b/tests/js/arrays/numbers-negative.js @@ -0,0 +1,11 @@ +const numbers1 = [-2017,-506252,-744011292,-7224,-70.4,-83353.6,-708.4,-174023963.52,-40385, +// comment1 +-380014, +-253951682,-728,-15.84,-2058467564.56,-43,-33,-85134845,-67092,-1,-78820379,-2371.6,-16,7, +// comment2 +-62454,-4282239912, +-10816495.36,0.88,-100622682,8.8,-67087.68000000001,-3758276,-25.5211,-54,-1184265243,-46073628,-280423.44, +-41833463,-27961.12,-305.36,-199875.28]; + +const numbers2 = [-234, -342 // comment3 +, -223, -333333.33,12345] diff --git a/tests/js/arrays/numbers-trailing-comma.js b/tests/js/arrays/numbers-trailing-comma.js new file mode 100644 index 000000000..80e5ba984 --- /dev/null +++ b/tests/js/arrays/numbers-trailing-comma.js @@ -0,0 +1,4 @@ +// --------------- print-width ------------------------------------------------- +c = [ + 66, 66, 57, 45, 47, 33, 53, 82, 81, 76, 66, 57, 45, 47, 33, 53, 82, 81, 223323, +]; diff --git a/tests/js/arrays/numbers-with-holes.js b/tests/js/arrays/numbers-with-holes.js new file mode 100644 index 000000000..101734233 --- /dev/null +++ b/tests/js/arrays/numbers-with-holes.js @@ -0,0 +1,29 @@ +const numberWithHoles1 = [ + 7234932941, + 7234932722, + 7234932312, + // comment before a hole 1 + , + 7234932841, + , + 7234932843, + , + // comment after a hole 1 + 7234932436, +]; + +const numberWithHoles2 = [ + 0x234932941, + 0x234932722, + 0x234932312, + + // comment before a hole 2 + , + 0x234932841, + , + 0x234932843, + , + + // comment after a hole 2 + 0x234932436, +]; diff --git a/tests/js/arrays/numbers-with-trailing-comments.js b/tests/js/arrays/numbers-with-trailing-comments.js new file mode 100644 index 000000000..cf789cea9 --- /dev/null +++ b/tests/js/arrays/numbers-with-trailing-comments.js @@ -0,0 +1,11 @@ +function isUnusedDiagnostic(code) { + return [ + 6133, // '{0}' is declared but never used. + 6138, // Property '{0}' is declared but its value is never read. + 6192, // All imports in import declaration are unused. + 6196, // '{0}' is declared but its value is never read. + 6198, + 6199, + 6205, // All type parameters are unused. + ].includes(code); +} diff --git a/tests/js/arrays/numbers-with-tricky-comments.js b/tests/js/arrays/numbers-with-tricky-comments.js new file mode 100644 index 000000000..6b0fb2128 --- /dev/null +++ b/tests/js/arrays/numbers-with-tricky-comments.js @@ -0,0 +1,7 @@ +const lazyCatererNumbers = [1, 2, 4, 7, 11, 16, 22, 29, 37, 46, +56, 67, 79, 92, 106, 121, 137, 154, 172, 191, 211, 232, 254, 277, 301, 326, 352, 379, 407, 436, 466, /*block*/ +// line +497, 529, 562, 596, 631, 667, 704, 742, 781, +821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226, +// line 2 +1276, 1327, 1379]; diff --git a/tests/js/arrays/numbers1.js b/tests/js/arrays/numbers1.js new file mode 100644 index 000000000..5bb0ddc2f --- /dev/null +++ b/tests/js/arrays/numbers1.js @@ -0,0 +1,102 @@ +const numbers1 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + 7234932843, + 7234932978, + 7234932436, + 7234932687, + 7234932269, + 7234932573, + 7234932913, + 7234932873, + 7234932748, + 7234932354, + 7234932153, + 7234932181, + 7234932947, + 7234932563, + 7234932324, + 7234932952, + 7234932885, + 7234932911, + 7234932698, + 7234932248, + 7234932764, + 7234932431, + 7234932811, + 7234932344, + 7234932855, + 7234932430, + 7234932396, + 7234932981, + 7234932594, + 7234932131, + 7234932489, + 7234932552, + 7234932116, + 7234932833, + 7234932521, + 7234932252, + 7234932503, + 7234932540, + 7234932893, + 7234932736, + 7234932969, + 7234932145, + 7234932925, + 7234932417, + 7234932344, + 7234932108, + 7234932161, + 7234932777, + 7234932971, + 7234932159, + 7234932158, + 7234932908, + 7234932511, + 7234932876, + 7234932768, + 7234932284, + 7234932640, + 7234932309, + 7234932651, + 7234932292, + 7234932898, + 7234932284, + 7234932201, + 7234932506, + 7234932654, + 7234932840, + 7234932334, + 7234932246, + 7234932376, + 7234932398, + 7234932714, + 7234932134, + 7234932435, + 7234932181, + 7234932980, + 7234932594, + 7234932396, + 7234932100, + 7234932743, + 7234932812, + 7234932583, + 7234932622, + 7234932800, + 7234932310, + 7234932111, + 7234932537, + 7234932751, + 7234932920, + 7234932872, + 7234932700, + 7234932702, + 7234932655, + 7234932515, + 7234932298 +]; diff --git a/tests/js/arrays/numbers2.js b/tests/js/arrays/numbers2.js new file mode 100644 index 000000000..6501e26b1 --- /dev/null +++ b/tests/js/arrays/numbers2.js @@ -0,0 +1,49 @@ +const userIds1 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, +]; + +const userIds2 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + 7234932843, + 7234932978, + 7234932436, +]; + +const userIds3 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + 7234932843, + + 7234932978, + 7234932436, +]; + +const userIds4 = [ + 7234932941, + 7234932722, + 7234932312, + 7234932933, + 7234932841, + 7234932166, + // comment 1 + 7234932843, + + 7234932978, + + // comment 2 + 7234932436, + // comment 3 +]; + diff --git a/tests/js/arrays/numbers3.js b/tests/js/arrays/numbers3.js new file mode 100644 index 000000000..f1cec5040 --- /dev/null +++ b/tests/js/arrays/numbers3.js @@ -0,0 +1,16 @@ +let test_case = [ + [ + 66,57,45,47,33,53,82,81,76,78,10,78,15,98,24,29,32,27,28,76,41,65,84,35, + 97,90,75,24,88,45,23,75,63,86,24,39,9,51,33,40,58,17,49,86,63,59,97,91, + 98,99,5,69,51,44,34,69,17,91,27,83,26,34,93,29,66,88,49,33,49,73,9,81,4, + 36,5,14,43,31,86,27,39,75,98,99,55,19,39,21,85,86,46,82,11,44,48,77,35, + 48,78,97 + ], + [ + 41,83,31,62,15,70,10,90,/*21,*/48,39,76,14,48,63,62,16,17,61,97,86,80,34,27, + 39,53,90,80,56,71,31,22,29,7,71,90,65,17,48,85,14,94,16,32,4,96,49,97, + 53,87,54,2,78,37,21,3,97,62,93,62,11,27,14,29,64,44,11,5,39,43,94,52,0, + 4,86,58,63,42,97,54,2,1,53,17,92,79,52,47,81,93,34,17,93,20,61,68,58,49, + 27,45 + ] +]; diff --git a/tests/json/json/__snapshots__/jsfmt.spec.js.snap b/tests/json/json/__snapshots__/jsfmt.spec.js.snap index ab23b450d..51c8c164f 100644 --- a/tests/json/json/__snapshots__/jsfmt.spec.js.snap +++ b/tests/json/json/__snapshots__/jsfmt.spec.js.snap @@ -524,7 +524,11 @@ trailingComma: "all" "url": "http://www.JSON.org/", "comment": "// /* */": " ", - " s p a c e d ": [1, 2, 3, 4, 5, 6, 7], + " s p a c e d ": [ + 1, 2, 3, + + 4, 5, 6, 7 + ], "compact": [1, 2, 3, 4, 5, 6, 7], "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}", "quotes": "" \\u0022 %22 0x22 034 "", @@ -650,7 +654,11 @@ trailingComma: "all" url: "http://www.JSON.org/", comment: "// /* */": " ", - " s p a c e d ": [1, 2, 3, 4, 5, 6, 7], + " s p a c e d ": [ + 1, 2, 3, + + 4, 5, 6, 7, + ], compact: [1, 2, 3, 4, 5, 6, 7], jsontext: '{"object with 1 member":["array with 1 element"]}', quotes: "" \\u0022 %22 0x22 034 "", @@ -775,7 +783,11 @@ printWidth: 80 "url": "http://www.JSON.org/", "comment": "// /* */": " ", - " s p a c e d ": [1, 2, 3, 4, 5, 6, 7], + " s p a c e d ": [ + 1, 2, 3, + + 4, 5, 6, 7 + ], "compact": [1, 2, 3, 4, 5, 6, 7], "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}", "quotes": "" \\u0022 %22 0x22 034 "", @@ -900,7 +912,11 @@ printWidth: 80 url: "http://www.JSON.org/", comment: "// /* */": " ", - " s p a c e d ": [1, 2, 3, 4, 5, 6, 7], + " s p a c e d ": [ + 1, 2, 3, + + 4, 5, 6, 7, + ], compact: [1, 2, 3, 4, 5, 6, 7], jsontext: '{"object with 1 member":["array with 1 element"]}', quotes: "" \\u0022 %22 0x22 034 "",