From 1d0c666f0dfceecceac637efe1152b08b66662a3 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 23 Nov 2025 16:23:23 +0800 Subject: [PATCH] Remove empty line in for statement without "update" (#18300) --- CONTRIBUTING.md | 2 +- changelog_unreleased/javascript/18300.md | 20 +++++++++++++++++++ package.json | 2 +- src/language-js/print/estree.js | 3 +-- .../__snapshots__/format.test.js.snap | 1 - .../js/for/__snapshots__/format.test.js.snap | 19 ++++++++++++++++-- tests/format/js/for/no-update.js | 1 + 7 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 changelog_unreleased/javascript/18300.md create mode 100644 tests/format/js/for/no-update.js diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b2dac7db1..17a4b56fd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -70,7 +70,7 @@ Prettier is an opinionated formatter and is not accepting pull requests that add The project uses ESLint for linting and Prettier for formatting. If your editor isn't set up to work with them, you can lint and format all files from the command line using `yarn fix`. -After opening a PR, describe your changes in a file in the `changelog_unreleased` directory following the template [`changelog_unreleased/TEMPLATE.md`](changelog_unreleased/TEMPLATE.md) and commit this file to your PR. You can use `yarn gen:changelog` to generate a changelog file. Please see comments of the script file for usage. +After opening a PR, describe your changes in a file in the `changelog_unreleased` directory following the template [`changelog_unreleased/TEMPLATE.md`](changelog_unreleased/TEMPLATE.md) and commit this file to your PR. You can use `yarn generate-changelog` to generate a changelog file. Please see comments of the script file for usage. ## Other diff --git a/changelog_unreleased/javascript/18300.md b/changelog_unreleased/javascript/18300.md new file mode 100644 index 000000000..e38b7b8fb --- /dev/null +++ b/changelog_unreleased/javascript/18300.md @@ -0,0 +1,20 @@ +#### Remove empty line in for statement without "update" (#18300 by @fisker) + + +```jsx +// Input +for ( let i = 0, j = 0, len = allMatches.length, lenJ = selections.length;i < len;) {} + +// Prettier stable +for ( + let i = 0, j = 0, len = allMatches.length, lenJ = selections.length; + i < len; + +) {} + +// Prettier main +for ( + let i = 0, j = 0, len = allMatches.length, lenJ = selections.length; + i < len; +) {} +``` diff --git a/package.json b/package.json index 9f6ef28f5..e551f378e 100644 --- a/package.json +++ b/package.json @@ -207,7 +207,7 @@ "build": "node ./scripts/build/build.js", "build:website": "node ./scripts/build-website.js", "build:website:pr": "cross-env PULL_REQUEST=true yarn build:website", - "gen:changelog": "node ./scripts/generate-changelog.js", + "generate-changelog": "node ./scripts/generate-changelog.js", "debug": "node bin/prettier.js --ignore-path=.prettierignore --plugin=./packages/plugin-oxc/index.js --plugin=./packages/plugin-hermes/index.js", "debug:watch": "node --watch bin/prettier.js --ignore-path=.prettierignore --plugin=./packages/plugin-oxc/index.js --plugin=./packages/plugin-hermes/index.js", "debug:inspect": "node --inspect-brk bin/prettier.js --ignore-path=.prettierignore --plugin=./packages/plugin-oxc/index.js --plugin=./packages/plugin-hermes/index.js" diff --git a/src/language-js/print/estree.js b/src/language-js/print/estree.js index fc1fea4d8..616a01ae5 100644 --- a/src/language-js/print/estree.js +++ b/src/language-js/print/estree.js @@ -430,8 +430,7 @@ function printEstree(path, options, print, args) { line, print("test"), ";", - line, - print("update"), + node.update ? [line, print("update")] : ifBreak("", line), ]), softline, ]), diff --git a/tests/format/js/explicit-resource-management/__snapshots__/format.test.js.snap b/tests/format/js/explicit-resource-management/__snapshots__/format.test.js.snap index efe996c67..3c29fe40a 100644 --- a/tests/format/js/explicit-resource-management/__snapshots__/format.test.js.snap +++ b/tests/format/js/explicit-resource-management/__snapshots__/format.test.js.snap @@ -651,7 +651,6 @@ async function f() { /*0*/ for ( /*1*/ /*2*/ await using /*3*/ /*4*/ b /*5*/ = /*6*/ x /*7*/ /*8*/ /*9*/; ; - ) /*10*/ ; } { diff --git a/tests/format/js/for/__snapshots__/format.test.js.snap b/tests/format/js/for/__snapshots__/format.test.js.snap index 30b864e01..e3a4fb72f 100644 --- a/tests/format/js/for/__snapshots__/format.test.js.snap +++ b/tests/format/js/for/__snapshots__/format.test.js.snap @@ -1048,6 +1048,23 @@ for (a in b) 0; ================================================================================ `; +exports[`no-update.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +for ( let i = 0, j = 0, len = allMatches.length, lenJ = selections.length;i < len;) {} + +=====================================output===================================== +for ( + let i = 0, j = 0, len = allMatches.length, lenJ = selections.length; + i < len; +) {} + +================================================================================ +`; + exports[`parentheses.js [flow] format 1`] = ` "Unexpected token \`=>\`, expected the token \`;\` (11:27) 9 | for (var a = [b in c];;); @@ -1128,7 +1145,6 @@ for ( (b in c); }; ; - ); for (var a = [(b in c)]; ; ); for (var a = { b: (b in c) }; ; ); @@ -1160,7 +1176,6 @@ for ( (a = () => { b in c; }); - ); for (; (a = [b in c]); ); for (; (a = { b: b in c }); ); diff --git a/tests/format/js/for/no-update.js b/tests/format/js/for/no-update.js new file mode 100644 index 000000000..6fe996e62 --- /dev/null +++ b/tests/format/js/for/no-update.js @@ -0,0 +1 @@ +for ( let i = 0, j = 0, len = allMatches.length, lenJ = selections.length;i < len;) {}