]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Parse exported functions as free-standing.
authorDaniel Jasper <djasper@google.com>
Mon, 11 May 2015 09:03:10 +0000 (09:03 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 11 May 2015 09:03:10 +0000 (09:03 +0000)
Before:
  export function foo() {} export function bar() {}

After:
  export function foo() {
  }
  export function bar() {
  }

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

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

index a85d9c7731660aa8cea4e557a318cc7e6309a83c..437d688dc57bd9354cd7c83ed5bf7c6a1c579e16 100644 (file)
@@ -1666,8 +1666,16 @@ void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
   assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
   nextToken();
 
-  if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
-                         Keywords.kw_var))
+  // Consume "function" and "default function", so that these get parsed as
+  // free-standing JS functions, i.e. do not require a trailing semicolon.
+  if (FormatTok->is(tok::kw_default))
+    nextToken();
+  if (FormatTok->is(Keywords.kw_function)) {
+    nextToken();
+    return;
+  }
+
+  if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_var))
     return; // Fall through to parsing the corresponding structure.
 
   if (FormatTok->is(tok::kw_default)) {
index 7494bccea5eec0c4e8602b42a35ee0b9e685748b..41ffbedea74e204c4b1124aaa357ba7dacce0b9a 100644 (file)
@@ -600,6 +600,12 @@ TEST_F(FormatTestJS, Modules) {
   verifyFormat("export function fn() {\n"
                "  return 'fn';\n"
                "}");
+  verifyFormat("export function A() {\n"
+               "}\n"
+               "export default function B() {\n"
+               "}\n"
+               "export function C() {\n"
+               "}");
   verifyFormat("export const x = 12;");
   verifyFormat("export default class X {}");
   verifyFormat("export {X, Y} from 'some/module.js';");