New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaScript: Prefer a trailing comment when comment on between test and consequent on ternary #8554
base: main
Are you sure you want to change the base?
Changes from 15 commits
b2d58f2
90ba3e5
e88bf46
3d01c85
dac148e
4295766
d4e7ad7
cb097e5
1d77560
8afc04d
1feddd4
57da1e1
74fb69d
ebe3a81
2ed9474
f2bbf89
62290e6
776f858
a5b4a08
aa4cb49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#### Prefer a trailing comment when comment on between test and consequent on ternary ([#8554](https://github.com/prettier/prettier/pull/8554) by [@sosukesuzuki](https://github.com/sosukesuzuki)) | ||
|
||
<!-- prettier-ignore --> | ||
```js | ||
// Input | ||
test | ||
// comment | ||
? foo | ||
: bar; | ||
|
||
// Prettier (stable) | ||
test | ||
? // comment | ||
foo | ||
: bar; | ||
|
||
// Prettier (master) | ||
test | ||
// comment | ||
? foo | ||
: bar; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,14 @@ const { | |
getNextNonSpaceNonCommentCharacterIndex, | ||
} = require("../common/util-shared"); | ||
|
||
function handleOwnLineComment(comment, text, options, ast, isLastComment) { | ||
function handleOwnLineComment( | ||
comment, | ||
text, | ||
options, | ||
ast, | ||
isLastComment, | ||
comments | ||
) { | ||
const { precedingNode, enclosingNode, followingNode } = comment; | ||
return ( | ||
handleLastFunctionArgComments( | ||
|
@@ -67,7 +74,15 @@ function handleOwnLineComment(comment, text, options, ast, isLastComment) { | |
comment, | ||
options | ||
) || | ||
handleLabeledStatementComments(enclosingNode, comment) | ||
handleLabeledStatementComments(enclosingNode, comment) || | ||
handleTernaryTrailingComments( | ||
enclosingNode, | ||
precedingNode, | ||
comment, | ||
comments, | ||
text, | ||
options | ||
) | ||
); | ||
} | ||
|
||
|
@@ -406,9 +421,7 @@ function handleConditionalExpressionComments( | |
|
||
if ( | ||
(!precedingNode || !isSameLineAsPrecedingNode) && | ||
enclosingNode && | ||
(enclosingNode.type === "ConditionalExpression" || | ||
enclosingNode.type === "TSConditionalType") && | ||
isTernaryExpression(enclosingNode) && | ||
followingNode | ||
) { | ||
addLeadingComment(followingNode, comment); | ||
|
@@ -942,6 +955,56 @@ function handleTSMappedTypeComments( | |
return false; | ||
} | ||
|
||
function hasQuestionInRange(text, start, end, comments, options) { | ||
for (let i = start; i < end; ++i) { | ||
if (text.charAt(i) === "?") { | ||
let hasQuestion = true; | ||
// Ignore "?" in comments | ||
for (const comment of comments) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll consider it.
Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed with using |
||
const commentStart = options.locStart(comment); | ||
const commentEnd = options.locEnd(comment); | ||
if (commentStart < i && i < commentEnd) { | ||
hasQuestion = false; | ||
} | ||
} | ||
return hasQuestion; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function handleTernaryTrailingComments( | ||
enclosingNode, | ||
precedingNode, | ||
comment, | ||
comments, | ||
text, | ||
options | ||
) { | ||
// test | ||
// // comment | ||
// ? first | ||
// : second | ||
if ( | ||
isTernaryTest(precedingNode, enclosingNode) && | ||
// test ? | ||
// // comment | ||
// first | ||
// : second | ||
!hasQuestionInRange( | ||
text, | ||
options.locEnd(precedingNode), | ||
options.locStart(comment), | ||
comments, | ||
options | ||
) | ||
) { | ||
addTrailingComment(precedingNode, comment); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function isBlockComment(comment) { | ||
return comment.type === "Block" || comment.type === "CommentBlock"; | ||
} | ||
|
@@ -1008,6 +1071,20 @@ function getCommentChildNodes(node, options) { | |
} | ||
} | ||
|
||
function shouldIndentComment(path) { | ||
return ( | ||
isCommentParentTernaryTest(path) && | ||
!isTernaryExpression(path.getParentNode(2)) | ||
); | ||
} | ||
|
||
function shouldDedentComment(path) { | ||
return ( | ||
isCommentParentTernaryTest(path) && | ||
isTernaryExpression(path.getParentNode(2)) | ||
); | ||
} | ||
|
||
function isTypeCastComment(comment) { | ||
return ( | ||
isBlockComment(comment) && | ||
|
@@ -1019,6 +1096,31 @@ function isTypeCastComment(comment) { | |
); | ||
} | ||
|
||
function isTernaryExpression(node) { | ||
return ( | ||
node && | ||
(node.type === "ConditionalExpression" || node.type === "TSConditionalType") | ||
); | ||
} | ||
|
||
/** | ||
* Check if node matches the ternary test node | ||
*/ | ||
function isTernaryTest(node, ternary) { | ||
if (!node || !isTernaryExpression(ternary)) { | ||
return false; | ||
} | ||
const testField = ternary.test ? "test" : "extendsType"; | ||
return node === ternary[testField]; | ||
} | ||
|
||
function isCommentParentTernaryTest(commentPath) { | ||
return isTernaryTest( | ||
commentPath.getParentNode(), | ||
commentPath.getParentNode(1) | ||
); | ||
} | ||
|
||
module.exports = { | ||
handleOwnLineComment, | ||
handleEndOfLineComment, | ||
|
@@ -1028,4 +1130,6 @@ module.exports = { | |
isTypeCastComment, | ||
getGapRegex, | ||
getCommentChildNodes, | ||
shouldIndentComment, | ||
shouldDedentComment, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
text.indexOf('?', start)