[PATCH] D21273: clang-format: [JS] Introduce WrapJavaScriptImports option.

Martin Probst via cfe-commits cfe-commits at lists.llvm.org
Sun Jun 12 18:58:26 PDT 2016


mprobst created this revision.
mprobst added a reviewer: djasper.
mprobst added a subscriber: cfe-commits.
Herald added a subscriber: klimek.

When turned on, clang-format wraps JavaScript imports (and importing exports),
instead of forcing the entire import statement onto one line.

http://reviews.llvm.org/D21273

Files:
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestJS.cpp

Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1078,6 +1078,27 @@
                "}");
 }
 
+TEST_F(FormatTestJS, ImportWrapping) {
+  FormatStyle Style = getGoogleJSStyleWithColumns(80);
+  Style.WrapJavaScriptImports = true;
+  verifyFormat("import {\n"
+               "  VeryLongImportsAreAnnoying,\n"
+               "  VeryLongImportsAreAnnoying,\n"
+               "  VeryLongImportsAreAnnoying,\n"
+               "} from 'some/module.js';",
+               Style);
+  verifyFormat("import {\n"
+               "  A,\n"
+               "  A,\n"
+               "} from 'some/module.js';",
+               Style);
+  verifyFormat("export {\n"
+               "  A,\n"
+               "  A,\n"
+               "} from 'some/module.js';",
+               Style);
+}
+
 TEST_F(FormatTestJS, TemplateStrings) {
   // Keeps any whitespace/indentation within the template string.
   verifyFormat("var x = `hello\n"
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -786,7 +786,7 @@
 
     // import {...} from '...';
     if (Style.Language == FormatStyle::LK_JavaScript &&
-        CurrentToken->is(Keywords.kw_import))
+        CurrentToken->is(Keywords.kw_import) && !Style.WrapJavaScriptImports)
       return LT_ImportStatement;
 
     bool KeywordVirtualFound = false;
@@ -804,7 +804,7 @@
         if (Line.First->is(tok::kw_export) &&
             CurrentToken->is(Keywords.kw_from) && CurrentToken->Next &&
             CurrentToken->Next->isStringLiteral())
-          ImportStatement = true;
+          ImportStatement = !Style.WrapJavaScriptImports;
         if (isClosureImportStatement(*CurrentToken))
           ImportStatement = true;
       }
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -354,6 +354,7 @@
     IO.mapOptional("TabWidth", Style.TabWidth);
     IO.mapOptional("UseTab", Style.UseTab);
     IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes);
+    IO.mapOptional("WrapJavaScriptImports", Style.WrapJavaScriptImports);
   }
 };
 
@@ -613,6 +614,7 @@
     GoogleStyle.MaxEmptyLinesToKeep = 3;
     GoogleStyle.SpacesInContainerLiterals = false;
     GoogleStyle.JavaScriptQuotes = FormatStyle::JSQS_Single;
+    GoogleStyle.WrapJavaScriptImports = false;
   } else if (Language == FormatStyle::LK_Proto) {
     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
     GoogleStyle.SpacesInContainerLiterals = false;
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -627,6 +627,9 @@
   /// \brief The JavaScriptQuoteStyle to use for JavaScript strings.
   JavaScriptQuoteStyle JavaScriptQuotes;
 
+  /// \brief Whether to wrap JavaScript import/export statements.
+  bool WrapJavaScriptImports;
+
   bool operator==(const FormatStyle &R) const {
     return AccessModifierOffset == R.AccessModifierOffset &&
            AlignAfterOpenBracket == R.AlignAfterOpenBracket &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21273.60484.patch
Type: text/x-patch
Size: 3327 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160613/885e0b72/attachment-0001.bin>


More information about the cfe-commits mailing list