diff options
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Format/UnwrappedLineParser.cpp | 15 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 3 |
2 files changed, 18 insertions, 0 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 91856a74c35..6d2d2cb44c0 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1007,6 +1007,21 @@ void UnwrappedLineParser::parseStructuralElement() { if ((Style.Language == FormatStyle::LK_JavaScript || Style.Language == FormatStyle::LK_Java) && FormatTok->is(Keywords.kw_interface)) { + if (Style.Language == FormatStyle::LK_JavaScript) { + // In JavaScript/TypeScript, "interface" can be used as a standalone + // identifier, e.g. in `var interface = 1;`. If "interface" is + // followed by another identifier, it is very like to be an actual + // interface declaration. + unsigned StoredPosition = Tokens->getPosition(); + FormatToken *Next = Tokens->getNextToken(); + FormatTok = Tokens->setPosition(StoredPosition); + if (Next && (Next->isNot(tok::identifier) || + Next->isOneOf(Keywords.kw_instanceof, Keywords.kw_of, + Keywords.kw_in))) { + nextToken(); + break; + } + } parseRecord(); addUnwrappedLine(); return; diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index aa79fa30245..d23a55e2708 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -136,6 +136,9 @@ TEST_F(FormatTestJS, ReservedWords) { "};"); verifyFormat("var struct = 2;"); verifyFormat("var union = 2;"); + verifyFormat("var interface = 2;"); + verifyFormat("interface = 2;"); + verifyFormat("x = interface instanceof y;"); } TEST_F(FormatTestJS, CppKeywords) { |