Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow break require() argument #15256

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions changelog_unreleased/javascript/15256.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#### Allow argument of `require()` to break (#15256 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
const plugin = require(
global.STANDALONE
? path.join(__dirname, "../standalone.js")
: path.join(__dirname, "..")
);

// Prettier stable
const plugin = require(global.STANDALONE
? path.join(__dirname, "../standalone.js")
: path.join(__dirname, ".."));

// Prettier main
const plugin = require(
global.STANDALONE
? path.join(__dirname, "../standalone.js")
: path.join(__dirname, "..")
);
```
3 changes: 2 additions & 1 deletion src/language-js/print/call-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ function isCommonsJsOrAmdCall(node, parentNode) {
}

if (node.callee.name === "require") {
return true;
const args = getCallArguments(node);
return (args.length === 1 && isStringLiteral(args[0])) || args.length > 1;
}

if (node.callee.name === "define") {
Expand Down
23 changes: 23 additions & 0 deletions tests/format/js/require/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const { one1, two1, three1, four1, five1, six1, seven1, eight1, nine1, ten1, ele

const MyReallyExtrememlyLongModuleName = require('MyReallyExtrememlyLongModuleName');

const plugin = require(
global.STANDALONE
? path.join(__dirname, "../standalone.js")
: path.join(__dirname, "..")
);

const plugin2 = require(
path.join(
__dirname,
global.STANDALONE ? "../standalone.js" : '..'
)
);

=====================================output=====================================
const {
one,
Expand Down Expand Up @@ -40,5 +53,15 @@ const {

const MyReallyExtrememlyLongModuleName = require("MyReallyExtrememlyLongModuleName");

const plugin = require(
global.STANDALONE
? path.join(__dirname, "../standalone.js")
: path.join(__dirname, ".."),
);

const plugin2 = require(
path.join(__dirname, global.STANDALONE ? "../standalone.js" : ".."),
);

================================================================================
`;
13 changes: 13 additions & 0 deletions tests/format/js/require/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,16 @@ const { one, two, three, four, five, six, seven, eight, nine, ten } = require('.
const { one1, two1, three1, four1, five1, six1, seven1, eight1, nine1, ten1, eleven1 } = require('./my-utils');

const MyReallyExtrememlyLongModuleName = require('MyReallyExtrememlyLongModuleName');

const plugin = require(
global.STANDALONE
? path.join(__dirname, "../standalone.js")
: path.join(__dirname, "..")
);

const plugin2 = require(
path.join(
__dirname,
global.STANDALONE ? "../standalone.js" : '..'
)
);