[PATCH] D24804: clang-format: [JS] reserved words in method names.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 21 08:55:17 PDT 2016


mprobst updated this revision to Diff 72061.
mprobst added a comment.

- move up to support let in methods.


https://reviews.llvm.org/D24804

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

Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -147,6 +147,21 @@
   verifyFormat("x = interface instanceof y;");
 }
 
+TEST_F(FormatTestJS, ReservedWordsMethods) {
+  verifyFormat(
+      "class X {\n"
+      "  delete() {\n"
+      "    x();\n"
+      "  }\n"
+      "  interface() {\n"
+      "    x();\n"
+      "  }\n"
+      "  let() {\n"
+      "    x();\n"
+      "  }\n"
+      "}\n");
+}
+
 TEST_F(FormatTestJS, CppKeywords) {
   // Make sure we don't mess stuff up because of C++ keywords.
   verifyFormat("return operator && (aa);");
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2125,6 +2125,10 @@
     if (Right.is(tok::star) &&
         Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
       return false;
+    // JS methods can use some keywords as names (e.g. `delete()`).
+    if (Right.is(tok::l_paren) && Line.MustBeDeclaration &&
+        Left.Tok.getIdentifierInfo())
+      return false;
     if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
                      Keywords.kw_of, tok::kw_const) &&
         (!Left.Previous || !Left.Previous->is(tok::period)))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24804.72061.patch
Type: text/x-patch
Size: 1379 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160921/f5442f89/attachment.bin>


More information about the cfe-commits mailing list