[PATCH] clang-format: [JS] handle comments in template strings.
Martin Probst
martinprobst at google.com
Tue Apr 14 17:01:17 PDT 2015
Using EXPECT_EQ and manually messed up strings.
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
@@ -648,6 +648,33 @@
// Two template strings.
verifyFormat("var x = `hello` == `hello`;");
+
+ // Comments in template strings.
+ EXPECT_EQ("var x = `//a`;\n"
+ "var y;",
+ format("var x =\n `//a`;\n"
+ "var y ;"));
+ EXPECT_EQ("var x = `/*a`;\n"
+ "var y;",
+ format("var x =\n `/*a`;\n"
+ "var y;"));
+ // Backticks in a comment - not a template string.
+ EXPECT_EQ("var x = 1 // `/*a`;\n"
+ " ;",
+ format("var x =\n 1 // `/*a`;\n"
+ " ;"));
+ EXPECT_EQ("/* ` */ var x = 1; /* ` */",
+ format("/* ` */ var x\n= 1; /* ` */"));
+ // Comment spans multiple template strings.
+ EXPECT_EQ("var x = `/*a`;\n"
+ "var y = ` */ `;",
+ format("var x =\n `/*a`;\n"
+ "var y =\n ` */ `;"));
+ // Escaped backtick.
+ EXPECT_EQ("var x = ` \\` a`;\n"
+ "var y;",
+ format("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.23749.patch
Type: text/x-patch
Size: 2863 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150415/d939c876/attachment.bin>
More information about the cfe-commits
mailing list