[PATCH] D36148: clang-format: [JS] support fields with case/switch/default labels.
Martin Probst via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 1 07:11:41 PDT 2017
mprobst created this revision.
Herald added a subscriber: klimek.
`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.
https://reviews.llvm.org/D36148
Files:
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -242,6 +242,12 @@
verifyFormat("var interface = 2;");
verifyFormat("interface = 2;");
verifyFormat("x = interface instanceof y;");
+ verifyFormat("interface Test {\n"
+ " x: string;\n"
+ " switch: string;\n"
+ " case: string;\n"
+ " default: string;\n"
+ "}\n");
}
TEST_F(FormatTestJS, ReservedWordsMethods) {
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -326,6 +326,11 @@
break;
case tok::kw_default:
case tok::kw_case:
+ if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) {
+ // A 'case: string' style field declaration.
+ parseStructuralElement();
+ break;
+ }
if (!SwitchLabelEncountered &&
(Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
++Line->Level;
@@ -953,13 +958,22 @@
parseDoWhile();
return;
case tok::kw_switch:
+ if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
+ // 'switch: string' field declaration.
+ break;
parseSwitch();
return;
case tok::kw_default:
+ if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
+ // 'default: string' field declaration.
+ break;
nextToken();
parseLabel();
return;
case tok::kw_case:
+ if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
+ // 'case: string' field declaration.
+ break;
parseCaseLabel();
return;
case tok::kw_try:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36148.109105.patch
Type: text/x-patch
Size: 1896 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170801/90ecb821/attachment-0001.bin>
More information about the cfe-commits
mailing list