Fix flow union comments (#853)

The comments infra has been architected with trailing operators and fails for leading `|`. Instead of having leading comments, we can put trailing comments on the previous one and use the same technique as assignment to deal with the indentation.

Fixes #849
master
Christopher Chedeau 2017-03-03 13:54:45 -08:00 committed by GitHub
parent 184382dd00
commit 7d24b0a1ff
4 changed files with 84 additions and 6 deletions

View File

@ -148,7 +148,8 @@ function attach(comments, ast, text, options) {
handleIfStatementComments(enclosingNode, followingNode, comment) ||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
handleImportSpecifierComments(enclosingNode, comment) ||
handleClassComments(enclosingNode, comment)
handleClassComments(enclosingNode, comment) ||
handleUnionTypeComments(precedingNode, enclosingNode, followingNode, comment)
) {
// We're good
} else if (followingNode) {
@ -512,6 +513,16 @@ function handleCallExpressionComments(precedingNode, enclosingNode, comment) {
return false;
}
function handleUnionTypeComments(precedingNode, enclosingNode, followingNode, comment) {
if (enclosingNode && enclosingNode.type === 'UnionTypeAnnotation' &&
precedingNode && precedingNode.type === 'ObjectTypeAnnotation' &&
followingNode && followingNode.type === 'ObjectTypeAnnotation') {
addTrailingComment(precedingNode, comment);
return true;
}
return false;
}
function printComment(commentPath) {
const comment = commentPath.getValue();
comment.printed = true;

View File

@ -1622,12 +1622,19 @@ function genericPrintNoParens(path, options, print) {
return join(" & ", types);
}
const parent = path.getParentNode();
// If there's a leading comment, the parent is doing the indentation
const shouldIndent = !(parent.type === 'TypeAlias' &&
hasLeadingOwnLineComment(options.originalText, n));
const token = isIntersection ? "&" : "|";
return group(
indent(
options.tabWidth,
shouldIndent ? options.tabWidth : 0,
concat([
ifBreak(concat([line, isIntersection ? "&" : "|", " "])),
join(concat([line, isIntersection ? "&" : "|", " "]), types)
ifBreak(concat([shouldIndent ? line : "", token, " "])),
join(concat([line, token, " "]), types)
])
)
);
@ -1710,8 +1717,13 @@ function genericPrintNoParens(path, options, print) {
"type ",
path.call(print, "id"),
path.call(print, "typeParameters"),
" = ",
path.call(print, "right"),
" =",
hasLeadingOwnLineComment(options.originalText, n.right)
? indent(
options.tabWidth,
concat([hardline, path.call(print, "right")])
)
: concat([" ", path.call(print, "right")]),
";"
);

View File

@ -244,6 +244,52 @@ b;
"
`;
exports[`flow_union.js 1`] = `
"type UploadState<E, EM, D>
// The upload hasnt begun yet
= {type: \\"Not_begun\\"}
// The upload timed out
| {type: \\"Timed_out\\"}
// Failed somewhere on the line
| {type: \\"Failed\\", error: E, errorMsg: EM}
// Uploading to aws3 and CreatePostMutation succeeded
| {type: \\"Success\\", data: D};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type UploadState<E, EM, D> =
// The upload hasnt begun yet
| { type: \\"Not_begun\\" }
// The upload timed out
| { type: \\"Timed_out\\" }
// Failed somewhere on the line
| { type: \\"Failed\\", error: E, errorMsg: EM }
// Uploading to aws3 and CreatePostMutation succeeded
| { type: \\"Success\\", data: D };
"
`;
exports[`flow_union.js 2`] = `
"type UploadState<E, EM, D>
// The upload hasnt begun yet
= {type: \\"Not_begun\\"}
// The upload timed out
| {type: \\"Timed_out\\"}
// Failed somewhere on the line
| {type: \\"Failed\\", error: E, errorMsg: EM}
// Uploading to aws3 and CreatePostMutation succeeded
| {type: \\"Success\\", data: D};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type UploadState<E, EM, D> =
// The upload hasnt begun yet
| { type: \\"Not_begun\\" }
// The upload timed out
| { type: \\"Timed_out\\" }
// Failed somewhere on the line
| { type: \\"Failed\\", error: E, errorMsg: EM }
// Uploading to aws3 and CreatePostMutation succeeded
| { type: \\"Success\\", data: D };
"
`;
exports[`function-declaration.js 1`] = `
"function a(/* comment */) {} // comment
function b() {} // comment

View File

@ -0,0 +1,9 @@
type UploadState<E, EM, D>
// The upload hasnt begun yet
= {type: "Not_begun"}
// The upload timed out
| {type: "Timed_out"}
// Failed somewhere on the line
| {type: "Failed", error: E, errorMsg: EM}
// Uploading to aws3 and CreatePostMutation succeeded
| {type: "Success", data: D};