Skip to content

Commit 776f858

Browse files
committed
Fix
1 parent 62290e6 commit 776f858

3 files changed

Lines changed: 49 additions & 3 deletions

File tree

src/language-js/comments.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,12 +947,32 @@ function handleTSMappedTypeComments(
947947
return false;
948948
}
949949

950-
function hasQuestionBetweenTestAndComment(testNode, comment, text, options) {
950+
function hasQuestionBetweenTestAndComment(
951+
testNode,
952+
comment,
953+
text,
954+
options,
955+
n = 0
956+
) {
957+
const testNodeLocEnd = options.locEnd(testNode) + n;
958+
const commentLocStart = options.locStart(comment);
951959
const idx = privateUtil.getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
952960
text,
953-
options.locEnd(testNode)
961+
testNodeLocEnd
954962
);
955-
return text[idx] === "?" && idx < options.locStart(comment);
963+
if (testNodeLocEnd < commentLocStart) {
964+
if (text[idx] === "?") {
965+
return idx < commentLocStart;
966+
}
967+
return hasQuestionBetweenTestAndComment(
968+
testNode,
969+
comment,
970+
text,
971+
options,
972+
n + 1
973+
);
974+
}
975+
return false;
956976
}
957977

958978
function handleTernaryTrailingComments(

tests/js/conditional/__snapshots__/jsfmt.spec.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ c */? foo : bar : bar;
180180
?
181181
foo : bar;
182182
183+
(test) ?
184+
// comment
185+
foo : bar;
186+
187+
test ?
188+
// comment
189+
foo : bar;
190+
183191
=====================================output=====================================
184192
var inspect =
185193
4 === util.inspect.length
@@ -356,6 +364,16 @@ test
356364
? foo
357365
: bar;
358366
367+
test
368+
? // comment
369+
foo
370+
: bar;
371+
372+
test
373+
? // comment
374+
foo
375+
: bar;
376+
359377
================================================================================
360378
`;
361379

tests/js/conditional/comments.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,11 @@ c */? foo : bar : bar;
171171
// comment
172172
?
173173
foo : bar;
174+
175+
(test) ?
176+
// comment
177+
foo : bar;
178+
179+
test ?
180+
// comment
181+
foo : bar;

0 commit comments

Comments
 (0)