[clang] b44eb20 - [clang-format] Refactor condition in AnnotatingParser::modifyContext(). NFC.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 11 04:13:50 PST 2022


Author: Marek Kurdej
Date: 2022-03-11T13:12:43+01:00
New Revision: b44eb207e96a5e20aecbb6084147ae97e3eabd22

URL: https://github.com/llvm/llvm-project/commit/b44eb207e96a5e20aecbb6084147ae97e3eabd22
DIFF: https://github.com/llvm/llvm-project/commit/b44eb207e96a5e20aecbb6084147ae97e3eabd22.diff

LOG: [clang-format] Refactor condition in AnnotatingParser::modifyContext(). NFC.

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 8f5f4e0f43362..1c662c9a909c9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1506,15 +1506,24 @@ class AnnotatingParser {
   };
 
   void modifyContext(const FormatToken &Current) {
-    if (Current.getPrecedence() == prec::Assignment &&
-        !Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return) &&
-        // Type aliases use `type X = ...;` in TypeScript and can be exported
-        // using `export type ...`.
-        !(Style.isJavaScript() &&
+    auto AssignmentStartsExpression = [&]() {
+      if (Current.getPrecedence() != prec::Assignment)
+        return false;
+
+      if (Line.First->isOneOf(tok::kw_template, tok::kw_using, tok::kw_return))
+        return false;
+
+      // Type aliases use `type X = ...;` in TypeScript and can be exported
+      // using `export type ...`.
+      if (Style.isJavaScript() &&
           (Line.startsWith(Keywords.kw_type, tok::identifier) ||
-           Line.startsWith(tok::kw_export, Keywords.kw_type,
-                           tok::identifier))) &&
-        (!Current.Previous || Current.Previous->isNot(tok::kw_operator))) {
+           Line.startsWith(tok::kw_export, Keywords.kw_type, tok::identifier)))
+        return false;
+
+      return !Current.Previous || Current.Previous->isNot(tok::kw_operator);
+    };
+
+    if (AssignmentStartsExpression()) {
       Contexts.back().IsExpression = true;
       if (!Line.startsWith(TT_UnaryOperator)) {
         for (FormatToken *Previous = Current.Previous;


        


More information about the cfe-commits mailing list