Fix printing of catch clause with a comment (#5202)

master
Lucas Duailibe 2018-10-08 14:18:30 -03:00 committed by GitHub
parent 303f344bff
commit ce952fc8c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 8 deletions

View File

@ -28,7 +28,12 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) {
comment,
options
) ||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
handleTryStatementComments(
enclosingNode,
precedingNode,
followingNode,
comment
) ||
handleClassComments(enclosingNode, precedingNode, followingNode, comment) ||
handleImportSpecifierComments(enclosingNode, comment) ||
handleForComments(enclosingNode, precedingNode, comment) ||
@ -248,15 +253,26 @@ function handleIfStatementComments(
}
// Same as IfStatement but for TryStatement
function handleTryStatementComments(enclosingNode, followingNode, comment) {
function handleTryStatementComments(
enclosingNode,
precedingNode,
followingNode,
comment
) {
if (
!enclosingNode ||
enclosingNode.type !== "TryStatement" ||
(enclosingNode.type !== "TryStatement" &&
enclosingNode.type !== "CatchClause") ||
!followingNode
) {
return false;
}
if (enclosingNode.type === "CatchClause" && precedingNode) {
addTrailingComment(precedingNode, comment);
return true;
}
if (followingNode.type === "BlockStatement") {
addBlockStatementFirstComment(followingNode, comment);
return true;

View File

@ -1737,11 +1737,31 @@ function printPathNoParens(path, options, print, args) {
n.finalizer ? concat([" finally ", path.call(print, "finalizer")]) : ""
]);
case "CatchClause":
return concat([
"catch ",
n.param ? concat(["(", path.call(print, "param"), ") "]) : "",
path.call(print, "body")
]);
if (n.param) {
const hasComments =
n.param.comments &&
n.param.comments.some(
comment =>
!handleComments.isBlockComment(comment) ||
(comment.leading &&
hasNewline(options.originalText, options.locEnd(comment))) ||
(comment.trailing &&
hasNewline(options.originalText, options.locStart(comment), {
backwards: true
}))
);
const param = path.call(print, "param");
return concat([
"catch ",
hasComments
? concat(["(", indent(concat([softline, param])), softline, ") "])
: concat(["(", param, ") "]),
path.call(print, "body")
]);
}
return concat(["catch ", path.call(print, "body")]);
case "ThrowStatement":
return concat(["throw ", path.call(print, "argument"), semi]);
// Note: ignoring n.lexical because it has no printing consequences.

View File

@ -1,5 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`catch.js - flow-verify 1`] = `
try {}
catch(
// comment
foo
) {}
try {}
catch(foo //comment
) {}
try {}
catch(
/* comment */ foo
) {}
try {}
catch(
foo /* comment */
) {}
try {}
catch(
foo
/* comment */
) {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
try {
} catch (
// comment
foo
) {}
try {
} catch (
foo //comment
) {}
try {
} catch (/* comment */ foo) {}
try {
} catch (foo /* comment */) {}
try {
} catch (
foo
/* comment */
) {}
`;
exports[`empty.js - flow-verify 1`] = `
try {
} catch (e) {

25
tests/try/catch.js Normal file
View File

@ -0,0 +1,25 @@
try {}
catch(
// comment
foo
) {}
try {}
catch(foo //comment
) {}
try {}
catch(
/* comment */ foo
) {}
try {}
catch(
foo /* comment */
) {}
try {}
catch(
foo
/* comment */
) {}