From 2b6c9fe2e7f3a149cfbd40f6346f51589ed5ffa8 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 24 Jan 2026 14:17:02 +0800 Subject: [PATCH] Fix range of `IfStatement` (#18735) --- src/language-js/loc.js | 10 +++++++++- src/language-js/print/array.js | 5 ++--- .../call-or-new-expression-parentheses.js | 2 +- tests/config/run-format-test.js | 4 ---- .../__snapshots__/format.test.js.snap | 20 ++++++++----------- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/language-js/loc.js b/src/language-js/loc.js index 88ea7cf97..5c0aebc09 100644 --- a/src/language-js/loc.js +++ b/src/language-js/loc.js @@ -1,5 +1,5 @@ /** -@import {Node} from "./types/estree.js"; +@import {Node, Comment} from "./types/estree.js"; */ const isIndex = (value) => Number.isInteger(value) && value >= 0; @@ -21,6 +21,10 @@ function locStart(node) { return start; } +/** +@param {Node | Comment} node +@return {number} +*/ function locEnd(node) { if (node.type === "BreakStatement" || node.type === "ContinueStatement") { const start = locStart(node); @@ -35,6 +39,10 @@ function locEnd(node) { return locEnd(lastDeclaration); } + if (node.type === "IfStatement") { + return locEnd(node.alternate ?? node.consequent); + } + return locEndWithFullText(node); } diff --git a/src/language-js/print/array.js b/src/language-js/print/array.js index 93cdde860..653289f57 100644 --- a/src/language-js/print/array.js +++ b/src/language-js/print/array.js @@ -168,9 +168,8 @@ function isLineAfterElementEmpty({ node }, { originalText: text }) { break; } - currentIdx = skipInlineComment( - text, - skipTrailingComment(text, currentIdx + 1), + currentIdx = /** @type {number} */ ( + skipInlineComment(text, skipTrailingComment(text, currentIdx + 1)) ); } diff --git a/src/language-js/utilities/call-or-new-expression-parentheses.js b/src/language-js/utilities/call-or-new-expression-parentheses.js index d8113a6df..41fd24774 100644 --- a/src/language-js/utilities/call-or-new-expression-parentheses.js +++ b/src/language-js/utilities/call-or-new-expression-parentheses.js @@ -13,7 +13,7 @@ import getTextWithoutComments from "./get-text-without-comments.js"; /** @param {CallOrNewExpression} callOrNewExpression -@returns {number | void} +@returns {number | undefined} */ function getCallOrNewExpressionClosingParenthesisIndex( callOrNewExpression, diff --git a/tests/config/run-format-test.js b/tests/config/run-format-test.js index 16aa9e81b..f3107b189 100644 --- a/tests/config/run-format-test.js +++ b/tests/config/run-format-test.js @@ -24,10 +24,6 @@ const unstableTests = new Map( [ ["js/comments/dangling_array.js", (options) => options.semi === false], ["js/comments/jsx.js", (options) => options.semi === false], - [ - "js/comments/break-continue-statements-2.js", - (options) => options.semi !== false, - ], ["js/no-semi/comments.js", (options) => options.semi === false], ["flow/no-semi/comments.js", (options) => options.semi === false], ["js/ignore/semi/asi.js", (options) => options.semi === false], diff --git a/tests/format/js/comments/__snapshots__/format.test.js.snap b/tests/format/js/comments/__snapshots__/format.test.js.snap index dfae9738b..8c0b78952 100644 --- a/tests/format/js/comments/__snapshots__/format.test.js.snap +++ b/tests/format/js/comments/__snapshots__/format.test.js.snap @@ -1502,18 +1502,16 @@ for (;;) { =====================================output===================================== // https://github.com/prettier/prettier/issues/7126 for (;;) { - if (condition) - continue + if (condition) continue - // breaking comment + // breaking comment ;(possibleArray || []).sort() } lbl: for (;;) { - if (condition) - continue lbl + if (condition) continue lbl - // breaking comment + // breaking comment ;(possibleArray || []).sort() } @@ -1600,18 +1598,16 @@ for (;;) { =====================================output===================================== // https://github.com/prettier/prettier/issues/7126 for (;;) { - if (condition) - continue; + if (condition) continue; - // breaking comment + // breaking comment (possibleArray || []).sort(); } lbl: for (;;) { - if (condition) - continue lbl; + if (condition) continue lbl; - // breaking comment + // breaking comment (possibleArray || []).sort(); }