diff options
author | Martin Probst <martin@probst.io> | 2017-08-01 15:46:10 +0000 |
---|---|---|
committer | Martin Probst <martin@probst.io> | 2017-08-01 15:46:10 +0000 |
commit | cb870c57b334236dc7b0822c7366a735ac3c1880 (patch) | |
tree | 115eb9d34489b7f703a28821722c86ae8545a1d8 | |
parent | 7ebbe655edd411bd79a726251ec23d9477ccb64c (diff) | |
download | bcm5719-llvm-cb870c57b334236dc7b0822c7366a735ac3c1880.tar.gz bcm5719-llvm-cb870c57b334236dc7b0822c7366a735ac3c1880.zip |
clang-format: [JS] handle object types in extends positions.
Summary:
clang-format would previously drop the whitespace after `extends` in code such as:
class Foo extends {} {}
Where the first set of curly braces is an inline object literal type.
Reviewers: djasper
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D36131
llvm-svn: 309695
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 11 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 11 |
3 files changed, 24 insertions, 1 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index b2ffbbb1678..318bb259509 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2341,7 +2341,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) return false; if (Right.isOneOf(tok::l_brace, tok::l_square) && - Left.isOneOf(Keywords.kw_function, Keywords.kw_yield)) + Left.isOneOf(Keywords.kw_function, Keywords.kw_yield, + Keywords.kw_extends, Keywords.kw_implements)) return true; // JS methods can use some keywords as names (e.g. `delete()`). if (Right.is(tok::l_paren) && Line.MustBeDeclaration && diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index bbed83263ce..753eada8f1b 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1970,6 +1970,17 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) { ((Style.Language == FormatStyle::LK_Java || Style.Language == FormatStyle::LK_JavaScript) && FormatTok->isOneOf(tok::period, tok::comma))) { + if (Style.Language == FormatStyle::LK_JavaScript && + FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) { + // JavaScript/TypeScript supports inline object types in + // extends/implements positions: + // class Foo implements {bar: number} { } + nextToken(); + if (FormatTok->is(tok::l_brace)) { + tryToParseBracedList(); + continue; + } + } bool IsNonMacroIdentifier = FormatTok->is(tok::identifier) && FormatTok->TokenText != FormatTok->TokenText.upper(); diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index ba922b0f59f..d91df54f3da 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -1400,6 +1400,17 @@ TEST_F(FormatTestJS, InterfaceDeclarations) { "}"); } +TEST_F(FormatTestJS, ObjectTypesInExtendsImplements) { + verifyFormat("class C extends {} {}"); + verifyFormat("class C implements {bar: number} {}"); + // Somewhat odd, but probably closest to reasonable formatting? + verifyFormat("class C implements {\n" + " bar: number,\n" + " baz: string,\n" + "} {}"); + verifyFormat("class C<P extends {}> {}"); +} + TEST_F(FormatTestJS, EnumDeclarations) { verifyFormat("enum Foo {\n" " A = 1,\n" |