From: Martin Probst Date: Fri, 24 Nov 2017 17:05:56 +0000 (+0000) Subject: clang-format: [JS] do not break in ArrayType[]. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=57465f36a29695496e9b4997962419c869eaf6b9;p=clang clang-format: [JS] do not break in ArrayType[]. Summary: Wrapping between the type name and the array type indicator creates invalid syntax in TypeScript. Before: const xIsALongIdent: YJustBarelyFitsLinex []; // illegal syntax. After: const xIsALongIdent: YJustBarelyFitsLinex[]; Reviewers: djasper Subscribers: klimek Differential Revision: https://reviews.llvm.org/D40436 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318959 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index d8d8bcbfc1..4e05521059 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2706,6 +2706,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get, Keywords.kw_set)) return false; // Otherwise automatic semicolon insertion would trigger. + if (Left.Tok.getIdentifierInfo() && + Right.startsSequence(tok::l_square, tok::r_square)) + return false; // breaking in "foo[]" creates illegal TS type syntax. if (Left.is(TT_JsFatArrow) && Right.is(tok::l_brace)) return false; if (Left.is(TT_JsTypeColon)) diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 2665c8b428..a107599188 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -1426,6 +1426,8 @@ TEST_F(FormatTestJS, TypeAnnotations) { verifyFormat( "var someValue = (v as aaaaaaaaaaaaaaaaaaaa[])\n" " .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("const xIsALongIdent:\n"" YJustBarelyFitsLinex[];", + getGoogleJSStyleWithColumns(20)); } TEST_F(FormatTestJS, UnionIntersectionTypes) {