[PATCH] clang-format: [JS] handle comments in template strings.
Martin Probst
martinprobst at google.com
Sat Apr 11 16:21:29 PDT 2015
Hi djasper,
http://reviews.llvm.org/D8990
Files:
lib/Format/Format.cpp
unittests/Format/FormatTestJS.cpp
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -780,7 +780,15 @@
return false;
FormatToken *EndBacktick = Tokens.back();
- if (!(EndBacktick->is(tok::unknown) && EndBacktick->TokenText == "`"))
+ // Backticks get lexed as tok:unknown tokens. If a template string contains
+ // a comment start, it gets lexed as a tok::comment, or tok::unknown if
+ // unterminated.
+ if (!EndBacktick->isOneOf(tok::comment, tok::unknown))
+ return false;
+ size_t CommentBacktickPos = EndBacktick->TokenText.find('`');
+ // Unknown token that's not actually a backtick, or a comment that doesn't
+ // contain a backtick.
+ if (CommentBacktickPos == StringRef::npos)
return false;
unsigned TokenCount = 0;
@@ -812,7 +820,14 @@
Tokens.resize(Tokens.size() - TokenCount);
Tokens.back()->Type = TT_TemplateString;
- const char *EndOffset = EndBacktick->TokenText.data() + 1;
+ const char *EndOffset =
+ EndBacktick->TokenText.data() + 1 + CommentBacktickPos;
+ if (CommentBacktickPos != 0) {
+ // If the backtick was not the first character (e.g. in a comment),
+ // re-lex after the backtick position.
+ SourceLocation Loc = EndBacktick->Tok.getLocation();
+ resetLexer(SourceMgr.getFileOffset(Loc) + CommentBacktickPos + 1);
+ }
Tokens.back()->TokenText =
StringRef(Tokens.back()->TokenText.data(),
EndOffset - Tokens.back()->TokenText.data());
Index: unittests/Format/FormatTestJS.cpp
===================================================================
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -647,6 +647,22 @@
// Two template strings.
verifyFormat("var x = `hello` == `hello`;");
+
+ // Comments in template strings.
+ verifyFormat("var x = `//a`;\n"
+ "var y");
+ verifyFormat("var x = `/*a`;\n"
+ "var y;");
+ // Backticks in a comment - not a template string.
+ verifyFormat("var x = 1 // `/*a`;\n"
+ " ;");
+ verifyFormat("/* ` */ var x = 1; /* ` */");
+ // Comment spans multiple template strings.
+ verifyFormat("var x = `/*a`;\n"
+ "var y = ` */ `;");
+ // Escaped backtick.
+ verifyFormat("var x = ` \\` a`;\n"
+ "var y;");
}
TEST_F(FormatTestJS, CastSyntax) {
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8990.23649.patch
Type: text/x-patch
Size: 2464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150411/748d2886/attachment.bin>
More information about the cfe-commits
mailing list