From: Martin Probst Date: Thu, 10 Nov 2016 16:20:58 +0000 (+0000) Subject: clang-format: [JS] do not break after declare namespace. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=20ecf8b618e650bfc40b05ed623bda96d3edca91;p=clang clang-format: [JS] do not break after declare namespace. See TypeScript grammar for tokens following 'declare': https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@286467 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 43b1625136..dd7e991342 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -580,12 +580,14 @@ struct AdditionalKeywords { kw_as = &IdentTable.get("as"); kw_async = &IdentTable.get("async"); kw_await = &IdentTable.get("await"); + kw_declare = &IdentTable.get("declare"); kw_finally = &IdentTable.get("finally"); kw_from = &IdentTable.get("from"); kw_function = &IdentTable.get("function"); kw_import = &IdentTable.get("import"); kw_is = &IdentTable.get("is"); kw_let = &IdentTable.get("let"); + kw_module = &IdentTable.get("module"); kw_type = &IdentTable.get("type"); kw_var = &IdentTable.get("var"); kw_yield = &IdentTable.get("yield"); @@ -632,12 +634,14 @@ struct AdditionalKeywords { IdentifierInfo *kw_as; IdentifierInfo *kw_async; IdentifierInfo *kw_await; + IdentifierInfo *kw_declare; IdentifierInfo *kw_finally; IdentifierInfo *kw_from; IdentifierInfo *kw_function; IdentifierInfo *kw_import; IdentifierInfo *kw_is; IdentifierInfo *kw_let; + IdentifierInfo *kw_module; IdentifierInfo *kw_type; IdentifierInfo *kw_var; IdentifierInfo *kw_yield; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index bba803dc3e..628659ef2b 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2426,10 +2426,10 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, } else if (Style.Language == FormatStyle::LK_JavaScript) { const FormatToken *NonComment = Right.getPreviousNonComment(); if (Left.isOneOf(tok::kw_return, tok::kw_continue, tok::kw_break, - tok::kw_throw) || + tok::kw_throw, Keywords.kw_module) || (NonComment && NonComment->isOneOf(tok::kw_return, tok::kw_continue, tok::kw_break, - tok::kw_throw))) + tok::kw_throw, Keywords.kw_module))) return false; // Otherwise a semicolon is inserted. if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace)) return false; @@ -2443,6 +2443,18 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None; if (Right.is(Keywords.kw_as)) return false; // must not break before as in 'x as type' casts + if (Left.is(Keywords.kw_declare) && + Right.isOneOf(Keywords.kw_module, tok::kw_namespace, + Keywords.kw_function, tok::kw_class, tok::kw_enum, + Keywords.kw_interface, Keywords.kw_type, Keywords.kw_var, + Keywords.kw_let, tok::kw_const)) + // See grammar for 'declare' statements at: + // https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#A.10 + return false; + if (Left.isOneOf(Keywords.kw_module, tok::kw_namespace) && + Right.isOneOf(tok::identifier, tok::string_literal)) { + return false; // must not break in "module foo { ...}" + } } if (Left.is(tok::at)) diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 764159882c..90a12941bb 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -349,6 +349,37 @@ TEST_F(FormatTestJS, FormatsNamespaces) { "}\n"); } +TEST_F(FormatTestJS, NamespacesMayNotWrap) { + verifyFormat("declare namespace foobarbaz {\n" + "}\n", getGoogleJSStyleWithColumns(18)); + verifyFormat("declare module foobarbaz {\n" + "}\n", getGoogleJSStyleWithColumns(15)); + verifyFormat("namespace foobarbaz {\n" + "}\n", getGoogleJSStyleWithColumns(10)); + verifyFormat("module foobarbaz {\n" + "}\n", getGoogleJSStyleWithColumns(7)); +} + +TEST_F(FormatTestJS, AmbientDeclarations) { + FormatStyle NineCols = getGoogleJSStyleWithColumns(9); + verifyFormat( + "declare class\n" + " X {}", + NineCols); + verifyFormat( + "declare function\n" + "x();", // TODO(martinprobst): should ideally be indented. + NineCols); + verifyFormat( + "declare enum X {\n" + "}", + NineCols); + verifyFormat( + "declare let\n" + " x: number;", + NineCols); +} + TEST_F(FormatTestJS, FormatsFreestandingFunctions) { verifyFormat("function outer1(a, b) {\n" " function inner1(a, b) {\n"