[PATCH] D54753: [clang-format] JS: don't treat is: as a type matcher
Krasimir Georgiev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 20 06:25:37 PST 2018
This revision was automatically updated to reflect the committed changes.
Closed by commit rC347307: [clang-format] JS: don't treat is: as a type matcher (authored by krasimir, committed by ).
Changed prior to commit:
https://reviews.llvm.org/D54753?vs=174759&id=174764#toc
Repository:
rC Clang
https://reviews.llvm.org/D54753
Files:
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -3113,8 +3113,21 @@
// Don't wrap between ":" and "!" of a strict prop init ("field!: type;").
if (Left.is(tok::exclaim) && Right.is(tok::colon))
return false;
- if (Right.is(Keywords.kw_is))
- return false;
+ // Look for is type annotations like:
+ // function f(): a is B { ... }
+ // Do not break before is in these cases.
+ if (Right.is(Keywords.kw_is)) {
+ const FormatToken* Next = Right.getNextNonComment();
+ // If `is` is followed by a colon, it's likely that it's a dict key, so
+ // ignore it for this check.
+ // For example this is common in Polymer:
+ // Polymer({
+ // is: 'name',
+ // ...
+ // });
+ if (!Next || !Next->is(tok::colon))
+ return false;
+ }
if (Left.is(Keywords.kw_in))
return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None;
if (Right.is(Keywords.kw_in))
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1186,6 +1186,16 @@
getGoogleJSStyleWithColumns(25));
}
+TEST_F(FormatTestJS, AddsIsTheDictKeyOnNewline) {
+ // Do not confuse is, the dict key with is, the type matcher. Put is, the dict
+ // key, on a newline.
+ verifyFormat("Polymer({\n"
+ " is: '', //\n"
+ " rest: 1\n"
+ "});",
+ getGoogleJSStyleWithColumns(20));
+}
+
TEST_F(FormatTestJS, AutomaticSemicolonInsertionHeuristic) {
verifyFormat("a\n"
"b;",
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54753.174764.patch
Type: text/x-patch
Size: 1785 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181120/8718296a/attachment.bin>
More information about the cfe-commits
mailing list