diff --git a/src/printer.js b/src/printer.js index 5f112409d39f..9efe308733d3 100644 --- a/src/printer.js +++ b/src/printer.js @@ -805,31 +805,16 @@ function genericPrintNoParens(path, options, print) { const needsForcedTrailingComma = canHaveTrailingComma && lastElem === null; - var printedElements = []; - let separatorParts = []; - path.each( - function(childPath) { - printedElements.push(concat(separatorParts)); - printedElements.push(group(print(childPath))); - - separatorParts = [",", line]; - if ( - childPath.getValue() && - util.isNextLineEmpty(options.originalText, childPath.getValue()) - ) { - separatorParts.push(softline); - } - }, - "elements" - ); - parts.push( group( concat([ "[", indent( options.tabWidth, - concat([softline, concat(printedElements)]) + concat([ + softline, + printArrayItems(path, options, "elements", print) + ]) ), needsForcedTrailingComma ? "," : "", ifBreak( @@ -1484,7 +1469,27 @@ function genericPrintNoParens(path, options, print) { return ""; case "TupleTypeAnnotation": - return concat(["[", join(", ", path.map(print, "types")), "]"]); + return group( + concat([ + "[", + indent( + options.tabWidth, + concat([ + softline, + printArrayItems(path, options, "types", print) + ]) + ), + ifBreak(shouldPrintComma(options) ? "," : ""), + comments.printDanglingComments( + path, + options, + /* sameIndent */ true + ), + softline, + "]" + ]) + ); + case "ExistentialTypeParam": case "ExistsTypeAnnotation": return "*"; @@ -3040,6 +3045,29 @@ function isFlowNodeStartingWithDeclare(node, options) { .match(/declare\s*$/); } +function printArrayItems(path, options, printPath, print) { + const printedElements = []; + let separatorParts = []; + + path.each( + function(childPath) { + printedElements.push(concat(separatorParts)); + printedElements.push(group(print(childPath))); + + separatorParts = [",", line]; + if ( + childPath.getValue() && + util.isNextLineEmpty(options.originalText, childPath.getValue()) + ) { + separatorParts.push(softline); + } + }, + printPath + ); + + return concat(printedElements); +} + function printAstToDoc(ast, options) { function printGenerically(path) { return comments.printComments( diff --git a/tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap b/tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap index d1e816dc43dd..b98fa23dc4b3 100644 --- a/tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap @@ -82,6 +82,27 @@ module.exports = \\"arrays\\"; " `; +exports[`comments.js 1`] = ` +"export type FileMetaData = [ + /* id */ string, + /* mtime */ number, + /* visited */ 0|1, + /* dependencies */ Array, +]; + +export type ModuleMetaData = [Path, /* type */ number]; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +export type FileMetaData = [ + /* id */ string, + /* mtime */ number, + /* visited */ 0 | 1, + /* dependencies */ Array +]; + +export type ModuleMetaData = [Path, /* type */ number]; +" +`; + exports[`numeric_elem.js 1`] = ` "var arr = []; var day = new Date; diff --git a/tests/flow/arrays/comments.js b/tests/flow/arrays/comments.js new file mode 100644 index 000000000000..aa5df0224ba2 --- /dev/null +++ b/tests/flow/arrays/comments.js @@ -0,0 +1,8 @@ +export type FileMetaData = [ + /* id */ string, + /* mtime */ number, + /* visited */ 0|1, + /* dependencies */ Array, +]; + +export type ModuleMetaData = [Path, /* type */ number];