clang-format: [JS] improve calculateBraceType heuristic
Summary:
calculateBraceTypes decides for braced init for empty brace pairs ({}).
In context of a function declaration, this incorrectly classifies empty
function or method bodies as braced inits, leading to missing wraps:
class C {
foo() {}[bar]() {}
}
Where code should have wrapped after "}", before "[". This change adds
another piece of contextual information in that braces following closing
parentheses must always be the opening braces of function blocks. This
fixes brace detection for methods immediately followed by brackets
(computed property declarations), but also curlies.
Reviewers: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D33714
llvm-svn: 304290
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index d8f887f..92a1131 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -474,9 +474,8 @@
"(function f() {\n"
" var x = 1;\n"
"}());\n");
- // Known issue: this should wrap after {}, but calculateBraceTypes
- // misclassifies the first braces as a BK_BracedInit.
- verifyFormat("function aFunction(){} {\n"
+ verifyFormat("function aFunction() {}\n"
+ "{\n"
" let x = 1;\n"
" console.log(x);\n"
"}\n");
@@ -1233,6 +1232,10 @@
verifyFormat("class C {\n x: string = 12;\n}");
verifyFormat("class C {\n x(): string => 12;\n}");
verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
+ verifyFormat("class C {\n"
+ " foo() {}\n"
+ " [bar]() {}\n"
+ "}\n");
verifyFormat("class C {\n private x: string = 12;\n}");
verifyFormat("class C {\n private static x: string = 12;\n}");
verifyFormat("class C {\n static x(): string {\n return 'asd';\n }\n}");