Repository: jlongster/prettier
Pull request #299 (2017-01-18 19:03:58)

Fix bracketSpacing typo in tests
Basically `bracketsSpacing` -> `bracketSpacing`.

Do you think it's worth to validate config options, so such typos could be easily caught?

I actually was testing [`jest-validate`](https://github.com/facebook/jest/tree/master/packages/jest-validate) here because I thought it may come in handy and this showed up:
 
<img width="662" alt="screen shot 2017-01-18 at 20 02 20" src="https://cloud.githubusercontent.com/assets/5106466/22078679/1f4021a0-ddb9-11e6-9db9-23151db3db8d.png">

From some very rough (measured Jest total time) tests the impact on performance is ~1-2% 

jlongster: Ooh interesting. How do you use `jest-validate`? Do you run it in `tests_config/run_spec.js` ?
vjeux: Thanks for fixing my typo :)
thymikee: It's basically a function with some configuration options. I can send a PR, so you can see

Patch:
diff --git a/src/fast-path.js b/src/fast-path.js
index ca3e73e76..de8b4572d 100644
--- a/src/fast-path.js
+++ b/src/fast-path.js
@@ -373,6 +373,7 @@ FPp.needsParens = function(assumeExpressionContext) {
       }
 
       switch (parent.type) {
+        case "ExpressionStatement":
         case "MemberExpression":
         case "TaggedTemplateExpression":
         case "UnaryExpression":
diff --git a/src/pp.js b/src/pp.js
index df76dae4e..4a85a4460 100644
--- a/src/pp.js
+++ b/src/pp.js
@@ -51,8 +51,15 @@ function conditionalGroup(states, opts) {
   );
 }
 
-function ifBreak(contents) {
-  return { type: "if-break", contents };
+function ifBreak(breakContents, flatContents) {
+  if (breakContents) {
+    assertDoc(breakContents);
+  }
+  if (flatContents) {
+    assertDoc(flatContents);
+  }
+
+  return { type: "if-break", breakContents, flatContents };
 }
 
 function iterDoc(topDoc, func) {
@@ -78,6 +85,13 @@ function iterDoc(topDoc, func) {
         for (var i = doc.parts.length - 1; i >= 0; i--) {
           docs.push(doc.parts[i]);
         }
+      } else if (doc.type === "if-break") {
+        if (doc.breakContents) {
+          docs.push(doc.breakContents);
+        }
+        if (doc.flatContents) {
+          docs.push(doc.flatContents);
+        }
       } else if (doc.type !== "line") {
         docs.push(doc.contents);
       }
@@ -175,7 +189,14 @@ function fits(next, restCommands, width) {
           break;
         case "if-break":
           if (mode === MODE_BREAK) {
-            cmds.push([ ind, mode, doc.contents ]);
+            if (doc.breakContents) {
+              cmds.push([ ind, mode, doc.breakContents ]);              
+            }
+          }
+          if (mode === MODE_FLAT) {
+            if (doc.flatContents) {
+              cmds.push([ ind, mode, doc.flatContents ]);
+            }
           }
 
           break;
@@ -297,7 +318,14 @@ function print(w, doc) {
           break;
         case "if-break":
           if (mode === MODE_BREAK) {
-            cmds.push([ ind, MODE_BREAK, doc.contents ]);
+            if (doc.breakContents) {
+              cmds.push([ ind, mode, doc.breakContents ]);              
+            }
+          }
+          if (mode === MODE_FLAT) {
+            if (doc.flatContents) {
+              cmds.push([ ind, mode, doc.flatContents ]);
+            }
           }
 
           break;
diff --git a/src/printer.js b/src/printer.js
index fbd6c54d4..52f9ed72b 100644
--- a/src/printer.js
+++ b/src/printer.js
@@ -1830,6 +1830,12 @@ function printReturnType(path, print) {
   return concat(parts);
 }
 
+function typeIsFunction(type) {
+  return type === "FunctionExpression" ||
+    type === "ArrowFunctionExpression" ||
+    type === "NewExpression";
+}
+
 function printExportDeclaration(path, options, print) {
   var decl = path.getValue();
   var parts = [ "export " ];
@@ -1847,7 +1853,8 @@ function printExportDeclaration(path, options, print) {
     if (
       decl.type === "ExportDefaultDeclaration" &&
         (decl.declaration.type == "Identifier" ||
-          decl.declaration.type === "CallExpression")
+          decl.declaration.type === "CallExpression" ||
+          typeIsFunction(decl.declaration.type))
     ) {
       parts.push(";");
     }
@@ -1986,9 +1993,7 @@ function printMemberChain(node, options, print) {
   function argIsFunction(call) {
     if (call.arguments.length > 0) {
       const type = call.arguments[(0)].type;
-      return type === "FunctionExpression" ||
-        type === "ArrowFunctionExpression" ||
-        type === "NewExpression";
+      return typeIsFunction(type);
     }
     return false;
   }


Test patch:
diff --git a/tests/async_iteration/__snapshots__/jsfmt.spec.js.snap b/tests/async_iteration/__snapshots__/jsfmt.spec.js.snap
index 3d2a56d29..8173c5d8c 100644
--- a/tests/async_iteration/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/async_iteration/__snapshots__/jsfmt.spec.js.snap
@@ -40,11 +40,11 @@ async function* delegate_yield() {
   }
   yield* inner();
 }
-async () => {
+(async () => {
   for await (const x of delegate_yield()) {
     (x: void); // error: number ~> void
   }
-};
+});
 
 async function* delegate_return() {
   async function* inner() {
@@ -197,13 +197,13 @@ async function* catch_return() {
   }
 }
 
-async () => {
+(async () => {
   catch_return().throw(\"\").then(({ value }) => {
     if (value !== undefined) {
       (value: void); // error: number ~> void
     }
   });
-};
+});
 
 async function* yield_return() {
   try {
@@ -214,12 +214,12 @@ async function* yield_return() {
   }
 }
 
-async () => {
+(async () => {
   yield_return().throw(\"\").then(({ value }) => {
     if (value !== undefined) {
       (value: void); // error: number ~> void
     }
   });
-};
+});
 "
 `;
diff --git a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap
index df441d6b2..efd59b9c1 100644
--- a/tests/es6modules/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/es6modules/__snapshots__/jsfmt.spec.js.snap
@@ -1347,10 +1347,33 @@ import {
 "
 `;
 
+exports[`test export_default_arrow_expression.js 1`] = `
+"export default () => {};
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+export default () => {
+};
+"
+`;
+
 exports[`test export_default_call_expression.js 1`] = `
-"export default connect(mapStateToProps)(MyComponent);
+"export default foo()
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+export default foo();
+"
+`;
+
+exports[`test export_default_function_expression.js 1`] = `
+"export default (function() {});
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+export default function() {
+};
+"
+`;
+
+exports[`test export_default_new_expression.js 1`] = `
+"export default new Foo();
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-export default connect(mapStateToProps)(MyComponent);
+export default new Foo();
 "
 `;
 
diff --git a/tests/es6modules/export_default_arrow_expression.js b/tests/es6modules/export_default_arrow_expression.js
new file mode 100644
index 000000000..2d1ec2382
--- /dev/null
+++ b/tests/es6modules/export_default_arrow_expression.js
@@ -0,0 +1,1 @@
+export default () => {};
diff --git a/tests/es6modules/export_default_call_expression.js b/tests/es6modules/export_default_call_expression.js
index c3fae600c..4b9b7b3d8 100644
--- a/tests/es6modules/export_default_call_expression.js
+++ b/tests/es6modules/export_default_call_expression.js
@@ -1,1 +1,1 @@
-export default connect(mapStateToProps)(MyComponent);
+export default foo()
diff --git a/tests/es6modules/export_default_function_expression.js b/tests/es6modules/export_default_function_expression.js
new file mode 100644
index 000000000..01e4503a7
--- /dev/null
+++ b/tests/es6modules/export_default_function_expression.js
@@ -0,0 +1,1 @@
+export default (function() {});
diff --git a/tests/es6modules/export_default_new_expression.js b/tests/es6modules/export_default_new_expression.js
new file mode 100644
index 000000000..92c970c73
--- /dev/null
+++ b/tests/es6modules/export_default_new_expression.js
@@ -0,0 +1,1 @@
+export default new Foo();
diff --git a/tests/export/__snapshots__/jsfmt.spec.js.snap b/tests/export/__snapshots__/jsfmt.spec.js.snap
index ce563d1ef..b353903ca 100644
--- a/tests/export/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/export/__snapshots__/jsfmt.spec.js.snap
@@ -48,7 +48,7 @@ export {
   andMore,
   soWeCanGetItTo80Columns
 };
-export { fitsIn, oneLine };
+export {fitsIn, oneLine};
 "
 `;
 
diff --git a/tests/export/jsfmt.spec.js b/tests/export/jsfmt.spec.js
index b6e518ff7..7e2f65bc9 100644
--- a/tests/export/jsfmt.spec.js
+++ b/tests/export/jsfmt.spec.js
@@ -1,2 +1,2 @@
 run_spec(__dirname);
-run_spec(__dirname, {bracketsSpacing: false});
+run_spec(__dirname, {bracketSpacing: false});
diff --git a/tests/function/__snapshots__/jsfmt.spec.js.snap b/tests/function/__snapshots__/jsfmt.spec.js.snap
index 2cd319933..93ef91369 100644
--- a/tests/function/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/function/__snapshots__/jsfmt.spec.js.snap
@@ -370,6 +370,7 @@ export default (function() {})();
 (function() {})()\`\`;
 (function() {})\`\`;
 new (function() {});
+(function() {})
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 (function() {
 }).length;
@@ -383,6 +384,8 @@ export default (function() {
 })\`\`;
 new (function() {
 })();
+(function() {
+});
 "
 `;
 
diff --git a/tests/function/function_expression.js b/tests/function/function_expression.js
index e680a3d1a..aa9c6c35f 100644
--- a/tests/function/function_expression.js
+++ b/tests/function/function_expression.js
@@ -4,3 +4,4 @@ export default (function() {})();
 (function() {})()``;
 (function() {})``;
 new (function() {});
+(function() {})
diff --git a/tests/import/__snapshots__/jsfmt.spec.js.snap b/tests/import/__snapshots__/jsfmt.spec.js.snap
index f428cf7fa..25cd1a55a 100644
--- a/tests/import/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/import/__snapshots__/jsfmt.spec.js.snap
@@ -48,6 +48,6 @@ import {
   andMore,
   soWeCanGetItTo80Columns
 } from \".\";
-import { fitsIn, oneLine } from \".\";
+import {fitsIn, oneLine} from \".\";
 "
 `;
diff --git a/tests/import/jsfmt.spec.js b/tests/import/jsfmt.spec.js
index b6e518ff7..7e2f65bc9 100644
--- a/tests/import/jsfmt.spec.js
+++ b/tests/import/jsfmt.spec.js
@@ -1,2 +1,2 @@
 run_spec(__dirname);
-run_spec(__dirname, {bracketsSpacing: false});
+run_spec(__dirname, {bracketSpacing: false});
diff --git a/tests/predicates-parsing/__snapshots__/jsfmt.spec.js.snap b/tests/predicates-parsing/__snapshots__/jsfmt.spec.js.snap
index b28d1c664..e6078c8e3 100644
--- a/tests/predicates-parsing/__snapshots__/jsfmt.spec.js.snap
+++ b/tests/predicates-parsing/__snapshots__/jsfmt.spec.js.snap
@@ -112,14 +112,14 @@ var a0 = (x: mixed) => x !== null;
 
 var a1 = (x: mixed): %checks => x !== null;
 
-(x): %checks => x !== null;
+((x): %checks => x !== null);
 
 const insert_a_really_big_predicated_arrow_function_name_here = (x): %checks =>
   x !== null;
 
 declare var x;
 x;
-checks => 123;
+(checks => 123);
 
 type checks = any;