]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] do not break in ArrayType[].
authorMartin Probst <martin@probst.io>
Fri, 24 Nov 2017 17:05:56 +0000 (17:05 +0000)
committerMartin Probst <martin@probst.io>
Fri, 24 Nov 2017 17:05:56 +0000 (17:05 +0000)
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

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

index d8d8bcbfc17094229ff23d267ed4d24d22cdaebb..4e055210598d320d822c0f6a4133d3337b0efc81 100644 (file)
@@ -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))
index 2665c8b428e18a1a47527fa5e13a29505536422b..a1075991885c02c90ee0ca787e8f53554c008a69 100644 (file)
@@ -1426,6 +1426,8 @@ TEST_F(FormatTestJS, TypeAnnotations) {
   verifyFormat(
       "var someValue = (v as aaaaaaaaaaaaaaaaaaaa<T>[])\n"
       "                    .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
+  verifyFormat("const xIsALongIdent:\n""    YJustBarelyFitsLinex[];",
+      getGoogleJSStyleWithColumns(20));
 }
 
 TEST_F(FormatTestJS, UnionIntersectionTypes) {