Do not expand empty catch (#783)

I originally wanted to expand it but I think that it may not be a good decision because it's very common for them to be empty and you want it to take as little space as possible.

I tried to remove all those conditions but I think that there are valid places where we always want to expand like empty if/for/while loops.

Fixes #778
master
Christopher Chedeau 2017-02-23 07:29:05 -08:00 committed by GitHub
parent c84027745b
commit 694816517a
5 changed files with 40 additions and 15 deletions

View File

@ -555,6 +555,7 @@ function genericPrintNoParens(path, options, print) {
const hasDirectives = n.directives && n.directives.length > 0; const hasDirectives = n.directives && n.directives.length > 0;
var parent = path.getParentNode(); var parent = path.getParentNode();
const parentParent = path.getParentNode(1);
if ( if (
!hasContent && !hasContent &&
!hasDirectives && !hasDirectives &&
@ -563,7 +564,8 @@ function genericPrintNoParens(path, options, print) {
parent.type === "FunctionExpression" || parent.type === "FunctionExpression" ||
parent.type === "FunctionDeclaration" || parent.type === "FunctionDeclaration" ||
parent.type === "ObjectMethod" || parent.type === "ObjectMethod" ||
parent.type === "ClassMethod") parent.type === "ClassMethod" ||
(parent.type === "CatchClause" && !parentParent.finalizer))
) { ) {
return "{}"; return "{}";
} }

View File

@ -205,8 +205,7 @@ function f() {
try { try {
var x: number = 0; var x: number = 0;
var y: number = x; var y: number = x;
} catch (e) { } catch (e) {}
}
} }
// and within catch // and within catch
@ -311,16 +310,14 @@ function f() {
var y: number = x; // error var y: number = x; // error
try { try {
var x: number = 0; var x: number = 0;
} catch (e) { } catch (e) {}
}
} }
// another non-dominated post // another non-dominated post
function f() { function f() {
try { try {
var x: number = 0; var x: number = 0;
} catch (e) { } catch (e) {}
}
var y: number = x; // error var y: number = x; // error
} }
@ -341,8 +338,7 @@ function f(b) {
throw new Error(); throw new Error();
} }
x = 0; x = 0;
} catch (e) { } catch (e) {}
}
var y: number = x; // error var y: number = x; // error
} }
" "
@ -448,8 +444,7 @@ function baz(): string {
function qux(): string { function qux(): string {
try { try {
throw new Error(\\"foo\\"); throw new Error(\\"foo\\");
} catch (e) { } catch (e) {}
}
console.log(); console.log();
return \\"bar\\"; return \\"bar\\";
} }
@ -457,8 +452,7 @@ function qux(): string {
function quux(): string { function quux(): string {
try { try {
return qux(); return qux();
} catch (e) { } catch (e) {}
}
return \\"bar\\"; return \\"bar\\";
} }

View File

@ -280,7 +280,6 @@ try {
try { try {
throw \\"foo\\"; throw \\"foo\\";
} catch (e) { } catch (e) {}
}
" "
`; `;

View File

@ -1,5 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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`] = ` exports[`try.js 1`] = `
"try "try
/* missing comment */ /* missing comment */

9
tests/try/empty.js Normal file
View File

@ -0,0 +1,9 @@
try {
} catch (e) {
}
finally {
}
try {
} catch (e) {
}