[PATCH] clang-format: [js] Support ES6 module exports.

Martin Probst martinprobst at google.com
Thu Feb 19 05:38:32 PST 2015


Hi djasper,

http://reviews.llvm.org/D7754

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

Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1759,7 +1759,8 @@
     if (Right.is(TT_JsTypeColon))
       return false;
     if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
-        Line.First->is(Keywords.kw_import))
+        (Line.First->is(Keywords.kw_import) ||
+         Line.First->is(tok::kw_export)))
       return false;
   } else if (Style.Language == FormatStyle::LK_Java) {
     if (Left.is(tok::r_square) && Right.is(tok::l_brace))
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -734,14 +734,16 @@
       }
     }
     break;
+  case tok::kw_export:
   case tok::identifier:
     if (FormatTok->IsForEachMacro) {
       parseForOrWhileLoop();
       return;
     }
     if (Style.Language == FormatStyle::LK_JavaScript &&
-        FormatTok->is(Keywords.kw_import)) {
-      parseJavaScriptEs6Import();
+        (FormatTok->is(Keywords.kw_import) ||
+         FormatTok->is(tok::kw_export))) {
+      parseJavaScriptEs6ImportExport();
       return;
     }
     // In all other cases, parse the declaration.
@@ -1603,8 +1605,8 @@
   parseObjCUntilAtEnd();
 }
 
-void UnwrappedLineParser::parseJavaScriptEs6Import() {
-  nextToken();  // 'import'
+void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
+  nextToken();  // 'import' or 'export'
   if (FormatTok->is(tok::l_brace)) {
     FormatTok->BlockKind = BK_Block;
     parseBracedList();
Index: lib/Format/UnwrappedLineParser.h
===================================================================
--- lib/Format/UnwrappedLineParser.h
+++ lib/Format/UnwrappedLineParser.h
@@ -103,7 +103,7 @@
   void parseObjCUntilAtEnd();
   void parseObjCInterfaceOrImplementation();
   void parseObjCProtocol();
-  void parseJavaScriptEs6Import();
+  void parseJavaScriptEs6ImportExport();
   bool tryToParseLambda();
   bool tryToParseLambdaIntroducer();
   void tryToParseJSFunction();
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -545,9 +545,14 @@
   verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
   verifyFormat("import * as lib from 'some/module.js';");
   verifyFormat("var x = {\n  import: 1\n};\nx.import = 2;");
-  verifyFormat("export function fn() {\n  return 'fn';\n}");
+  verifyFormat("export function fn() { return 'fn'; }");
   verifyFormat("export const x = 12;");
   verifyFormat("export default class X {}");
+  verifyFormat("export {X, Y} from 'some/module.js';");
+  verifyFormat("export {\n"
+               "  X,\n"
+               "  Y,\n"
+               "} from 'some/module.js';");
 }
 
 } // end namespace tooling

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7754.20286.patch
Type: text/x-patch
Size: 2959 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150219/b0d43fd4/attachment.bin>


More information about the cfe-commits mailing list