]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] handle `as const`.
authorMartin Probst <martin@probst.io>
Mon, 26 Aug 2019 15:37:05 +0000 (15:37 +0000)
committerMartin Probst <martin@probst.io>
Mon, 26 Aug 2019 15:37:05 +0000 (15:37 +0000)
Summary:
TypeScript 3.4 supports casting into a const type using `as const`:

    const x = {x: 1} as const;

Previously, clang-format would insert a space after the `const`. With
this patch, no space is inserted after the sequence `as const`.

Reviewers: krasimir

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D66736

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

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

index c391c489dc293f54148fef5f71dbe75a39a15640..5be398565c962e45b2d9c56b54ec95f5d8495a06 100644 (file)
@@ -2746,6 +2746,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
                        tok::kw_void))
         return true;
     }
+    // `foo as const;` casts into a const type.
+    if (Left.endsSequence(tok::kw_const, Keywords.kw_as)) {
+      return false;
+    }
     if ((Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
                       tok::kw_const) ||
          // "of" is only a keyword if it appears after another identifier
index b332f1bd9751731461efd040f9149d8c9cd94039..ca2093a05cce1c8ff0974b33c7b38067774fa29e 100644 (file)
@@ -1479,6 +1479,9 @@ TEST_F(FormatTestJS, TypeAnnotations) {
       "                    .someFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
   verifyFormat("const xIsALongIdent:\n""    YJustBarelyFitsLinex[];",
       getGoogleJSStyleWithColumns(20));
+  verifyFormat("const x = {\n"
+               "  y: 1\n"
+               "} as const;");
 }
 
 TEST_F(FormatTestJS, UnionIntersectionTypes) {