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 #849master
parent
184382dd00
commit
7d24b0a1ff
|
@ -148,7 +148,8 @@ function attach(comments, ast, text, options) {
|
||||||
handleIfStatementComments(enclosingNode, followingNode, comment) ||
|
handleIfStatementComments(enclosingNode, followingNode, comment) ||
|
||||||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
|
handleTryStatementComments(enclosingNode, followingNode, comment) ||
|
||||||
handleImportSpecifierComments(enclosingNode, comment) ||
|
handleImportSpecifierComments(enclosingNode, comment) ||
|
||||||
handleClassComments(enclosingNode, comment)
|
handleClassComments(enclosingNode, comment) ||
|
||||||
|
handleUnionTypeComments(precedingNode, enclosingNode, followingNode, comment)
|
||||||
) {
|
) {
|
||||||
// We're good
|
// We're good
|
||||||
} else if (followingNode) {
|
} else if (followingNode) {
|
||||||
|
@ -512,6 +513,16 @@ function handleCallExpressionComments(precedingNode, enclosingNode, comment) {
|
||||||
return false;
|
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) {
|
function printComment(commentPath) {
|
||||||
const comment = commentPath.getValue();
|
const comment = commentPath.getValue();
|
||||||
comment.printed = true;
|
comment.printed = true;
|
||||||
|
|
|
@ -1622,12 +1622,19 @@ function genericPrintNoParens(path, options, print) {
|
||||||
return join(" & ", types);
|
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(
|
return group(
|
||||||
indent(
|
indent(
|
||||||
options.tabWidth,
|
shouldIndent ? options.tabWidth : 0,
|
||||||
concat([
|
concat([
|
||||||
ifBreak(concat([line, isIntersection ? "&" : "|", " "])),
|
ifBreak(concat([shouldIndent ? line : "", token, " "])),
|
||||||
join(concat([line, isIntersection ? "&" : "|", " "]), types)
|
join(concat([line, token, " "]), types)
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -1711,7 +1718,12 @@ function genericPrintNoParens(path, options, print) {
|
||||||
path.call(print, "id"),
|
path.call(print, "id"),
|
||||||
path.call(print, "typeParameters"),
|
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")]),
|
||||||
";"
|
";"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -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`] = `
|
exports[`function-declaration.js 1`] = `
|
||||||
"function a(/* comment */) {} // comment
|
"function a(/* comment */) {} // comment
|
||||||
function b() {} // comment
|
function b() {} // comment
|
||||||
|
|
|
@ -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};
|
Loading…
Reference in New Issue