Improve require() call with comments (#18037)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-10-10 23:43:08 +08:00
1 parent fbcdccbf27
commit fc7da89a54
4 files changed
+94 -1

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
#### Improve `require()` call with comments (#18037 by @fisker)
<!-- prettier-ignore -->
```jsx
// Input
require(
// Comment
"foo"
);
call(
// Comment
"foo"
);
// Prettier stable
require(// Comment
"foo");
call(
// Comment
"foo",
);
// Prettier main
require(
// Comment
"foo",
);
call(
// Comment
"foo",
);
```
+4 -1
View File
@@ -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
@@ -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"]
+13
View File
@@ -0,0 +1,13 @@
require(
// 1
"foo"
);
require.resolve(// 2
"foo");
require.resolve.paths(// 3
"foo");
call(
// 4
"foo"
);