]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] handle let (ES6)
authorDaniel Jasper <djasper@google.com>
Mon, 28 Sep 2015 14:28:08 +0000 (14:28 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 28 Sep 2015 14:28:08 +0000 (14:28 +0000)
Patch by Martin Probst. Thank you!

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

lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp

index 8586337c86c1b7d5ccc266bcf77e0ae90363c742..c78f6a65bd5a2c8d0ac7f81212667a1947526e06 100644 (file)
@@ -536,6 +536,7 @@ struct AdditionalKeywords {
     kw_finally = &IdentTable.get("finally");
     kw_function = &IdentTable.get("function");
     kw_import = &IdentTable.get("import");
+    kw_let = &IdentTable.get("let");
     kw_var = &IdentTable.get("var");
 
     kw_abstract = &IdentTable.get("abstract");
@@ -577,6 +578,7 @@ struct AdditionalKeywords {
   IdentifierInfo *kw_finally;
   IdentifierInfo *kw_function;
   IdentifierInfo *kw_import;
+  IdentifierInfo *kw_let;
   IdentifierInfo *kw_var;
 
   // Java keywords.
index 53027c79b3642d10d9dc89c735384d68121aea71..068f9727b89cd320b5b3f9e931ecfdfd772fe362 100644 (file)
@@ -1951,7 +1951,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
         Left.isOneOf(Keywords.kw_returns, Keywords.kw_option))
       return true;
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
-    if (Left.isOneOf(Keywords.kw_var, TT_JsFatArrow))
+    if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, TT_JsFatArrow))
       return true;
     if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
       return false;
@@ -2082,8 +2082,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
         Left.Previous && Left.Previous->is(tok::equal) &&
         Line.First->isOneOf(tok::identifier, Keywords.kw_import, tok::kw_export,
                             tok::kw_const) &&
-        // kw_var is a pseudo-token that's a tok::identifier, so matches above.
-        !Line.startsWith(Keywords.kw_var))
+        // kw_var/kw_let are pseudo-tokens that are tok::identifier, so match
+        // above.
+        !Line.First->isOneOf(Keywords.kw_var, Keywords.kw_let))
       // Object literals on the top level of a file are treated as "enum-style".
       // Each key/value pair is put on a separate line, instead of bin-packing.
       return true;
index e628072c6392e083c26f4e9185179d1de75fbfd2..d8301e69612dff7bf5f26a9cf2ff99f74780034b 100644 (file)
@@ -1782,7 +1782,7 @@ void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
   }
 
   if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
-                         Keywords.kw_var))
+                         Keywords.kw_let, Keywords.kw_var))
     return; // Fall through to parsing the corresponding structure.
 
   if (FormatTok->is(tok::l_brace)) {
index d068d35d7d39346ad3f24351b15ee19ea4824691..c02d51be5058a09ca25616db462170d2ed5d3df8 100644 (file)
@@ -101,7 +101,9 @@ TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
 
 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
   verifyFormat("var [a, b, c] = [1, 2, 3];");
+  verifyFormat("let [a, b, c] = [1, 2, 3];");
   verifyFormat("var {a, b} = {a: 1, b: 2};");
+  verifyFormat("let {a, b} = {a: 1, b: 2};");
 }
 
 TEST_F(FormatTestJS, ContainerLiterals) {