clang-format: [JS] Support getters, setters and methods in object literals.

llvm-svn: 233698
This commit is contained in:
Daniel Jasper 2015-03-31 14:34:15 +00:00
parent 462501ee7e
commit f46dec86b6
2 changed files with 32 additions and 0 deletions

View File

@ -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;

View File

@ -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});");