Hi djasper,

When an exported function would follow a class declaration, it would not be 
recognized as a stand-alone function. That would then collapse the following 
line with the current one, e.g.

    class C {}
    export function f() {} var x;

This change recognizes functions as free standing even when the line starts 
with an `export` keyword.

http://reviews.llvm.org/D10408

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

Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -838,9 +838,14 @@
       return;
     case tok::identifier: {
       // Parse function literal unless 'function' is the first token in a line
-      // in which case this should be treated as a free-standing function.
+      // (or it is part of an export statement) in which case this should be
+      // treated as a free-standing function.
       if (Style.Language == FormatStyle::LK_JavaScript &&
-          FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
+          FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0 &&
+          (true ||
+           !(Line->Tokens.size() == 1 &&
+             Line->Tokens.begin()->Tok->isOneOf(tok::kw_export,
+                                                Keywords.kw_import)))) {
         tryToParseJSFunction();
         break;
       }
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -726,6 +726,9 @@
   verifyFormat("export default class X { y: number }");
   verifyFormat("export default function() {\n  return 1;\n}");
   verifyFormat("export var x = 12;");
+  verifyFormat("class C {}\n"
+               "export function f() {}\n"
+               "var v;");
   verifyFormat("export var x: number = 12;");
   verifyFormat("export const y = {\n"
                "  a: 1,\n"

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -838,9 +838,14 @@
       return;
     case tok::identifier: {
       // Parse function literal unless 'function' is the first token in a line
-      // in which case this should be treated as a free-standing function.
+      // (or it is part of an export statement) in which case this should be
+      // treated as a free-standing function.
       if (Style.Language == FormatStyle::LK_JavaScript &&
-          FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
+          FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0 &&
+          (true ||
+           !(Line->Tokens.size() == 1 &&
+             Line->Tokens.begin()->Tok->isOneOf(tok::kw_export,
+                                                Keywords.kw_import)))) {
         tryToParseJSFunction();
         break;
       }
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -726,6 +726,9 @@
   verifyFormat("export default class X { y: number }");
   verifyFormat("export default function() {\n  return 1;\n}");
   verifyFormat("export var x = 12;");
+  verifyFormat("class C {}\n"
+               "export function f() {}\n"
+               "var v;");
   verifyFormat("export var x: number = 12;");
   verifyFormat("export const y = {\n"
                "  a: 1,\n"
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to