summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2015-12-29 08:54:23 +0000
committerDaniel Jasper <djasper@google.com>2015-12-29 08:54:23 +0000
commit6f5a1933b788d7eb6653deed871fd9ea0d1eae6d (patch)
tree57cc8d7c2608b080c5d483f13933939d88166f2e
parentafb72f38f8bee35c64801667201ee0d5882c4834 (diff)
downloadbcm5719-llvm-6f5a1933b788d7eb6653deed871fd9ea0d1eae6d.tar.gz
bcm5719-llvm-6f5a1933b788d7eb6653deed871fd9ea0d1eae6d.zip
clang-format: [JS/TypeScript] Support "enum" as property name.
Before: enum: string []; After: enum: string[]; llvm-svn: 256546
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp23
-rw-r--r--clang/lib/Format/UnwrappedLineParser.h2
-rw-r--r--clang/unittests/Format/FormatTestJS.cpp1
3 files changed, 18 insertions, 8 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 34c0c838072..94b84988194 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -818,7 +818,8 @@ void UnwrappedLineParser::parseStructuralElement() {
case tok::kw_enum:
// parseEnum falls through and does not yet add an unwrapped line as an
// enum definition can start a structural element.
- parseEnum();
+ if (!parseEnum())
+ break;
// This only applies for C++.
if (Style.Language != FormatStyle::LK_Cpp) {
addUnwrappedLine();
@@ -1524,11 +1525,17 @@ void UnwrappedLineParser::parseAccessSpecifier() {
addUnwrappedLine();
}
-void UnwrappedLineParser::parseEnum() {
+bool UnwrappedLineParser::parseEnum() {
// Won't be 'enum' for NS_ENUMs.
if (FormatTok->Tok.is(tok::kw_enum))
nextToken();
+ // In TypeScript, "enum" can also be used as property name, e.g. in interface
+ // declarations. An "enum" keyword followed by a colon would be a syntax
+ // error and thus assume it is just an identifier.
+ if (Style.Language == FormatStyle::LK_JavaScript && FormatTok->is(tok::colon))
+ return false;
+
// Eat up enum class ...
if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
nextToken();
@@ -1546,22 +1553,23 @@ void UnwrappedLineParser::parseEnum() {
// return type. In Java, this can be "implements", etc.
if (Style.Language == FormatStyle::LK_Cpp &&
FormatTok->is(tok::identifier))
- return;
+ return false;
}
}
// Just a declaration or something is wrong.
if (FormatTok->isNot(tok::l_brace))
- return;
+ return true;
FormatTok->BlockKind = BK_Block;
if (Style.Language == FormatStyle::LK_Java) {
// Java enums are different.
parseJavaEnumBody();
- return;
- } else if (Style.Language == FormatStyle::LK_Proto) {
+ return true;
+ }
+ if (Style.Language == FormatStyle::LK_Proto) {
parseBlock(/*MustBeDeclaration=*/true);
- return;
+ return true;
}
// Parse enum body.
@@ -1571,6 +1579,7 @@ void UnwrappedLineParser::parseEnum() {
nextToken();
addUnwrappedLine();
}
+ return true;
// There is no addUnwrappedLine() here so that we fall through to parsing a
// structural element afterwards. Thus, in "enum A {} n, m;",
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index c2fa0295768..a13c03f9408 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -96,7 +96,7 @@ private:
void parseNamespace();
void parseNew();
void parseAccessSpecifier();
- void parseEnum();
+ bool parseEnum();
void parseJavaEnumBody();
void parseRecord();
void parseObjCProtocolList();
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 9bfd587412d..56b493dae1c 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -762,6 +762,7 @@ TEST_F(FormatTestJS, ClassDeclarations) {
TEST_F(FormatTestJS, InterfaceDeclarations) {
verifyFormat("interface I {\n"
" x: string;\n"
+ " enum: string[];\n"
"}\n"
"var y;");
// Ensure that state is reset after parsing the interface.
OpenPOWER on IntegriCloud