Skip to content

Commit 7777ca0

Browse files
committed
clang-format: [JS] support fields with case/switch/default labels.
Summary: `case:` and `default:` would normally parse as labels for a `switch` block. However in TypeScript, they can be used in field declarations, e.g.: interface I { case: string; } This change special cases parsing them in declaration lines to avoid wrapping them. Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D36148 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310070 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5e564d3 commit 7777ca0

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/Format/UnwrappedLineParser.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
326326
break;
327327
case tok::kw_default:
328328
case tok::kw_case:
329+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) {
330+
// A 'case: string' style field declaration.
331+
parseStructuralElement();
332+
break;
333+
}
329334
if (!SwitchLabelEncountered &&
330335
(Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
331336
++Line->Level;
@@ -953,13 +958,22 @@ void UnwrappedLineParser::parseStructuralElement() {
953958
parseDoWhile();
954959
return;
955960
case tok::kw_switch:
961+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
962+
// 'switch: string' field declaration.
963+
break;
956964
parseSwitch();
957965
return;
958966
case tok::kw_default:
967+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
968+
// 'default: string' field declaration.
969+
break;
959970
nextToken();
960971
parseLabel();
961972
return;
962973
case tok::kw_case:
974+
if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
975+
// 'case: string' field declaration.
976+
break;
963977
parseCaseLabel();
964978
return;
965979
case tok::kw_try:

unittests/Format/FormatTestJS.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ TEST_F(FormatTestJS, ReservedWords) {
242242
verifyFormat("var interface = 2;");
243243
verifyFormat("interface = 2;");
244244
verifyFormat("x = interface instanceof y;");
245+
verifyFormat("interface Test {\n"
246+
" x: string;\n"
247+
" switch: string;\n"
248+
" case: string;\n"
249+
" default: string;\n"
250+
"}\n");
245251
}
246252

247253
TEST_F(FormatTestJS, ReservedWordsMethods) {

0 commit comments

Comments
 (0)