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

Daniel Jasper djasper at google.com
Thu Feb 19 07:58:13 PST 2015


================
Comment at: lib/Format/TokenAnnotator.cpp:1762
@@ -1761,2 +1761,3 @@
     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)))
----------------
  Line.First->isOneOf(Keywords.kw_import, tok::kw_export)

================
Comment at: lib/Format/UnwrappedLineParser.cpp:737
@@ -736,2 +736,3 @@
     break;
+  case tok::kw_export:
   case tok::identifier:
----------------
I'd add:

  if (Style.Language == FormatStyle::LK_JavaScript) {
    parseJavaScriptEs6ImportExport();
    return;
  }
  break;

here and not fall through or change the block below.

================
Comment at: lib/Format/UnwrappedLineParser.cpp:744
@@ -742,3 +743,3 @@
     if (Style.Language == FormatStyle::LK_JavaScript &&
-        FormatTok->is(Keywords.kw_import)) {
-      parseJavaScriptEs6Import();
+        (FormatTok->is(Keywords.kw_import) ||
+         FormatTok->is(tok::kw_export))) {
----------------
  Line.First->isOneOf(Keywords.kw_import, tok::kw_export)

================
Comment at: lib/Format/UnwrappedLineParser.cpp:1609
@@ +1608,3 @@
+void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
+  nextToken(); // 'import' or 'export'
+  switch (FormatTok->Tok.getKind()) {
----------------
Maybe assert instead of the comment?

================
Comment at: lib/Format/UnwrappedLineParser.cpp:1622
@@ +1621,3 @@
+  default:
+    if (FormatTok->isOneOf(Keywords.kw_function, Keywords.kw_var))
+      return; // export function ...
----------------
I'd probably use if statements instead of the switch. Seems a bit bad to mix switch and if (which you can't avoid because of Keywords):

  if (FormatTok->isOneOf(tok::kw_const, tok::kw_class,
                         Keywords.kw_function, Keywords.kw_var))
    return; // Fall through to parsing the corresponding structure.

  if (FormatTok->is(tok::kw_default)) {
    nextToken();
    return;
  }

  if (FormatTok->is(tok::l_brace)) {
    FormatTok->BlockKind = BK_Block;
    parseBracedList();
  }

http://reviews.llvm.org/D7754

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list