[PATCH] D36131: clang-format: [JS] handle object types in extends positions.
Martin Probst via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 1 01:26:21 PDT 2017
mprobst created this revision.
Herald added a subscriber: klimek.
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.
https://reviews.llvm.org/D36131
Files:
lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1400,6 +1400,17 @@
"}");
}
+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"
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -1970,6 +1970,17 @@
((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();
@@ -1989,7 +2000,7 @@
// and thus rule out the record production in case there is no template
// (this would still leave us with an ambiguity between template function
// and class declarations).
- if (FormatTok->isOneOf(tok::colon, tok::less)) {
+ if (FormatTok->isOneOf(tok::colon, tok::less, Keywords.kw_extends)) {
while (!eof()) {
if (FormatTok->is(tok::l_brace)) {
calculateBraceTypes(/*ExpectClassBody=*/true);
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2341,7 +2341,8 @@
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 &&
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36131.109058.patch
Type: text/x-patch
Size: 2818 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170801/66a77022/attachment.bin>
More information about the cfe-commits
mailing list