Fix printing of catch clause with a comment (#5202)
parent
303f344bff
commit
ce952fc8c1
|
@ -28,7 +28,12 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) {
|
||||||
comment,
|
comment,
|
||||||
options
|
options
|
||||||
) ||
|
) ||
|
||||||
handleTryStatementComments(enclosingNode, followingNode, comment) ||
|
handleTryStatementComments(
|
||||||
|
enclosingNode,
|
||||||
|
precedingNode,
|
||||||
|
followingNode,
|
||||||
|
comment
|
||||||
|
) ||
|
||||||
handleClassComments(enclosingNode, precedingNode, followingNode, comment) ||
|
handleClassComments(enclosingNode, precedingNode, followingNode, comment) ||
|
||||||
handleImportSpecifierComments(enclosingNode, comment) ||
|
handleImportSpecifierComments(enclosingNode, comment) ||
|
||||||
handleForComments(enclosingNode, precedingNode, comment) ||
|
handleForComments(enclosingNode, precedingNode, comment) ||
|
||||||
|
@ -248,15 +253,26 @@ function handleIfStatementComments(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Same as IfStatement but for TryStatement
|
// Same as IfStatement but for TryStatement
|
||||||
function handleTryStatementComments(enclosingNode, followingNode, comment) {
|
function handleTryStatementComments(
|
||||||
|
enclosingNode,
|
||||||
|
precedingNode,
|
||||||
|
followingNode,
|
||||||
|
comment
|
||||||
|
) {
|
||||||
if (
|
if (
|
||||||
!enclosingNode ||
|
!enclosingNode ||
|
||||||
enclosingNode.type !== "TryStatement" ||
|
(enclosingNode.type !== "TryStatement" &&
|
||||||
|
enclosingNode.type !== "CatchClause") ||
|
||||||
!followingNode
|
!followingNode
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (enclosingNode.type === "CatchClause" && precedingNode) {
|
||||||
|
addTrailingComment(precedingNode, comment);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (followingNode.type === "BlockStatement") {
|
if (followingNode.type === "BlockStatement") {
|
||||||
addBlockStatementFirstComment(followingNode, comment);
|
addBlockStatementFirstComment(followingNode, comment);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1737,11 +1737,31 @@ function printPathNoParens(path, options, print, args) {
|
||||||
n.finalizer ? concat([" finally ", path.call(print, "finalizer")]) : ""
|
n.finalizer ? concat([" finally ", path.call(print, "finalizer")]) : ""
|
||||||
]);
|
]);
|
||||||
case "CatchClause":
|
case "CatchClause":
|
||||||
return concat([
|
if (n.param) {
|
||||||
"catch ",
|
const hasComments =
|
||||||
n.param ? concat(["(", path.call(print, "param"), ") "]) : "",
|
n.param.comments &&
|
||||||
path.call(print, "body")
|
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":
|
case "ThrowStatement":
|
||||||
return concat(["throw ", path.call(print, "argument"), semi]);
|
return concat(["throw ", path.call(print, "argument"), semi]);
|
||||||
// Note: ignoring n.lexical because it has no printing consequences.
|
// Note: ignoring n.lexical because it has no printing consequences.
|
||||||
|
|
|
@ -1,5 +1,57 @@
|
||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// 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`] = `
|
exports[`empty.js - flow-verify 1`] = `
|
||||||
try {
|
try {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
try {}
|
||||||
|
catch(
|
||||||
|
// comment
|
||||||
|
foo
|
||||||
|
) {}
|
||||||
|
|
||||||
|
try {}
|
||||||
|
catch(foo //comment
|
||||||
|
) {}
|
||||||
|
|
||||||
|
try {}
|
||||||
|
catch(
|
||||||
|
/* comment */ foo
|
||||||
|
) {}
|
||||||
|
|
||||||
|
try {}
|
||||||
|
catch(
|
||||||
|
foo /* comment */
|
||||||
|
) {}
|
||||||
|
|
||||||
|
try {}
|
||||||
|
catch(
|
||||||
|
foo
|
||||||
|
/* comment */
|
||||||
|
) {}
|
Loading…
Reference in New Issue