Fix if-else formatting for JSDoc type casting (#16798)

Keep block comment position for block less else
This commit is contained in:
SUZUKI Sosuke authored and GitHub committed 2024-10-29 13:08:43 +09:00
1 parent c2abecd1eb
commit c10d7d14fa
3 files changed
+79 -15

No files matched your search

+29 -15
View File
@@ -198,25 +198,39 @@ function handleIfStatementComments({
precedingNode === enclosingNode.consequent &&
followingNode === enclosingNode.alternate
) {
if (precedingNode.type === "BlockStatement") {
addTrailingComment(precedingNode, comment);
} else {
const isSingleLineComment =
isLineComment(comment) ||
comment.loc.start.line === comment.loc.end.line;
const isSameLineComment =
comment.loc.start.line === precedingNode.loc.start.line;
if (isSingleLineComment && isSameLineComment) {
// example:
// if (cond1) expr1; // comment A
// else if (cond2) expr2; // comment A
// else expr3;
const maybeElseTokenIndex = getNextNonSpaceNonCommentCharacterIndex(
text,
locEnd(enclosingNode.consequent),
);
// With the above conditions alone, this code would also match. This is a false positive.
// So, ignore cases where the token "else" appears immediately after the consequent:
//
// if (cond) a;
// else /* foo */ b;
if (
locStart(comment) < maybeElseTokenIndex ||
enclosingNode.alternate.type === "BlockStatement"
) {
if (precedingNode.type === "BlockStatement") {
addTrailingComment(precedingNode, comment);
} else {
addDanglingComment(enclosingNode, comment);
const isSingleLineComment =
isLineComment(comment) ||
comment.loc.start.line === comment.loc.end.line;
const isSameLineComment =
comment.loc.start.line === precedingNode.loc.start.line;
if (isSingleLineComment && isSameLineComment) {
// example:
// if (cond1) expr1; // comment A
// else if (cond2) expr2; // comment A
// else expr3;
addTrailingComment(precedingNode, comment);
} else {
addDanglingComment(enclosingNode, comment);
}
}
return true;
}
return true;
}
if (followingNode.type === "BlockStatement") {
+5
View File
@@ -0,0 +1,5 @@
if (foo) a = b;
/* foo */ else foo.split;
if (foo) a = b;
else /* foo */ foo.split;
@@ -321,6 +321,51 @@ void (
================================================================================
`;
exports[`16398.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
if (foo) a = b;
/* foo */ else foo.split;
if (foo) a = b;
else /* foo */ foo.split;
=====================================output=====================================
if (foo) a = b
/* foo */ else foo.split
if (foo) a = b
else /* foo */ foo.split
================================================================================
`;
exports[`16398.js format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
if (foo) a = b;
/* foo */ else foo.split;
if (foo) a = b;
else /* foo */ foo.split;
=====================================output=====================================
if (foo) a = b;
/* foo */ else foo.split;
if (foo) a = b;
else /* foo */ foo.split;
================================================================================
`;
exports[`arrow.js - {"semi":false} format 1`] = `
====================================options=====================================
parsers: ["babel", "flow", "typescript"]