[PATCH] D69971: clang-format: [JS] support null operators.
Martin Probst via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 8 10:35:47 PST 2019
mprobst updated this revision to Diff 228495.
mprobst added a comment.
Add tests for .?(foo) and .?[foo].
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D69971/new/
https://reviews.llvm.org/D69971
Files:
clang/lib/Format/FormatToken.h
clang/lib/Format/FormatTokenLexer.cpp
clang/unittests/Format/FormatTestJS.cpp
Index: clang/unittests/Format/FormatTestJS.cpp
===================================================================
--- clang/unittests/Format/FormatTestJS.cpp
+++ clang/unittests/Format/FormatTestJS.cpp
@@ -2222,6 +2222,16 @@
verifyFormat("return !!x;\n");
}
+TEST_F(FormatTestJS, NullPropagatingOperator) {
+ verifyFormat("let x = foo?.bar?.baz();\n");
+ verifyFormat("let x = foo?.(foo);\n");
+ verifyFormat("let x = foo?.['arr'];\n");
+}
+
+TEST_F(FormatTestJS, NullishCoalescingOperator) {
+ verifyFormat("const val = something ?? 'some other default';\n");
+}
+
TEST_F(FormatTestJS, Conditional) {
verifyFormat("y = x ? 1 : 2;");
verifyFormat("x ? 1 : 2;");
Index: clang/lib/Format/FormatTokenLexer.cpp
===================================================================
--- clang/lib/Format/FormatTokenLexer.cpp
+++ clang/lib/Format/FormatTokenLexer.cpp
@@ -100,6 +100,10 @@
static const tok::TokenKind JSExponentiation[] = {tok::star, tok::star};
static const tok::TokenKind JSExponentiationEqual[] = {tok::star,
tok::starequal};
+ static const tok::TokenKind JSNullPropagatingOperator[] = {tok::question,
+ tok::period};
+ static const tok::TokenKind JSNullishOperator[] = {tok::question,
+ tok::question};
// FIXME: Investigate what token type gives the correct operator priority.
if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
@@ -116,6 +120,14 @@
Tokens.back()->Tok.setKind(tok::starequal);
return;
}
+ if (tryMergeTokens(JSNullishOperator, TT_JsNullishCoalescingOperator))
+ return;
+ if (tryMergeTokens(JSNullPropagatingOperator,
+ TT_JsNullPropagatingOperator)) {
+ // Treat like a regular "." access.
+ Tokens.back()->Tok.setKind(tok::period);
+ return;
+ }
if (tryMergeJSPrivateIdentifier())
return;
}
Index: clang/lib/Format/FormatToken.h
===================================================================
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -60,6 +60,8 @@
TYPE(JsExponentiationEqual) \
TYPE(JsFatArrow) \
TYPE(JsNonNullAssertion) \
+ TYPE(JsNullishCoalescingOperator) \
+ TYPE(JsNullPropagatingOperator) \
TYPE(JsPrivateIdentifier) \
TYPE(JsTypeColon) \
TYPE(JsTypeOperator) \
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69971.228495.patch
Type: text/x-patch
Size: 2878 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191108/e3dcc72e/attachment.bin>
More information about the cfe-commits
mailing list