Add support for breaks in TupleTypeAnnotation (#1003)

master
Brian Ng 2017-03-15 11:35:22 -05:00 committed by Christopher Chedeau
parent 2b93bf4f30
commit a2b560887c
3 changed files with 77 additions and 20 deletions

View File

@ -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(

View File

@ -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;

View File

@ -0,0 +1,8 @@
export type FileMetaData = [
/* id */ string,
/* mtime */ number,
/* visited */ 0|1,
/* dependencies */ Array<string>,
];
export type ModuleMetaData = [Path, /* type */ number];