From fc7da89a54f8c6401a14e3867e8ac1eb6e101b1a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 10 Oct 2025 23:43:08 +0800 Subject: [PATCH] Improve `require()` call with comments (#18037) --- changelog_unreleased/javascript/18037.md | 35 ++++++++++++++++ src/language-js/print/call-expression.js | 5 ++- .../require/__snapshots__/format.test.js.snap | 42 +++++++++++++++++++ tests/format/js/require/comments.js | 13 ++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 changelog_unreleased/javascript/18037.md create mode 100644 tests/format/js/require/comments.js diff --git a/changelog_unreleased/javascript/18037.md b/changelog_unreleased/javascript/18037.md new file mode 100644 index 000000000..51b53126a --- /dev/null +++ b/changelog_unreleased/javascript/18037.md @@ -0,0 +1,35 @@ +#### Improve `require()` call with comments (#18037 by @fisker) + + +```jsx +// Input +require( +// Comment +"foo" +); + +call( +// Comment +"foo" +); + +// Prettier stable +require(// Comment +"foo"); + +call( + // Comment + "foo", +); + +// Prettier main +require( + // Comment + "foo", +); + +call( + // Comment + "foo", +); +``` diff --git a/src/language-js/print/call-expression.js b/src/language-js/print/call-expression.js index c86399dbc..781345762 100644 --- a/src/language-js/print/call-expression.js +++ b/src/language-js/print/call-expression.js @@ -172,7 +172,10 @@ function isCommonsJsOrAmdModuleDefinition(path) { // AMD module if (node.callee.name === "require") { - return (args.length === 1 && isStringLiteral(args[0])) || args.length > 1; + return ( + ((args.length === 1 && isStringLiteral(args[0])) || args.length > 1) && + !hasComment(args[0]) + ); } // CommonJS module diff --git a/tests/format/js/require/__snapshots__/format.test.js.snap b/tests/format/js/require/__snapshots__/format.test.js.snap index 2241d3609..8724299cc 100644 --- a/tests/format/js/require/__snapshots__/format.test.js.snap +++ b/tests/format/js/require/__snapshots__/format.test.js.snap @@ -1,5 +1,47 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`comments.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +require( +// 1 +"foo" +); +require.resolve(// 2 +"foo"); +require.resolve.paths(// 3 +"foo"); + +call( +// 4 +"foo" +); + +=====================================output===================================== +require( + // 1 + "foo", +); +require.resolve( + // 2 + "foo", +); +require.resolve.paths( + // 3 + "foo", +); + +call( + // 4 + "foo", +); + +================================================================================ +`; + exports[`long-module-name.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/format/js/require/comments.js b/tests/format/js/require/comments.js new file mode 100644 index 000000000..95d654a53 --- /dev/null +++ b/tests/format/js/require/comments.js @@ -0,0 +1,13 @@ +require( +// 1 +"foo" +); +require.resolve(// 2 +"foo"); +require.resolve.paths(// 3 +"foo"); + +call( +// 4 +"foo" +);