Author: Martin Probst Date: 2020-01-20T17:07:14+01:00 New Revision: 4c9d6914453d970b7b8202b7efd7524b2f0a72ac
URL: https://github.com/llvm/llvm-project/commit/4c9d6914453d970b7b8202b7efd7524b2f0a72ac DIFF: https://github.com/llvm/llvm-project/commit/4c9d6914453d970b7b8202b7efd7524b2f0a72ac.diff LOG: clang-format: [JS] fix `??` opreator wrapping. Summary: clang-format currently treats the nullish coalescing operator `??` like the ternary operator. That causes multiple nullish terms to be each indented relative to the last `??`, as they would in a ternary. The `??` operator is often used in chains though, and as such more similar to other binary operators, such as `||`. So to fix the indent, set its token type to `||`, so it inherits the same treatment. This opens up the question of operator precedence. However, `??` is required to be parenthesized when mixed with `||` and `&&`, so this is not a problem that can come up in syntactically legal code. Reviewers: krasimir Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D73026 Added: Modified: clang/lib/Format/FormatTokenLexer.cpp clang/unittests/Format/FormatTestJS.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index ef20ba884fb3..9c9fee2b0c32 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -120,8 +120,11 @@ void FormatTokenLexer::tryMergePreviousTokens() { Tokens.back()->Tok.setKind(tok::starequal); return; } - if (tryMergeTokens(JSNullishOperator, TT_JsNullishCoalescingOperator)) + if (tryMergeTokens(JSNullishOperator, TT_JsNullishCoalescingOperator)) { + // Treat like the "||" operator (as opposed to the ternary ?). + Tokens.back()->Tok.setKind(tok::pipepipe); return; + } if (tryMergeTokens(JSNullPropagatingOperator, TT_JsNullPropagatingOperator)) { // Treat like a regular "." access. diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index ffeb53d9a33c..ef3c6361355a 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -2294,6 +2294,11 @@ TEST_F(FormatTestJS, NullPropagatingOperator) { TEST_F(FormatTestJS, NullishCoalescingOperator) { verifyFormat("const val = something ?? 'some other default';\n"); + verifyFormat( + "const val = something ?? otherDefault ??\n" + " evenMore ?? evenMore;\n", + "const val = something ?? otherDefault ?? evenMore ?? evenMore;\n", + getGoogleJSStyleWithColumns(40)); } TEST_F(FormatTestJS, Conditional) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits