[PATCH] D99495: clang-format: [JS] do not collapse - - to --.
Martin Probst via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 29 04:16:13 PDT 2021
mprobst created this revision.
mprobst added a reviewer: krasimir.
mprobst requested review of this revision.
Herald added a project: clang.
In JavaScript, `- -1;` is legal syntax, the language allows unary minus.
However the two tokens must not collapse together: `--1` is prefix
decrement, i.e. different syntax.
Before:
- -1; ==> --1;
After:
- -1; ==> - -1;
This change makes no attempt to format this "nicely", given by all
likelihood this represents a programming mistake by the user, or odd
generated code.
The check is not guarded by language: this appears to be a problem in
Java as well, and will also be beneficial when formatting syntactically
incorrect C++ (e.g. during editing).
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D99495
Files:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestJS.cpp
Index: clang/unittests/Format/FormatTestJS.cpp
===================================================================
--- clang/unittests/Format/FormatTestJS.cpp
+++ 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) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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".
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99495.333813.patch
Type: text/x-patch
Size: 1309 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210329/c6ff0a37/attachment.bin>
More information about the cfe-commits
mailing list