diff --git a/changelog_unreleased/javascript/18851.md b/changelog_unreleased/javascript/18851.md new file mode 100644 index 000000000..d64c39958 --- /dev/null +++ b/changelog_unreleased/javascript/18851.md @@ -0,0 +1,30 @@ +#### Improve `do..while` print in `no-semi` mode (#18851 by @fisker) + + +```jsx +// Input +do { + doStuff() +} while (1) + +// comment +;[foo, bar].forEach(doStuff) + +// Prettier stable +do { + doStuff() +} while ( + 1 + + // comment +) +;[foo, bar].forEach(doStuff) + +// Prettier main +do { + doStuff() +} while (1) + +// comment +;[foo, bar].forEach(doStuff) +``` diff --git a/src/language-js/location/overrides.js b/src/language-js/location/overrides.js index 691cbb9d8..41a2f351d 100644 --- a/src/language-js/location/overrides.js +++ b/src/language-js/location/overrides.js @@ -35,6 +35,7 @@ const nodeTypesWithContentEnd = /** @type {const} */ ([ "ExportAllDeclaration", "ReturnStatement", "ThrowStatement", + "DoWhileStatement", ]); const overrides = diff --git a/src/language-js/print/module-declaration.js b/src/language-js/print/module-declaration.js index a4e84c5bd..162c1e357 100644 --- a/src/language-js/print/module-declaration.js +++ b/src/language-js/print/module-declaration.js @@ -1,6 +1,9 @@ import { group } from "../../document/index.js"; import { printDeclareToken, printSemicolon } from "./miscellaneous.js"; +/* +- `TSModuleDeclaration` (TypeScript) +*/ function printModuleDeclaration(path, options, print) { const { node } = path; diff --git a/src/language-js/print/variable-declaration.js b/src/language-js/print/variable-declaration.js index ad3c637e6..96e108d66 100644 --- a/src/language-js/print/variable-declaration.js +++ b/src/language-js/print/variable-declaration.js @@ -9,7 +9,7 @@ function printVariableDeclaration(path, options, print) { // We generally want to terminate all variable declarations with a // semicolon, except when they in the () part of for loops. - const isForXInitializer = + const isForXStatementInitializer = (path.key === "init" && path.parent.type === "ForStatement") || (path.key === "left" && (path.parent.type === "ForInStatement" || @@ -34,11 +34,11 @@ function printVariableDeclaration(path, options, print) { .slice(1) .map((doc) => [ ",", - hasValue && !isForXInitializer ? hardline : line, + hasValue && !isForXStatementInitializer ? hardline : line, doc, ]), ), - isForXInitializer ? "" : printSemicolon(options), + isForXStatementInitializer ? "" : printSemicolon(options), ]); } diff --git a/tests/format/js/no-semi/__snapshots__/format.test.js.snap b/tests/format/js/no-semi/__snapshots__/format.test.js.snap index c5b636be9..ecc524b4a 100644 --- a/tests/format/js/no-semi/__snapshots__/format.test.js.snap +++ b/tests/format/js/no-semi/__snapshots__/format.test.js.snap @@ -703,6 +703,99 @@ debugger; /* comment */ ================================================================================ `; +exports[`do-while-statement.js - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["babel", "typescript", "flow"] +semi: false + printWidth: 80 (default) | +=====================================input====================================== +do;while(1) + +// 11 +;[] + +do;while(1) + +// 21 +;foo + +// prettier-ignore +do; while( 1) + +;[] + +do;while(1) /* comment */ ; + +=====================================output===================================== +do; +while (1) + +// 11 +;[] + +do; +while (1) + +// 21 +foo + +// prettier-ignore +do; while( 1) + +;[] + +do; +while (1) /* comment */ + +================================================================================ +`; + +exports[`do-while-statement.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "typescript", "flow"] + printWidth: 80 (default) | +=====================================input====================================== +do;while(1) + +// 11 +;[] + +do;while(1) + +// 21 +;foo + +// prettier-ignore +do; while( 1) + +;[] + +do;while(1) /* comment */ ; + +=====================================output===================================== +do; +while (1); + +// 11 +[]; + +do; +while (1); + +// 21 +foo; + +// prettier-ignore +do; while( 1); + +[]; + +do; +while (1); /* comment */ + +================================================================================ +`; + exports[`imports.js - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["babel", "typescript", "flow"] @@ -1381,7 +1474,6 @@ function a() { return /* comment */ ; } - =====================================output===================================== function a() { return @@ -1429,7 +1521,6 @@ function a() { return /* comment */ ; } - =====================================output===================================== function a() { return; diff --git a/tests/format/js/no-semi/do-while-statement.js b/tests/format/js/no-semi/do-while-statement.js new file mode 100644 index 000000000..1a7643edd --- /dev/null +++ b/tests/format/js/no-semi/do-while-statement.js @@ -0,0 +1,16 @@ +do;while(1) + +// 11 +;[] + +do;while(1) + +// 21 +;foo + +// prettier-ignore +do; while( 1) + +;[] + +do;while(1) /* comment */ ; diff --git a/tests/format/js/no-semi/return-statement.js b/tests/format/js/no-semi/return-statement.js index 0ac1254d4..3eee783fc 100644 --- a/tests/format/js/no-semi/return-statement.js +++ b/tests/format/js/no-semi/return-statement.js @@ -16,4 +16,3 @@ function a() { return /* comment */ ; } -