Exclude semicolon from range of VariableDeclaration (#18734)

This commit is contained in:
fisker Cheung authored and GitHub committed 2026-01-24 13:42:03 +08:00
1 parent 56860a7c58
commit 4531e18f75
9 files changed
+34 -23

No files matched your search

+10 -1
View File
@@ -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 };
@@ -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;
+3 -1
View File
@@ -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 += ";";
}
+10 -4
View File
@@ -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,
+4 -1
View File
@@ -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)),
@@ -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;
> \`\`\`
================================================================================
@@ -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;
@@ -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;
@@ -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;