Repository: prettier/prettier (JavaScript)
Date: 2017-03-01 17:11:18+00:00
Task type: Bug Fix

Issue:
Don't limit the length of require lines.
lines such as

`const MyReallyExtrememlyLongModuleName = require('MyReallyExtrememlyLongModuleName');`

should not be split apart. Once split, module system parsers will fail to understand the dependency.


Issue discussion:
> module system parsers will fail to understand the dependency

Can you tell us which require system you are working with? The ones I know have pretty permissive regarding to whitespace and should cover this case.
Facebook's...
I opened an internal task about it, we need to fix our stuff rather than hardcoding something inside of prettier :)

Solution patch:
diff --git a/src/printer.js b/src/printer.js
index ed98b21882fb..9bab3a4d8865 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -628,13 +628,20 @@ function genericPrintNoParens(path, options, print) {
 
       return concat(parts);
     case "CallExpression": {
-      const parent = path.getParentNode();
       // We detect calls on member lookups and possibly print them in a
       // special chain format. See `printMemberChain` for more info.
       if (n.callee.type === "MemberExpression") {
         return printMemberChain(path, options, print);
       }
 
+      // We want to keep require calls as a unit
+      if (n.callee.type === "Identifier" && n.callee.name === "require") {
+        return concat([
+          path.call(print, "callee"),
+          concat(["(", join(", ", path.map(print, "arguments")), ")"])
+        ]);
+      }
+
       return concat([
         path.call(print, "callee"),
         printArgumentsList(path, options, print)


Test patch:
diff --git a/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap b/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap
index 148fc4b7614c..4306470658f8 100644
--- a/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/flow/declare_export/__snapshots__/jsfmt.spec.js.snap
@@ -1116,9 +1116,7 @@ var numberValue6 = require(\\"ES6_ExportFrom_Intermediary2\\").numberValue1;
 var ap1: number = numberValue6;
 var ap2: string = numberValue6; // Error: number ~> string
 
-var numberValue2_renamed2 = require(
-  \\"ES6_ExportFrom_Intermediary2\\"
-).numberValue2_renamed2;
+var numberValue2_renamed2 = require(\\"ES6_ExportFrom_Intermediary2\\").numberValue2_renamed2;
 var aq1: number = numberValue2_renamed2;
 var aq2: string = numberValue2_renamed2; // Error: number ~> string
 
diff --git a/tests/flow/es6modules/__snapshots__/jsfmt.spec.js.snap b/tests/flow/es6modules/__snapshots__/jsfmt.spec.js.snap
index 081bbe6c5c8b..86e6efb5d615 100644
--- a/tests/flow/es6modules/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/flow/es6modules/__snapshots__/jsfmt.spec.js.snap
@@ -1245,9 +1245,7 @@ var numberValue6 = require(\\"ES6_ExportFrom_Intermediary2\\").numberValue1;
 var ap1: number = numberValue6;
 var ap2: string = numberValue6; // Error: number ~> string
 
-var numberValue2_renamed2 = require(
-  \\"ES6_ExportFrom_Intermediary2\\"
-).numberValue2_renamed2;
+var numberValue2_renamed2 = require(\\"ES6_ExportFrom_Intermediary2\\").numberValue2_renamed2;
 var aq1: number = numberValue2_renamed2;
 var aq2: string = numberValue2_renamed2; // Error: number ~> string
 
diff --git a/tests/require/__snapshots__/jsfmt.spec.js.snap b/tests/require/__snapshots__/jsfmt.spec.js.snap
new file mode 100644
index 000000000000..9c2bd95877a4
--- /dev/null
+++ b/tests/require/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,37 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`require.js 1`] = `
+"const { one, two, thee, four, five, six, seven, eight, nine, ten } = require('./my-utils');
+const { one, two, thee, four, five, six, seven, eight, nine, ten, eleven } = require('./my-utils');
+
+const MyReallyExtrememlyLongModuleName = require('MyReallyExtrememlyLongModuleName');
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+const {
+  one,
+  two,
+  thee,
+  four,
+  five,
+  six,
+  seven,
+  eight,
+  nine,
+  ten
+} = require(\\"./my-utils\\");
+const {
+  one,
+  two,
+  thee,
+  four,
+  five,
+  six,
+  seven,
+  eight,
+  nine,
+  ten,
+  eleven
+} = require(\\"./my-utils\\");
+
+const MyReallyExtrememlyLongModuleName = require(\\"MyReallyExtrememlyLongModuleName\\");
+"
+`;
diff --git a/tests/require/jsfmt.spec.js b/tests/require/jsfmt.spec.js
new file mode 100644
index 000000000000..989047bccc52
--- /dev/null
+++ b/tests/require/jsfmt.spec.js
@@ -0,0 +1,1 @@
+run_spec(__dirname);
diff --git a/tests/require/require.js b/tests/require/require.js
new file mode 100644
index 000000000000..f36550a9d8c4
--- /dev/null
+++ b/tests/require/require.js
@@ -0,0 +1,4 @@
+const { one, two, thee, four, five, six, seven, eight, nine, ten } = require('./my-utils');
+const { one, two, thee, four, five, six, seven, eight, nine, ten, eleven } = require('./my-utils');
+
+const MyReallyExtrememlyLongModuleName = require('MyReallyExtrememlyLongModuleName');