From: Martin Probst Date: Sat, 25 Nov 2017 09:24:33 +0000 (+0000) Subject: clang-format: [JS] handle `for` as object label. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=525eeeae5f1af0017fdafe109cfde6d49ab0c4b2;p=clang clang-format: [JS] handle `for` as object label. Summary: Previously, clang-format would fail formatting `{for: 1}`. Reviewers: djasper Subscribers: klimek Differential Revision: https://reviews.llvm.org/D40441 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@318974 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 4e05521059..4463f89755 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -617,7 +617,9 @@ private: break; case tok::kw_for: if (Style.Language == FormatStyle::LK_JavaScript) { - if (Tok->Previous && Tok->Previous->is(tok::period)) + // x.for and {for: ...} + if ((Tok->Previous && Tok->Previous->is(tok::period)) || + (Tok->Next && Tok->Next->is(tok::colon))) break; // JS' for await ( ... if (CurrentToken && CurrentToken->is(Keywords.kw_await)) diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 97ec843a56..cfdd4b375b 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -323,6 +323,11 @@ TEST_F(FormatTestJS, ReservedWords) { " case: string;\n" " default: string;\n" "}\n"); + verifyFormat("const Axis = {\n" + " for: 'for',\n" + " x: 'x'\n" + "};", + "const Axis = {for: 'for', x: 'x'};"); } TEST_F(FormatTestJS, ReservedWordsMethods) {