diff --git a/src/printer.js b/src/printer.js index 71693a5e..f9327f6a 100644 --- a/src/printer.js +++ b/src/printer.js @@ -555,6 +555,7 @@ function genericPrintNoParens(path, options, print) { const hasDirectives = n.directives && n.directives.length > 0; var parent = path.getParentNode(); + const parentParent = path.getParentNode(1); if ( !hasContent && !hasDirectives && @@ -563,7 +564,8 @@ function genericPrintNoParens(path, options, print) { parent.type === "FunctionExpression" || parent.type === "FunctionDeclaration" || parent.type === "ObjectMethod" || - parent.type === "ClassMethod") + parent.type === "ClassMethod" || + (parent.type === "CatchClause" && !parentParent.finalizer)) ) { return "{}"; } diff --git a/tests/flow/try/__snapshots__/jsfmt.spec.js.snap b/tests/flow/try/__snapshots__/jsfmt.spec.js.snap index 9dcf14ca..8004b039 100644 --- a/tests/flow/try/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/try/__snapshots__/jsfmt.spec.js.snap @@ -205,8 +205,7 @@ function f() { try { var x: number = 0; var y: number = x; - } catch (e) { - } + } catch (e) {} } // and within catch @@ -311,16 +310,14 @@ function f() { var y: number = x; // error try { var x: number = 0; - } catch (e) { - } + } catch (e) {} } // another non-dominated post function f() { try { var x: number = 0; - } catch (e) { - } + } catch (e) {} var y: number = x; // error } @@ -341,8 +338,7 @@ function f(b) { throw new Error(); } x = 0; - } catch (e) { - } + } catch (e) {} var y: number = x; // error } " @@ -448,8 +444,7 @@ function baz(): string { function qux(): string { try { throw new Error(\\"foo\\"); - } catch (e) { - } + } catch (e) {} console.log(); return \\"bar\\"; } @@ -457,8 +452,7 @@ function qux(): string { function quux(): string { try { return qux(); - } catch (e) { - } + } catch (e) {} return \\"bar\\"; } diff --git a/tests/flow/type-at-pos/__snapshots__/jsfmt.spec.js.snap b/tests/flow/type-at-pos/__snapshots__/jsfmt.spec.js.snap index 40e5891a..84dd2b20 100644 --- a/tests/flow/type-at-pos/__snapshots__/jsfmt.spec.js.snap +++ b/tests/flow/type-at-pos/__snapshots__/jsfmt.spec.js.snap @@ -280,7 +280,6 @@ try { try { throw \\"foo\\"; -} catch (e) { -} +} catch (e) {} " `; diff --git a/tests/try/__snapshots__/jsfmt.spec.js.snap b/tests/try/__snapshots__/jsfmt.spec.js.snap index 089c262f..54f36555 100644 --- a/tests/try/__snapshots__/jsfmt.spec.js.snap +++ b/tests/try/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,26 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`empty.js 1`] = ` +"try { +} catch (e) { +} +finally { +} + +try { +} catch (e) { +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +try { +} catch (e) { +} finally { +} + +try { +} catch (e) {} +" +`; + exports[`try.js 1`] = ` "try /* missing comment */ diff --git a/tests/try/empty.js b/tests/try/empty.js new file mode 100644 index 00000000..7621b80a --- /dev/null +++ b/tests/try/empty.js @@ -0,0 +1,9 @@ +try { +} catch (e) { +} +finally { +} + +try { +} catch (e) { +}