[PATCH] D91132: clang-format: [JS] support new assignment operators.
Martin Probst via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 10 00:22:59 PST 2020
mprobst updated this revision to Diff 304069.
mprobst added a comment.
- fix comment
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D91132/new/
https://reviews.llvm.org/D91132
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
@@ -2424,6 +2424,15 @@
getGoogleJSStyleWithColumns(40));
}
+TEST_F(FormatTestJS, AssignmentOperators) {
+ verifyFormat("a &&= b;\n");
+ verifyFormat("a ||= b;\n");
+ // NB: need to split ? ?= to avoid it being interpreted by C++ as a trigraph
+ // for #.
+ verifyFormat("a ?"
+ "?= b;\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
@@ -121,6 +121,10 @@
tok::period};
static const tok::TokenKind JSNullishOperator[] = {tok::question,
tok::question};
+ static const tok::TokenKind JSNullishEqual[] = {tok::question,
+ tok::question, tok::equal};
+ static const tok::TokenKind JSPipePipeEqual[] = {tok::pipepipe, tok::equal};
+ static const tok::TokenKind JSAndAndEqual[] = {tok::ampamp, tok::equal};
// FIXME: Investigate what token type gives the correct operator priority.
if (tryMergeTokens(JSIdentity, TT_BinaryOperator))
@@ -148,6 +152,13 @@
Tokens.back()->Tok.setKind(tok::period);
return;
}
+ if (tryMergeTokens(JSAndAndEqual, TT_JsAndAndEqual) ||
+ tryMergeTokens(JSPipePipeEqual, TT_JsPipePipeEqual) ||
+ tryMergeTokens(JSNullishEqual, TT_JsNullishCoalescingEqual)) {
+ // Treat like the "=" assignment operator.
+ Tokens.back()->Tok.setKind(tok::equal);
+ return;
+ }
if (tryMergeJSPrivateIdentifier())
return;
}
Index: clang/lib/Format/FormatToken.h
===================================================================
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -68,6 +68,9 @@
TYPE(JsTypeColon) \
TYPE(JsTypeOperator) \
TYPE(JsTypeOptionalQuestion) \
+ TYPE(JsAndAndEqual) \
+ TYPE(JsPipePipeEqual) \
+ TYPE(JsNullishCoalescingEqual) \
TYPE(LambdaArrow) \
TYPE(LambdaLBrace) \
TYPE(LambdaLSquare) \
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91132.304069.patch
Type: text/x-patch
Size: 2914 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201110/cbcf0f9a/attachment.bin>
More information about the cfe-commits
mailing list