Skip to content

Commit 6f5a193

Browse files
committed
clang-format: [JS/TypeScript] Support "enum" as property name.
Before: enum: string []; After: enum: string[]; llvm-svn: 256546
1 parent afb72f3 commit 6f5a193

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,8 @@ void UnwrappedLineParser::parseStructuralElement() {
818818
case tok::kw_enum:
819819
// parseEnum falls through and does not yet add an unwrapped line as an
820820
// enum definition can start a structural element.
821-
parseEnum();
821+
if (!parseEnum())
822+
break;
822823
// This only applies for C++.
823824
if (Style.Language != FormatStyle::LK_Cpp) {
824825
addUnwrappedLine();
@@ -1524,11 +1525,17 @@ void UnwrappedLineParser::parseAccessSpecifier() {
15241525
addUnwrappedLine();
15251526
}
15261527

1527-
void UnwrappedLineParser::parseEnum() {
1528+
bool UnwrappedLineParser::parseEnum() {
15281529
// Won't be 'enum' for NS_ENUMs.
15291530
if (FormatTok->Tok.is(tok::kw_enum))
15301531
nextToken();
15311532

1533+
// In TypeScript, "enum" can also be used as property name, e.g. in interface
1534+
// declarations. An "enum" keyword followed by a colon would be a syntax
1535+
// error and thus assume it is just an identifier.
1536+
if (Style.Language == FormatStyle::LK_JavaScript && FormatTok->is(tok::colon))
1537+
return false;
1538+
15321539
// Eat up enum class ...
15331540
if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
15341541
nextToken();
@@ -1546,22 +1553,23 @@ void UnwrappedLineParser::parseEnum() {
15461553
// return type. In Java, this can be "implements", etc.
15471554
if (Style.Language == FormatStyle::LK_Cpp &&
15481555
FormatTok->is(tok::identifier))
1549-
return;
1556+
return false;
15501557
}
15511558
}
15521559

15531560
// Just a declaration or something is wrong.
15541561
if (FormatTok->isNot(tok::l_brace))
1555-
return;
1562+
return true;
15561563
FormatTok->BlockKind = BK_Block;
15571564

15581565
if (Style.Language == FormatStyle::LK_Java) {
15591566
// Java enums are different.
15601567
parseJavaEnumBody();
1561-
return;
1562-
} else if (Style.Language == FormatStyle::LK_Proto) {
1568+
return true;
1569+
}
1570+
if (Style.Language == FormatStyle::LK_Proto) {
15631571
parseBlock(/*MustBeDeclaration=*/true);
1564-
return;
1572+
return true;
15651573
}
15661574

15671575
// Parse enum body.
@@ -1571,6 +1579,7 @@ void UnwrappedLineParser::parseEnum() {
15711579
nextToken();
15721580
addUnwrappedLine();
15731581
}
1582+
return true;
15741583

15751584
// There is no addUnwrappedLine() here so that we fall through to parsing a
15761585
// structural element afterwards. Thus, in "enum A {} n, m;",

clang/lib/Format/UnwrappedLineParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class UnwrappedLineParser {
9696
void parseNamespace();
9797
void parseNew();
9898
void parseAccessSpecifier();
99-
void parseEnum();
99+
bool parseEnum();
100100
void parseJavaEnumBody();
101101
void parseRecord();
102102
void parseObjCProtocolList();

clang/unittests/Format/FormatTestJS.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ TEST_F(FormatTestJS, ClassDeclarations) {
762762
TEST_F(FormatTestJS, InterfaceDeclarations) {
763763
verifyFormat("interface I {\n"
764764
" x: string;\n"
765+
" enum: string[];\n"
765766
"}\n"
766767
"var y;");
767768
// Ensure that state is reset after parsing the interface.

0 commit comments

Comments
 (0)