diff --git a/src/language-js/loc.js b/src/language-js/loc.js index 1c9560407..88ea7cf97 100644 --- a/src/language-js/loc.js +++ b/src/language-js/loc.js @@ -30,6 +30,15 @@ function locEnd(node) { (node.type === "BreakStatement" ? "break".length : "continue".length); } + if (node.type === "VariableDeclaration") { + const lastDeclaration = node.declarations.at(-1); + return locEnd(lastDeclaration); + } + + return locEndWithFullText(node); +} + +function locEndWithFullText(node) { const end = node.range?.[1] ?? node.end; /* c8 ignore next 3 */ @@ -69,4 +78,4 @@ function hasSameLoc(nodeA, nodeB) { return hasSameLocStart(nodeA, nodeB) && hasSameLocEnd(nodeA, nodeB); } -export { hasSameLoc, hasSameLocStart, locEnd, locStart }; +export { hasSameLoc, hasSameLocStart, locEnd, locEndWithFullText, locStart }; diff --git a/src/language-js/parse/postprocess/index.js b/src/language-js/parse/postprocess/index.js index 1cb479f18..cf139c6b2 100644 --- a/src/language-js/parse/postprocess/index.js +++ b/src/language-js/parse/postprocess/index.js @@ -137,15 +137,6 @@ function postprocess(ast, options) { } break; - // fix unexpected locEnd caused by --no-semi style - case "VariableDeclaration": { - const lastDeclaration = node.declarations.at(-1); - if (lastDeclaration.init && text[locEnd(lastDeclaration)] !== ";") { - node.range = [locStart(node), locEnd(lastDeclaration)]; - } - break; - } - // remove redundant TypeScript nodes case "TSParenthesizedType": return node.typeAnnotation; diff --git a/src/language-js/print/ignored.js b/src/language-js/print/ignored.js index 8ac89dbd1..26e3512ab 100644 --- a/src/language-js/print/ignored.js +++ b/src/language-js/print/ignored.js @@ -6,7 +6,9 @@ function printIgnored(path, options /* , print*/) { if ( options.semi && - (node.type === "BreakStatement" || node.type === "ContinueStatement") + (node.type === "BreakStatement" || + node.type === "ContinueStatement" || + node.type === "VariableDeclaration") ) { text += ";"; } diff --git a/src/language-js/printers.js b/src/language-js/printers.js index 99e37546f..9a6c314bc 100644 --- a/src/language-js/printers.js +++ b/src/language-js/printers.js @@ -1,12 +1,9 @@ -const features = { - // TODO: Make this default behavior - experimental_avoidAstMutation: true, -}; import canAttachComment from "./comments/can-attach-comment.js"; import handleComments from "./comments/handle-comments.js"; import isGap from "./comments/is-gap.js"; import willPrintOwnComments from "./comments/will-print-own-comments.js"; import embed from "./embed/index.js"; +import { locEndWithFullText, locStart } from "./loc.js"; import { massageAstNode } from "./massage-ast/index.js"; import { insertPragma } from "./pragma.js"; import { printComment } from "./print/comment.js"; @@ -15,6 +12,15 @@ import getVisitorKeys from "./traverse/get-visitor-keys.js"; import isBlockComment from "./utilities/is-block-comment.js"; import hasPrettierIgnore from "./utilities/is-ignored.js"; +const features = { + // TODO: Make this default behavior + experimental_avoidAstMutation: true, + experimental_locForRangeFormat: { + locStart, + locEnd: locEndWithFullText, + }, +}; + const estree = { features, massageAstNode, diff --git a/src/main/range.js b/src/main/range.js index ed2c16204..bfd11a4f4 100644 --- a/src/main/range.js +++ b/src/main/range.js @@ -191,7 +191,7 @@ function isSourceElement(opts, node, parentNode) { @returns {[number, number]} */ function calculateRange(text, opts, ast) { - let { rangeStart: start, rangeEnd: end, locStart, locEnd } = opts; + let { rangeStart: start, rangeEnd: end } = opts; assert.ok(end > start); // Contract the range so that it has non-whitespace characters at its endpoints. // This ensures we can format a range that doesn't end on a node. @@ -251,6 +251,9 @@ function calculateRange(text, opts, ast) { ); } + const { locStart, locEnd } = + opts.printer.features?.experimental_locForRangeFormat ?? opts; + return [ Math.min(locStart(startNode), locStart(endNode)), Math.max(locEnd(startNode), locEnd(endNode)), diff --git a/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap b/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap index a6828c667..816f26725 100644 --- a/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/blockquote/__snapshots__/format.test.js.snap @@ -283,7 +283,7 @@ proseWrap: "always" > \`\`\`js > // prettier-ignore > const x = 1, -> b = 2 +> b = 2; > \`\`\` ================================================================================ @@ -398,7 +398,7 @@ proseWrap: "never" > \`\`\`js > // prettier-ignore > const x = 1, -> b = 2 +> b = 2; > \`\`\` ================================================================================ @@ -513,7 +513,7 @@ proseWrap: "preserve" > \`\`\`js > // prettier-ignore > const x = 1, -> b = 2 +> b = 2; > \`\`\` ================================================================================ diff --git a/tests/format/typescript/intersection/__snapshots__/format.test.js.snap b/tests/format/typescript/intersection/__snapshots__/format.test.js.snap index c181a0c15..c39f90de5 100644 --- a/tests/format/typescript/intersection/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/intersection/__snapshots__/format.test.js.snap @@ -186,7 +186,7 @@ let a11: /*1*/ /*2*/ C; let aa1: /*1*/ /*2*/ C & D; let aa2: /*1*/ /*2*/ C & /*3*/ D; -let aa3: /*1*/ /*2*/ C & /*3*/ D /*4*/; +let aa3: /*1*/ /*2*/ C & /*3*/ D; /*4*/ type A1 = C; type A2 = C; @@ -621,7 +621,7 @@ let a11: /*1*/ /*2*/ C; let aa1: /*1*/ /*2*/ C & D; let aa2: /*1*/ /*2*/ C & /*3*/ D; -let aa3: /*1*/ /*2*/ C & /*3*/ D /*4*/; +let aa3: /*1*/ /*2*/ C & /*3*/ D; /*4*/ type A1 = C; type A2 = C; diff --git a/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/format.test.js.snap b/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/format.test.js.snap index 033d6a797..7373cc62e 100644 --- a/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/format.test.js.snap @@ -87,7 +87,7 @@ let b11: /*1*/ /*2*/ C; let bb1: /*1*/ /*2*/ C & D; let bb2: /*1*/ /*2*/ C & /*3*/ D; -let bb3: /*1*/ /*2*/ C & /*3*/ D /*5*/; +let bb3: /*1*/ /*2*/ C & /*3*/ D; /*5*/ type B2 = C; type B3 = C; diff --git a/tests/format/typescript/union/__snapshots__/format.test.js.snap b/tests/format/typescript/union/__snapshots__/format.test.js.snap index fca3d4e58..b27a75512 100644 --- a/tests/format/typescript/union/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/union/__snapshots__/format.test.js.snap @@ -380,7 +380,7 @@ let a11: /*1*/ /*2*/ C; let aa1: /*1*/ /*2*/ C | D; let aa2: /*1*/ /*2*/ C | /*3*/ D; -let aa3: /*1*/ /*2*/ C | /*3*/ D /*4*/; +let aa3: /*1*/ /*2*/ C | /*3*/ D; /*4*/ type A1 = C; type A2 = C;