summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp11
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp21
2 files changed, 32 insertions, 0 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index a036fb29da3..7e8efad26b6 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1036,6 +1036,17 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
FormatTok->BlockKind = BK_BracedInit;
parseBracedList();
break;
+ case tok::r_paren:
+ // JavaScript can just have free standing methods and getters/setters in
+ // object literals. Detect them by a "{" following ")".
+ if (Style.Language == FormatStyle::LK_JavaScript) {
+ nextToken();
+ if (FormatTok->is(tok::l_brace))
+ parseChildBlock();
+ break;
+ }
+ nextToken();
+ break;
case tok::r_brace:
nextToken();
return !HasError;
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 4378ded5c36..00744728ddb 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -144,6 +144,27 @@ TEST_F(FormatTestJS, ContainerLiterals) {
verifyFormat("x = foo && {a: 123};");
}
+TEST_F(FormatTestJS, MethodsInObjectLiterals) {
+ verifyFormat("var o = {\n"
+ " value: 'test',\n"
+ " get value() { // getter\n"
+ " return this.value;\n"
+ " }\n"
+ "};");
+ verifyFormat("var o = {\n"
+ " value: 'test',\n"
+ " set value(val) { // setter\n"
+ " this.value = val;\n"
+ " }\n"
+ "};");
+ verifyFormat("var o = {\n"
+ " value: 'test',\n"
+ " someMethod(val) { // method\n"
+ " doSomething(this.value + val);\n"
+ " }\n"
+ "};");
+}
+
TEST_F(FormatTestJS, SpacesInContainerLiterals) {
verifyFormat("var arr = [1, 2, 3];");
verifyFormat("f({a: 1, b: 2, c: 3});");
OpenPOWER on IntegriCloud