[clang-format] Support TypeScript override keyword

TypeScript 4.3 added a new "override" keyword for class members. This
lets clang-format know about it, so it can format code using it
properly.

Reviewed By: krasimir

Differential Revision: https://reviews.llvm.org/D108692
This commit is contained in:
Jan Kuehle 2021-08-25 14:11:43 +02:00 committed by Krasimir Georgiev
parent 4c4dbeeeea
commit e708808f87
5 changed files with 32 additions and 10 deletions

View File

@ -934,8 +934,8 @@ struct AdditionalKeywords {
// already initialized.
JsExtraKeywords = std::unordered_set<IdentifierInfo *>(
{kw_as, kw_async, kw_await, kw_declare, kw_finally, kw_from,
kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_readonly,
kw_set, kw_type, kw_typeof, kw_var, kw_yield,
kw_function, kw_get, kw_import, kw_is, kw_let, kw_module, kw_override,
kw_readonly, kw_set, kw_type, kw_typeof, kw_var, kw_yield,
// Keywords from the Java section.
kw_abstract, kw_extends, kw_implements, kw_instanceof, kw_interface});

View File

@ -3957,8 +3957,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
tok::kw_return, Keywords.kw_yield, tok::kw_continue, tok::kw_break,
tok::kw_throw, Keywords.kw_interface, Keywords.kw_type,
tok::kw_static, tok::kw_public, tok::kw_private, tok::kw_protected,
Keywords.kw_readonly, Keywords.kw_abstract, Keywords.kw_get,
Keywords.kw_set, Keywords.kw_async, Keywords.kw_await))
Keywords.kw_readonly, Keywords.kw_override, Keywords.kw_abstract,
Keywords.kw_get, Keywords.kw_set, Keywords.kw_async,
Keywords.kw_await))
return false; // Otherwise automatic semicolon insertion would trigger.
if (Right.NestingLevel == 0 &&
(Left.Tok.getIdentifierInfo() ||

View File

@ -632,10 +632,11 @@ private:
FormatToken *RecordTok = Line.First;
// Skip record modifiers.
while (RecordTok->Next &&
RecordTok->isOneOf(
tok::kw_typedef, tok::kw_export, Keywords.kw_declare,
Keywords.kw_abstract, tok::kw_default, tok::kw_public,
tok::kw_private, tok::kw_protected, Keywords.kw_internal))
RecordTok->isOneOf(tok::kw_typedef, tok::kw_export,
Keywords.kw_declare, Keywords.kw_abstract,
tok::kw_default, Keywords.kw_override,
tok::kw_public, tok::kw_private,
tok::kw_protected, Keywords.kw_internal))
RecordTok = RecordTok->Next;
if (RecordTok &&
RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,

View File

@ -962,8 +962,8 @@ static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
Keywords.kw_let, Keywords.kw_var, tok::kw_const,
Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
Keywords.kw_from));
Keywords.kw_instanceof, Keywords.kw_interface,
Keywords.kw_override, Keywords.kw_throws, Keywords.kw_from));
}
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,

View File

@ -854,6 +854,26 @@ TEST_F(FormatTestJS, AsyncFunctions) {
"}\n");
}
TEST_F(FormatTestJS, OverriddenMembers) {
verifyFormat(
"class C extends P {\n"
" protected override "
"anOverlyLongPropertyNameSoLongItHasToGoInASeparateLineWhenOverriden:\n"
" undefined;\n"
"}\n");
verifyFormat(
"class C extends P {\n"
" protected override "
"anOverlyLongMethodNameSoLongItHasToGoInASeparateLineWhenOverriden() {\n"
" }\n"
"}\n");
verifyFormat("class C extends P {\n"
" protected override aMethodName<ATypeParam extends {},\n"
" BTypeParam "
"extends {}>() {}\n"
"}\n");
}
TEST_F(FormatTestJS, FunctionParametersTrailingComma) {
verifyFormat("function trailingComma(\n"
" p1,\n"