Re: [PATCH] D23761: clang-format: [JS] supports casts to types starting with punctuation ("{[(").

2016-08-22 Thread Martin Probst via cfe-commits
This revision was automatically updated to reflect the committed changes.
mprobst marked an inline comment as done.
Closed by commit rL279436: clang-format: [JS] supports casts to types starting 
with punctuation ("{[("). (authored by mprobst).

Changed prior to commit:
  https://reviews.llvm.org/D23761?vs=68844=68858#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D23761

Files:
  cfe/trunk/lib/Format/TokenAnnotator.cpp
  cfe/trunk/unittests/Format/FormatTestJS.cpp

Index: cfe/trunk/lib/Format/TokenAnnotator.cpp
===
--- cfe/trunk/lib/Format/TokenAnnotator.cpp
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp
@@ -2121,6 +2121,9 @@
  Keywords.kw_of, tok::kw_const) &&
 (!Left.Previous || !Left.Previous->is(tok::period)))
   return true;
+if (Left.is(Keywords.kw_as) &&
+Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
+  return true;
 if (Left.is(tok::kw_default) && Left.Previous &&
 Left.Previous->is(tok::kw_export))
   return true;
Index: cfe/trunk/unittests/Format/FormatTestJS.cpp
===
--- cfe/trunk/unittests/Format/FormatTestJS.cpp
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp
@@ -1219,6 +1219,9 @@
"  2\n"
"];");
   verifyFormat("var x = [{x: 1} as type];");
+  verifyFormat("x = x as [a, b];");
+  verifyFormat("x = x as {a: string};");
+  verifyFormat("x = x as (string);");
 }
 
 TEST_F(FormatTestJS, TypeArguments) {


Index: cfe/trunk/lib/Format/TokenAnnotator.cpp
===
--- cfe/trunk/lib/Format/TokenAnnotator.cpp
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp
@@ -2121,6 +2121,9 @@
  Keywords.kw_of, tok::kw_const) &&
 (!Left.Previous || !Left.Previous->is(tok::period)))
   return true;
+if (Left.is(Keywords.kw_as) &&
+Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
+  return true;
 if (Left.is(tok::kw_default) && Left.Previous &&
 Left.Previous->is(tok::kw_export))
   return true;
Index: cfe/trunk/unittests/Format/FormatTestJS.cpp
===
--- cfe/trunk/unittests/Format/FormatTestJS.cpp
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp
@@ -1219,6 +1219,9 @@
"  2\n"
"];");
   verifyFormat("var x = [{x: 1} as type];");
+  verifyFormat("x = x as [a, b];");
+  verifyFormat("x = x as {a: string};");
+  verifyFormat("x = x as (string);");
 }
 
 TEST_F(FormatTestJS, TypeArguments) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D23761: clang-format: [JS] supports casts to types starting with punctuation ("{[(").

2016-08-22 Thread Daniel Jasper via cfe-commits
djasper accepted this revision.
djasper added a comment.
This revision is now accepted and ready to land.

Please add a "before" and "after" to the patch description before submitting. 
Otherwise looks good.


https://reviews.llvm.org/D23761



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D23761: clang-format: [JS] supports casts to types starting with punctuation ("{[(").

2016-08-22 Thread Martin Probst via cfe-commits
mprobst updated this revision to Diff 68844.
mprobst added a comment.

- drop accidentally included template string patch from this diff.


https://reviews.llvm.org/D23761

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestJS.cpp

Index: unittests/Format/FormatTestJS.cpp
===
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1219,6 +1219,9 @@
"  2\n"
"];");
   verifyFormat("var x = [{x: 1} as type];");
+  verifyFormat("x = x as [a, b];");
+  verifyFormat("x = x as {a: string};");
+  verifyFormat("x = x as (string);");
 }
 
 TEST_F(FormatTestJS, TypeArguments) {
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2121,6 +2121,9 @@
  Keywords.kw_of, tok::kw_const) &&
 (!Left.Previous || !Left.Previous->is(tok::period)))
   return true;
+if (Left.is(Keywords.kw_as) &&
+Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
+  return true;
 if (Left.is(tok::kw_default) && Left.Previous &&
 Left.Previous->is(tok::kw_export))
   return true;


Index: unittests/Format/FormatTestJS.cpp
===
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1219,6 +1219,9 @@
"  2\n"
"];");
   verifyFormat("var x = [{x: 1} as type];");
+  verifyFormat("x = x as [a, b];");
+  verifyFormat("x = x as {a: string};");
+  verifyFormat("x = x as (string);");
 }
 
 TEST_F(FormatTestJS, TypeArguments) {
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2121,6 +2121,9 @@
  Keywords.kw_of, tok::kw_const) &&
 (!Left.Previous || !Left.Previous->is(tok::period)))
   return true;
+if (Left.is(Keywords.kw_as) &&
+Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
+  return true;
 if (Left.is(tok::kw_default) && Left.Previous &&
 Left.Previous->is(tok::kw_export))
   return true;
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D23761: clang-format: [JS] supports casts to types starting with punctuation ("{[(").

2016-08-21 Thread Yunzhong Gao via cfe-commits
ygao added a subscriber: ygao.


Comment at: lib/Format/FormatTokenLexer.cpp:245
@@ -241,1 +244,3 @@
   ++Offset; // Skip the escaped character.
+if (Offset + 1 < Lex->getBuffer().end() && *Offset == '$' &&
+*(Offset + 1) == '{') {

What happens if the '${' is immediately after a backslash (the if statement 
above), should the '${' get escaped?


https://reviews.llvm.org/D23761



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D23761: clang-format: [JS] supports casts to types starting with punctuation ("{[(").

2016-08-21 Thread Martin Probst via cfe-commits
mprobst created this revision.
mprobst added a reviewer: djasper.
mprobst added subscribers: cfe-commits, klimek.

https://reviews.llvm.org/D23761

Files:
  lib/Format/FormatTokenLexer.cpp
  lib/Format/FormatTokenLexer.h
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestJS.cpp

Index: unittests/Format/FormatTestJS.cpp
===
--- unittests/Format/FormatTestJS.cpp
+++ unittests/Format/FormatTestJS.cpp
@@ -1122,7 +1122,7 @@
 TEST_F(FormatTestJS, TemplateStrings) {
   // Keeps any whitespace/indentation within the template string.
   verifyFormat("var x = `hello\n"
-" ${  name}\n"
+" ${name}\n"
 "  !`;",
 "var x=`hello\n"
" ${  name}\n"
@@ -1206,6 +1206,12 @@
"var y;",
"var x = ` \\` a`;\n"
"var y;");
+  verifyFormat(
+  "var x = `${xs.map(x => `${x}`).join('\\n')}`;");
+}
+
+TEST_F(FormatTestJS, TaggedTemplateStrings) {
+  verifyFormat("var x = html``;");
 }
 
 TEST_F(FormatTestJS, CastSyntax) {
@@ -1219,6 +1225,9 @@
"  2\n"
"];");
   verifyFormat("var x = [{x: 1} as type];");
+  verifyFormat("x = x as [a, b];");
+  verifyFormat("x = x as {a: string};");
+  verifyFormat("x = x as (string);");
 }
 
 TEST_F(FormatTestJS, TypeArguments) {
Index: lib/Format/TokenAnnotator.cpp
===
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -858,7 +858,7 @@
 if (!CurrentToken->isOneOf(TT_LambdaLSquare, TT_ForEachMacro,
TT_FunctionLBrace, TT_ImplicitStringLiteral,
TT_InlineASMBrace, TT_JsFatArrow, TT_LambdaArrow,
-   TT_RegexLiteral))
+   TT_RegexLiteral, TT_TemplateString))
   CurrentToken->Type = TT_Unknown;
 CurrentToken->Role.reset();
 CurrentToken->MatchingParen = nullptr;
@@ -1816,6 +1816,9 @@
   return 100;
 if (Left.is(TT_JsTypeColon))
   return 35;
+if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
+(Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
+  return 100;
   }
 
   if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
@@ -2114,13 +2117,21 @@
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
 if (Left.is(TT_JsFatArrow))
   return true;
+if ((Left.is(TT_TemplateString) && Left.TokenText.endswith("${")) ||
+(Right.is(TT_TemplateString) && Right.TokenText.startswith("}")))
+  return false;
+if (Left.is(tok::identifier) && Right.is(TT_TemplateString))
+  return false;
 if (Right.is(tok::star) &&
 Left.isOneOf(Keywords.kw_function, Keywords.kw_yield))
   return false;
 if (Left.isOneOf(Keywords.kw_let, Keywords.kw_var, Keywords.kw_in,
  Keywords.kw_of, tok::kw_const) &&
 (!Left.Previous || !Left.Previous->is(tok::period)))
   return true;
+if (Left.is(Keywords.kw_as) &&
+Right.isOneOf(tok::l_square, tok::l_brace, tok::l_paren))
+  return true;
 if (Left.is(tok::kw_default) && Left.Previous &&
 Left.Previous->is(tok::kw_export))
   return true;
Index: lib/Format/FormatTokenLexer.h
===
--- lib/Format/FormatTokenLexer.h
+++ lib/Format/FormatTokenLexer.h
@@ -66,6 +66,7 @@
   FormatToken *FormatTok;
   bool IsFirstToken;
   bool GreaterStashed, LessStashed;
+  unsigned TemplateStringDepth;
   unsigned Column;
   unsigned TrailingWhitespace;
   std::unique_ptr Lex;
Index: lib/Format/FormatTokenLexer.cpp
===
--- lib/Format/FormatTokenLexer.cpp
+++ lib/Format/FormatTokenLexer.cpp
@@ -27,8 +27,8 @@
const FormatStyle ,
encoding::Encoding Encoding)
 : FormatTok(nullptr), IsFirstToken(true), GreaterStashed(false),
-  LessStashed(false), Column(0), TrailingWhitespace(0),
-  SourceMgr(SourceMgr), ID(ID), Style(Style),
+  LessStashed(false), TemplateStringDepth(0), Column(0),
+  TrailingWhitespace(0), SourceMgr(SourceMgr), ID(ID), Style(Style),
   IdentTable(getFormattingLangOpts(Style)), Keywords(IdentTable),
   Encoding(Encoding), FirstInLineIndex(0), FormattingDisabled(false),
   MacroBlockBeginRegex(Style.MacroBlockBegin),
@@ -230,15 +230,25 @@
 
 void FormatTokenLexer::tryParseTemplateString() {
   FormatToken *BacktickToken = Tokens.back();
-  if (!BacktickToken->is(tok::unknown) || BacktickToken->TokenText != "`")
+  if (TemplateStringDepth > 0 && BacktickToken->TokenText == "}")
+TemplateStringDepth--;
+  else if (!BacktickToken->is(tok::unknown) ||
+ BacktickToken->TokenText != "`")
 return;
 
   //