[PATCH] D30705: clang-format: [JS] allow breaking after non-null assertions.
Martin Probst via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 7 11:04:11 PST 2017
mprobst updated this revision to Diff 90889.
mprobst added a comment.
- Introduce TT_JsNonNullAssertion to centralize determining token types.
https://reviews.llvm.org/D30705
Files:
lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJS.cpp
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1679,6 +1679,12 @@
verifyFormat("let x = (foo)!;\n");
verifyFormat("let x = foo! - 1;\n");
verifyFormat("let x = {foo: 1}!;\n");
+ verifyFormat(
+ "let x = hello.foo()!\n"
+ " .foo()!\n"
+ " .foo()!\n"
+ " .foo()!;\n",
+ getGoogleJSStyleWithColumns(20));
}
TEST_F(FormatTestJS, Conditional) {
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1052,7 +1052,22 @@
Contexts.back().CaretFound = true;
} else if (Current.isOneOf(tok::minusminus, tok::plusplus)) {
Current.Type = determineIncrementUsage(Current);
- } else if (Current.isOneOf(tok::exclaim, tok::tilde)) {
+ } else if (Current.is(tok::exclaim)) {
+ if (Style.Language == FormatStyle::LK_JavaScript) {
+ if (Current.Previous &&
+ (Current.Previous->isOneOf(tok::identifier, tok::r_paren,
+ tok::r_square, tok::r_brace) ||
+ Current.Previous->Tok.isLiteral())) {
+ Current.Type = TT_JsNonNullAssertion;
+ return;
+ } else if (Current.Next &&
+ Current.Next->isOneOf(TT_BinaryOperator, Keywords.kw_as)) {
+ Current.Type = TT_JsNonNullAssertion;
+ return;
+ }
+ }
+ Current.Type = TT_UnaryOperator;
+ } else if (Current.is(tok::tilde)) {
Current.Type = TT_UnaryOperator;
} else if (Current.is(tok::question)) {
if (Style.Language == FormatStyle::LK_JavaScript &&
@@ -2277,12 +2292,9 @@
// locations that should have whitespace following are identified by the
// above set of follower tokens.
return false;
- // Postfix non-null assertion operator, as in `foo!.bar()`.
- if (Right.is(tok::exclaim) && (Left.isOneOf(tok::identifier, tok::r_paren,
- tok::r_square, tok::r_brace) ||
- Left.Tok.isLiteral()))
+ if (Right.is(TT_JsNonNullAssertion))
return false;
- if (Left.is(tok::exclaim) && Right.is(Keywords.kw_as))
+ if (Left.is(TT_JsNonNullAssertion) && Right.is(Keywords.kw_as))
return true; // "x! as string"
} else if (Style.Language == FormatStyle::LK_Java) {
if (Left.is(tok::r_square) && Right.is(tok::l_brace))
@@ -2535,6 +2547,8 @@
return false; // must not break before as in 'x as type' casts
if (Left.is(Keywords.kw_as))
return true;
+ if (Left.is(TT_JsNonNullAssertion))
+ return true;
if (Left.is(Keywords.kw_declare) &&
Right.isOneOf(Keywords.kw_module, tok::kw_namespace,
Keywords.kw_function, tok::kw_class, tok::kw_enum,
Index: lib/Format/FormatToken.h
===================================================================
--- lib/Format/FormatToken.h
+++ lib/Format/FormatToken.h
@@ -53,6 +53,7 @@
TYPE(JavaAnnotation) \
TYPE(JsComputedPropertyName) \
TYPE(JsFatArrow) \
+ TYPE(JsNonNullAssertion) \
TYPE(JsTypeColon) \
TYPE(JsTypeOperator) \
TYPE(JsTypeOptionalQuestion) \
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30705.90889.patch
Type: text/x-patch
Size: 3355 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170307/f073bc70/attachment-0001.bin>
More information about the cfe-commits
mailing list