Skip to content

Commit

Permalink
Add support for breaks in TupleTypeAnnotation (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism authored and vjeux committed Mar 15, 2017
1 parent 2b93bf4 commit a2b5608
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 20 deletions.
68 changes: 48 additions & 20 deletions src/printer.js
Expand Up @@ -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(
Expand Down Expand Up @@ -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 "*";
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 21 additions & 0 deletions tests/flow/arrays/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -82,6 +82,27 @@ module.exports = \\"arrays\\";
"
`;

exports[`comments.js 1`] = `
"export type FileMetaData = [
/* id */ string,
/* mtime */ number,
/* visited */ 0|1,
/* dependencies */ Array<string>,
];
export type ModuleMetaData = [Path, /* type */ number];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export type FileMetaData = [
/* id */ string,
/* mtime */ number,
/* visited */ 0 | 1,
/* dependencies */ Array<string>
];
export type ModuleMetaData = [Path, /* type */ number];
"
`;
exports[`numeric_elem.js 1`] = `
"var arr = [];
var day = new Date;
Expand Down
8 changes: 8 additions & 0 deletions tests/flow/arrays/comments.js
@@ -0,0 +1,8 @@
export type FileMetaData = [
/* id */ string,
/* mtime */ number,
/* visited */ 0|1,
/* dependencies */ Array<string>,
];

export type ModuleMetaData = [Path, /* type */ number];

0 comments on commit a2b5608

Please sign in to comment.