[PATCH] D36142: clang-format: [JS] do not insert whitespace in call positions.

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 1 05:10:41 PDT 2017


mprobst created this revision.
Herald added a subscriber: klimek.

In JavaScript, may keywords can be used in method names and thus call sites:

  foo.delete();
  foo.instanceof();

clang-format would previously insert whitespace after the `instanceof`. This
change generically skips inserting whitespace between a keyword and a
parenthesis if preceded by a dot, i.e. in a callsite.


https://reviews.llvm.org/D36142

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestJS.cpp


Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -232,6 +232,7 @@
   verifyFormat("x.var() = 1;");
   verifyFormat("x.for() = 1;");
   verifyFormat("x.as() = 1;");
+  verifyFormat("x.instanceof() = 1;");
   verifyFormat("x = {\n"
                "  a: 12,\n"
                "  interface: 1,\n"
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2347,6 +2347,11 @@
     if (Right.is(tok::l_paren) && Line.MustBeDeclaration &&
         Left.Tok.getIdentifierInfo())
       return false;
+    // Valid JS method names can include keywords, e.g. `foo.delete()` or
+    // `bar.instanceof()`.
+    if (Right.is(tok::l_paren) && Left.Tok.getIdentifierInfo() &&
+        Left.Previous && Left.Previous->is(tok::period))
+      return false;
     if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
                       tok::kw_const) ||
          // "of" is only a keyword if it appears after another identifier


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36142.109087.patch
Type: text/x-patch
Size: 1189 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170801/247a4506/attachment.bin>


More information about the cfe-commits mailing list