]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] avoid indent after ambient function declarations.
authorMartin Probst <martin@probst.io>
Wed, 4 Jan 2017 13:36:43 +0000 (13:36 +0000)
committerMartin Probst <martin@probst.io>
Wed, 4 Jan 2017 13:36:43 +0000 (13:36 +0000)
Summary:
Before:
  declare function foo();
    let x = 1;

After:
  declare function foo();
  let x = 1;

The problem was that clang-format would unconditionally try to parse a child block, even though ambient function declarations do not have a body (similar to forward declarations).

Reviewers: djasper

Subscribers: cfe-commits, klimek

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@290959 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp

index 84e06d05c739f851b796fa4fc6912c1d870b62f4..370cf7afa3303c5cb9ed64dbe8a1c356875e1dba 100644 (file)
@@ -1255,10 +1255,13 @@ void UnwrappedLineParser::tryToParseJSFunction() {
     if (FormatTok->is(tok::l_brace))
       tryToParseBracedList();
     else
-      while (FormatTok->isNot(tok::l_brace) && !eof())
+      while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
         nextToken();
   }
 
+  if (FormatTok->is(tok::semi))
+    return;
+
   parseChildBlock();
 }
 
index 6f494db71d156d0d8a94a10659adc7c273339e68..90c99317bd79c3f93a8ff1732cb0ac35394a0c7c 100644 (file)
@@ -377,6 +377,16 @@ TEST_F(FormatTestJS, AmbientDeclarations) {
       "declare function\n"
       "x();",  // TODO(martinprobst): should ideally be indented.
       NineCols);
+  verifyFormat("declare function foo();\n"
+               "let x = 1;\n");
+  verifyFormat("declare function foo(): string;\n"
+               "let x = 1;\n");
+  verifyFormat("declare function foo(): {x: number};\n"
+               "let x = 1;\n");
+  verifyFormat("declare class X {}\n"
+               "let x = 1;\n");
+  verifyFormat("declare interface Y {}\n"
+               "let x = 1;\n");
   verifyFormat(
       "declare enum X {\n"
       "}",