@@ -94,6 +94,7 @@ export function handleLineComment(
9494) {
9595 return [
9696 handleBinaryExpressionComments ,
97+ handleConditionalExpressionComments ,
9798 handleFqnOrRefTypeComments ,
9899 handleIfStatementComments ,
99100 handleJumpStatementComments ,
@@ -116,11 +117,7 @@ function handleBinaryExpressionComments(
116117 options : JavaParserOptions
117118) {
118119 const { enclosingNode, precedingNode, followingNode } = commentNode ;
119- if (
120- enclosingNode &&
121- isNonTerminal ( enclosingNode ) &&
122- enclosingNode . name === "binaryExpression"
123- ) {
120+ if ( enclosingNode ?. name === "binaryExpression" ) {
124121 if ( isBinaryOperator ( followingNode ) ) {
125122 if ( options . experimentalOperatorPosition === "start" ) {
126123 util . addLeadingComment ( followingNode , commentNode ) ;
@@ -139,26 +136,37 @@ function handleBinaryExpressionComments(
139136 return false ;
140137}
141138
142- function handleFqnOrRefTypeComments ( commentNode : JavaComment ) {
143- const { enclosingNode, followingNode } = commentNode ;
139+ function handleConditionalExpressionComments ( commentNode : JavaComment ) {
140+ const { startLine, endLine, enclosingNode, precedingNode, followingNode } =
141+ commentNode ;
144142 if (
145- enclosingNode &&
146- isNonTerminal ( enclosingNode ) &&
147- enclosingNode . name === "fqnOrRefType" &&
148- followingNode
143+ enclosingNode ?. name === "conditionalExpression" &&
144+ precedingNode &&
145+ followingNode &&
146+ isNonTerminal ( precedingNode ) &&
147+ isNonTerminal ( followingNode ) &&
148+ precedingNode . location . endLine < startLine &&
149+ endLine < followingNode . location . startLine
149150 ) {
150151 util . addLeadingComment ( followingNode , commentNode ) ;
151152 return true ;
152153 }
153154 return false ;
154155}
155156
157+ function handleFqnOrRefTypeComments ( commentNode : JavaComment ) {
158+ const { enclosingNode, followingNode } = commentNode ;
159+ if ( enclosingNode ?. name === "fqnOrRefType" && followingNode ) {
160+ util . addLeadingComment ( followingNode , commentNode ) ;
161+ return true ;
162+ }
163+ return false ;
164+ }
165+
156166function handleIfStatementComments ( commentNode : JavaComment ) {
157167 const { enclosingNode, precedingNode } = commentNode ;
158168 if (
159- enclosingNode &&
160- isNonTerminal ( enclosingNode ) &&
161- enclosingNode . name === "ifStatement" &&
169+ enclosingNode ?. name === "ifStatement" &&
162170 precedingNode &&
163171 isNonTerminal ( precedingNode ) &&
164172 precedingNode . name === "statement"
@@ -175,7 +183,6 @@ function handleJumpStatementComments(commentNode: JavaComment) {
175183 enclosingNode &&
176184 ! precedingNode &&
177185 ! followingNode &&
178- isNonTerminal ( enclosingNode ) &&
179186 [ "breakStatement" , "continueStatement" , "returnStatement" ] . includes (
180187 enclosingNode . name
181188 )
@@ -189,10 +196,8 @@ function handleJumpStatementComments(commentNode: JavaComment) {
189196function handleLabeledStatementComments ( commentNode : JavaComment ) {
190197 const { enclosingNode, precedingNode } = commentNode ;
191198 if (
192- enclosingNode &&
199+ enclosingNode ?. name === "labeledStatement" &&
193200 precedingNode &&
194- isNonTerminal ( enclosingNode ) &&
195- enclosingNode . name === "labeledStatement" &&
196201 isTerminal ( precedingNode ) &&
197202 precedingNode . tokenType . name === "Identifier"
198203 ) {
@@ -205,9 +210,7 @@ function handleLabeledStatementComments(commentNode: JavaComment) {
205210function handleMethodDeclaratorComments ( commentNode : JavaComment ) {
206211 const { enclosingNode } = commentNode ;
207212 if (
208- enclosingNode &&
209- isNonTerminal ( enclosingNode ) &&
210- enclosingNode . name === "methodDeclarator" &&
213+ enclosingNode ?. name === "methodDeclarator" &&
211214 ! enclosingNode . children . receiverParameter &&
212215 ! enclosingNode . children . formalParameterList &&
213216 enclosingNode . children . LBrace [ 0 ] . startOffset < commentNode . startOffset &&
@@ -224,7 +227,6 @@ function handleNameComments(commentNode: JavaComment) {
224227 if (
225228 enclosingNode &&
226229 precedingNode &&
227- isNonTerminal ( enclosingNode ) &&
228230 isTerminal ( precedingNode ) &&
229231 precedingNode . tokenType . name === "Identifier" &&
230232 [
@@ -262,8 +264,8 @@ export type JavaComment = IToken & {
262264 trailing : boolean ;
263265 printed : boolean ;
264266 enclosingNode ?: JavaNonTerminal ;
265- precedingNode ?: JavaNonTerminal ;
266- followingNode ?: JavaNonTerminal ;
267+ precedingNode ?: JavaNode ;
268+ followingNode ?: JavaNode ;
267269} ;
268270
269271type FormatterOffOnRange = {
0 commit comments