From: Martin Probst Date: Mon, 26 Aug 2019 15:37:05 +0000 (+0000) Subject: clang-format: [JS] handle `as const`. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6d58eb5b44429547605a5139d7d070d35269cf2b;p=clang clang-format: [JS] handle `as const`. 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 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index c391c489dc..5be398565c 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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 diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index b332f1bd97..ca2093a05c 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -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) {