r233698 - clang-format: [JS] Support getters, setters and methods in object literals.
Daniel Jasper
djasper at google.com
Tue Mar 31 07:34:16 PDT 2015
Author: djasper
Date: Tue Mar 31 09:34:15 2015
New Revision: 233698
URL: http://llvm.org/viewvc/llvm-project?rev=233698&view=rev
Log:
clang-format: [JS] Support getters, setters and methods in object literals.
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=233698&r1=233697&r2=233698&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Tue Mar 31 09:34:15 2015
@@ -1036,6 +1036,17 @@ bool UnwrappedLineParser::parseBracedLis
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;
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=233698&r1=233697&r2=233698&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Mar 31 09:34:15 2015
@@ -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});");
More information about the cfe-commits
mailing list