[PATCH] clang-format: [JS] fix incorrectly collapsed lines after export statement.

Martin Probst martinprobst at google.com
Thu Jun 11 21:34:14 PDT 2015


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/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10408.27562.patch
Type: text/x-patch
Size: 1586 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150612/617216c6/attachment.bin>


More information about the cfe-commits mailing list