diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3370,6 +3370,12 @@ Style.BitFieldColonSpacing == FormatStyle::BFCS_Before; return true; } + // Do not merge "- -" into "--". + if ((Left.isOneOf(tok::minus, tok::minusminus) && + Right.isOneOf(tok::minus, tok::minusminus)) || + (Left.isOneOf(tok::plus, tok::plusplus) && + Right.isOneOf(tok::plus, tok::plusplus))) + return true; if (Left.is(TT_UnaryOperator)) { if (!Right.is(tok::l_paren)) { // The alternative operators for ~ and ! are "compl" and "not". diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -276,6 +276,12 @@ // ES6 spread operator. verifyFormat("someFunction(...a);"); verifyFormat("var x = [1, ...a, 2];"); + + // "- -1" is legal JS syntax, but must not collapse into "--". + verifyFormat("- -1;", " - -1;"); + verifyFormat("-- -1;", " -- -1;"); + verifyFormat("+ +1;", " + +1;"); + verifyFormat("++ +1;", " ++ +1;"); } TEST_F(FormatTestJS, UnderstandsAmpAmp) {