Skip to content

Commit 3315aed

Browse files
committedSep 27, 2018
clang-format: [JS] conditional types.
Summary: This change adds some rudimentary support for conditional types. Specifically it avoids breaking before `extends` and `infer` keywords, which are subject to Automatic Semicolon Insertion, so breaking before them creates incorrect syntax. The actual formatting of the type expression is odd, but there is as of yet no clear idea on how to format these. See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types. Reviewers: krasimir Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D52536 llvm-svn: 343179
1 parent 77708b2 commit 3315aed

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed
 

‎clang/lib/Format/FormatToken.h

+2
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ struct AdditionalKeywords {
680680
kw_function = &IdentTable.get("function");
681681
kw_get = &IdentTable.get("get");
682682
kw_import = &IdentTable.get("import");
683+
kw_infer = &IdentTable.get("infer");
683684
kw_is = &IdentTable.get("is");
684685
kw_let = &IdentTable.get("let");
685686
kw_module = &IdentTable.get("module");
@@ -751,6 +752,7 @@ struct AdditionalKeywords {
751752
IdentifierInfo *kw_function;
752753
IdentifierInfo *kw_get;
753754
IdentifierInfo *kw_import;
755+
IdentifierInfo *kw_infer;
754756
IdentifierInfo *kw_is;
755757
IdentifierInfo *kw_let;
756758
IdentifierInfo *kw_module;

‎clang/lib/Format/TokenAnnotator.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,12 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
30883088
return Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None;
30893089
if (Right.is(Keywords.kw_as))
30903090
return false; // must not break before as in 'x as type' casts
3091+
if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_infer)) {
3092+
// extends and infer can appear as keywords in conditional types:
3093+
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#conditional-types
3094+
// do not break before them, as the expressions are subject to ASI.
3095+
return false;
3096+
}
30913097
if (Left.is(Keywords.kw_as))
30923098
return true;
30933099
if (Left.is(TT_JsNonNullAssertion))

‎clang/unittests/Format/FormatTestJS.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -2308,5 +2308,14 @@ TEST_F(FormatTestJS, ParameterNamingComment) {
23082308
verifyFormat("callFoo(/*spaceAfterParameterNamingComment=*/ 1);");
23092309
}
23102310

2311+
TEST_F(FormatTestJS, ConditionalTypes) {
2312+
// Formatting below is not necessarily intentional, this just ensures that
2313+
// clang-format does not break the code.
2314+
verifyFormat( // wrap
2315+
"type UnionToIntersection<U> =\n"
2316+
" (U extends any ? (k: U) => void :\n"
2317+
" never) extends((k: infer I) => void) ? I : never;");
2318+
}
2319+
23112320
} // end namespace tooling
23122321
} // end namespace clang

0 commit comments

Comments
 (0)
Please sign in to comment.