From: Daniel Jasper Date: Wed, 30 Dec 2015 08:00:58 +0000 (+0000) Subject: clang-format: [JS] Support TypeScript 1.6 user defined type guards. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dd4bfa19073ab07b62fe70f1005c011e62495f43;p=clang clang-format: [JS] Support TypeScript 1.6 user defined type guards. 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 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 93baaf2932..78bc0edc45 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -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; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 14e94078a6..c3ea935e72 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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)) diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 56b493dae1..cba2ce3370 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -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"