Skip to content

Commit

Permalink
Fix flow annotation parenthesis
Browse files Browse the repository at this point in the history
The implementation is not super elegant but it seems to work.

Fixes prettier#719
  • Loading branch information
vjeux committed Feb 16, 2017
1 parent 74fb4a4 commit 7454b49
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/comments.js
Expand Up @@ -448,7 +448,7 @@ function printTrailingComment(commentPath, print, options, parentNode) {
return concat([" ", contents]);
}

return concat([lineSuffix(" " + contents), !isBlock ? breakParent : ""]);
return concat(["(", lineSuffix(" " + contents), !isBlock ? breakParent : "", ")"]);
}

function printDanglingComments(path, options, sameIndent) {
Expand Down Expand Up @@ -490,8 +490,17 @@ function printComments(path, print, options) {
return printed;
}

// There's a mode in flow where you can have annotations as inline comments
// Flow relies on parenthesis in order to find the right comment. We need to
// preserve it.
const needParens = comments.some(comment =>
comment.value.startsWith(':') &&
comment.type === 'CommentBlock' &&
comment.trailing
);

var leadingParts = [];
var trailingParts = [printed];
var trailingParts = [];

path.each(
function(commentPath) {
Expand All @@ -517,7 +526,24 @@ function printComments(path, print, options) {
"comments"
);

return concat(leadingParts.concat(trailingParts));
if (needsParens) {
return concat([].concat(
leadingParts,
["("],
[printed],
// We only put the first comment in parenthesis to make sure return
// type of arrow functions is not inside of the last argument type
trailingParts.slice(0, 1),
[")"],
trailingParts.slice(1)
));
}

return concat([].concat(
leadingParts,
[printed],
trailingParts
));
}

module.exports = { attach, printComments, printDanglingComments };

0 comments on commit 7454b49

Please sign in to comment.