]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support TypeScript 1.6 user defined type guards.
authorDaniel Jasper <djasper@google.com>
Wed, 30 Dec 2015 08:00:58 +0000 (08:00 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 30 Dec 2015 08:00:58 +0000 (08:00 +0000)
Before:
  function foo(check: Object): check
  is{foo: string, bar: string, baz: string, foobar: string} {
    return 'bar' in check;
  }

After:
  function foo(check: Object):
      check is {foo: string, bar: string, baz: string, foobar: string} {
    return 'bar' in check;
  }

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

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

index 93baaf2932137dbdff098c8df942ab3f0dfb78d0..78bc0edc45c0289d80c26c2e5b1c90df0666d5d3 100644 (file)
@@ -536,6 +536,7 @@ struct AdditionalKeywords {
     kw_finally = &IdentTable.get("finally");
     kw_function = &IdentTable.get("function");
     kw_import = &IdentTable.get("import");
+    kw_is = &IdentTable.get("is");
     kw_let = &IdentTable.get("let");
     kw_var = &IdentTable.get("var");
 
@@ -580,6 +581,7 @@ struct AdditionalKeywords {
   IdentifierInfo *kw_finally;
   IdentifierInfo *kw_function;
   IdentifierInfo *kw_import;
+  IdentifierInfo *kw_is;
   IdentifierInfo *kw_let;
   IdentifierInfo *kw_var;
 
index 14e94078a6e2cab846e0da26bb4e8dfa0991a075..c3ea935e727bbc90cd53b56759c42d688c32f4d3 100644 (file)
@@ -1996,6 +1996,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, TT_JsFatArrow,
                      Keywords.kw_in))
       return true;
+    if (Left.is(Keywords.kw_is) && Right.is(tok::l_brace))
+      return true;
     if (Right.isOneOf(TT_JsTypeColon, TT_JsTypeOptionalQuestion))
       return false;
     if ((Left.is(tok::l_brace) || Right.is(tok::r_brace)) &&
@@ -2239,6 +2241,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
       return false;
     if (Left.is(TT_JsTypeColon))
       return true;
+    if (Right.NestingLevel == 0 && Right.is(Keywords.kw_is))
+      return false;
   }
 
   if (Left.is(tok::at))
index 56b493dae1c14622667e05a9c1a58546256c8fa4..cba2ce3370c9e71df7e4abedf970a90df05f7ac1 100644 (file)
@@ -962,6 +962,14 @@ TEST_F(FormatTestJS, TypeArguments) {
                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa {}");
 }
 
+TEST_F(FormatTestJS, UserDefinedTypeGuards) {
+  verifyFormat(
+      "function foo(check: Object):\n"
+      "    check is {foo: string, bar: string, baz: string, foobar: string} {\n"
+      "  return 'bar' in check;\n"
+      "}\n");
+}
+
 TEST_F(FormatTestJS, OptionalTypes) {
   verifyFormat("function x(a?: b, c?, d?) {}");
   verifyFormat("class X {\n"