clang-format: [JS] reserved words in method names.
authorMartin Probst <martin@probst.io>
Thu, 22 Sep 2016 07:18:00 +0000 (07:18 +0000)
committerMartin Probst <martin@probst.io>
Thu, 22 Sep 2016 07:18:00 +0000 (07:18 +0000)
Summary:
Before:
    class X {
      delete () {
        ...
      }
    }

After:
    class X {
      delete() {
        ...
      }
    }

Reviewers: djasper

Subscribers: cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D24804

llvm-svn: 282138

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestJS.cpp

index 3b35212..75eb137 100644 (file)
@@ -2125,6 +2125,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     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)))
index c19c054..3296138 100644 (file)
@@ -147,6 +147,21 @@ TEST_F(FormatTestJS, ReservedWords) {
   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);");